telegram_push.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/Jinnrry/pmail/config"
  6. "github.com/Jinnrry/pmail/dto/parsemail"
  7. "github.com/Jinnrry/pmail/hooks/framework"
  8. "github.com/Jinnrry/pmail/models"
  9. "github.com/Jinnrry/pmail/utils/context"
  10. log "github.com/sirupsen/logrus"
  11. "net/http"
  12. "os"
  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. // GetName 获取插件名称
  33. func (w *TelegramPushHook) GetName(ctx *context.Context) string {
  34. return "TgPush"
  35. }
  36. // SettingsHtml 插件页面
  37. func (w *TelegramPushHook) SettingsHtml(ctx *context.Context, url string, requestData string) string {
  38. return fmt.Sprintf(`
  39. <div>
  40. TG push No Settings Page
  41. </div>
  42. `)
  43. }
  44. func (w *TelegramPushHook) SendBefore(ctx *context.Context, email *parsemail.Email) {
  45. }
  46. func (w *TelegramPushHook) SendAfter(ctx *context.Context, email *parsemail.Email, err map[string]error) {
  47. }
  48. func (w *TelegramPushHook) ReceiveParseBefore(ctx *context.Context, email *[]byte) {
  49. }
  50. func (w *TelegramPushHook) ReceiveParseAfter(ctx *context.Context, email *parsemail.Email) {}
  51. type SendMessageRequest struct {
  52. ChatID string `json:"chat_id"`
  53. Text string `json:"text"`
  54. ReplyMarkup ReplyMarkup `json:"reply_markup"`
  55. ParseMode string `json:"parse_mode"`
  56. }
  57. type ReplyMarkup struct {
  58. InlineKeyboard [][]InlineKeyboardButton `json:"inline_keyboard"`
  59. }
  60. type InlineKeyboardButton struct {
  61. Text string `json:"text"`
  62. URL string `json:"url"`
  63. }
  64. func (w *TelegramPushHook) sendUserMsg(ctx *context.Context, email *parsemail.Email) {
  65. url := w.webDomain
  66. if w.httpsEnabled > 1 {
  67. url = "http://" + url
  68. } else {
  69. url = "https://" + url
  70. }
  71. sendMsgReq, _ := json.Marshal(SendMessageRequest{
  72. ChatID: w.chatId,
  73. Text: fmt.Sprintf("📧<b>%s</b>&#60;%s&#62;\n\n%s", email.Subject, email.From.EmailAddress, string(email.Text)),
  74. ParseMode: "HTML",
  75. ReplyMarkup: ReplyMarkup{
  76. InlineKeyboard: [][]InlineKeyboardButton{
  77. {
  78. {
  79. Text: "查收邮件",
  80. URL: url,
  81. },
  82. },
  83. },
  84. },
  85. })
  86. _, err := http.Post(fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", w.botToken), "application/json", strings.NewReader(string(sendMsgReq)))
  87. if err != nil {
  88. log.WithContext(ctx).Errorf("telegram push error %+v", err)
  89. }
  90. }
  91. type Config struct {
  92. TgBotToken string `json:"tgBotToken"`
  93. TgChatId string `json:"tgChatId"`
  94. }
  95. func NewTelegramPushHook() *TelegramPushHook {
  96. var cfgData []byte
  97. var err error
  98. cfgData, err = os.ReadFile("./config/config.json")
  99. if err != nil {
  100. panic(err)
  101. }
  102. var mainConfig *config.Config
  103. err = json.Unmarshal(cfgData, &mainConfig)
  104. if err != nil {
  105. panic(err)
  106. }
  107. var pluginConfig *Config
  108. if _, err := os.Stat("./plugins/telegram_push_config.json"); err == nil {
  109. cfgData, err = os.ReadFile("./plugins/telegram_push_config.json")
  110. if err != nil {
  111. panic(err)
  112. }
  113. err = json.Unmarshal(cfgData, &pluginConfig)
  114. if err != nil {
  115. panic(err)
  116. }
  117. }
  118. token := ""
  119. chatID := ""
  120. if pluginConfig != nil {
  121. token = pluginConfig.TgBotToken
  122. chatID = pluginConfig.TgChatId
  123. } else {
  124. token = mainConfig.TgBotToken
  125. chatID = mainConfig.TgChatId
  126. }
  127. ret := &TelegramPushHook{
  128. botToken: token,
  129. chatId: chatID,
  130. webDomain: mainConfig.WebDomain,
  131. httpsEnabled: mainConfig.HttpsEnabled,
  132. }
  133. return ret
  134. }
  135. func main() {
  136. framework.CreatePlugin("telegram_push", NewTelegramPushHook()).Run()
  137. }