msg.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. // GetGroupMsgListV2URL 获取群发记录列表
  10. GetGroupMsgListV2URL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_groupmsg_list_v2?access_token=%s"
  11. // GetGroupMsgTaskURL 获取群发成员发送任务列表
  12. GetGroupMsgTaskURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_groupmsg_task?access_token=%s"
  13. // GetGroupMsgSendResultURL 获取企业群发成员执行结果
  14. GetGroupMsgSendResultURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_groupmsg_send_result?access_token=%s"
  15. // SendWelcomeMsgURL 发送新客户欢迎语
  16. SendWelcomeMsgURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/send_welcome_msg?access_token=%s"
  17. )
  18. // AddMsgTemplateRequest 创建企业群发请求
  19. type AddMsgTemplateRequest struct {
  20. ChatType string `json:"chat_type"`
  21. ExternalUserID []string `json:"external_userid"`
  22. Sender string `json:"sender,omitempty"`
  23. Text MsgText `json:"text"`
  24. Attachments []*Attachment `json:"attachments"`
  25. }
  26. // MsgText 文本消息
  27. type MsgText struct {
  28. Content string `json:"content"`
  29. }
  30. type (
  31. // Attachment 附件
  32. Attachment struct {
  33. MsgType string `json:"msgtype"`
  34. Image AttachmentImg `json:"image,omitempty"`
  35. Link AttachmentLink `json:"link,omitempty"`
  36. MiniProgram AttachmentMiniProgram `json:"miniprogram,omitempty"`
  37. Video AttachmentVideo `json:"video,omitempty"`
  38. File AttachmentFile `json:"file,omitempty"`
  39. }
  40. // AttachmentImg 图片消息
  41. AttachmentImg struct {
  42. MediaID string `json:"media_id"`
  43. PicURL string `json:"pic_url"`
  44. }
  45. // AttachmentLink 图文消息
  46. AttachmentLink struct {
  47. Title string `json:"title"`
  48. PicURL string `json:"picurl"`
  49. Desc string `json:"desc"`
  50. URL string `json:"url"`
  51. }
  52. // AttachmentMiniProgram 小程序消息
  53. AttachmentMiniProgram struct {
  54. Title string `json:"title"`
  55. PicMediaID string `json:"pic_media_id"`
  56. AppID string `json:"appid"`
  57. Page string `json:"page"`
  58. }
  59. // AttachmentVideo 视频消息
  60. AttachmentVideo struct {
  61. MediaID string `json:"media_id"`
  62. }
  63. // AttachmentFile 文件消息
  64. AttachmentFile struct {
  65. MediaID string `json:"media_id"`
  66. }
  67. )
  68. // AddMsgTemplateResponse 创建企业群发响应
  69. type AddMsgTemplateResponse struct {
  70. util.CommonError
  71. FailList []string `json:"fail_list"`
  72. MsgID string `json:"msgid"`
  73. }
  74. // AddMsgTemplate 创建企业群发
  75. // see https://developer.work.weixin.qq.com/document/path/92135
  76. func (r *Client) AddMsgTemplate(req *AddMsgTemplateRequest) (*AddMsgTemplateResponse, error) {
  77. var (
  78. accessToken string
  79. err error
  80. )
  81. if accessToken, err = r.GetAccessToken(); err != nil {
  82. return nil, err
  83. }
  84. var response []byte
  85. if response, err = util.PostJSON(fmt.Sprintf(AddMsgTemplateURL, accessToken), req); err != nil {
  86. return nil, err
  87. }
  88. result := &AddMsgTemplateResponse{}
  89. if err = util.DecodeWithError(response, result, "AddMsgTemplate"); err != nil {
  90. return nil, err
  91. }
  92. return result, nil
  93. }
  94. // GetGroupMsgListV2Request 获取群发记录列表请求
  95. type GetGroupMsgListV2Request struct {
  96. ChatType string `json:"chat_type"`
  97. StartTime int `json:"start_time"`
  98. EndTime int `json:"end_time"`
  99. Creator string `json:"creator,omitempty"`
  100. FilterType int `json:"filter_type"`
  101. Limit int `json:"limit"`
  102. Cursor string `json:"cursor"`
  103. }
  104. // GetGroupMsgListV2Response 获取群发记录列表响应
  105. type GetGroupMsgListV2Response struct {
  106. util.CommonError
  107. NextCursor string `json:"next_cursor"`
  108. GroupMsgList []*GroupMsg `json:"group_msg_list"`
  109. }
  110. // GroupMsg 群发消息
  111. type GroupMsg struct {
  112. MsgID string `json:"msgid"`
  113. Creator string `json:"creator"`
  114. CreateTime int `json:"create_time"`
  115. CreateType int `json:"create_type"`
  116. Text MsgText `json:"text"`
  117. Attachments []*Attachment `json:"attachments"`
  118. }
  119. // GetGroupMsgListV2 获取群发记录列表
  120. // see https://developer.work.weixin.qq.com/document/path/93338#%E8%8E%B7%E5%8F%96%E7%BE%A4%E5%8F%91%E8%AE%B0%E5%BD%95%E5%88%97%E8%A1%A8
  121. func (r *Client) GetGroupMsgListV2(req *GetGroupMsgListV2Request) (*GetGroupMsgListV2Response, error) {
  122. var (
  123. accessToken string
  124. err error
  125. )
  126. if accessToken, err = r.GetAccessToken(); err != nil {
  127. return nil, err
  128. }
  129. var response []byte
  130. if response, err = util.PostJSON(fmt.Sprintf(GetGroupMsgListV2URL, accessToken), req); err != nil {
  131. return nil, err
  132. }
  133. result := &GetGroupMsgListV2Response{}
  134. if err = util.DecodeWithError(response, result, "GetGroupMsgListV2"); err != nil {
  135. return nil, err
  136. }
  137. return result, nil
  138. }
  139. // GetGroupMsgTaskRequest 获取群发成员发送任务列表请求
  140. type GetGroupMsgTaskRequest struct {
  141. MsgID string `json:"msgid"`
  142. Limit int `json:"limit"`
  143. Cursor string `json:"cursor"`
  144. }
  145. // GetGroupMsgTaskResponse 获取群发成员发送任务列表响应
  146. type GetGroupMsgTaskResponse struct {
  147. util.CommonError
  148. NextCursor string `json:"next_cursor"`
  149. TaskList []*Task `json:"task_list"`
  150. }
  151. // Task 获取群发成员发送任务列表任务
  152. type Task struct {
  153. UserID string `json:"userid"`
  154. Status int `json:"status"`
  155. SendTime int `json:"send_time"`
  156. }
  157. // GetGroupMsgTask 获取群发成员发送任务列表
  158. // see https://developer.work.weixin.qq.com/document/path/93338#%E8%8E%B7%E5%8F%96%E7%BE%A4%E5%8F%91%E6%88%90%E5%91%98%E5%8F%91%E9%80%81%E4%BB%BB%E5%8A%A1%E5%88%97%E8%A1%A8
  159. func (r *Client) GetGroupMsgTask(req *GetGroupMsgTaskRequest) (*GetGroupMsgTaskResponse, error) {
  160. var (
  161. accessToken string
  162. err error
  163. )
  164. if accessToken, err = r.GetAccessToken(); err != nil {
  165. return nil, err
  166. }
  167. var response []byte
  168. if response, err = util.PostJSON(fmt.Sprintf(GetGroupMsgTaskURL, accessToken), req); err != nil {
  169. return nil, err
  170. }
  171. result := &GetGroupMsgTaskResponse{}
  172. if err = util.DecodeWithError(response, result, "GetGroupMsgTask"); err != nil {
  173. return nil, err
  174. }
  175. return result, nil
  176. }
  177. // GetGroupMsgSendResultRequest 获取企业群发成员执行结果请求
  178. type GetGroupMsgSendResultRequest struct {
  179. MsgID string `json:"msgid"`
  180. UserID string `json:"userid"`
  181. Limit int `json:"limit"`
  182. Cursor string `json:"cursor"`
  183. }
  184. // GetGroupMsgSendResultResponse 获取企业群发成员执行结果响应
  185. type GetGroupMsgSendResultResponse struct {
  186. util.CommonError
  187. NextCursor string `json:"next_cursor"`
  188. SendList []*Send `json:"send_list"`
  189. }
  190. // Send 企业群发成员执行结果
  191. type Send struct {
  192. ExternalUserID string `json:"external_userid"`
  193. ChatID string `json:"chat_id"`
  194. UserID string `json:"userid"`
  195. Status int `json:"status"`
  196. SendTime int `json:"send_time"`
  197. }
  198. // GetGroupMsgSendResult 获取企业群发成员执行结果
  199. // see https://developer.work.weixin.qq.com/document/path/93338#%E8%8E%B7%E5%8F%96%E4%BC%81%E4%B8%9A%E7%BE%A4%E5%8F%91%E6%88%90%E5%91%98%E6%89%A7%E8%A1%8C%E7%BB%93%E6%9E%9C
  200. func (r *Client) GetGroupMsgSendResult(req *GetGroupMsgSendResultRequest) (*GetGroupMsgSendResultResponse, error) {
  201. var (
  202. accessToken string
  203. err error
  204. )
  205. if accessToken, err = r.GetAccessToken(); err != nil {
  206. return nil, err
  207. }
  208. var response []byte
  209. if response, err = util.PostJSON(fmt.Sprintf(GetGroupMsgSendResultURL, accessToken), req); err != nil {
  210. return nil, err
  211. }
  212. result := &GetGroupMsgSendResultResponse{}
  213. if err = util.DecodeWithError(response, result, "GetGroupMsgSendResult"); err != nil {
  214. return nil, err
  215. }
  216. return result, nil
  217. }
  218. // SendWelcomeMsgRequest 发送新客户欢迎语请求
  219. type SendWelcomeMsgRequest struct {
  220. WelcomeCode string `json:"welcome_code"`
  221. Text MsgText `json:"text"`
  222. Attachments []*Attachment `json:"attachments"`
  223. }
  224. // SendWelcomeMsgResponse 发送新客户欢迎语响应
  225. type SendWelcomeMsgResponse struct {
  226. util.CommonError
  227. }
  228. // SendWelcomeMsg 发送新客户欢迎语
  229. // see https://developer.work.weixin.qq.com/document/path/92137
  230. func (r *Client) SendWelcomeMsg(req *SendWelcomeMsgRequest) error {
  231. var (
  232. accessToken string
  233. err error
  234. )
  235. if accessToken, err = r.GetAccessToken(); err != nil {
  236. return err
  237. }
  238. var response []byte
  239. if response, err = util.PostJSON(fmt.Sprintf(SendWelcomeMsgURL, accessToken), req); err != nil {
  240. return err
  241. }
  242. result := &SendWelcomeMsgResponse{}
  243. if err = util.DecodeWithError(response, result, "SendWelcomeMsg"); err != nil {
  244. return err
  245. }
  246. return nil
  247. }