feat: Add Slack as a new IM provider for notification channels

This commit is contained in:
Marvin Zhang
2024-07-23 17:45:52 +08:00
parent 7f9fac0fc0
commit 13c6a20ee3
2 changed files with 24 additions and 5 deletions

View File

@@ -19,6 +19,8 @@ func SendIMNotification(s *models.NotificationSettingV2, ch *models.Notification
switch ch.Provider {
case ChannelIMProviderLark:
return sendImLark(ch, title, content)
case ChannelIMProviderSlack:
return sendImSlack(ch, title, content)
}
// request header
@@ -120,3 +122,14 @@ func sendImLark(ch *models.NotificationChannelV2, title, content string) error {
}
return performIMRequest(ch.WebhookUrl, data)
}
func sendImSlack(ch *models.NotificationChannelV2, title, content string) error {
// request header
data := req.Param{
"blocks": []req.Param{
{"type": "header", "text": req.Param{"type": "plain_text", "text": title}},
{"type": "section", "text": req.Param{"type": "mrkdwn", "text": content}},
},
}
return performIMRequest(ch.WebhookUrl, data)
}

View File

@@ -20,7 +20,6 @@ type ServiceV2 struct {
func (svc *ServiceV2) Send(s *models.NotificationSettingV2, args ...any) {
title := s.Title
content := svc.getContent(s, args...)
wg := sync.WaitGroup{}
wg.Add(len(s.ChannelIds))
@@ -32,6 +31,7 @@ func (svc *ServiceV2) Send(s *models.NotificationSettingV2, args ...any) {
log.Errorf("[NotificationServiceV2] get channel error: %v", err)
return
}
content := svc.getContent(s, ch, args...)
switch ch.Type {
case TypeMail:
svc.SendMail(s, ch, title, content)
@@ -63,7 +63,7 @@ func (svc *ServiceV2) SendIM(s *models.NotificationSettingV2, ch *models.Notific
}
}
func (svc *ServiceV2) getContent(s *models.NotificationSettingV2, args ...any) (content string) {
func (svc *ServiceV2) getContent(s *models.NotificationSettingV2, ch *models.NotificationChannelV2, args ...any) (content string) {
switch s.TriggerTarget {
case constants.NotificationTriggerTargetTask:
vd := svc.getTaskVariableData(args...)
@@ -71,11 +71,17 @@ func (svc *ServiceV2) getContent(s *models.NotificationSettingV2, args ...any) (
case constants.NotificationTemplateModeMarkdown:
variables := svc.parseTemplateVariables(s.TemplateMarkdown)
content = svc.getTaskContent(s.TemplateMarkdown, variables, vd)
content = svc.convertMarkdownToHtml(content)
if ch.Type == TypeMail {
content = svc.convertMarkdownToHtml(content)
}
return content
case constants.NotificationTemplateModeRichText:
variables := svc.parseTemplateVariables(s.TemplateRichText)
return svc.getTaskContent(s.TemplateRichText, variables, vd)
template := s.TemplateRichText
if ch.Type == TypeIM {
template = s.TemplateMarkdown
}
variables := svc.parseTemplateVariables(template)
return svc.getTaskContent(template, variables, vd)
}
case constants.NotificationTriggerTargetNode: