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 4.1 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. switch msg.Event {
  33. case wechat.WECHAT_EVENT_SCAN:
  34. HandleEventScan(ctx, msg)
  35. case wechat.WECHAT_EVENT_SUBSCRIBE:
  36. if msg.EventKey != "" {
  37. HandleEventScan(ctx, msg)
  38. } else {
  39. HandleEventSubscribe(ctx, msg)
  40. }
  41. }
  42. }
  43. func HandleEventScan(ctx *context.Context, msg wechat.WechatMsg) {
  44. replyStr := wechat.HandleScanEvent(msg)
  45. if replyStr == "" {
  46. log.Info("reply str is empty")
  47. return
  48. }
  49. reply := &wechat.MsgReply{
  50. ToUserName: msg.FromUserName,
  51. FromUserName: msg.ToUserName,
  52. CreateTime: time.Now().Unix(),
  53. MsgType: wechat.WECHAT_MSG_TYPE_TEXT,
  54. Content: replyStr,
  55. }
  56. ctx.XML(200, reply)
  57. }
  58. func HandleEventSubscribe(ctx *context.Context, msg wechat.WechatMsg) {
  59. r := wechat.HandleSubscribeEvent(msg)
  60. if r == nil {
  61. return
  62. }
  63. reply := buildReplyContent(msg, r)
  64. ctx.XML(200, reply)
  65. }
  66. func HandleTextMsg(ctx *context.Context, msg wechat.WechatMsg) {
  67. r := wechat.GetAutomaticReply(msg.Content)
  68. if r == nil {
  69. log.Info("TextMsg reply is empty")
  70. return
  71. }
  72. reply := buildReplyContent(msg, r)
  73. ctx.XML(200, reply)
  74. }
  75. func buildReplyContent(msg wechat.WechatMsg, r *wechat.WechatReplyContent) interface{} {
  76. reply := &wechat.MsgReply{
  77. ToUserName: msg.FromUserName,
  78. FromUserName: msg.ToUserName,
  79. CreateTime: time.Now().Unix(),
  80. MsgType: r.ReplyType,
  81. }
  82. switch r.ReplyType {
  83. case wechat.ReplyTypeText:
  84. return &wechat.TextMsgReply{
  85. ToUserName: msg.FromUserName,
  86. FromUserName: msg.ToUserName,
  87. CreateTime: time.Now().Unix(),
  88. MsgType: r.ReplyType,
  89. Content: r.Reply.Content,
  90. }
  91. case wechat.ReplyTypeImage:
  92. return &wechat.ImageMsgReply{
  93. ToUserName: msg.FromUserName,
  94. FromUserName: msg.ToUserName,
  95. CreateTime: time.Now().Unix(),
  96. MsgType: r.ReplyType,
  97. Image: wechat.ImageContent{
  98. MediaId: r.Reply.MediaId,
  99. },
  100. }
  101. case wechat.ReplyTypeVoice:
  102. return &wechat.VoiceMsgReply{
  103. ToUserName: msg.FromUserName,
  104. FromUserName: msg.ToUserName,
  105. CreateTime: time.Now().Unix(),
  106. MsgType: r.ReplyType,
  107. Voice: wechat.VoiceContent{
  108. MediaId: r.Reply.MediaId,
  109. },
  110. }
  111. case wechat.ReplyTypeVideo:
  112. return &wechat.VideoMsgReply{
  113. ToUserName: msg.FromUserName,
  114. FromUserName: msg.ToUserName,
  115. CreateTime: time.Now().Unix(),
  116. MsgType: r.ReplyType,
  117. Video: wechat.VideoContent{
  118. MediaId: r.Reply.MediaId,
  119. Title: r.Reply.Title,
  120. Description: r.Reply.Description,
  121. },
  122. }
  123. case wechat.ReplyTypeMusic:
  124. return &wechat.MusicMsgReply{
  125. ToUserName: msg.FromUserName,
  126. FromUserName: msg.ToUserName,
  127. CreateTime: time.Now().Unix(),
  128. MsgType: r.ReplyType,
  129. Music: wechat.MusicContent{
  130. Title: r.Reply.Title,
  131. Description: r.Reply.Description,
  132. MusicUrl: r.Reply.MusicUrl,
  133. HQMusicUrl: r.Reply.HQMusicUrl,
  134. ThumbMediaId: r.Reply.ThumbMediaId,
  135. },
  136. }
  137. case wechat.ReplyTypeNews:
  138. return &wechat.NewsMsgReply{
  139. ToUserName: msg.FromUserName,
  140. FromUserName: msg.ToUserName,
  141. CreateTime: time.Now().Unix(),
  142. MsgType: r.ReplyType,
  143. ArticleCount: len(r.Reply.Articles),
  144. Articles: wechat.ArticleItem{
  145. Item: r.Reply.Articles},
  146. }
  147. }
  148. return reply
  149. }