From 30b600fe368c66ea1e5d61db9537cd02103ddf7e Mon Sep 17 00:00:00 2001 From: Paul Chu Date: Fri, 4 Dec 2020 16:18:05 +0800 Subject: [PATCH] =?UTF-8?q?BUGFIX:=20=E4=BF=AE=E5=A4=8D=E7=9F=AD=E4=BF=A1?= =?UTF-8?q?=E6=8A=A5=E8=AD=A6=E6=A8=A1=E6=9D=BF=E7=9A=84=E8=BD=AC=E4=B9=89?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20(#440)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * FIX: 修复短信报警模板的转义问题 报警说明里的信息由于 html template 的转义,会将部分字符转义为 html 表示,但是短信内容不需要转义。 向 template 模板添加 unescaped 处理函数,并在模板文件中使用 unescaped 标识不需要转义的字段,实现避免转义 * FIX: html template func 需要在 phase 之前添加 * FIX: use the filename as template name * FIX: template name Co-authored-by: zhupeiyuan --- etc/sms.tpl | 8 ++++---- src/modules/monapi/notify/notify.go | 5 ++++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/etc/sms.tpl b/etc/sms.tpl index 221116bf..429ea031 100644 --- a/etc/sms.tpl +++ b/etc/sms.tpl @@ -7,8 +7,8 @@ {{end}}监控指标:{{.Metric}} 指标标签:{{.Tags}} 当前值:{{.Value}} -报警说明:{{.Info}} +报警说明:{{.Info | unescaped}} 触发时间:{{.Etime}} -报警详情:{{.Elink}} -报警策略:{{.Slink}} -{{if .HasClaim}}认领报警:{{.Clink}}{{end}} \ No newline at end of file +报警详情:{{.Elink | urlconvert}} +报警策略:{{.Slink | urlconvert}} +{{if .HasClaim}}认领报警:{{.Clink | urlconvert}}{{end}} \ No newline at end of file diff --git a/src/modules/monapi/notify/notify.go b/src/modules/monapi/notify/notify.go index 595dbc7a..7fa9df8f 100644 --- a/src/modules/monapi/notify/notify.go +++ b/src/modules/monapi/notify/notify.go @@ -189,7 +189,10 @@ func genContent(isUpgrade bool, events []*models.Event) (string, string) { // 生成告警短信,短信和IM复用一个内容模板 fp = path.Join(file.SelfDir(), "etc", "sms.tpl") - t, err = template.ParseFiles(fp) + t, err = template.New("sms.tpl").Funcs(template.FuncMap{ + "unescaped": func(str string) interface{} { return template.HTML(str) }, + "urlconvert": func(str string) interface{} { return template.URL(str) }, + }).ParseFiles(fp) if err != nil { logger.Errorf("InternalServerError: cannot parse %s %v", fp, err) smsContent = fmt.Sprintf("InternalServerError: cannot parse %s %v", fp, err)