|
- package authentication
-
- import (
- "code.gitea.io/gitea/modules/auth/wechat"
- "code.gitea.io/gitea/modules/context"
- "code.gitea.io/gitea/modules/log"
- "encoding/xml"
- "io/ioutil"
- "time"
- )
-
- // AcceptWechatEvent
- // https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Receiving_event_pushes.html
- // https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Passive_user_reply_message.html
- func AcceptWechatEvent(ctx *context.Context) {
- b, _ := ioutil.ReadAll(ctx.Req.Request.Body)
- we := wechat.WechatEvent{}
- xml.Unmarshal(b, &we)
-
- log.Info("accept wechat event= %+v", we)
- var replyStr string
- switch we.Event {
- case wechat.WECHAT_EVENT_SUBSCRIBE, wechat.WECHAT_EVENT_SCAN:
- replyStr = wechat.HandleSubscribeEvent(we)
- break
- }
-
- if replyStr == "" {
- log.Info("reply str is empty")
- return
- }
- reply := &wechat.EventReply{
- ToUserName: we.FromUserName,
- FromUserName: we.ToUserName,
- CreateTime: time.Now().Unix(),
- MsgType: wechat.WECHAT_MSG_TYPE_TEXT,
- Content: replyStr,
- }
- ctx.XML(200, reply)
- }
-
- // ValidEventSource
- func ValidEventSource(ctx *context.Context) {
- echostr := ctx.Query("echostr")
- ctx.Write([]byte(echostr))
- return
- }
|