消息发送

企业微信开放了消息发送接口,企业可以使用这些接口让自定义应用与企业微信后台或用户间进行双向通信。在使用PowerWeChat发送应用消息之前,请先确保你已经初始化过WeComApp实例。

发送应用消息

文本消息

  1. import "github.com/ArtisanCloud/PowerWeChat/v3/src/work/message/request"
  2. messages := &request.RequestMessageSendText{
  3. RequestMessageSend: request.RequestMessageSend{
  4. ToUser: "UserID1|UserID2|UserID3",
  5. ToParty: "PartyID1|PartyID2",
  6. ToTag: "TagID1 | TagID2",
  7. MsgType: "text",
  8. AgentID: 1,
  9. Safe: 0,
  10. EnableIDTrans: 0,
  11. EnableDuplicateCheck: 0,
  12. DuplicateCheckInterval: 1800,
  13. },
  14. Text: &request.RequestText{
  15. Content: "你的快递已到,请携带工卡前往邮件中心领取。\n出发前可查看<a href=\"http://work.weixin.qq.com\">邮件中心视频实况</a>,聪明避开排队。",
  16. },
  17. }
  18. WeComApp.Message.SendText(ctx,messages)

微信官方文档

图片消息

  1. messages := &request.RequestMessageSendImage{
  2. RequestMessageSend: request.RequestMessageSend{
  3. ToUser: "UserID1|UserID2|UserID3",
  4. ToParty: "PartyID1|PartyID2",
  5. ToTag: "TagID1 | TagID2",
  6. MsgType: "image",
  7. AgentID: 1,
  8. Safe: 0,
  9. EnableDuplicateCheck: 0,
  10. DuplicateCheckInterval: 1800,
  11. },
  12. Image: &request.RequestImage{
  13. MediaID: "MEDIA_ID",
  14. },
  15. }
  16. WeComApp.Message.SendImage(ctx,messages)

微信官方文档

语音消息

  1. messages := &request.RequestMessageSendVoice{
  2. RequestMessageSend: request.RequestMessageSend{
  3. ToUser: "UserID1|UserID2|UserID3",
  4. ToParty: "PartyID1|PartyID2",
  5. ToTag: "TagID1 | TagID2",
  6. MsgType: "voice",
  7. AgentID: 1,
  8. EnableDuplicateCheck: 0,
  9. DuplicateCheckInterval: 1800,
  10. },
  11. Voice: &request.RequestVoice{
  12. MediaId: "MEDIA_ID",
  13. },
  14. }
  15. WeComApp.Message.SendVoice(ctx,messages)

微信官方文档

视频消息

  1. messages := &request.RequestMessageSendVideo{
  2. RequestMessageSend: request.RequestMessageSend{
  3. ToUser: "UserID1|UserID2|UserID3",
  4. ToParty: "PartyID1|PartyID2",
  5. ToTag: "TagID1 | TagID2",
  6. MsgType: "video",
  7. AgentID: 1,
  8. Safe: 0,
  9. EnableDuplicateCheck: 0,
  10. DuplicateCheckInterval: 1800,
  11. },
  12. Video: &request.RequestVideo{
  13. MediaID: "MEDIA_ID",
  14. Title: "Title",
  15. Description: "Description",
  16. },
  17. }
  18. WeComApp.Message.SendVideo(ctx,messages)

微信官方文档

文件消息

  1. messages := &request.RequestMessageSendFile{
  2. RequestMessageSend: request.RequestMessageSend{
  3. ToUser: "UserID1|UserID2|UserID3",
  4. ToParty: "PartyID1|PartyID2",
  5. ToTag: "TagID1 | TagID2",
  6. MsgType: "file",
  7. AgentID: 1,
  8. Safe: 0,
  9. EnableDuplicateCheck: 0,
  10. DuplicateCheckInterval: 1800,
  11. },
  12. File: &request.RequestFile{
  13. MediaID: "1Yv-zXfHjSjU-7LH-GwtYqDGS-zz6w22KmWAT5COgP7o",
  14. },
  15. }
  16. WeComApp.Message.SendFile(ctx,messages)

微信官方文档

文本卡片消息

  1. messages := &request.RequestMessageSendTextCard{
  2. RequestMessageSend: request.RequestMessageSend{
  3. ToUser: "UserID1|UserID2|UserID3",
  4. ToParty: "PartyID1 | PartyID2",
  5. ToTag: "TagID1 | TagID2",
  6. MsgType: "textcard",
  7. AgentID: 1,
  8. EnableIDTrans: 0,
  9. EnableDuplicateCheck: 0,
  10. DuplicateCheckInterval: 1800,
  11. },
  12. TextCard: &request.RequestTextCard{
  13. Title: "领奖通知",
  14. Description: "<div class=\"gray\">2016年9月26日</div> <div class=\"normal\">恭喜你抽中iPhone 7一台,领奖码:xxxx</div><div class=\"highlight\">请于2016年10月10日前联系行政同事领取</div>",
  15. Url: "URL",
  16. BtnTXT: "更多",
  17. },
  18. }
  19. WeComApp.Message.SendTextCard(ctx,messages)

微信官方文档

图文消息

  1. messages := &request.RequestMessageSendNews{
  2. RequestMessageSend: request.RequestMessageSend{
  3. ToUser: "UserID1|UserID2|UserID3",
  4. ToParty: "PartyID1 | PartyID2",
  5. ToTag: "TagID1 | TagID2",
  6. MsgType: "news",
  7. AgentID: 1,
  8. EnableIDTrans: 0,
  9. EnableDuplicateCheck: 0,
  10. DuplicateCheckInterval: 1800,
  11. },
  12. News: &request.RequestNews{
  13. Article: []*request.RequestNewsArticle{
  14. {
  15. Title: "中秋节礼品领取",
  16. Description: "今年中秋节公司有豪礼相送",
  17. URL: "URL",
  18. PicURL: "http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png",
  19. AppId: "wx123123123123123",
  20. PagePath: "pages/index?userid=zhangsan&orderid=123123123",
  21. },
  22. },
  23. },
  24. }
  25. WeComApp.Message.SendNews(ctx,messages)

微信官方文档

图文消息 (mpnews)

  1. messages := &request.RequestMessageSendMPNews{
  2. RequestMessageSend: request.RequestMessageSend{
  3. ToUser: "UserID1|UserID2|UserID3",
  4. ToParty: "PartyID1 | PartyID2",
  5. ToTag: "TagID1 | TagID2",
  6. MsgType: "mpnews",
  7. AgentID: 1,
  8. Safe: 0,
  9. EnableIDTrans: 0,
  10. EnableDuplicateCheck: 0,
  11. DuplicateCheckInterval: 1800,
  12. },
  13. MPNews: &request.RequestMPNews{
  14. Article: []*request.RequestMPNewsArticle{
  15. {
  16. Title: "Title",
  17. ThumbMediaID: "MEDIA_ID",
  18. Author: "Author",
  19. ContentSourceURL: "URL",
  20. Content: "Content",
  21. Digest: "Digest description",
  22. },
  23. },
  24. },
  25. }
  26. WeComApp.Message.SendMpNews(ctx,messages)

微信官方文档

Markdown消息

  1. messages := &request.RequestMessageSendMarkdown{
  2. RequestMessageSend: request.RequestMessageSend{
  3. ToUser: "UserID1|UserID2|UserID3",
  4. ToParty: "PartyID1|PartyID2",
  5. ToTag: "TagID1 | TagID2",
  6. MsgType: "markdown",
  7. AgentID: 1,
  8. EnableDuplicateCheck: 0,
  9. DuplicateCheckInterval: 1800,
  10. },
  11. Markdown: &request.RequestMarkdown{
  12. Content: `"您的会议室已经预定,稍后会同步到邮箱
  13. >**事项详情**
  14. >事 项:<font color=\"info\">开会</font>
  15. >组织者:@miglioguan
  16. >参与者:@miglioguan、@kunliu、@jamdeezhou、@kanexiong、@kisonwang
  17. >
  18. >会议室:<font color=\"info\">广州TIT 1楼 301</font>
  19. >日 期:<font color=\"warning\">2018年5月18日</font>
  20. >时 间:<font color=\"comment\">上午9:00-11:00</font>
  21. >
  22. >请准时参加会议。
  23. >
  24. >如需修改会议信息,请点击:[修改会议信息](https://work.weixin.qq.com)"`,
  25. },
  26. }
  27. WeComApp.Message.SendMarkdown(ctx,messages)

微信官方文档

小程序通知消息

  1. messages := &request.RequestMessageSendMiniProgramNotice{
  2. RequestMessageSend: request.RequestMessageSend{
  3. ToUser: "zhangsan|lisi",
  4. ToParty: "1|2",
  5. ToTag: "1|2",
  6. MsgType: "miniprogram_notice",
  7. EnableIDTrans: 0,
  8. EnableDuplicateCheck: 0,
  9. DuplicateCheckInterval: 1800,
  10. },
  11. MiniProgramNotice: &request.MiniProgramNotice{
  12. Appid: "wx123123123123123",
  13. Page: "pages/index?userid=zhangsan&orderid=123123123",
  14. Title: "会议室预订成功通知",
  15. Description: "4月27日 16:16",
  16. EmphasisFirstItem: true,
  17. ContentItem: []*request.ContentItem{
  18. {
  19. Key: "会议室",
  20. Value: "402",
  21. },
  22. {
  23. Key: "会议地点",
  24. Value: "广州TIT-402会议室",
  25. },
  26. {
  27. Key: "会议时间",
  28. Value: "2018年8月1日 09:00-09:30",
  29. },
  30. {
  31. Key: "参与人员",
  32. Value: "周剑轩",
  33. },
  34. },
  35. },
  36. }
  37. WeComApp.Message.SendMiniProgramNotice(ctx,messages)

微信官方文档

模版卡片消息

文本通知型

  1. messages := &request.RequestMessageSendTemplateCard{
  2. RequestMessageSend: request.RequestMessageSend{
  3. ToUser: "UserID1|UserID2|UserID3",
  4. ToParty: "PartyID1 | PartyID2",
  5. ToTag: "TagID1 | TagID2",
  6. MsgType: "template_card",
  7. AgentID: 1,
  8. EnableIDTrans: 0,
  9. EnableDuplicateCheck: 0,
  10. DuplicateCheckInterval: 1800,
  11. },
  12. TemplateCard: &request.RequestTemplateCard{
  13. CardType: "text_notice",
  14. Source: &request.TemplateCardSource{
  15. IconUrl: "图片的url",
  16. Desc: "企业微信",
  17. DescColor: 1,
  18. },
  19. ActionMenu: &request.TemplateCardActionMenu{
  20. Desc: "卡片副交互辅助文本说明",
  21. ActionList: []*request.TemplateCardActionListItem{
  22. {Text: "接受推送", Key: "A"},
  23. {Text: "不再推送", Key: "B"},
  24. },
  25. },
  26. TaskID: "task_id",
  27. MainTitle: &request.TemplateCardMainTitle{
  28. Title: "欢迎使用企业微信",
  29. Desc: "您的好友正在邀请您加入企业微信",
  30. },
  31. QuoteArea: &request.TemplateCardQuoteArea{
  32. Type: 1,
  33. Url: "https://work.weixin.qq.com",
  34. Title: "企业微信的引用样式",
  35. QuoteText: "企业微信真好用呀真好用",
  36. },
  37. EmphasisContent: &request.TemplateCardEmphasisContent{
  38. Title: "100",
  39. Desc: "核心数据",
  40. },
  41. SubTitleText: "下载企业微信还能抢红包!",
  42. HorizontalContentList: []*request.TemplateCardHorizontalContentListItem{
  43. {
  44. Keyname: "邀请人",
  45. Value: "张三",
  46. },
  47. {
  48. Type: 1,
  49. Keyname: "企业微信官网",
  50. Value: "点击访问",
  51. Url: "https://work.weixin.qq.com",
  52. },
  53. {
  54. Type: 2,
  55. Keyname: "企业微信下载",
  56. Value: "企业微信.apk",
  57. MediaID: "文件的media_id",
  58. },
  59. {
  60. Type: 3,
  61. Keyname: "员工信息",
  62. Value: "点击查看",
  63. UserID: "zhangsan",
  64. },
  65. },
  66. JumpList: []*request.TemplateCardJumpListItem{
  67. {
  68. Type: 1,
  69. Title: "企业微信官网",
  70. Url: "https://work.weixin.qq.com",
  71. },
  72. {
  73. Type: 2,
  74. Title: "跳转小程序",
  75. AppID: "小程序的appid",
  76. Pagepath: "/index.html",
  77. },
  78. },
  79. CardAction: &request.TemplateCardAction{
  80. Type: 2,
  81. Url: "https://work.weixin.qq.com",
  82. AppID: "小程序的appid",
  83. Pagepath: "/index.html",
  84. },
  85. },
  86. }
  87. WeComApp.Message.SendTemplateCard(ctx,messages)

微信官方文档

PowerWeChat还支持图文展示型按钮交互型投票选择型多项选择型等,出于篇幅的原因没有一一列出,其他的类型也是在同一个结构体里面有定义,请自行使用。

撤回应用消息

  1. resRecall, err := WeComApp.Message.Recall(ctx,msgID)

msgID是发送消息成功之后得到的,例如: resSend.MsgID

微信官方文档

消息回调

由于篇幅问题,放在了服务端

使用示例

参考:PowerWechatTutorial