You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

wechat_event.go 3.8 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package authentication
  2. import (
  3. "code.gitea.io/gitea/modules/auth/wechat"
  4. "code.gitea.io/gitea/modules/context"
  5. "code.gitea.io/gitea/modules/log"
  6. "encoding/xml"
  7. "io/ioutil"
  8. "time"
  9. )
  10. // AcceptWechatEvent
  11. // https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Receiving_event_pushes.html
  12. // https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Passive_user_reply_message.html
  13. func AcceptWechatEvent(ctx *context.Context) {
  14. b, _ := ioutil.ReadAll(ctx.Req.Request.Body)
  15. we := wechat.WechatMsg{}
  16. xml.Unmarshal(b, &we)
  17. switch we.MsgType {
  18. case wechat.WECHAT_MSG_TYPE_EVENT:
  19. HandleEventMsg(ctx, we)
  20. case wechat.WECHAT_MSG_TYPE_TEXT:
  21. HandleTextMsg(ctx, we)
  22. }
  23. log.Info("accept wechat event= %+v", we)
  24. }
  25. // ValidEventSource
  26. func ValidEventSource(ctx *context.Context) {
  27. echostr := ctx.Query("echostr")
  28. ctx.Write([]byte(echostr))
  29. return
  30. }
  31. func HandleEventMsg(ctx *context.Context, msg wechat.WechatMsg) {
  32. var replyStr string
  33. switch msg.Event {
  34. case wechat.WECHAT_EVENT_SUBSCRIBE, wechat.WECHAT_EVENT_SCAN:
  35. replyStr = wechat.HandleSubscribeEvent(msg)
  36. break
  37. }
  38. if replyStr == "" {
  39. log.Info("reply str is empty")
  40. return
  41. }
  42. reply := &wechat.MsgReply{
  43. ToUserName: msg.FromUserName,
  44. FromUserName: msg.ToUserName,
  45. CreateTime: time.Now().Unix(),
  46. MsgType: wechat.WECHAT_MSG_TYPE_TEXT,
  47. Content: replyStr,
  48. }
  49. ctx.XML(200, reply)
  50. }
  51. func HandleTextMsg(ctx *context.Context, msg wechat.WechatMsg) {
  52. r := wechat.GetAutomaticReply(msg.Content)
  53. if r == nil {
  54. log.Info("TextMsg reply is empty")
  55. return
  56. }
  57. reply := buildReplyContent(msg, r)
  58. ctx.XML(200, reply)
  59. }
  60. func buildReplyContent(msg wechat.WechatMsg, r *wechat.AutomaticResponseContent) interface{} {
  61. reply := &wechat.MsgReply{
  62. ToUserName: msg.FromUserName,
  63. FromUserName: msg.ToUserName,
  64. CreateTime: time.Now().Unix(),
  65. MsgType: r.ReplyType,
  66. }
  67. switch r.ReplyType {
  68. case wechat.ReplyTypeText:
  69. return &wechat.TextMsgReply{
  70. ToUserName: msg.FromUserName,
  71. FromUserName: msg.ToUserName,
  72. CreateTime: time.Now().Unix(),
  73. MsgType: r.ReplyType,
  74. Content: r.Reply.Content,
  75. }
  76. case wechat.ReplyTypeImage:
  77. return &wechat.ImageMsgReply{
  78. ToUserName: msg.FromUserName,
  79. FromUserName: msg.ToUserName,
  80. CreateTime: time.Now().Unix(),
  81. MsgType: r.ReplyType,
  82. Image: wechat.ImageContent{
  83. MediaId: r.Reply.MediaId,
  84. },
  85. }
  86. case wechat.ReplyTypeVoice:
  87. return &wechat.VoiceMsgReply{
  88. ToUserName: msg.FromUserName,
  89. FromUserName: msg.ToUserName,
  90. CreateTime: time.Now().Unix(),
  91. MsgType: r.ReplyType,
  92. Voice: wechat.VoiceContent{
  93. MediaId: r.Reply.MediaId,
  94. },
  95. }
  96. case wechat.ReplyTypeVideo:
  97. return &wechat.VideoMsgReply{
  98. ToUserName: msg.FromUserName,
  99. FromUserName: msg.ToUserName,
  100. CreateTime: time.Now().Unix(),
  101. MsgType: r.ReplyType,
  102. Video: wechat.VideoContent{
  103. MediaId: r.Reply.MediaId,
  104. Title: r.Reply.Title,
  105. Description: r.Reply.Description,
  106. },
  107. }
  108. case wechat.ReplyTypeMusic:
  109. return &wechat.MusicMsgReply{
  110. ToUserName: msg.FromUserName,
  111. FromUserName: msg.ToUserName,
  112. CreateTime: time.Now().Unix(),
  113. MsgType: r.ReplyType,
  114. Music: wechat.MusicContent{
  115. Title: r.Reply.Title,
  116. Description: r.Reply.Description,
  117. MusicUrl: r.Reply.MusicUrl,
  118. HQMusicUrl: r.Reply.HQMusicUrl,
  119. ThumbMediaId: r.Reply.ThumbMediaId,
  120. },
  121. }
  122. case wechat.ReplyTypeNews:
  123. return &wechat.NewsMsgReply{
  124. ToUserName: msg.FromUserName,
  125. FromUserName: msg.ToUserName,
  126. CreateTime: time.Now().Unix(),
  127. MsgType: r.ReplyType,
  128. ArticleCount: len(r.Reply.Articles),
  129. Articles: wechat.ArticleItem{
  130. Item: r.Reply.Articles},
  131. }
  132. }
  133. return reply
  134. }