Skip to content

Commit

Permalink
lib:notification新增slack通知
Browse files Browse the repository at this point in the history
  • Loading branch information
keepchen committed Jan 17, 2025
1 parent e69f2d9 commit 773b127
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/notification/dingtalk.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ type DingTalkResponseEntity struct {
func DingTalkEmit(conf DingTalkConf, content string) (DingTalkResponseEntity, error) {
headers := map[string]string{"Content-Type": "application/json"}
timestamp := time.Now().UnixMilli()
sign, _ := genLarkSign(conf.Secret, timestamp)
sign, _ := genDingTalkSign(conf.Secret, timestamp)

var response DingTalkResponseEntity
url := fmt.Sprintf("%s?timestamp=%d&sign=%s", conf.WebhookUrl, timestamp, sign)
resp, _, err := utils.SendRequest(http.MethodPost, url, []byte(content), headers)
resp, _, err := utils.SendRequest(http.MethodPost, url, []byte(content), headers, time.Second*10)
if err != nil {
return response, err
}
Expand Down
2 changes: 1 addition & 1 deletion lib/notification/lark.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func LarkEmit(conf LarkConf, content string) (LarkResponseEntity, error) {
payload := fmt.Sprintf(Payload, timestamp, sign, content)

var response LarkResponseEntity
resp, _, err := utils.SendRequest(http.MethodPost, conf.WebhookUrl, []byte(payload), headers)
resp, _, err := utils.SendRequest(http.MethodPost, conf.WebhookUrl, []byte(payload), headers, time.Second*10)
if err != nil {
return response, err
}
Expand Down
49 changes: 49 additions & 0 deletions lib/notification/slack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package notification

import (
"encoding/json"
"fmt"
"net/http"
"time"

"github.com/keepchen/go-sail/v3/utils"
)

// SlackConf slack配置
type SlackConf struct {
WebhookUrl string `yaml:"webhook_url" toml:"webhook_url" json:"webhook_url"` //webhook地址
BotToken string `yaml:"bot_token" toml:"bot_token" json:"bot_token"` //bot token,注意:确保Bot Token 拥有 chat:write 权限
ChannelID string `yaml:"channel_id" toml:"channel_id" json:"channel_id"` //频道
}

// SlackResponseEntity 响应实体
type SlackResponseEntity struct {
OK bool `json:"ok"`
Channel string `json:"channel"`
Timestamp string `json:"ts"` //eg. "1640968740.000200"
Message struct {
Text string `json:"text"`
User string `json:"user"`
Timestamp string `json:"ts"` //eg. "1640968740.000200"
} `json:"message"`
}

// SlackEmit 发射slack通知
func SlackEmit(conf SlackConf, content string) (SlackResponseEntity, error) {
headers := map[string]string{
"Content-Type": "application/json",
"Authorization": fmt.Sprintf("Bearer %s", conf.BotToken),
}
message := map[string]interface{}{
"channel": conf.ChannelID,
"text": content,
}
payload, _ := json.Marshal(message)
var response SlackResponseEntity
resp, _, err := utils.SendRequest(http.MethodPost, conf.WebhookUrl, payload, headers, time.Second*10)
if err != nil {
return response, err
}
err = json.Unmarshal(resp, &response)
return response, err
}

0 comments on commit 773b127

Please sign in to comment.