Skip to content

Commit

Permalink
refactor: Update IM notification function to support DingTalk and Wec…
Browse files Browse the repository at this point in the history
…hat Work providers
  • Loading branch information
tikazyq committed Jul 24, 2024
1 parent 7b1fa48 commit 46839f1
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 7 deletions.
66 changes: 62 additions & 4 deletions core/notification/im.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package notification

import (
"errors"
"fmt"
"github.com/apex/log"
"github.com/crawlab-team/crawlab/core/models/models/v2"
"github.com/crawlab-team/crawlab/trace"
"github.com/imroc/req"
"regexp"
"strings"
)

Expand All @@ -14,13 +16,17 @@ type ResBody struct {
ErrMsg string `json:"errmsg"`
}

func SendIMNotification(s *models.NotificationSettingV2, ch *models.NotificationChannelV2, title, content string) error {
func SendIMNotification(ch *models.NotificationChannelV2, title, content string) error {
// TODO: compatibility with different IM providers
switch ch.Provider {
case ChannelIMProviderLark:
return sendImLark(ch, title, content)
case ChannelIMProviderSlack:
return sendImSlack(ch, title, content)
case ChannelIMProviderDingtalk:
return sendImDingTalk(ch, title, content)
case ChannelIMProviderWechatWork:
return sendImWechatWork(ch, title, content)
}

// request header
Expand Down Expand Up @@ -101,8 +107,40 @@ func performIMRequest(webhookUrl string, data req.Param) error {
return nil
}

func convertMarkdownToSlack(markdown string) string {
// Convert bold text
reBold := regexp.MustCompile(`\*\*(.*?)\*\*`)
slack := reBold.ReplaceAllString(markdown, `*$1*`)

// Convert italic text
reItalic := regexp.MustCompile(`\*(.*?)\*`)
slack = reItalic.ReplaceAllString(slack, `_$1_`)

// Convert links
reLink := regexp.MustCompile(`\[(.*?)\]\((.*?)\)`)
slack = reLink.ReplaceAllString(slack, `<$2|$1>`)

// Convert inline code
reInlineCode := regexp.MustCompile("`(.*?)`")
slack = reInlineCode.ReplaceAllString(slack, "`$1`")

// Convert unordered list
slack = strings.ReplaceAll(slack, "- ", "• ")

// Convert ordered list
reOrderedList := regexp.MustCompile(`^\d+\. `)
slack = reOrderedList.ReplaceAllStringFunc(slack, func(s string) string {
return strings.Replace(s, ". ", ". ", 1)
})

// Convert blockquote
reBlockquote := regexp.MustCompile(`^> (.*)`)
slack = reBlockquote.ReplaceAllString(slack, `> $1`)

return slack
}

func sendImLark(ch *models.NotificationChannelV2, title, content string) error {
// request header
data := req.Param{
"msg_type": "interactive",
"card": req.Param{
Expand All @@ -124,11 +162,31 @@ func sendImLark(ch *models.NotificationChannelV2, title, content string) error {
}

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}},
{"type": "section", "text": req.Param{"type": "mrkdwn", "text": convertMarkdownToSlack(content)}},
},
}
return performIMRequest(ch.WebhookUrl, data)
}

func sendImDingTalk(ch *models.NotificationChannelV2, title string, content string) error {
data := req.Param{
"msgtype": "markdown",
"markdown": req.Param{
"title": title,
"text": fmt.Sprintf("# %s\n\n%s", title, content),
},
}
return performIMRequest(ch.WebhookUrl, data)
}

func sendImWechatWork(ch *models.NotificationChannelV2, title string, content string) error {
data := req.Param{
"msgtype": "markdown",
"markdown": req.Param{
"content": fmt.Sprintf("# %s\n\n%s", title, content),
},
}
return performIMRequest(ch.WebhookUrl, data)
Expand Down
6 changes: 3 additions & 3 deletions core/notification/service_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (svc *ServiceV2) Send(s *models.NotificationSettingV2, args ...any) {
case TypeMail:
svc.SendMail(s, ch, title, content)
case TypeIM:
svc.SendIM(s, ch, title, content)
svc.SendIM(ch, title, content)
}
}(chId)
}
Expand All @@ -58,8 +58,8 @@ func (svc *ServiceV2) SendMail(s *models.NotificationSettingV2, ch *models.Notif
}
}

func (svc *ServiceV2) SendIM(s *models.NotificationSettingV2, ch *models.NotificationChannelV2, title, content string) {
err := SendIMNotification(s, ch, title, content)
func (svc *ServiceV2) SendIM(ch *models.NotificationChannelV2, title, content string) {
err := SendIMNotification(ch, title, content)
if err != nil {
log.Errorf("[NotificationServiceV2] send mobile notification error: %v", err)
}
Expand Down

0 comments on commit 46839f1

Please sign in to comment.