feat(通知): SafeW 只发正文并注入推送时间

去掉渠道标题前缀,模板可用 pushedAt 显示本地推送时间。
This commit is contained in:
2026-08-16 01:18:27 +08:00
parent a9b6234208
commit 14504af950
5 changed files with 42 additions and 6 deletions
+1 -1
View File
@@ -53,7 +53,7 @@ func (s *SafeWSender) Send(title, content string, config json.RawMessage) error
payload := safewMessage{
ChatID: chatID,
Text: title + "\n" + content,
Text: content,
}
body, err := json.Marshal(payload)
if err != nil {
+1 -1
View File
@@ -49,7 +49,7 @@ func TestSafeWSenderSendSuccess(t *testing.T) {
if gotBody["parse_mode"] != nil && gotBody["parse_mode"] != "" {
t.Fatalf("parse_mode = %#v, want omitted/plain", gotBody["parse_mode"])
}
wantText := "hello_world.\nprice=1.5"
wantText := "price=1.5"
if gotBody["text"] != wantText {
t.Fatalf("text = %#v, want %#v", gotBody["text"], wantText)
}
+4
View File
@@ -8,6 +8,7 @@ import (
"log/slog"
"strconv"
"strings"
"time"
"aiaa-notification-service/internal/condition"
"aiaa-notification-service/internal/engine"
@@ -85,6 +86,9 @@ func (s *Service) Process(ctx context.Context, req Request) (Result, error) {
if _, ok := req.Data["event"]; !ok && req.Event != "" {
req.Data["event"] = req.Event
}
if _, ok := req.Data["pushedAt"]; !ok {
req.Data["pushedAt"] = time.Now().In(time.Local).Format("2006.01.02 15:04:05")
}
content, err := s.renderer.Render(tmpl.Content, req.Data)
if err != nil {
+32 -2
View File
@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"strings"
"testing"
"aiaa-notification-service/internal/engine"
@@ -28,9 +29,15 @@ func (f *fakeTemplates) GetTemplate(context.Context, int) (*model.Template, erro
return f.tmpl, f.err
}
type fakeRouter struct{ channels []string }
type fakeRouter struct {
channels []string
title string
content string
}
func (f *fakeRouter) Route(context.Context, *model.Rule, string, string) []string {
func (f *fakeRouter) Route(_ context.Context, _ *model.Rule, title, content string) []string {
f.title = title
f.content = content
return f.channels
}
@@ -111,6 +118,29 @@ func TestProcessTemplateCanSwitchOnEvent(t *testing.T) {
}
}
func TestProcessInjectsPushedAt(t *testing.T) {
rt := &fakeRouter{channels: []string{"safew:1"}}
svc := newSvc(
&fakeMatcher{rule: &model.Rule{ID: 9, TemplateID: 1}},
&fakeTemplates{tmpl: &model.Template{ID: 1, Content: `{{line "推送时间" .pushedAt}}`}},
rt,
)
res, err := svc.Process(context.Background(), Request{
Source: &model.Source{ID: 1, Name: "crypto-strategy"},
Event: "trade.open",
Data: map[string]interface{}{"symbol": "QNT"},
})
if err != nil {
t.Fatal(err)
}
if !res.Matched {
t.Fatalf("%+v", res)
}
if !strings.Contains(rt.content, "推送时间:") {
t.Fatalf("content=%q", rt.content)
}
}
func TestProcessInvalidConditions(t *testing.T) {
raw := json.RawMessage(`not-json`)
svc := newSvc(&fakeMatcher{rule: &model.Rule{ID: 1, TemplateID: 1, Conditions: &raw}}, &fakeTemplates{}, &fakeRouter{})