From 21f63ee545f1694305ba650f5cea57a720ece31a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=A5=E6=B5=B7?= Date: Wed, 27 Dec 2023 18:26:35 +0800 Subject: [PATCH] Impl Forward Message --- clients/gohttp/README.md | 21 +++++++++- clients/gohttp/httpd/wcfrest/controller.go | 45 ++++++++++++++++++--- clients/gohttp/httpd/wcfrest/router.go | 1 + clients/gohttp/public/assets/icon.png | 0 clients/gohttp/public/assets/image/icon.svg | 5 +++ clients/gohttp/public/index.html | 10 +++-- clients/gohttp/public/swagger.json | 44 +++++++++++++++++++- 7 files changed, 113 insertions(+), 13 deletions(-) delete mode 100644 clients/gohttp/public/assets/icon.png create mode 100644 clients/gohttp/public/assets/image/icon.svg diff --git a/clients/gohttp/README.md b/clients/gohttp/README.md index b09be95..4c578b1 100644 --- a/clients/gohttp/README.md +++ b/clients/gohttp/README.md @@ -12,6 +12,23 @@ 4、浏览器打开 `http://localhost:7600` 查看支持的接口 +## 配置说明 + +```yml +httpd: + address: 127.0.0.1:7600 # api 监听地址 + token: "" # 使用 token 验证请求 +logger: + dir: logs # 日志目录 + level: info # 日志级别 + target: stdout # 日志输出方式 +wcf: + address: 127.0.0.1:10080 # rpc 监听地址 + sdklibrary: libs/sdk.dll # sdk 依赖库 + wechatauto: true # 自动启动或停止微信 + msgprint: true # 打印收到的消息 +``` + ## 功能清单 - 检查登录状态 @@ -40,8 +57,8 @@ - 获取群成员昵称 - 邀请群成员 - 拍一拍群友 -- 开启消息转发 -- 停止消息转发 +- 转发消息给好友 +- 转发收到的消息到URL ## 生成 OpenApi 文档 diff --git a/clients/gohttp/httpd/wcfrest/controller.go b/clients/gohttp/httpd/wcfrest/controller.go index a6084c7..d0372a1 100644 --- a/clients/gohttp/httpd/wcfrest/controller.go +++ b/clients/gohttp/httpd/wcfrest/controller.go @@ -1,6 +1,7 @@ package wcfrest import ( + "net" "strings" "github.com/gin-gonic/gin" @@ -13,14 +14,18 @@ import ( ) var wc *wcferry.Client +var forwardUrls = map[string]bool{} func initService() { - parts := strings.Split(args.Wcf.Address, ":") + host, port, err := net.SplitHostPort(args.Wcf.Address) + if err != nil { + logman.Fatal("failed to start wcf", "error", err) + } wc = &wcferry.Client{ - ListenAddr: parts[0], - ListenPort: strutil.ToInt(parts[1]), + ListenAddr: host, + ListenPort: strutil.ToInt(port), SdkLibrary: args.Wcf.SdkLibrary, WeChatAuto: args.Wcf.WeChatAuto, } @@ -283,6 +288,27 @@ func revokeMsg(c *gin.Context) { } +// @Summary 转发消息 +// @Produce json +// @Param body body wcferry.ForwardMsg true "转发消息请求参数" +// @Success 200 {object} RespPayload +// @Router /forward_msg [post] +func forwardMsg(c *gin.Context) { + + var req wcferry.ForwardMsg + if err := c.ShouldBindJSON(&req); err != nil { + c.Set("Error", err) + return + } + + status := wc.CmdClient.ForwardMsg(req.Id, req.Receiver) + + c.Set("Payload", RespPayload{ + Success: status == 1, + }) + +} + // @Summary 发送文本消息 // @Produce json // @Param body body wcferry.TextMsg true "文本消息请求参数" @@ -534,7 +560,7 @@ func receiveTransfer(c *gin.Context) { } -// @Summary 开启消息转发 +// @Summary 开启转发消息到URL // @Produce json // @Param body body ForwardMsgRequest true "消息转发请求参数" // @Success 200 {object} RespPayload @@ -552,11 +578,20 @@ func enableForwardMsg(c *gin.Context) { return } + if _, ok := forwardUrls[req.Url]; ok { + c.Set("Error", "url already exists") + return + } + err := wc.EnrollReceiver(true, func(msg *wcferry.WxMsg) { logman.Info("forward msg", "url", req.Url, "Id", msg.Id) request.JsonPost(req.Url, msg, request.H{}) }) + if err == nil { + forwardUrls[req.Url] = true + } + c.Set("Payload", RespPayload{ Success: err == nil, Error: err, @@ -564,7 +599,7 @@ func enableForwardMsg(c *gin.Context) { } -// @Summary 关闭消息转发 +// @Summary 关闭转发消息到URL // @Produce json // @Param body body ForwardMsgRequest true "消息转发请求参数" // @Success 200 {object} RespPayload diff --git a/clients/gohttp/httpd/wcfrest/router.go b/clients/gohttp/httpd/wcfrest/router.go index 8fcb424..2851225 100644 --- a/clients/gohttp/httpd/wcfrest/router.go +++ b/clients/gohttp/httpd/wcfrest/router.go @@ -30,6 +30,7 @@ func Route(rg *gin.RouterGroup) { rg.POST("del_chatroom_members", delChatRoomMembers) rg.GET("revoke_msg/:msgid", revokeMsg) + rg.POST("forward_msg", forwardMsg) rg.POST("send_txt", sendTxt) rg.POST("send_img", sendImg) rg.POST("send_file", sendFile) diff --git a/clients/gohttp/public/assets/icon.png b/clients/gohttp/public/assets/icon.png deleted file mode 100644 index e69de29..0000000 diff --git a/clients/gohttp/public/assets/image/icon.svg b/clients/gohttp/public/assets/image/icon.svg new file mode 100644 index 0000000..bfb4e71 --- /dev/null +++ b/clients/gohttp/public/assets/image/icon.svg @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/clients/gohttp/public/index.html b/clients/gohttp/public/index.html index 35057f6..a7ed7b5 100644 --- a/clients/gohttp/public/index.html +++ b/clients/gohttp/public/index.html @@ -1,12 +1,14 @@ - + - WeChat Rest API + - - + + + + WeChat Rest API diff --git a/clients/gohttp/public/swagger.json b/clients/gohttp/public/swagger.json index 44eb12a..a65c009 100644 --- a/clients/gohttp/public/swagger.json +++ b/clients/gohttp/public/swagger.json @@ -274,7 +274,7 @@ "produces": [ "application/json" ], - "summary": "关闭消息转发", + "summary": "关闭转发消息到URL", "parameters": [ { "description": "消息转发请求参数", @@ -355,7 +355,7 @@ "produces": [ "application/json" ], - "summary": "开启消息转发", + "summary": "开启转发消息到URL", "parameters": [ { "description": "消息转发请求参数", @@ -377,6 +377,33 @@ } } }, + "/forward_msg": { + "post": { + "produces": [ + "application/json" + ], + "summary": "转发消息", + "parameters": [ + { + "description": "转发消息请求参数", + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/wcferry.ForwardMsg" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/wcfrest.RespPayload" + } + } + } + } + }, "/friends": { "get": { "produces": [ @@ -796,6 +823,19 @@ } } }, + "wcferry.ForwardMsg": { + "type": "object", + "properties": { + "id": { + "description": "待转发消息 ID", + "type": "integer" + }, + "receiver": { + "description": "转发接收目标,群为 roomId,个人为 wxid", + "type": "string" + } + } + }, "wcferry.MemberMgmt": { "type": "object", "properties": {