msg.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. // addGroupWelcomeTemplateURL 添加入群欢迎语素材
  18. addGroupWelcomeTemplateURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/group_welcome_template/add?access_token=%s"
  19. // editGroupWelcomeTemplateURL 编辑入群欢迎语素材
  20. editGroupWelcomeTemplateURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/group_welcome_template/edit?access_token=%s"
  21. // getGroupWelcomeTemplateURL 获取入群欢迎语素材
  22. getGroupWelcomeTemplateURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/group_welcome_template/get?access_token=%s"
  23. // delGroupWelcomeTemplateURL 删除入群欢迎语素材
  24. delGroupWelcomeTemplateURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/group_welcome_template/del?access_token=%s"
  25. // remindGroupMsgSendURL 提醒成员群发
  26. remindGroupMsgSendURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/remind_groupmsg_send?access_token=%s"
  27. // cancelGroupMsgSendURL 停止企业群发
  28. cancelGroupMsgSendURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/cancel_groupmsg_send?access_token=%s"
  29. )
  30. // AddMsgTemplateRequest 创建企业群发请求
  31. type AddMsgTemplateRequest struct {
  32. ChatType string `json:"chat_type"`
  33. ExternalUserID []string `json:"external_userid"`
  34. Sender string `json:"sender,omitempty"`
  35. Text MsgText `json:"text"`
  36. Attachments []*Attachment `json:"attachments"`
  37. }
  38. // MsgText 文本消息
  39. type MsgText struct {
  40. Content string `json:"content"`
  41. }
  42. type (
  43. // Attachment 附件
  44. Attachment struct {
  45. MsgType string `json:"msgtype"`
  46. Image AttachmentImg `json:"image,omitempty"`
  47. Link AttachmentLink `json:"link,omitempty"`
  48. MiniProgram AttachmentMiniProgram `json:"miniprogram,omitempty"`
  49. Video AttachmentVideo `json:"video,omitempty"`
  50. File AttachmentFile `json:"file,omitempty"`
  51. }
  52. // AttachmentImg 图片消息
  53. AttachmentImg struct {
  54. MediaID string `json:"media_id"`
  55. PicURL string `json:"pic_url"`
  56. }
  57. // AttachmentLink 图文消息
  58. AttachmentLink struct {
  59. Title string `json:"title"`
  60. PicURL string `json:"picurl"`
  61. Desc string `json:"desc"`
  62. URL string `json:"url"`
  63. }
  64. // AttachmentMiniProgram 小程序消息
  65. AttachmentMiniProgram struct {
  66. Title string `json:"title"`
  67. PicMediaID string `json:"pic_media_id"`
  68. AppID string `json:"appid"`
  69. Page string `json:"page"`
  70. }
  71. // AttachmentVideo 视频消息
  72. AttachmentVideo struct {
  73. MediaID string `json:"media_id"`
  74. }
  75. // AttachmentFile 文件消息
  76. AttachmentFile struct {
  77. MediaID string `json:"media_id"`
  78. }
  79. )
  80. // AddMsgTemplateResponse 创建企业群发响应
  81. type AddMsgTemplateResponse struct {
  82. util.CommonError
  83. FailList []string `json:"fail_list"`
  84. MsgID string `json:"msgid"`
  85. }
  86. // AddMsgTemplate 创建企业群发
  87. // see https://developer.work.weixin.qq.com/document/path/92135
  88. func (r *Client) AddMsgTemplate(req *AddMsgTemplateRequest) (*AddMsgTemplateResponse, error) {
  89. var (
  90. accessToken string
  91. err error
  92. )
  93. if accessToken, err = r.GetAccessToken(); err != nil {
  94. return nil, err
  95. }
  96. var response []byte
  97. if response, err = util.PostJSON(fmt.Sprintf(addMsgTemplateURL, accessToken), req); err != nil {
  98. return nil, err
  99. }
  100. result := &AddMsgTemplateResponse{}
  101. err = util.DecodeWithError(response, result, "AddMsgTemplate")
  102. return result, err
  103. }
  104. // GetGroupMsgListV2Request 获取群发记录列表请求
  105. type GetGroupMsgListV2Request struct {
  106. ChatType string `json:"chat_type"`
  107. StartTime int64 `json:"start_time"`
  108. EndTime int64 `json:"end_time"`
  109. Creator string `json:"creator,omitempty"`
  110. FilterType int `json:"filter_type"`
  111. Limit int `json:"limit"`
  112. Cursor string `json:"cursor"`
  113. }
  114. // GetGroupMsgListV2Response 获取群发记录列表响应
  115. type GetGroupMsgListV2Response struct {
  116. util.CommonError
  117. NextCursor string `json:"next_cursor"`
  118. GroupMsgList []*GroupMsg `json:"group_msg_list"`
  119. }
  120. // GroupMsg 群发消息
  121. type GroupMsg struct {
  122. MsgID string `json:"msgid"`
  123. Creator string `json:"creator"`
  124. CreateTime int64 `json:"create_time"`
  125. CreateType int `json:"create_type"`
  126. Text MsgText `json:"text"`
  127. Attachments []*Attachment `json:"attachments"`
  128. }
  129. // GetGroupMsgListV2 获取群发记录列表
  130. // 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
  131. func (r *Client) GetGroupMsgListV2(req *GetGroupMsgListV2Request) (*GetGroupMsgListV2Response, error) {
  132. var (
  133. accessToken string
  134. err error
  135. )
  136. if accessToken, err = r.GetAccessToken(); err != nil {
  137. return nil, err
  138. }
  139. var response []byte
  140. if response, err = util.PostJSON(fmt.Sprintf(getGroupMsgListV2URL, accessToken), req); err != nil {
  141. return nil, err
  142. }
  143. result := &GetGroupMsgListV2Response{}
  144. err = util.DecodeWithError(response, result, "GetGroupMsgListV2")
  145. return result, err
  146. }
  147. // GetGroupMsgTaskRequest 获取群发成员发送任务列表请求
  148. type GetGroupMsgTaskRequest struct {
  149. MsgID string `json:"msgid"`
  150. Limit int `json:"limit"`
  151. Cursor string `json:"cursor"`
  152. }
  153. // GetGroupMsgTaskResponse 获取群发成员发送任务列表响应
  154. type GetGroupMsgTaskResponse struct {
  155. util.CommonError
  156. NextCursor string `json:"next_cursor"`
  157. TaskList []*Task `json:"task_list"`
  158. }
  159. // Task 获取群发成员发送任务列表任务
  160. type Task struct {
  161. UserID string `json:"userid"`
  162. Status int `json:"status"`
  163. SendTime int `json:"send_time"`
  164. }
  165. // GetGroupMsgTask 获取群发成员发送任务列表
  166. // 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
  167. func (r *Client) GetGroupMsgTask(req *GetGroupMsgTaskRequest) (*GetGroupMsgTaskResponse, error) {
  168. var (
  169. accessToken string
  170. err error
  171. )
  172. if accessToken, err = r.GetAccessToken(); err != nil {
  173. return nil, err
  174. }
  175. var response []byte
  176. if response, err = util.PostJSON(fmt.Sprintf(getGroupMsgTaskURL, accessToken), req); err != nil {
  177. return nil, err
  178. }
  179. result := &GetGroupMsgTaskResponse{}
  180. err = util.DecodeWithError(response, result, "GetGroupMsgTask")
  181. return result, err
  182. }
  183. // GetGroupMsgSendResultRequest 获取企业群发成员执行结果请求
  184. type GetGroupMsgSendResultRequest struct {
  185. MsgID string `json:"msgid"`
  186. UserID string `json:"userid"`
  187. Limit int `json:"limit"`
  188. Cursor string `json:"cursor"`
  189. }
  190. // GetGroupMsgSendResultResponse 获取企业群发成员执行结果响应
  191. type GetGroupMsgSendResultResponse struct {
  192. util.CommonError
  193. NextCursor string `json:"next_cursor"`
  194. SendList []*Send `json:"send_list"`
  195. }
  196. // Send 企业群发成员执行结果
  197. type Send struct {
  198. ExternalUserID string `json:"external_userid"`
  199. ChatID string `json:"chat_id"`
  200. UserID string `json:"userid"`
  201. Status int `json:"status"`
  202. SendTime int `json:"send_time"`
  203. }
  204. // GetGroupMsgSendResult 获取企业群发成员执行结果
  205. // 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
  206. func (r *Client) GetGroupMsgSendResult(req *GetGroupMsgSendResultRequest) (*GetGroupMsgSendResultResponse, error) {
  207. var (
  208. accessToken string
  209. err error
  210. )
  211. if accessToken, err = r.GetAccessToken(); err != nil {
  212. return nil, err
  213. }
  214. var response []byte
  215. if response, err = util.PostJSON(fmt.Sprintf(getGroupMsgSendResultURL, accessToken), req); err != nil {
  216. return nil, err
  217. }
  218. result := &GetGroupMsgSendResultResponse{}
  219. err = util.DecodeWithError(response, result, "GetGroupMsgSendResult")
  220. return result, err
  221. }
  222. // SendWelcomeMsgRequest 发送新客户欢迎语请求
  223. type SendWelcomeMsgRequest struct {
  224. WelcomeCode string `json:"welcome_code"`
  225. Text MsgText `json:"text"`
  226. Attachments []*Attachment `json:"attachments"`
  227. }
  228. // SendWelcomeMsgResponse 发送新客户欢迎语响应
  229. type SendWelcomeMsgResponse struct {
  230. util.CommonError
  231. }
  232. // SendWelcomeMsg 发送新客户欢迎语
  233. // see https://developer.work.weixin.qq.com/document/path/92137
  234. func (r *Client) SendWelcomeMsg(req *SendWelcomeMsgRequest) error {
  235. var (
  236. accessToken string
  237. err error
  238. )
  239. if accessToken, err = r.GetAccessToken(); err != nil {
  240. return err
  241. }
  242. var response []byte
  243. if response, err = util.PostJSON(fmt.Sprintf(sendWelcomeMsgURL, accessToken), req); err != nil {
  244. return err
  245. }
  246. result := &SendWelcomeMsgResponse{}
  247. return util.DecodeWithError(response, result, "SendWelcomeMsg")
  248. }
  249. // AddGroupWelcomeTemplateRequest 添加入群欢迎语素材请求
  250. type AddGroupWelcomeTemplateRequest struct {
  251. Text MsgText `json:"text"`
  252. Image AttachmentImg `json:"image"`
  253. Link AttachmentLink `json:"link"`
  254. MiniProgram AttachmentMiniProgram `json:"miniprogram"`
  255. File AttachmentFile `json:"file"`
  256. Video AttachmentVideo `json:"video"`
  257. AgentID int `json:"agentid"`
  258. Notify int `json:"notify"`
  259. }
  260. // AddGroupWelcomeTemplateResponse 添加入群欢迎语素材响应
  261. type AddGroupWelcomeTemplateResponse struct {
  262. util.CommonError
  263. TemplateID string `json:"template_id"`
  264. }
  265. // AddGroupWelcomeTemplate 添加入群欢迎语素材
  266. // see https://developer.work.weixin.qq.com/document/path/92366#%E6%B7%BB%E5%8A%A0%E5%85%A5%E7%BE%A4%E6%AC%A2%E8%BF%8E%E8%AF%AD%E7%B4%A0%E6%9D%90
  267. func (r *Client) AddGroupWelcomeTemplate(req *AddGroupWelcomeTemplateRequest) (*AddGroupWelcomeTemplateResponse, error) {
  268. var (
  269. accessToken string
  270. err error
  271. )
  272. if accessToken, err = r.GetAccessToken(); err != nil {
  273. return nil, err
  274. }
  275. var response []byte
  276. if response, err = util.PostJSON(fmt.Sprintf(addGroupWelcomeTemplateURL, accessToken), req); err != nil {
  277. return nil, err
  278. }
  279. result := &AddGroupWelcomeTemplateResponse{}
  280. err = util.DecodeWithError(response, result, "AddGroupWelcomeTemplate")
  281. return result, err
  282. }
  283. // EditGroupWelcomeTemplateRequest 编辑入群欢迎语素材请求
  284. type EditGroupWelcomeTemplateRequest struct {
  285. TemplateID string `json:"template_id"`
  286. Text MsgText `json:"text"`
  287. Image AttachmentImg `json:"image"`
  288. Link AttachmentLink `json:"link"`
  289. MiniProgram AttachmentMiniProgram `json:"miniprogram"`
  290. File AttachmentFile `json:"file"`
  291. Video AttachmentVideo `json:"video"`
  292. AgentID int `json:"agentid"`
  293. }
  294. // EditGroupWelcomeTemplateResponse 编辑入群欢迎语素材响应
  295. type EditGroupWelcomeTemplateResponse struct {
  296. util.CommonError
  297. }
  298. // EditGroupWelcomeTemplate 编辑入群欢迎语素材
  299. // see https://developer.work.weixin.qq.com/document/path/92366#%E7%BC%96%E8%BE%91%E5%85%A5%E7%BE%A4%E6%AC%A2%E8%BF%8E%E8%AF%AD%E7%B4%A0%E6%9D%90
  300. func (r *Client) EditGroupWelcomeTemplate(req *EditGroupWelcomeTemplateRequest) error {
  301. var (
  302. accessToken string
  303. err error
  304. )
  305. if accessToken, err = r.GetAccessToken(); err != nil {
  306. return err
  307. }
  308. var response []byte
  309. if response, err = util.PostJSON(fmt.Sprintf(editGroupWelcomeTemplateURL, accessToken), req); err != nil {
  310. return err
  311. }
  312. result := &EditGroupWelcomeTemplateResponse{}
  313. return util.DecodeWithError(response, result, "EditGroupWelcomeTemplate")
  314. }
  315. // GetGroupWelcomeTemplateRequest 获取入群欢迎语素材请求
  316. type GetGroupWelcomeTemplateRequest struct {
  317. TemplateID string `json:"template_id"`
  318. }
  319. // GetGroupWelcomeTemplateResponse 获取入群欢迎语素材响应
  320. type GetGroupWelcomeTemplateResponse struct {
  321. util.CommonError
  322. Text MsgText `json:"text"`
  323. Image AttachmentImg `json:"image"`
  324. Link AttachmentLink `json:"link"`
  325. MiniProgram AttachmentMiniProgram `json:"miniprogram"`
  326. File AttachmentFile `json:"file"`
  327. Video AttachmentVideo `json:"video"`
  328. }
  329. // GetGroupWelcomeTemplate 获取入群欢迎语素材
  330. // see https://developer.work.weixin.qq.com/document/path/92366#%E8%8E%B7%E5%8F%96%E5%85%A5%E7%BE%A4%E6%AC%A2%E8%BF%8E%E8%AF%AD%E7%B4%A0%E6%9D%90
  331. func (r *Client) GetGroupWelcomeTemplate(req *GetGroupWelcomeTemplateRequest) (*GetGroupWelcomeTemplateResponse, error) {
  332. var (
  333. accessToken string
  334. err error
  335. )
  336. if accessToken, err = r.GetAccessToken(); err != nil {
  337. return nil, err
  338. }
  339. var response []byte
  340. if response, err = util.PostJSON(fmt.Sprintf(getGroupWelcomeTemplateURL, accessToken), req); err != nil {
  341. return nil, err
  342. }
  343. result := &GetGroupWelcomeTemplateResponse{}
  344. err = util.DecodeWithError(response, result, "GetGroupWelcomeTemplate")
  345. return result, err
  346. }
  347. // DelGroupWelcomeTemplateRequest 删除入群欢迎语素材请求
  348. type DelGroupWelcomeTemplateRequest struct {
  349. TemplateID string `json:"template_id"`
  350. AgentID int `json:"agentid"`
  351. }
  352. // DelGroupWelcomeTemplateResponse 删除入群欢迎语素材响应
  353. type DelGroupWelcomeTemplateResponse struct {
  354. util.CommonError
  355. }
  356. // DelGroupWelcomeTemplate 删除入群欢迎语素材
  357. // see https://developer.work.weixin.qq.com/document/path/92366#%E5%88%A0%E9%99%A4%E5%85%A5%E7%BE%A4%E6%AC%A2%E8%BF%8E%E8%AF%AD%E7%B4%A0%E6%9D%90
  358. func (r *Client) DelGroupWelcomeTemplate(req *DelGroupWelcomeTemplateRequest) error {
  359. var (
  360. accessToken string
  361. err error
  362. )
  363. if accessToken, err = r.GetAccessToken(); err != nil {
  364. return err
  365. }
  366. var response []byte
  367. if response, err = util.PostJSON(fmt.Sprintf(delGroupWelcomeTemplateURL, accessToken), req); err != nil {
  368. return err
  369. }
  370. result := &DelGroupWelcomeTemplateResponse{}
  371. return util.DecodeWithError(response, result, "DelGroupWelcomeTemplate")
  372. }
  373. // RemindGroupMsgSendRequest 提醒成员群发请求
  374. type RemindGroupMsgSendRequest struct {
  375. MsgID string `json:"msgid"`
  376. }
  377. // RemindGroupMsgSend 提醒成员群发
  378. // see https://developer.work.weixin.qq.com/document/path/97610
  379. func (r *Client) RemindGroupMsgSend(req *RemindGroupMsgSendRequest) error {
  380. var (
  381. accessToken string
  382. err error
  383. )
  384. if accessToken, err = r.GetAccessToken(); err != nil {
  385. return err
  386. }
  387. var response []byte
  388. if response, err = util.PostJSON(fmt.Sprintf(remindGroupMsgSendURL, accessToken), req); err != nil {
  389. return err
  390. }
  391. return util.DecodeWithCommonError(response, "RemindGroupMsgSend")
  392. }
  393. // CancelGroupMsgSendRequest 停止企业群发请求
  394. type CancelGroupMsgSendRequest struct {
  395. MsgID string `json:"msgid"`
  396. }
  397. // CancelGroupMsgSend 提醒成员群发
  398. // see https://developer.work.weixin.qq.com/document/path/97611
  399. func (r *Client) CancelGroupMsgSend(req *CancelGroupMsgSendRequest) error {
  400. var (
  401. accessToken string
  402. err error
  403. )
  404. if accessToken, err = r.GetAccessToken(); err != nil {
  405. return err
  406. }
  407. var response []byte
  408. if response, err = util.PostJSON(fmt.Sprintf(cancelGroupMsgSendURL, accessToken), req); err != nil {
  409. return err
  410. }
  411. return util.DecodeWithCommonError(response, "CancelGroupMsgSend")
  412. }