| @@ -25,7 +25,34 @@ const ( | |||
| ReplyTypeNews = "news" | |||
| ) | |||
| type AutomaticResponseContent struct { | |||
| type ReplyConfigType string | |||
| const ( | |||
| SubscribeReply ReplyConfigType = "subscribe" | |||
| AutoMsgReply ReplyConfigType = "autoMsg" | |||
| ) | |||
| func (r ReplyConfigType) Name() string { | |||
| switch r { | |||
| case SubscribeReply: | |||
| return "subscribe" | |||
| case AutoMsgReply: | |||
| return "autoMsg" | |||
| } | |||
| return "" | |||
| } | |||
| func (r ReplyConfigType) TreePath() string { | |||
| switch r { | |||
| case SubscribeReply: | |||
| return setting.TreePathOfSubscribe | |||
| case AutoMsgReply: | |||
| return setting.TreePathOfAutoMsgReply | |||
| } | |||
| return "" | |||
| } | |||
| type WechatReplyContent struct { | |||
| Reply *ReplyContent | |||
| ReplyType string | |||
| KeyWords []string | |||
| @@ -43,8 +70,8 @@ type ReplyContent struct { | |||
| Articles []ArticlesContent | |||
| } | |||
| func GetAutomaticReply(msg string) *AutomaticResponseContent { | |||
| r, err := LoadAutomaticReplyFromCacheAndDisk() | |||
| func GetAutomaticReply(msg string) *WechatReplyContent { | |||
| r, err := LoadReplyFromCacheAndDisk(AutoMsgReply) | |||
| if err != nil { | |||
| return nil | |||
| } | |||
| @@ -70,19 +97,19 @@ func GetAutomaticReply(msg string) *AutomaticResponseContent { | |||
| } | |||
| func loadAutomaticReplyFromDisk() ([]*AutomaticResponseContent, error) { | |||
| log.Info("LoadAutomaticResponseMap from disk") | |||
| repo, err := models.GetRepositoryByOwnerAndAlias(setting.UserNameOfAutoReply, setting.RepoNameOfAutoReply) | |||
| func loadReplyFromDisk(replyConfig ReplyConfigType) ([]*WechatReplyContent, error) { | |||
| log.Info("LoadReply from disk") | |||
| repo, err := models.GetRepositoryByOwnerAndAlias(setting.UserNameOfWechatReply, setting.RepoNameOfWechatReply) | |||
| if err != nil { | |||
| log.Error("get AutomaticReply repo failed, error=%v", err) | |||
| return nil, err | |||
| } | |||
| repoFile, err := models.ReadLatestFileInRepo(setting.UserNameOfAutoReply, repo.Name, setting.RefNameOfAutoReply, setting.TreePathOfAutoReply) | |||
| repoFile, err := models.ReadLatestFileInRepo(setting.UserNameOfWechatReply, repo.Name, setting.RefNameOfWechatReply, replyConfig.TreePath()) | |||
| if err != nil { | |||
| log.Error("get AutomaticReply failed, error=%v", err) | |||
| return nil, err | |||
| } | |||
| res := make([]*AutomaticResponseContent, 0) | |||
| res := make([]*WechatReplyContent, 0) | |||
| json.Unmarshal(repoFile.Content, &res) | |||
| if res == nil || len(res) == 0 { | |||
| return nil, err | |||
| @@ -90,23 +117,23 @@ func loadAutomaticReplyFromDisk() ([]*AutomaticResponseContent, error) { | |||
| return res, nil | |||
| } | |||
| func LoadAutomaticReplyFromCacheAndDisk() ([]*AutomaticResponseContent, error) { | |||
| v, success := WechatReplyCache.Get(WECHAT_REPLY_CACHE_KEY) | |||
| func LoadReplyFromCacheAndDisk(replyConfig ReplyConfigType) ([]*WechatReplyContent, error) { | |||
| v, success := WechatReplyCache.Get(replyConfig.Name()) | |||
| if success { | |||
| log.Info("LoadAutomaticResponse from cache,value = %v", v) | |||
| log.Info("LoadReply from cache,value = %v", v) | |||
| if v == nil { | |||
| return nil, nil | |||
| } | |||
| n := v.([]*AutomaticResponseContent) | |||
| n := v.([]*WechatReplyContent) | |||
| return n, nil | |||
| } | |||
| content, err := loadAutomaticReplyFromDisk() | |||
| content, err := loadReplyFromDisk(replyConfig) | |||
| if err != nil { | |||
| log.Error("GetNewestNotice failed, error=%v", err) | |||
| WechatReplyCache.Set(WECHAT_REPLY_CACHE_KEY, nil, 30*time.Second) | |||
| log.Error("LoadReply failed, error=%v", err) | |||
| WechatReplyCache.Set(replyConfig.Name(), nil, 30*time.Second) | |||
| return nil, err | |||
| } | |||
| WechatReplyCache.Set(WECHAT_REPLY_CACHE_KEY, content, 60*time.Second) | |||
| WechatReplyCache.Set(replyConfig.Name(), content, 60*time.Second) | |||
| return content, nil | |||
| } | |||
| @@ -131,7 +131,7 @@ const ( | |||
| WECHAT_MSG_TYPE_EVENT = "event" | |||
| ) | |||
| func HandleSubscribeEvent(we WechatMsg) string { | |||
| func HandleScanEvent(we WechatMsg) string { | |||
| eventKey := we.EventKey | |||
| if eventKey == "" { | |||
| return "" | |||
| @@ -159,3 +159,11 @@ func HandleSubscribeEvent(we WechatMsg) string { | |||
| return BIND_REPLY_SUCCESS | |||
| } | |||
| func HandleSubscribeEvent(we WechatMsg) *WechatReplyContent { | |||
| r, err := LoadReplyFromCacheAndDisk(SubscribeReply) | |||
| if err != nil || len(r) == 0 { | |||
| return nil | |||
| } | |||
| return r[0] | |||
| } | |||
| @@ -546,10 +546,11 @@ var ( | |||
| WechatAuthSwitch bool | |||
| //wechat auto reply config | |||
| UserNameOfAutoReply string | |||
| RepoNameOfAutoReply string | |||
| RefNameOfAutoReply string | |||
| TreePathOfAutoReply string | |||
| UserNameOfWechatReply string | |||
| RepoNameOfWechatReply string | |||
| RefNameOfWechatReply string | |||
| TreePathOfAutoMsgReply string | |||
| TreePathOfSubscribe string | |||
| //nginx proxy | |||
| PROXYURL string | |||
| @@ -1378,10 +1379,11 @@ func NewContext() { | |||
| WechatAppSecret = sec.Key("APP_SECRET").MustString("e48e13f315adc32749ddc7057585f198") | |||
| WechatQRCodeExpireSeconds = sec.Key("QR_CODE_EXPIRE_SECONDS").MustInt(120) | |||
| WechatAuthSwitch = sec.Key("AUTH_SWITCH").MustBool(true) | |||
| UserNameOfAutoReply = sec.Key("AUTO_REPLY_USER_NAME").MustString("OpenIOSSG") | |||
| RepoNameOfAutoReply = sec.Key("AUTO_REPLY_REPO_NAME").MustString("promote") | |||
| RefNameOfAutoReply = sec.Key("AUTO_REPLY_REF_NAME").MustString("master") | |||
| TreePathOfAutoReply = sec.Key("AUTO_REPLY_TREE_PATH").MustString("wechat/auto_reply.json") | |||
| UserNameOfWechatReply = sec.Key("AUTO_REPLY_USER_NAME").MustString("OpenIOSSG") | |||
| RepoNameOfWechatReply = sec.Key("AUTO_REPLY_REPO_NAME").MustString("promote") | |||
| RefNameOfWechatReply = sec.Key("AUTO_REPLY_REF_NAME").MustString("master") | |||
| TreePathOfAutoMsgReply = sec.Key("AUTO_REPLY_TREE_PATH").MustString("wechat/auto_reply.json") | |||
| TreePathOfSubscribe = sec.Key("SUBSCRIBE_TREE_PATH").MustString("wechat/subscribe_reply.json") | |||
| SetRadarMapConfig() | |||
| @@ -34,13 +34,21 @@ func ValidEventSource(ctx *context.Context) { | |||
| } | |||
| func HandleEventMsg(ctx *context.Context, msg wechat.WechatMsg) { | |||
| var replyStr string | |||
| switch msg.Event { | |||
| case wechat.WECHAT_EVENT_SUBSCRIBE, wechat.WECHAT_EVENT_SCAN: | |||
| replyStr = wechat.HandleSubscribeEvent(msg) | |||
| break | |||
| case wechat.WECHAT_EVENT_SCAN: | |||
| HandleEventScan(ctx, msg) | |||
| case wechat.WECHAT_EVENT_SUBSCRIBE: | |||
| if msg.EventKey != "" { | |||
| HandleEventScan(ctx, msg) | |||
| } else { | |||
| HandleEventSubscribe(ctx, msg) | |||
| } | |||
| } | |||
| } | |||
| func HandleEventScan(ctx *context.Context, msg wechat.WechatMsg) { | |||
| replyStr := wechat.HandleScanEvent(msg) | |||
| if replyStr == "" { | |||
| log.Info("reply str is empty") | |||
| return | |||
| @@ -55,6 +63,15 @@ func HandleEventMsg(ctx *context.Context, msg wechat.WechatMsg) { | |||
| ctx.XML(200, reply) | |||
| } | |||
| func HandleEventSubscribe(ctx *context.Context, msg wechat.WechatMsg) { | |||
| r := wechat.HandleSubscribeEvent(msg) | |||
| if r == nil { | |||
| return | |||
| } | |||
| reply := buildReplyContent(msg, r) | |||
| ctx.XML(200, reply) | |||
| } | |||
| func HandleTextMsg(ctx *context.Context, msg wechat.WechatMsg) { | |||
| r := wechat.GetAutomaticReply(msg.Content) | |||
| if r == nil { | |||
| @@ -65,7 +82,7 @@ func HandleTextMsg(ctx *context.Context, msg wechat.WechatMsg) { | |||
| ctx.XML(200, reply) | |||
| } | |||
| func buildReplyContent(msg wechat.WechatMsg, r *wechat.AutomaticResponseContent) interface{} { | |||
| func buildReplyContent(msg wechat.WechatMsg, r *wechat.WechatReplyContent) interface{} { | |||
| reply := &wechat.MsgReply{ | |||
| ToUserName: msg.FromUserName, | |||
| FromUserName: msg.ToUserName, | |||