✨ feat: Add Telegram push hook functionality and configuration options
- Add "tgChatId" and "tgBotToken" fields to configuration files - Modify "config.go" to include new configuration fields - Implement Telegram push hook functionality in "telegram_push.go" - Add test file "telegram_push_test.go" to test Telegram push hook functionality
This commit is contained in:
parent
6afd95d82c
commit
f2a642bf79
@ -12,6 +12,8 @@
|
|||||||
"weChatPushSecret": "",
|
"weChatPushSecret": "",
|
||||||
"weChatPushTemplateId": "",
|
"weChatPushTemplateId": "",
|
||||||
"weChatPushUserId": "",
|
"weChatPushUserId": "",
|
||||||
|
"tgChatId": "",
|
||||||
|
"tgBotToken": "",
|
||||||
"isInit": true,
|
"isInit": true,
|
||||||
"httpsEnabled": 2
|
"httpsEnabled": 2
|
||||||
}
|
}
|
||||||
@ -24,6 +24,8 @@ type Config struct {
|
|||||||
WeChatPushSecret string `json:"weChatPushSecret"`
|
WeChatPushSecret string `json:"weChatPushSecret"`
|
||||||
WeChatPushTemplateId string `json:"weChatPushTemplateId"`
|
WeChatPushTemplateId string `json:"weChatPushTemplateId"`
|
||||||
WeChatPushUserId string `json:"weChatPushUserId"`
|
WeChatPushUserId string `json:"weChatPushUserId"`
|
||||||
|
TgBotToken string `json:"tgBotToken"`
|
||||||
|
TgChatId string `json:"tgChatId"`
|
||||||
IsInit bool `json:"isInit"`
|
IsInit bool `json:"isInit"`
|
||||||
HttpsEnabled int `json:"httpsEnabled"` //后台页面是否启用https,0默认(启用),1启用,2不启用
|
HttpsEnabled int `json:"httpsEnabled"` //后台页面是否启用https,0默认(启用),1启用,2不启用
|
||||||
Tables map[string]string `json:"-"`
|
Tables map[string]string `json:"-"`
|
||||||
|
|||||||
@ -12,6 +12,8 @@
|
|||||||
"weChatPushSecret": "",
|
"weChatPushSecret": "",
|
||||||
"weChatPushTemplateId": "",
|
"weChatPushTemplateId": "",
|
||||||
"weChatPushUserId": "",
|
"weChatPushUserId": "",
|
||||||
|
"tgChatId": "",
|
||||||
|
"tgBotToken": "",
|
||||||
"isInit": true,
|
"isInit": true,
|
||||||
"httpsEnabled": 2
|
"httpsEnabled": 2
|
||||||
}
|
}
|
||||||
@ -12,6 +12,8 @@
|
|||||||
"weChatPushSecret": "",
|
"weChatPushSecret": "",
|
||||||
"weChatPushTemplateId": "",
|
"weChatPushTemplateId": "",
|
||||||
"weChatPushUserId": "",
|
"weChatPushUserId": "",
|
||||||
|
"tgChatId": "",
|
||||||
|
"tgBotToken": "",
|
||||||
"isInit": true,
|
"isInit": true,
|
||||||
"httpsEnabled": 2
|
"httpsEnabled": 2
|
||||||
}
|
}
|
||||||
@ -3,6 +3,7 @@ package hooks
|
|||||||
import (
|
import (
|
||||||
"pmail/dto"
|
"pmail/dto"
|
||||||
"pmail/dto/parsemail"
|
"pmail/dto/parsemail"
|
||||||
|
"pmail/hooks/telegram_push"
|
||||||
"pmail/hooks/wechat_push"
|
"pmail/hooks/wechat_push"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -24,5 +25,6 @@ var HookList []EmailHook
|
|||||||
func Init() {
|
func Init() {
|
||||||
HookList = []EmailHook{
|
HookList = []EmailHook{
|
||||||
wechat_push.NewWechatPushHook(),
|
wechat_push.NewWechatPushHook(),
|
||||||
|
telegram_push.NewTelegramPushHook(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
96
server/hooks/telegram_push/telegram_push.go
Normal file
96
server/hooks/telegram_push/telegram_push.go
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
package telegram_push
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"pmail/config"
|
||||||
|
"pmail/dto"
|
||||||
|
"pmail/dto/parsemail"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TelegramPushHook struct {
|
||||||
|
chatId string
|
||||||
|
botToken string
|
||||||
|
httpsEnabled int
|
||||||
|
webDomain string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *TelegramPushHook) SendBefore(ctx *dto.Context, email *parsemail.Email) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *TelegramPushHook) SendAfter(ctx *dto.Context, email *parsemail.Email, err map[string]error) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *TelegramPushHook) ReceiveParseBefore(email []byte) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *TelegramPushHook) ReceiveParseAfter(email *parsemail.Email) {
|
||||||
|
if w.chatId == "" || w.botToken == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.sendUserMsg(nil, email)
|
||||||
|
}
|
||||||
|
|
||||||
|
type SendMessageRequest struct {
|
||||||
|
ChatID string `json:"chat_id"`
|
||||||
|
Text string `json:"text"`
|
||||||
|
ReplyMarkup ReplyMarkup `json:"reply_markup"`
|
||||||
|
ParseMode string `json:"parse_mode"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ReplyMarkup struct {
|
||||||
|
InlineKeyboard [][]InlineKeyboardButton `json:"inline_keyboard"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type InlineKeyboardButton struct {
|
||||||
|
Text string `json:"text"`
|
||||||
|
URL string `json:"url"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *TelegramPushHook) sendUserMsg(ctx *dto.Context, email *parsemail.Email) {
|
||||||
|
url := w.webDomain
|
||||||
|
if w.httpsEnabled > 1 {
|
||||||
|
url = "http://" + url
|
||||||
|
} else {
|
||||||
|
url = "https://" + url
|
||||||
|
}
|
||||||
|
sendMsgReq, _ := json.Marshal(SendMessageRequest{
|
||||||
|
ChatID: w.chatId,
|
||||||
|
Text: fmt.Sprintf("📧<b>%s</b><%s>\n\n%s", email.Subject, email.From.EmailAddress, string(email.Text)),
|
||||||
|
ParseMode: "HTML",
|
||||||
|
ReplyMarkup: ReplyMarkup{
|
||||||
|
InlineKeyboard: [][]InlineKeyboardButton{
|
||||||
|
{
|
||||||
|
{
|
||||||
|
Text: "查收邮件",
|
||||||
|
URL: url,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
_, err := http.Post(fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", w.botToken), "application/json", strings.NewReader(string(sendMsgReq)))
|
||||||
|
if err != nil {
|
||||||
|
log.WithContext(ctx).Errorf("telegram push error %+v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
func NewTelegramPushHook() *TelegramPushHook {
|
||||||
|
ret := &TelegramPushHook{
|
||||||
|
botToken: config.Instance.TgBotToken,
|
||||||
|
chatId: config.Instance.TgChatId,
|
||||||
|
webDomain: config.Instance.WebDomain,
|
||||||
|
httpsEnabled: config.Instance.HttpsEnabled,
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
|
||||||
|
}
|
||||||
21
server/hooks/telegram_push/telegram_push_test.go
Normal file
21
server/hooks/telegram_push/telegram_push_test.go
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package telegram_push
|
||||||
|
|
||||||
|
import (
|
||||||
|
"pmail/config"
|
||||||
|
"pmail/dto/parsemail"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func testInit() {
|
||||||
|
|
||||||
|
config.Init()
|
||||||
|
|
||||||
|
}
|
||||||
|
func TestWeChatPushHook_ReceiveParseAfter(t *testing.T) {
|
||||||
|
testInit()
|
||||||
|
|
||||||
|
w := NewTelegramPushHook()
|
||||||
|
w.ReceiveParseAfter(&parsemail.Email{Subject: "标题", Text: []byte("文本内容"), From: &parsemail.User{
|
||||||
|
EmailAddress: "hello@gmail.com",
|
||||||
|
}})
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user