telegram_push.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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) SendBefore(ctx *context.Context, email *parsemail.Email) {
  21. }
  22. func (w *TelegramPushHook) SendAfter(ctx *context.Context, email *parsemail.Email, err map[string]error) {
  23. }
  24. func (w *TelegramPushHook) ReceiveParseBefore(ctx *context.Context, email *[]byte) {
  25. }
  26. func (w *TelegramPushHook) ReceiveParseAfter(ctx *context.Context, email *parsemail.Email) {
  27. if w.chatId == "" || w.botToken == "" {
  28. return
  29. }
  30. w.sendUserMsg(nil, email)
  31. }
  32. type SendMessageRequest struct {
  33. ChatID string `json:"chat_id"`
  34. Text string `json:"text"`
  35. ReplyMarkup ReplyMarkup `json:"reply_markup"`
  36. ParseMode string `json:"parse_mode"`
  37. }
  38. type ReplyMarkup struct {
  39. InlineKeyboard [][]InlineKeyboardButton `json:"inline_keyboard"`
  40. }
  41. type InlineKeyboardButton struct {
  42. Text string `json:"text"`
  43. URL string `json:"url"`
  44. }
  45. func (w *TelegramPushHook) sendUserMsg(ctx *context.Context, email *parsemail.Email) {
  46. url := w.webDomain
  47. if w.httpsEnabled > 1 {
  48. url = "http://" + url
  49. } else {
  50. url = "https://" + url
  51. }
  52. sendMsgReq, _ := json.Marshal(SendMessageRequest{
  53. ChatID: w.chatId,
  54. Text: fmt.Sprintf("📧<b>%s</b>&#60;%s&#62;\n\n%s", email.Subject, email.From.EmailAddress, string(email.Text)),
  55. ParseMode: "HTML",
  56. ReplyMarkup: ReplyMarkup{
  57. InlineKeyboard: [][]InlineKeyboardButton{
  58. {
  59. {
  60. Text: "查收邮件",
  61. URL: url,
  62. },
  63. },
  64. },
  65. },
  66. })
  67. _, err := http.Post(fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", w.botToken), "application/json", strings.NewReader(string(sendMsgReq)))
  68. if err != nil {
  69. log.WithContext(ctx).Errorf("telegram push error %+v", err)
  70. }
  71. }
  72. type Config struct {
  73. TgBotToken string `json:"tgBotToken"`
  74. TgChatId string `json:"tgChatId"`
  75. }
  76. func NewTelegramPushHook() *TelegramPushHook {
  77. var cfgData []byte
  78. var err error
  79. cfgData, err = os.ReadFile("../config/config.json")
  80. if err != nil {
  81. panic(err)
  82. }
  83. var mainConfig *config.Config
  84. err = json.Unmarshal(cfgData, &mainConfig)
  85. if err != nil {
  86. panic(err)
  87. }
  88. var pluginConfig *Config
  89. if _, err := os.Stat("./telegram_push_config.json"); err == nil {
  90. cfgData, err = os.ReadFile("./telegram_push_config.json")
  91. if err != nil {
  92. panic(err)
  93. }
  94. err = json.Unmarshal(cfgData, &pluginConfig)
  95. if err != nil {
  96. panic(err)
  97. }
  98. }
  99. token := ""
  100. chatID := ""
  101. if pluginConfig != nil {
  102. token = pluginConfig.TgBotToken
  103. chatID = pluginConfig.TgChatId
  104. } else {
  105. token = mainConfig.TgBotToken
  106. chatID = mainConfig.TgChatId
  107. }
  108. ret := &TelegramPushHook{
  109. botToken: token,
  110. chatId: chatID,
  111. webDomain: mainConfig.WebDomain,
  112. httpsEnabled: mainConfig.HttpsEnabled,
  113. }
  114. return ret
  115. }
  116. func main() {
  117. framework.CreatePlugin(NewTelegramPushHook()).Run()
  118. }