telegram_push.go 3.3 KB

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