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 2.7 kB

3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 AutomaticResponseContent struct {
  24. Reply *ReplyContent
  25. ReplyType string
  26. KeyWords []string
  27. IsFullMatch int
  28. }
  29. type ReplyContent struct {
  30. Content string
  31. MediaId string
  32. Title string
  33. Description string
  34. MusicUrl string
  35. HQMusicUrl string
  36. ThumbMediaId string
  37. Articles []ArticlesContent
  38. }
  39. func GetAutomaticReply(msg string) *AutomaticResponseContent {
  40. r, err := LoadAutomaticReplyFromCacheAndDisk()
  41. if err != nil {
  42. return nil
  43. }
  44. if r == nil || len(r) == 0 {
  45. return nil
  46. }
  47. for i := 0; i < len(r); i++ {
  48. if r[i].IsFullMatch == 0 {
  49. for _, v := range r[i].KeyWords {
  50. if strings.Contains(msg, v) {
  51. return r[i]
  52. }
  53. }
  54. } else if r[i].IsFullMatch > 0 {
  55. for _, v := range r[i].KeyWords {
  56. if msg == v {
  57. return r[i]
  58. }
  59. }
  60. }
  61. }
  62. return nil
  63. }
  64. func loadAutomaticReplyFromDisk() ([]*AutomaticResponseContent, error) {
  65. log.Info("LoadAutomaticResponseMap from disk")
  66. repo, err := models.GetRepositoryByOwnerAndAlias(setting.UserNameOfAutoReply, setting.RepoNameOfAutoReply)
  67. if err != nil {
  68. log.Error("get AutomaticReply repo failed, error=%v", err)
  69. return nil, err
  70. }
  71. repoFile, err := models.ReadLatestFileInRepo(setting.UserNameOfAutoReply, repo.Name, setting.RefNameOfAutoReply, setting.TreePathOfAutoReply)
  72. if err != nil {
  73. log.Error("get AutomaticReply failed, error=%v", err)
  74. return nil, err
  75. }
  76. res := make([]*AutomaticResponseContent, 0)
  77. json.Unmarshal(repoFile.Content, &res)
  78. if res == nil || len(res) == 0 {
  79. return nil, err
  80. }
  81. return res, nil
  82. }
  83. func LoadAutomaticReplyFromCacheAndDisk() ([]*AutomaticResponseContent, error) {
  84. v, success := WechatReplyCache.Get(WECHAT_REPLY_CACHE_KEY)
  85. if success {
  86. log.Info("LoadAutomaticResponse from cache,value = %v", v)
  87. if v == nil {
  88. return nil, nil
  89. }
  90. n := v.([]*AutomaticResponseContent)
  91. return n, nil
  92. }
  93. content, err := loadAutomaticReplyFromDisk()
  94. if err != nil {
  95. log.Error("GetNewestNotice failed, error=%v", err)
  96. WechatReplyCache.Set(WECHAT_REPLY_CACHE_KEY, nil, 30*time.Second)
  97. return nil, err
  98. }
  99. WechatReplyCache.Set(WECHAT_REPLY_CACHE_KEY, content, 60*time.Second)
  100. return content, nil
  101. }