appchat.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package appchat
  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/appchat/send?access_token=%s"
  10. )
  11. type (
  12. // SendRequestCommon 发送应用推送消息请求公共参数
  13. SendRequestCommon struct {
  14. // 群聊id
  15. ChatID string `json:"chatid"`
  16. // 消息类型
  17. MsgType string `json:"msgtype"`
  18. // 表示是否是保密消息,0表示否,1表示是,默认0
  19. Safe int `json:"safe"`
  20. }
  21. // SendResponse 发送应用消息响应参数
  22. SendResponse struct {
  23. util.CommonError
  24. }
  25. // SendTextRequest 发送文本消息的请求
  26. SendTextRequest struct {
  27. *SendRequestCommon
  28. Text TextField `json:"text"`
  29. }
  30. // TextField 文本消息参数
  31. TextField struct {
  32. // 消息内容,最长不超过2048个字节
  33. Content string `json:"content"`
  34. }
  35. // SendImageRequest 发送图片消息的请求
  36. SendImageRequest struct {
  37. *SendRequestCommon
  38. Image ImageField `json:"image"`
  39. }
  40. // ImageField 图片消息参数
  41. ImageField struct {
  42. // 图片媒体文件id,可以调用上传临时素材接口获取
  43. MediaID string `json:"media_id"`
  44. }
  45. // SendVoiceRequest 发送语音消息的请求
  46. SendVoiceRequest struct {
  47. *SendRequestCommon
  48. Voice VoiceField `json:"voice"`
  49. }
  50. // VoiceField 语音消息参数
  51. VoiceField struct {
  52. // 语音文件id,可以调用上传临时素材接口获取
  53. MediaID string `json:"media_id"`
  54. }
  55. )
  56. // Send 发送应用消息
  57. // @desc 实现企业微信发送应用消息接口:https://developer.work.weixin.qq.com/document/path/90248
  58. func (r *Client) Send(apiName string, request interface{}) (*SendResponse, error) {
  59. // 获取accessToken
  60. accessToken, err := r.GetAccessToken()
  61. if err != nil {
  62. return nil, err
  63. }
  64. // 请求参数转 JSON 格式
  65. jsonData, err := json.Marshal(request)
  66. if err != nil {
  67. return nil, err
  68. }
  69. // 发起http请求
  70. response, err := util.HTTPPost(fmt.Sprintf(sendURL, accessToken), string(jsonData))
  71. if err != nil {
  72. return nil, err
  73. }
  74. // 按照结构体解析返回值
  75. result := &SendResponse{}
  76. if err = util.DecodeWithError(response, result, apiName); err != nil {
  77. return nil, err
  78. }
  79. // 返回数据
  80. return result, nil
  81. }
  82. // SendText 发送文本消息
  83. func (r *Client) SendText(request SendTextRequest) (*SendResponse, error) {
  84. // 发送文本消息MsgType参数固定为:text
  85. request.MsgType = "text"
  86. return r.Send("MessageSendText", request)
  87. }
  88. // SendImage 发送图片消息
  89. func (r *Client) SendImage(request SendImageRequest) (*SendResponse, error) {
  90. // 发送图片消息MsgType参数固定为:image
  91. request.MsgType = "image"
  92. return r.Send("MessageSendImage", request)
  93. }
  94. // SendVoice 发送语音消息
  95. func (r *Client) SendVoice(request SendVoiceRequest) (*SendResponse, error) {
  96. // 发送语音消息MsgType参数固定为:voice
  97. request.MsgType = "voice"
  98. return r.Send("MessageSendVoice", request)
  99. }
  100. // 以上实现了部分常用消息推送:SendText 发送文本消息、SendImage 发送图片消息、SendVoice 发送语音消息,
  101. // 如需扩展其他消息类型,建议按照以上格式,扩展对应消息类型的参数即可
  102. // 也可以直接使用Send方法,按照企业微信消息推送的接口文档传对应消息类型的参数来使用