feat: Add web push hook functionality and configuration options

This commit is contained in:
Jeeklin committed 2024-02-27 12:04:28 +08:00
1 parent 483bfa14c1
commit 1b2b3a33ee
10 files changed
+142 -18

No files matched your search

+2
View File
@@ -17,6 +17,8 @@
"weChatPushUserId": "",
"tgChatId": "",
"tgBotToken": "",
"webPushUrl": "",
"webPushToken": "",
"isInit": true,
"httpsEnabled": 2
}
+2
View File
@@ -32,6 +32,8 @@ type Config struct {
TgBotToken string `json:"tgBotToken"`
TgChatId string `json:"tgChatId"`
IsInit bool `json:"isInit"`
WebPushUrl string `json:"webPushUrl"`
WebPushToken string `json:"webPushToken"`
Tables map[string]string `json:"-"`
TablesInitData map[string]string `json:"-"`
}
+2
View File
@@ -17,6 +17,8 @@
"weChatPushUserId": "",
"tgChatId": "",
"tgBotToken": "",
"webPushUrl": "",
"webPushToken": "",
"isInit": true,
"httpsEnabled": 1
}
+2
View File
@@ -17,6 +17,8 @@
"weChatPushUserId": "",
"tgChatId": "",
"tgBotToken": "",
"webPushUrl": "",
"webPushToken": "",
"isInit": true,
"httpsEnabled": 2
}
+2
View File
@@ -3,6 +3,7 @@ package hooks
import (
"pmail/dto/parsemail"
"pmail/hooks/telegram_push"
"pmail/hooks/web_push"
"pmail/hooks/wechat_push"
"pmail/utils/context"
)
@@ -26,5 +27,6 @@ func Init() {
HookList = []EmailHook{
wechat_push.NewWechatPushHook(),
telegram_push.NewTelegramPushHook(),
web_push.NewWebPushHook(),
}
}
+88
View File
@@ -0,0 +1,88 @@
package web_push
import (
"bytes"
"encoding/json"
"net/http"
"pmail/config"
"pmail/dto/parsemail"
"pmail/utils/context"
log "github.com/sirupsen/logrus"
)
type WebPushHook struct {
url string
token string
}
// EmailData 用于存储解析后的邮件数据
type EmailData struct {
From string `json:"from"`
To []string `json:"to"`
Subject string `json:"subject"`
Body string `json:"body"`
Token string `json:"token"`
}
func (w *WebPushHook) SendBefore(ctx *context.Context, email *parsemail.Email) {
}
func (w *WebPushHook) SendAfter(ctx *context.Context, email *parsemail.Email, err map[string]error) {
}
func (w *WebPushHook) ReceiveParseBefore(email []byte) {
}
func (w *WebPushHook) ReceiveParseAfter(email *parsemail.Email) {
if w.url == "" {
return
}
content := string(email.Text)
if content == "" {
content = email.Subject
}
webhookURL := w.url // 替换为您的 Webhook URL
to := make([]string, len(email.To))
for i, user := range email.To {
to[i] = user.EmailAddress
}
data := EmailData{
From: email.From.EmailAddress,
To: to,
Subject: email.Subject,
Body: content,
Token: w.token,
}
var ctx *context.Context = nil
jsonData, err := json.Marshal(data)
if err != nil {
log.WithContext(ctx).Errorf("web push error %+v", err)
}
resp, err := http.Post(webhookURL, "application/json", bytes.NewBuffer(jsonData))
if err != nil {
log.WithContext(ctx).Errorf("web push error %+v", err)
}
defer resp.Body.Close()
}
func NewWebPushHook() *WebPushHook {
ret := &WebPushHook{
url: config.Instance.WebPushUrl,
token: config.Instance.WebPushToken,
}
return ret
}
+19
View File
@@ -0,0 +1,19 @@
package web_push
import (
"pmail/config"
"pmail/dto/parsemail"
"testing"
)
func testInit() {
config.Init()
}
func TestWebPushHook_ReceiveParseAfter(t *testing.T) {
testInit()
w := NewWebPushHook()
w.ReceiveParseAfter(&parsemail.Email{Subject: "标题", Text: []byte("文本内容")})
}
+3 -2
View File
@@ -4,8 +4,6 @@ import (
"embed"
"encoding/json"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/spf13/cast"
"io/fs"
olog "log"
"net/http"
@@ -19,6 +17,9 @@ import (
"pmail/utils/context"
"pmail/utils/id"
"time"
log "github.com/sirupsen/logrus"
"github.com/spf13/cast"
)
//go:embed dist/*