telegram_push.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. log "github.com/sirupsen/logrus"
  6. "net/http"
  7. "os"
  8. "pmail/config"
  9. "pmail/dto/parsemail"
  10. "pmail/hooks/framework"
  11. "pmail/models"
  12. "pmail/utils/context"
  13. "strings"
  14. )
  15. type TelegramPushHook struct {
  16. chatId string
  17. botToken string
  18. httpsEnabled int
  19. webDomain string
  20. }
  21. func (w *TelegramPushHook) ReceiveSaveAfter(ctx *context.Context, email *parsemail.Email, ue []*models.UserEmail) {
  22. if w.chatId == "" || w.botToken == "" {
  23. return
  24. }
  25. for _, u := range ue {
  26. // 管理员(Uid=1)收到邮件且非已读、非已删除 触发通知
  27. if u.UserID == 1 && u.IsRead == 0 && u.Status != 3 && email.MessageId > 0 {
  28. w.sendUserMsg(nil, email)
  29. }
  30. }
  31. }
  32. func (w *TelegramPushHook) SendBefore(ctx *context.Context, email *parsemail.Email) {
  33. }
  34. func (w *TelegramPushHook) SendAfter(ctx *context.Context, email *parsemail.Email, err map[string]error) {
  35. }
  36. func (w *TelegramPushHook) ReceiveParseBefore(ctx *context.Context, email *[]byte) {
  37. }
  38. func (w *TelegramPushHook) ReceiveParseAfter(ctx *context.Context, email *parsemail.Email) {}
  39. type SendMessageRequest struct {
  40. ChatID string `json:"chat_id"`
  41. Text string `json:"text"`
  42. ReplyMarkup ReplyMarkup `json:"reply_markup"`
  43. ParseMode string `json:"parse_mode"`
  44. }
  45. type ReplyMarkup struct {
  46. InlineKeyboard [][]InlineKeyboardButton `json:"inline_keyboard"`
  47. }
  48. type InlineKeyboardButton struct {
  49. Text string `json:"text"`
  50. URL string `json:"url"`
  51. }
  52. func (w *TelegramPushHook) sendUserMsg(ctx *context.Context, email *parsemail.Email) {
  53. url := w.webDomain
  54. if w.httpsEnabled > 1 {
  55. url = "http://" + url
  56. } else {
  57. url = "https://" + url
  58. }
  59. sendMsgReq, _ := json.Marshal(SendMessageRequest{
  60. ChatID: w.chatId,
  61. Text: fmt.Sprintf("📧<b>%s</b>&#60;%s&#62;\n\n%s", email.Subject, email.From.EmailAddress, string(email.Text)),
  62. ParseMode: "HTML",
  63. ReplyMarkup: ReplyMarkup{
  64. InlineKeyboard: [][]InlineKeyboardButton{
  65. {
  66. {
  67. Text: "查收邮件",
  68. URL: url,
  69. },
  70. },
  71. },
  72. },
  73. })
  74. _, err := http.Post(fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", w.botToken), "application/json", strings.NewReader(string(sendMsgReq)))
  75. if err != nil {
  76. log.WithContext(ctx).Errorf("telegram push error %+v", err)
  77. }
  78. }
  79. type Config struct {
  80. TgBotToken string `json:"tgBotToken"`
  81. TgChatId string `json:"tgChatId"`
  82. }
  83. func NewTelegramPushHook() *TelegramPushHook {
  84. var cfgData []byte
  85. var err error
  86. cfgData, err = os.ReadFile("./config/config.json")
  87. if err != nil {
  88. panic(err)
  89. }
  90. var mainConfig *config.Config
  91. err = json.Unmarshal(cfgData, &mainConfig)
  92. if err != nil {
  93. panic(err)
  94. }
  95. var pluginConfig *Config
  96. if _, err := os.Stat("./plugins/telegram_push_config.json"); err == nil {
  97. cfgData, err = os.ReadFile("./plugins/telegram_push_config.json")
  98. if err != nil {
  99. panic(err)
  100. }
  101. err = json.Unmarshal(cfgData, &pluginConfig)
  102. if err != nil {
  103. panic(err)
  104. }
  105. }
  106. token := ""
  107. chatID := ""
  108. if pluginConfig != nil {
  109. token = pluginConfig.TgBotToken
  110. chatID = pluginConfig.TgChatId
  111. } else {
  112. token = mainConfig.TgBotToken
  113. chatID = mainConfig.TgChatId
  114. }
  115. ret := &TelegramPushHook{
  116. botToken: token,
  117. chatId: chatID,
  118. webDomain: mainConfig.WebDomain,
  119. httpsEnabled: mainConfig.HttpsEnabled,
  120. }
  121. return ret
  122. }
  123. func main() {
  124. framework.CreatePlugin("telegram_push", NewTelegramPushHook()).Run()
  125. }