From 0a37184c2f5cda6ebdb67b425bd0eb3958215a37 Mon Sep 17 00:00:00 2001 From: just5325 <532508307@qq.com> Date: Tue, 16 May 2023 19:22:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=BA=94=E7=94=A8=E6=8E=A8?= =?UTF-8?q?=E9=80=81=E6=B6=88=E6=81=AF=E6=A8=A1=E5=9D=97=EF=BC=8C=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E9=83=A8=E5=88=86=E5=B8=B8=E7=94=A8=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E6=8E=A8=E9=80=81=EF=BC=9ASendText=20=E5=8F=91=E9=80=81?= =?UTF-8?q?=E6=96=87=E6=9C=AC=E6=B6=88=E6=81=AF=E3=80=81SendImage=20?= =?UTF-8?q?=E5=8F=91=E9=80=81=E5=9B=BE=E7=89=87=E6=B6=88=E6=81=AF=E3=80=81?= =?UTF-8?q?SendVoice=20=E5=8F=91=E9=80=81=E8=AF=AD=E9=9F=B3=E6=B6=88?= =?UTF-8?q?=E6=81=AF=20=E4=BC=81=E4=B8=9A=E5=BE=AE=E4=BF=A1=E5=BA=94?= =?UTF-8?q?=E7=94=A8=E6=8E=A8=E9=80=81=E6=B6=88=E6=81=AF=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=EF=BC=9Ahttps://qyapi.weixin.qq.com/cgi-bin/appchat/send=20(#6?= =?UTF-8?q?78)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 修正work.GetMessage()方法的注释信息 (cherry picked from commit 3dfaf9222910ae4ad977e2852908692d04118abc) * 新增应用推送消息模块,实现部分常用消息推送:SendText 发送文本消息、SendImage 发送图片消息、SendVoice 发送语音消息 企业微信应用推送消息接口:https://qyapi.weixin.qq.com/cgi-bin/appchat/send (cherry picked from commit 654ea158d6590a006a8c78ac3c38eafe361fe303) --- work/appchat/appchat.go | 115 ++++++++++++++++++++++++++++++++++++++++ work/appchat/client.go | 16 ++++++ work/work.go | 8 ++- 3 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 work/appchat/appchat.go create mode 100644 work/appchat/client.go diff --git a/work/appchat/appchat.go b/work/appchat/appchat.go new file mode 100644 index 0000000..65d5650 --- /dev/null +++ b/work/appchat/appchat.go @@ -0,0 +1,115 @@ +package appchat + +import ( + "encoding/json" + "fmt" + + "github.com/silenceper/wechat/v2/util" +) + +const ( + // 应用推送消息接口地址 + sendURL = "https://qyapi.weixin.qq.com/cgi-bin/appchat/send?access_token=%s" +) + +type ( + // SendRequestCommon 发送应用推送消息请求公共参数 + SendRequestCommon struct { + // 群聊id + ChatID string `json:"chatid"` + // 消息类型 + MsgType string `json:"msgtype"` + // 表示是否是保密消息,0表示否,1表示是,默认0 + Safe int `json:"safe"` + } + + // SendResponse 发送应用消息响应参数 + SendResponse struct { + util.CommonError + } + + // SendTextRequest 发送文本消息的请求 + SendTextRequest struct { + *SendRequestCommon + Text TextField `json:"text"` + } + // TextField 文本消息参数 + TextField struct { + // 消息内容,最长不超过2048个字节 + Content string `json:"content"` + } + + // SendImageRequest 发送图片消息的请求 + SendImageRequest struct { + *SendRequestCommon + Image ImageField `json:"image"` + } + // ImageField 图片消息参数 + ImageField struct { + // 图片媒体文件id,可以调用上传临时素材接口获取 + MediaID string `json:"media_id"` + } + + // SendVoiceRequest 发送语音消息的请求 + SendVoiceRequest struct { + *SendRequestCommon + Voice VoiceField `json:"voice"` + } + // VoiceField 语音消息参数 + VoiceField struct { + // 语音文件id,可以调用上传临时素材接口获取 + MediaID string `json:"media_id"` + } +) + +// Send 发送应用消息 +// @desc 实现企业微信发送应用消息接口:https://developer.work.weixin.qq.com/document/path/90248 +func (r *Client) Send(apiName string, request interface{}) (*SendResponse, error) { + // 获取accessToken + accessToken, err := r.GetAccessToken() + if err != nil { + return nil, err + } + // 请求参数转 JSON 格式 + jsonData, err := json.Marshal(request) + if err != nil { + return nil, err + } + // 发起http请求 + response, err := util.HTTPPost(fmt.Sprintf(sendURL, accessToken), string(jsonData)) + if err != nil { + return nil, err + } + // 按照结构体解析返回值 + result := &SendResponse{} + if err = util.DecodeWithError(response, result, apiName); err != nil { + return nil, err + } + // 返回数据 + return result, nil +} + +// SendText 发送文本消息 +func (r *Client) SendText(request SendTextRequest) (*SendResponse, error) { + // 发送文本消息MsgType参数固定为:text + request.MsgType = "text" + return r.Send("MessageSendText", request) +} + +// SendImage 发送图片消息 +func (r *Client) SendImage(request SendImageRequest) (*SendResponse, error) { + // 发送图片消息MsgType参数固定为:image + request.MsgType = "image" + return r.Send("MessageSendImage", request) +} + +// SendVoice 发送语音消息 +func (r *Client) SendVoice(request SendVoiceRequest) (*SendResponse, error) { + // 发送语音消息MsgType参数固定为:voice + request.MsgType = "voice" + return r.Send("MessageSendVoice", request) +} + +// 以上实现了部分常用消息推送:SendText 发送文本消息、SendImage 发送图片消息、SendVoice 发送语音消息, +// 如需扩展其他消息类型,建议按照以上格式,扩展对应消息类型的参数即可 +// 也可以直接使用Send方法,按照企业微信消息推送的接口文档传对应消息类型的参数来使用 diff --git a/work/appchat/client.go b/work/appchat/client.go new file mode 100644 index 0000000..246ad35 --- /dev/null +++ b/work/appchat/client.go @@ -0,0 +1,16 @@ +// Package appchat 应用发送消息到群聊会话,企业微信接口:https://developer.work.weixin.qq.com/document/path/90248 +package appchat + +import ( + "github.com/silenceper/wechat/v2/work/context" +) + +// Client 接口实例 +type Client struct { + *context.Context +} + +// NewClient 初始化实例 +func NewClient(ctx *context.Context) *Client { + return &Client{ctx} +} diff --git a/work/work.go b/work/work.go index 869809b..d394658 100644 --- a/work/work.go +++ b/work/work.go @@ -3,6 +3,7 @@ package work import ( "github.com/silenceper/wechat/v2/credential" "github.com/silenceper/wechat/v2/work/addresslist" + "github.com/silenceper/wechat/v2/work/appchat" "github.com/silenceper/wechat/v2/work/config" "github.com/silenceper/wechat/v2/work/context" "github.com/silenceper/wechat/v2/work/externalcontact" @@ -69,7 +70,12 @@ func (wk *Work) GetRobot() *robot.Client { return robot.NewClient(wk.ctx) } -// GetMessage get robot +// GetMessage 获取发送应用消息接口实例 func (wk *Work) GetMessage() *message.Client { return message.NewClient(wk.ctx) } + +// GetAppChat 获取应用发送消息到群聊会话接口实例 +func (wk *Work) GetAppChat() *appchat.Client { + return appchat.NewClient(wk.ctx) +}