message.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package message
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/silenceper/wechat/v2/util"
  6. )
  7. const (
  8. // 发送应用消息的接口地址
  9. sendURL = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s"
  10. )
  11. type (
  12. // SendRequestCommon 发送应用消息请求公共参数
  13. SendRequestCommon struct {
  14. // 指定接收消息的成员,成员ID列表(多个接收者用‘|’分隔,最多支持1000个)。 特殊情况:指定为"@all",则向该企业应用的全部成员发送
  15. ToUser string `json:"touser"`
  16. // 指定接收消息的部门,部门ID列表,多个接收者用‘|’分隔,最多支持100个。 当touser为"@all"时忽略本参数
  17. ToParty string `json:"toparty"`
  18. // 指定接收消息的标签,标签ID列表,多个接收者用‘|’分隔,最多支持100个。 当touser为"@all"时忽略本参数
  19. ToTag string `json:"totag"`
  20. // 消息类型,此时固定为:text
  21. MsgType string `json:"msgtype"`
  22. // 企业应用的id,整型。企业内部开发,可在应用的设置页面查看;第三方服务商,可通过接口 获取企业授权信息 获取该参数值
  23. AgentID string `json:"agentid"`
  24. // 表示是否是保密消息,0表示可对外分享,1表示不能分享且内容显示水印,默认为0
  25. Safe int `json:"safe"`
  26. // 表示是否开启id转译,0表示否,1表示是,默认0。仅第三方应用需要用到,企业自建应用可以忽略。
  27. EnableIDTrans int `json:"enable_id_trans"`
  28. // 表示是否开启重复消息检查,0表示否,1表示是,默认0
  29. EnableDuplicateCheck int `json:"enable_duplicate_check"`
  30. // 表示是否重复消息检查的时间间隔,默认1800s,最大不超过4小时
  31. DuplicateCheckInterval int `json:"duplicate_check_interval"`
  32. }
  33. // SendResponse 发送应用消息响应参数
  34. SendResponse struct {
  35. util.CommonError
  36. InvalidUser string `json:"invaliduser"` // 不合法的userid,不区分大小写,统一转为小写
  37. InvalidParty string `json:"invalidparty"` // 不合法的partyid
  38. InvalidTag string `json:"invalidtag"` // 不合法的标签id
  39. UnlicensedUser string `json:"unlicenseduser"` // 没有基础接口许可(包含已过期)的userid
  40. MsgID string `json:"msgid"` // 消息id
  41. ResponseCode string `json:"response_code"`
  42. }
  43. // SendTextRequest 发送文本消息的请求
  44. SendTextRequest struct {
  45. *SendRequestCommon
  46. Text TextField `json:"text"`
  47. }
  48. // TextField 文本消息参数
  49. TextField struct {
  50. // 消息内容,最长不超过2048个字节,超过将截断(支持id转译)
  51. Content string `json:"content"`
  52. }
  53. // SendImageRequest 发送图片消息的请求
  54. SendImageRequest struct {
  55. *SendRequestCommon
  56. Image ImageField `json:"image"`
  57. }
  58. // ImageField 图片消息参数
  59. ImageField struct {
  60. // 图片媒体文件id,可以调用上传临时素材接口获取
  61. MediaID string `json:"media_id"`
  62. }
  63. // SendVoiceRequest 发送语音消息的请求
  64. SendVoiceRequest struct {
  65. *SendRequestCommon
  66. Voice VoiceField `json:"voice"`
  67. }
  68. // VoiceField 语音消息参数
  69. VoiceField struct {
  70. // 语音文件id,可以调用上传临时素材接口获取
  71. MediaID string `json:"media_id"`
  72. }
  73. )
  74. // Send 发送应用消息
  75. // @desc 实现企业微信发送应用消息接口:https://developer.work.weixin.qq.com/document/path/90236
  76. func (r *Client) Send(apiName string, request interface{}) (*SendResponse, error) {
  77. // 获取accessToken
  78. accessToken, err := r.GetAccessToken()
  79. if err != nil {
  80. return nil, err
  81. }
  82. // 请求参数转 JSON 格式
  83. jsonData, err := json.Marshal(request)
  84. if err != nil {
  85. return nil, err
  86. }
  87. // 发起http请求
  88. response, err := util.HTTPPost(fmt.Sprintf(sendURL, accessToken), string(jsonData))
  89. if err != nil {
  90. return nil, err
  91. }
  92. // 按照结构体解析返回值
  93. result := &SendResponse{}
  94. err = util.DecodeWithError(response, result, apiName)
  95. // 返回数据
  96. return result, err
  97. }
  98. // SendText 发送文本消息
  99. func (r *Client) SendText(request SendTextRequest) (*SendResponse, error) {
  100. // 发送文本消息MsgType参数固定为:text
  101. request.MsgType = "text"
  102. return r.Send("MessageSendText", request)
  103. }
  104. // SendImage 发送图片消息
  105. func (r *Client) SendImage(request SendImageRequest) (*SendResponse, error) {
  106. // 发送图片消息MsgType参数固定为:image
  107. request.MsgType = "image"
  108. return r.Send("MessageSendImage", request)
  109. }
  110. // SendVoice 发送语音消息
  111. func (r *Client) SendVoice(request SendVoiceRequest) (*SendResponse, error) {
  112. // 发送语音消息MsgType参数固定为:voice
  113. request.MsgType = "voice"
  114. return r.Send("MessageSendVoice", request)
  115. }
  116. // 以上实现了部分常用消息推送:SendText 发送文本消息、SendImage 发送图片消息、SendVoice 发送语音消息,
  117. // 如需扩展其他消息类型,建议按照以上格式,扩展对应消息类型的参数即可
  118. // 也可以直接使用Send方法,按照企业微信消息推送的接口文档传对应消息类型的参数来使用