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 1.2 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
3 years ago
3 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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.WechatEvent{}
  16. xml.Unmarshal(b, &we)
  17. log.Info("accept wechat event= %+v", we)
  18. var replyStr string
  19. switch we.Event {
  20. case wechat.WECHAT_EVENT_SUBSCRIBE, wechat.WECHAT_EVENT_SCAN:
  21. replyStr = wechat.HandleSubscribeEvent(we)
  22. break
  23. }
  24. if replyStr == "" {
  25. log.Info("reply str is empty")
  26. return
  27. }
  28. reply := &wechat.EventReply{
  29. ToUserName: we.FromUserName,
  30. FromUserName: we.ToUserName,
  31. CreateTime: time.Now().Unix(),
  32. MsgType: wechat.WECHAT_MSG_TYPE_TEXT,
  33. Content: replyStr,
  34. }
  35. ctx.XML(200, reply)
  36. }
  37. // ValidEventSource
  38. func ValidEventSource(ctx *context.Context) {
  39. echostr := ctx.Query("echostr")
  40. ctx.Write([]byte(echostr))
  41. return
  42. }