feat(通知模板): 支持 case 取值映射与事件名匹配渲染
Motivation: 让通知模板能够将 action、event 等原始枚举值映射为可读中文文案,并支持按事件名后缀匹配;同时简化 SafeW 消息发送格式,避免 Markdown 转义引入的显示问题。 Changes: * 新增 `case` 模板函数,按值匹配 key/文案并支持末尾奇数参数作为默认值 * `case` 匹配兼容事件名后缀(如 trade.close 匹配 .close) * `line` 前缀自动去除末尾冒号,避免重复冒号 * 模板渲染前自动注入 event 字段,便于模板按事件名取值 * SafeW 消息改为纯文本发送,移除 MarkdownV2 转义
This commit is contained in:
@@ -79,6 +79,13 @@ func (s *Service) Process(ctx context.Context, req Request) (Result, error) {
|
||||
return Result{}, fmt.Errorf("template not found")
|
||||
}
|
||||
|
||||
if req.Data == nil {
|
||||
req.Data = map[string]interface{}{}
|
||||
}
|
||||
if _, ok := req.Data["event"]; !ok && req.Event != "" {
|
||||
req.Data["event"] = req.Event
|
||||
}
|
||||
|
||||
content, err := s.renderer.Render(tmpl.Content, req.Data)
|
||||
if err != nil {
|
||||
return Result{}, fmt.Errorf("%w: template render failed: %s", ErrUnprocessable, err.Error())
|
||||
|
||||
@@ -92,6 +92,25 @@ func TestProcessMatched(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestProcessTemplateCanSwitchOnEvent(t *testing.T) {
|
||||
svc := newSvc(
|
||||
&fakeMatcher{rule: &model.Rule{ID: 9, TemplateID: 1}},
|
||||
&fakeTemplates{tmpl: &model.Template{ID: 1, Content: `{{case .event ".open" "开仓" ".close" "平仓"}}`}},
|
||||
&fakeRouter{channels: []string{"safew:1"}},
|
||||
)
|
||||
res, err := svc.Process(context.Background(), Request{
|
||||
Source: &model.Source{ID: 1, Name: "crypto-strategy"},
|
||||
Event: "trade.close",
|
||||
Data: map[string]interface{}{"symbol": "XAU"},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !res.Matched {
|
||||
t.Fatalf("%+v", res)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProcessInvalidConditions(t *testing.T) {
|
||||
raw := json.RawMessage(`not-json`)
|
||||
svc := newSvc(&fakeMatcher{rule: &model.Rule{ID: 1, TemplateID: 1, Conditions: &raw}}, &fakeTemplates{}, &fakeRouter{})
|
||||
|
||||
Reference in New Issue
Block a user