BUGFIX: 修复短信报警模板的转义问题 (#440)

* 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 <zhupeiyuan@fenbi.com>
This commit is contained in:
Paul Chu 2020-12-04 16:18:05 +08:00 committed by GitHub
parent eedfc99064
commit 30b600fe36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 5 deletions

View File

@ -7,8 +7,8 @@
{{end}}监控指标:{{.Metric}}
指标标签:{{.Tags}}
当前值:{{.Value}}
报警说明:{{.Info}}
报警说明:{{.Info | unescaped}}
触发时间:{{.Etime}}
报警详情:{{.Elink}}
报警策略:{{.Slink}}
{{if .HasClaim}}认领报警:{{.Clink}}{{end}}
报警详情:{{.Elink | urlconvert}}
报警策略:{{.Slink | urlconvert}}
{{if .HasClaim}}认领报警:{{.Clink | urlconvert}}{{end}}

View File

@ -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)