Skip to content

Commit

Permalink
feat: Add Slack as a new IM provider for notification channels
Browse files Browse the repository at this point in the history
  • Loading branch information
tikazyq committed Jul 23, 2024
1 parent 7f9fac0 commit 13c6a20
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
13 changes: 13 additions & 0 deletions core/notification/im.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
16 changes: 11 additions & 5 deletions core/notification/service_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)
Expand Down Expand Up @@ -63,19 +63,25 @@ 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...)
switch s.TemplateMode {
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:
Expand Down

0 comments on commit 13c6a20

Please sign in to comment.