msg.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package externalcontact
  2. import (
  3. "fmt"
  4. "github.com/silenceper/wechat/v2/util"
  5. )
  6. const (
  7. // AddMsgTemplateURL 创建企业群发
  8. AddMsgTemplateURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/add_msg_template?access_token=%s"
  9. // GetGroupMsgTaskURL 获取群发成员发送任务列表
  10. GetGroupMsgTaskURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_groupmsg_task?access_token=%s"
  11. // GetGroupMsgSendResultURL 获取企业群发成员执行结果
  12. GetGroupMsgSendResultURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_groupmsg_send_result?access_token=%s"
  13. // SendWelcomeMsgURL 发送新客户欢迎语
  14. SendWelcomeMsgURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/send_welcome_msg?access_token=%s"
  15. )
  16. // AddMsgTemplateRequest 创建企业群发请求
  17. type AddMsgTemplateRequest struct {
  18. ChatType string `json:"chat_type"`
  19. ExternalUserID []string `json:"external_userid"`
  20. Sender string `json:"sender,omitempty"`
  21. Text MsgText `json:"text"`
  22. Attachments []*Attachment `json:"attachments"`
  23. }
  24. // MsgText 文本消息
  25. type MsgText struct {
  26. Content string `json:"content"`
  27. }
  28. type (
  29. // Attachment 附件
  30. Attachment struct {
  31. MsgType string `json:"msgtype"`
  32. Image AttachmentImg `json:"image,omitempty"`
  33. Link AttachmentLink `json:"link,omitempty"`
  34. MiniProgram AttachmentMiniProgram `json:"miniprogram,omitempty"`
  35. Video AttachmentVideo `json:"video,omitempty"`
  36. File AttachmentFile `json:"file,omitempty"`
  37. }
  38. // AttachmentImg 图片消息
  39. AttachmentImg struct {
  40. MediaID string `json:"media_id"`
  41. PicURL string `json:"pic_url"`
  42. }
  43. // AttachmentLink 图文消息
  44. AttachmentLink struct {
  45. Title string `json:"title"`
  46. PicURL string `json:"picurl"`
  47. Desc string `json:"desc"`
  48. URL string `json:"url"`
  49. }
  50. // AttachmentMiniProgram 小程序消息
  51. AttachmentMiniProgram struct {
  52. Title string `json:"title"`
  53. PicMediaID string `json:"pic_media_id"`
  54. AppID string `json:"appid"`
  55. Page string `json:"page"`
  56. }
  57. // AttachmentVideo 视频消息
  58. AttachmentVideo struct {
  59. MediaID string `json:"media_id"`
  60. }
  61. // AttachmentFile 文件消息
  62. AttachmentFile struct {
  63. MediaID string `json:"media_id"`
  64. }
  65. )
  66. // AddMsgTemplateResponse 创建企业群发响应
  67. type AddMsgTemplateResponse struct {
  68. util.CommonError
  69. FailList []string `json:"fail_list"`
  70. MsgID string `json:"msgid"`
  71. }
  72. // AddMsgTemplate 创建企业群发
  73. // see https://developer.work.weixin.qq.com/document/path/92135
  74. func (r *Client) AddMsgTemplate(req *AddMsgTemplateRequest) (*AddMsgTemplateResponse, error) {
  75. var (
  76. accessToken string
  77. err error
  78. )
  79. if accessToken, err = r.GetAccessToken(); err != nil {
  80. return nil, err
  81. }
  82. var response []byte
  83. if response, err = util.PostJSON(fmt.Sprintf(AddMsgTemplateURL, accessToken), req); err != nil {
  84. return nil, err
  85. }
  86. result := &AddMsgTemplateResponse{}
  87. if err = util.DecodeWithError(response, result, "AddMsgTemplate"); err != nil {
  88. return nil, err
  89. }
  90. return result, nil
  91. }
  92. // GetGroupMsgTaskRequest 获取群发成员发送任务列表请求
  93. type GetGroupMsgTaskRequest struct {
  94. MsgID string `json:"msgid"`
  95. Limit int `json:"limit"`
  96. Cursor string `json:"cursor"`
  97. }
  98. // GetGroupMsgTaskResponse 获取群发成员发送任务列表响应
  99. type GetGroupMsgTaskResponse struct {
  100. util.CommonError
  101. NextCursor string `json:"next_cursor"`
  102. TaskList []*Task `json:"task_list"`
  103. }
  104. // Task 获取群发成员发送任务列表任务
  105. type Task struct {
  106. UserID string `json:"userid"`
  107. Status int `json:"status"`
  108. SendTime int `json:"send_time"`
  109. }
  110. // GetGroupMsgTask 获取群发成员发送任务列表
  111. // see https://developer.work.weixin.qq.com/document/path/93338
  112. func (r *Client) GetGroupMsgTask(req *GetGroupMsgTaskRequest) (*GetGroupMsgTaskResponse, error) {
  113. var (
  114. accessToken string
  115. err error
  116. )
  117. if accessToken, err = r.GetAccessToken(); err != nil {
  118. return nil, err
  119. }
  120. var response []byte
  121. if response, err = util.PostJSON(fmt.Sprintf(GetGroupMsgTaskURL, accessToken), req); err != nil {
  122. return nil, err
  123. }
  124. result := &GetGroupMsgTaskResponse{}
  125. if err = util.DecodeWithError(response, result, "GetGroupMsgTask"); err != nil {
  126. return nil, err
  127. }
  128. return result, nil
  129. }
  130. // GetGroupMsgSendResultRequest 获取企业群发成员执行结果请求
  131. type GetGroupMsgSendResultRequest struct {
  132. MsgID string `json:"msgid"`
  133. UserID string `json:"userid"`
  134. Limit int `json:"limit"`
  135. Cursor string `json:"cursor"`
  136. }
  137. // GetGroupMsgSendResultResponse 获取企业群发成员执行结果响应
  138. type GetGroupMsgSendResultResponse struct {
  139. util.CommonError
  140. NextCursor string `json:"next_cursor"`
  141. SendList []*Send `json:"send_list"`
  142. }
  143. // Send 企业群发成员执行结果
  144. type Send struct {
  145. ExternalUserID string `json:"external_userid"`
  146. ChatID string `json:"chat_id"`
  147. UserID string `json:"userid"`
  148. Status int `json:"status"`
  149. SendTime int `json:"send_time"`
  150. }
  151. // GetGroupMsgSendResult 获取企业群发成员执行结果
  152. // see https://developer.work.weixin.qq.com/document/path/93338
  153. func (r *Client) GetGroupMsgSendResult(req *GetGroupMsgSendResultRequest) (*GetGroupMsgSendResultResponse, error) {
  154. var (
  155. accessToken string
  156. err error
  157. )
  158. if accessToken, err = r.GetAccessToken(); err != nil {
  159. return nil, err
  160. }
  161. var response []byte
  162. if response, err = util.PostJSON(fmt.Sprintf(GetGroupMsgSendResultURL, accessToken), req); err != nil {
  163. return nil, err
  164. }
  165. result := &GetGroupMsgSendResultResponse{}
  166. if err = util.DecodeWithError(response, result, "GetGroupMsgSendResult"); err != nil {
  167. return nil, err
  168. }
  169. return result, nil
  170. }
  171. // SendWelcomeMsgRequest 发送新客户欢迎语请求
  172. type SendWelcomeMsgRequest struct {
  173. WelcomeCode string `json:"welcome_code"`
  174. Text MsgText `json:"text"`
  175. Attachments []*Attachment `json:"attachments"`
  176. }
  177. // SendWelcomeMsgResponse 发送新客户欢迎语响应
  178. type SendWelcomeMsgResponse struct {
  179. util.CommonError
  180. }
  181. // SendWelcomeMsg 发送新客户欢迎语
  182. // see https://developer.work.weixin.qq.com/document/path/92137
  183. func (r *Client) SendWelcomeMsg(req *SendWelcomeMsgRequest) error {
  184. var (
  185. accessToken string
  186. err error
  187. )
  188. if accessToken, err = r.GetAccessToken(); err != nil {
  189. return err
  190. }
  191. var response []byte
  192. if response, err = util.PostJSON(fmt.Sprintf(SendWelcomeMsgURL, accessToken), req); err != nil {
  193. return err
  194. }
  195. result := &SendWelcomeMsgResponse{}
  196. if err = util.DecodeWithError(response, result, "SendWelcomeMsg"); err != nil {
  197. return err
  198. }
  199. return nil
  200. }