msg.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. if err = util.DecodeWithError(response, result, "AddMsgTemplate"); err != nil {
  102. return nil, err
  103. }
  104. return result, nil
  105. }
  106. // GetGroupMsgListV2Request 获取群发记录列表请求
  107. type GetGroupMsgListV2Request struct {
  108. ChatType string `json:"chat_type"`
  109. StartTime int64 `json:"start_time"`
  110. EndTime int64 `json:"end_time"`
  111. Creator string `json:"creator,omitempty"`
  112. FilterType int `json:"filter_type"`
  113. Limit int `json:"limit"`
  114. Cursor string `json:"cursor"`
  115. }
  116. // GetGroupMsgListV2Response 获取群发记录列表响应
  117. type GetGroupMsgListV2Response struct {
  118. util.CommonError
  119. NextCursor string `json:"next_cursor"`
  120. GroupMsgList []*GroupMsg `json:"group_msg_list"`
  121. }
  122. // GroupMsg 群发消息
  123. type GroupMsg struct {
  124. MsgID string `json:"msgid"`
  125. Creator string `json:"creator"`
  126. CreateTime int64 `json:"create_time"`
  127. CreateType int `json:"create_type"`
  128. Text MsgText `json:"text"`
  129. Attachments []*Attachment `json:"attachments"`
  130. }
  131. // GetGroupMsgListV2 获取群发记录列表
  132. // 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
  133. func (r *Client) GetGroupMsgListV2(req *GetGroupMsgListV2Request) (*GetGroupMsgListV2Response, error) {
  134. var (
  135. accessToken string
  136. err error
  137. )
  138. if accessToken, err = r.GetAccessToken(); err != nil {
  139. return nil, err
  140. }
  141. var response []byte
  142. if response, err = util.PostJSON(fmt.Sprintf(getGroupMsgListV2URL, accessToken), req); err != nil {
  143. return nil, err
  144. }
  145. result := &GetGroupMsgListV2Response{}
  146. if err = util.DecodeWithError(response, result, "GetGroupMsgListV2"); err != nil {
  147. return nil, err
  148. }
  149. return result, nil
  150. }
  151. // GetGroupMsgTaskRequest 获取群发成员发送任务列表请求
  152. type GetGroupMsgTaskRequest struct {
  153. MsgID string `json:"msgid"`
  154. Limit int `json:"limit"`
  155. Cursor string `json:"cursor"`
  156. }
  157. // GetGroupMsgTaskResponse 获取群发成员发送任务列表响应
  158. type GetGroupMsgTaskResponse struct {
  159. util.CommonError
  160. NextCursor string `json:"next_cursor"`
  161. TaskList []*Task `json:"task_list"`
  162. }
  163. // Task 获取群发成员发送任务列表任务
  164. type Task struct {
  165. UserID string `json:"userid"`
  166. Status int `json:"status"`
  167. SendTime int `json:"send_time"`
  168. }
  169. // GetGroupMsgTask 获取群发成员发送任务列表
  170. // 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
  171. func (r *Client) GetGroupMsgTask(req *GetGroupMsgTaskRequest) (*GetGroupMsgTaskResponse, error) {
  172. var (
  173. accessToken string
  174. err error
  175. )
  176. if accessToken, err = r.GetAccessToken(); err != nil {
  177. return nil, err
  178. }
  179. var response []byte
  180. if response, err = util.PostJSON(fmt.Sprintf(getGroupMsgTaskURL, accessToken), req); err != nil {
  181. return nil, err
  182. }
  183. result := &GetGroupMsgTaskResponse{}
  184. if err = util.DecodeWithError(response, result, "GetGroupMsgTask"); err != nil {
  185. return nil, err
  186. }
  187. return result, nil
  188. }
  189. // GetGroupMsgSendResultRequest 获取企业群发成员执行结果请求
  190. type GetGroupMsgSendResultRequest struct {
  191. MsgID string `json:"msgid"`
  192. UserID string `json:"userid"`
  193. Limit int `json:"limit"`
  194. Cursor string `json:"cursor"`
  195. }
  196. // GetGroupMsgSendResultResponse 获取企业群发成员执行结果响应
  197. type GetGroupMsgSendResultResponse struct {
  198. util.CommonError
  199. NextCursor string `json:"next_cursor"`
  200. SendList []*Send `json:"send_list"`
  201. }
  202. // Send 企业群发成员执行结果
  203. type Send struct {
  204. ExternalUserID string `json:"external_userid"`
  205. ChatID string `json:"chat_id"`
  206. UserID string `json:"userid"`
  207. Status int `json:"status"`
  208. SendTime int `json:"send_time"`
  209. }
  210. // GetGroupMsgSendResult 获取企业群发成员执行结果
  211. // 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
  212. func (r *Client) GetGroupMsgSendResult(req *GetGroupMsgSendResultRequest) (*GetGroupMsgSendResultResponse, error) {
  213. var (
  214. accessToken string
  215. err error
  216. )
  217. if accessToken, err = r.GetAccessToken(); err != nil {
  218. return nil, err
  219. }
  220. var response []byte
  221. if response, err = util.PostJSON(fmt.Sprintf(getGroupMsgSendResultURL, accessToken), req); err != nil {
  222. return nil, err
  223. }
  224. result := &GetGroupMsgSendResultResponse{}
  225. if err = util.DecodeWithError(response, result, "GetGroupMsgSendResult"); err != nil {
  226. return nil, err
  227. }
  228. return result, nil
  229. }
  230. // SendWelcomeMsgRequest 发送新客户欢迎语请求
  231. type SendWelcomeMsgRequest struct {
  232. WelcomeCode string `json:"welcome_code"`
  233. Text MsgText `json:"text"`
  234. Attachments []*Attachment `json:"attachments"`
  235. }
  236. // SendWelcomeMsgResponse 发送新客户欢迎语响应
  237. type SendWelcomeMsgResponse struct {
  238. util.CommonError
  239. }
  240. // SendWelcomeMsg 发送新客户欢迎语
  241. // see https://developer.work.weixin.qq.com/document/path/92137
  242. func (r *Client) SendWelcomeMsg(req *SendWelcomeMsgRequest) error {
  243. var (
  244. accessToken string
  245. err error
  246. )
  247. if accessToken, err = r.GetAccessToken(); err != nil {
  248. return err
  249. }
  250. var response []byte
  251. if response, err = util.PostJSON(fmt.Sprintf(sendWelcomeMsgURL, accessToken), req); err != nil {
  252. return err
  253. }
  254. result := &SendWelcomeMsgResponse{}
  255. if err = util.DecodeWithError(response, result, "SendWelcomeMsg"); err != nil {
  256. return err
  257. }
  258. return nil
  259. }
  260. // AddGroupWelcomeTemplateRequest 添加入群欢迎语素材请求
  261. type AddGroupWelcomeTemplateRequest struct {
  262. Text MsgText `json:"text"`
  263. Image AttachmentImg `json:"image"`
  264. Link AttachmentLink `json:"link"`
  265. MiniProgram AttachmentMiniProgram `json:"miniprogram"`
  266. File AttachmentFile `json:"file"`
  267. Video AttachmentVideo `json:"video"`
  268. AgentID int `json:"agentid"`
  269. Notify int `json:"notify"`
  270. }
  271. // AddGroupWelcomeTemplateResponse 添加入群欢迎语素材响应
  272. type AddGroupWelcomeTemplateResponse struct {
  273. util.CommonError
  274. TemplateID string `json:"template_id"`
  275. }
  276. // AddGroupWelcomeTemplate 添加入群欢迎语素材
  277. // 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
  278. func (r *Client) AddGroupWelcomeTemplate(req *AddGroupWelcomeTemplateRequest) (*AddGroupWelcomeTemplateResponse, error) {
  279. var (
  280. accessToken string
  281. err error
  282. )
  283. if accessToken, err = r.GetAccessToken(); err != nil {
  284. return nil, err
  285. }
  286. var response []byte
  287. if response, err = util.PostJSON(fmt.Sprintf(addGroupWelcomeTemplateURL, accessToken), req); err != nil {
  288. return nil, err
  289. }
  290. result := &AddGroupWelcomeTemplateResponse{}
  291. if err = util.DecodeWithError(response, result, "AddGroupWelcomeTemplate"); err != nil {
  292. return nil, err
  293. }
  294. return result, nil
  295. }
  296. // EditGroupWelcomeTemplateRequest 编辑入群欢迎语素材请求
  297. type EditGroupWelcomeTemplateRequest struct {
  298. TemplateID string `json:"template_id"`
  299. Text MsgText `json:"text"`
  300. Image AttachmentImg `json:"image"`
  301. Link AttachmentLink `json:"link"`
  302. MiniProgram AttachmentMiniProgram `json:"miniprogram"`
  303. File AttachmentFile `json:"file"`
  304. Video AttachmentVideo `json:"video"`
  305. AgentID int `json:"agentid"`
  306. }
  307. // EditGroupWelcomeTemplateResponse 编辑入群欢迎语素材响应
  308. type EditGroupWelcomeTemplateResponse struct {
  309. util.CommonError
  310. }
  311. // EditGroupWelcomeTemplate 编辑入群欢迎语素材
  312. // 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
  313. func (r *Client) EditGroupWelcomeTemplate(req *EditGroupWelcomeTemplateRequest) error {
  314. var (
  315. accessToken string
  316. err error
  317. )
  318. if accessToken, err = r.GetAccessToken(); err != nil {
  319. return err
  320. }
  321. var response []byte
  322. if response, err = util.PostJSON(fmt.Sprintf(editGroupWelcomeTemplateURL, accessToken), req); err != nil {
  323. return err
  324. }
  325. result := &EditGroupWelcomeTemplateResponse{}
  326. if err = util.DecodeWithError(response, result, "EditGroupWelcomeTemplate"); err != nil {
  327. return err
  328. }
  329. return nil
  330. }
  331. // GetGroupWelcomeTemplateRequest 获取入群欢迎语素材请求
  332. type GetGroupWelcomeTemplateRequest struct {
  333. TemplateID string `json:"template_id"`
  334. }
  335. // GetGroupWelcomeTemplateResponse 获取入群欢迎语素材响应
  336. type GetGroupWelcomeTemplateResponse struct {
  337. util.CommonError
  338. Text MsgText `json:"text"`
  339. Image AttachmentImg `json:"image"`
  340. Link AttachmentLink `json:"link"`
  341. MiniProgram AttachmentMiniProgram `json:"miniprogram"`
  342. File AttachmentFile `json:"file"`
  343. Video AttachmentVideo `json:"video"`
  344. }
  345. // GetGroupWelcomeTemplate 获取入群欢迎语素材
  346. // 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
  347. func (r *Client) GetGroupWelcomeTemplate(req *GetGroupWelcomeTemplateRequest) (*GetGroupWelcomeTemplateResponse, error) {
  348. var (
  349. accessToken string
  350. err error
  351. )
  352. if accessToken, err = r.GetAccessToken(); err != nil {
  353. return nil, err
  354. }
  355. var response []byte
  356. if response, err = util.PostJSON(fmt.Sprintf(getGroupWelcomeTemplateURL, accessToken), req); err != nil {
  357. return nil, err
  358. }
  359. result := &GetGroupWelcomeTemplateResponse{}
  360. if err = util.DecodeWithError(response, result, "GetGroupWelcomeTemplate"); err != nil {
  361. return nil, err
  362. }
  363. return result, nil
  364. }
  365. // DelGroupWelcomeTemplateRequest 删除入群欢迎语素材请求
  366. type DelGroupWelcomeTemplateRequest struct {
  367. TemplateID string `json:"template_id"`
  368. AgentID int `json:"agentid"`
  369. }
  370. // DelGroupWelcomeTemplateResponse 删除入群欢迎语素材响应
  371. type DelGroupWelcomeTemplateResponse struct {
  372. util.CommonError
  373. }
  374. // DelGroupWelcomeTemplate 删除入群欢迎语素材
  375. // 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
  376. func (r *Client) DelGroupWelcomeTemplate(req *DelGroupWelcomeTemplateRequest) error {
  377. var (
  378. accessToken string
  379. err error
  380. )
  381. if accessToken, err = r.GetAccessToken(); err != nil {
  382. return err
  383. }
  384. var response []byte
  385. if response, err = util.PostJSON(fmt.Sprintf(delGroupWelcomeTemplateURL, accessToken), req); err != nil {
  386. return err
  387. }
  388. result := &DelGroupWelcomeTemplateResponse{}
  389. if err = util.DecodeWithError(response, result, "DelGroupWelcomeTemplate"); err != nil {
  390. return err
  391. }
  392. return nil
  393. }
  394. // RemindGroupMsgSendRequest 提醒成员群发请求
  395. type RemindGroupMsgSendRequest struct {
  396. MsgID string `json:"msgid"`
  397. }
  398. // RemindGroupMsgSend 提醒成员群发
  399. // see https://developer.work.weixin.qq.com/document/path/97610
  400. func (r *Client) RemindGroupMsgSend(req *RemindGroupMsgSendRequest) error {
  401. var (
  402. accessToken string
  403. err error
  404. )
  405. if accessToken, err = r.GetAccessToken(); err != nil {
  406. return err
  407. }
  408. var response []byte
  409. if response, err = util.PostJSON(fmt.Sprintf(remindGroupMsgSendURL, accessToken), req); err != nil {
  410. return err
  411. }
  412. return util.DecodeWithCommonError(response, "RemindGroupMsgSend")
  413. }
  414. // CancelGroupMsgSendRequest 停止企业群发请求
  415. type CancelGroupMsgSendRequest struct {
  416. MsgID string `json:"msgid"`
  417. }
  418. // CancelGroupMsgSend 提醒成员群发
  419. // see https://developer.work.weixin.qq.com/document/path/97611
  420. func (r *Client) CancelGroupMsgSend(req *CancelGroupMsgSendRequest) error {
  421. var (
  422. accessToken string
  423. err error
  424. )
  425. if accessToken, err = r.GetAccessToken(); err != nil {
  426. return err
  427. }
  428. var response []byte
  429. if response, err = util.PostJSON(fmt.Sprintf(cancelGroupMsgSendURL, accessToken), req); err != nil {
  430. return err
  431. }
  432. return util.DecodeWithCommonError(response, "CancelGroupMsgSend")
  433. }