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.

auto_reply.go 3.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package wechat
  2. import (
  3. "code.gitea.io/gitea/models"
  4. "code.gitea.io/gitea/modules/log"
  5. "code.gitea.io/gitea/modules/setting"
  6. "encoding/json"
  7. "github.com/patrickmn/go-cache"
  8. "strings"
  9. "time"
  10. )
  11. var WechatReplyCache = cache.New(2*time.Minute, 1*time.Minute)
  12. const (
  13. WECHAT_REPLY_CACHE_KEY = "wechat_response"
  14. )
  15. const (
  16. ReplyTypeText = "text"
  17. ReplyTypeImage = "image"
  18. ReplyTypeVoice = "voice"
  19. ReplyTypeVideo = "video"
  20. ReplyTypeMusic = "music"
  21. ReplyTypeNews = "news"
  22. )
  23. type ReplyConfigType string
  24. const (
  25. SubscribeReply ReplyConfigType = "subscribe"
  26. AutoMsgReply ReplyConfigType = "autoMsg"
  27. )
  28. func (r ReplyConfigType) Name() string {
  29. switch r {
  30. case SubscribeReply:
  31. return "subscribe"
  32. case AutoMsgReply:
  33. return "autoMsg"
  34. }
  35. return ""
  36. }
  37. func (r ReplyConfigType) TreePath() string {
  38. switch r {
  39. case SubscribeReply:
  40. return setting.TreePathOfSubscribe
  41. case AutoMsgReply:
  42. return setting.TreePathOfAutoMsgReply
  43. }
  44. return ""
  45. }
  46. type WechatReplyContent struct {
  47. Reply *ReplyContent
  48. ReplyType string
  49. KeyWords []string
  50. IsFullMatch int
  51. }
  52. type ReplyContent struct {
  53. Content string
  54. MediaId string
  55. Title string
  56. Description string
  57. MusicUrl string
  58. HQMusicUrl string
  59. ThumbMediaId string
  60. Articles []ArticlesContent
  61. }
  62. func GetAutomaticReply(msg string) *WechatReplyContent {
  63. r, err := LoadReplyFromCacheAndDisk(AutoMsgReply)
  64. if err != nil {
  65. return nil
  66. }
  67. if r == nil || len(r) == 0 {
  68. return nil
  69. }
  70. for i := 0; i < len(r); i++ {
  71. if r[i].IsFullMatch == 0 {
  72. for _, v := range r[i].KeyWords {
  73. if strings.Contains(msg, v) {
  74. return r[i]
  75. }
  76. }
  77. } else if r[i].IsFullMatch > 0 {
  78. for _, v := range r[i].KeyWords {
  79. if msg == v {
  80. return r[i]
  81. }
  82. }
  83. }
  84. }
  85. return nil
  86. }
  87. func loadReplyFromDisk(replyConfig ReplyConfigType) ([]*WechatReplyContent, error) {
  88. log.Info("LoadReply from disk")
  89. repo, err := models.GetRepositoryByOwnerAndAlias(setting.UserNameOfWechatReply, setting.RepoNameOfWechatReply)
  90. if err != nil {
  91. log.Error("get AutomaticReply repo failed, error=%v", err)
  92. return nil, err
  93. }
  94. repoFile, err := models.ReadLatestFileInRepo(setting.UserNameOfWechatReply, repo.Name, setting.RefNameOfWechatReply, replyConfig.TreePath())
  95. if err != nil {
  96. log.Error("get AutomaticReply failed, error=%v", err)
  97. return nil, err
  98. }
  99. res := make([]*WechatReplyContent, 0)
  100. json.Unmarshal(repoFile.Content, &res)
  101. if res == nil || len(res) == 0 {
  102. return nil, err
  103. }
  104. return res, nil
  105. }
  106. func LoadReplyFromCacheAndDisk(replyConfig ReplyConfigType) ([]*WechatReplyContent, error) {
  107. v, success := WechatReplyCache.Get(replyConfig.Name())
  108. if success {
  109. log.Info("LoadReply from cache,value = %v", v)
  110. if v == nil {
  111. return nil, nil
  112. }
  113. n := v.([]*WechatReplyContent)
  114. return n, nil
  115. }
  116. content, err := loadReplyFromDisk(replyConfig)
  117. if err != nil {
  118. log.Error("LoadReply failed, error=%v", err)
  119. WechatReplyCache.Set(replyConfig.Name(), nil, 30*time.Second)
  120. return nil, err
  121. }
  122. WechatReplyCache.Set(replyConfig.Name(), content, 60*time.Second)
  123. return content, nil
  124. }