Skip to content

Commit 773b127

Browse files
committed
lib:notification新增slack通知
1 parent e69f2d9 commit 773b127

3 files changed

Lines changed: 52 additions & 3 deletions

File tree

lib/notification/dingtalk.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ type DingTalkResponseEntity struct {
2525
func DingTalkEmit(conf DingTalkConf, content string) (DingTalkResponseEntity, error) {
2626
headers := map[string]string{"Content-Type": "application/json"}
2727
timestamp := time.Now().UnixMilli()
28-
sign, _ := genLarkSign(conf.Secret, timestamp)
28+
sign, _ := genDingTalkSign(conf.Secret, timestamp)
2929

3030
var response DingTalkResponseEntity
3131
url := fmt.Sprintf("%s?timestamp=%d&sign=%s", conf.WebhookUrl, timestamp, sign)
32-
resp, _, err := utils.SendRequest(http.MethodPost, url, []byte(content), headers)
32+
resp, _, err := utils.SendRequest(http.MethodPost, url, []byte(content), headers, time.Second*10)
3333
if err != nil {
3434
return response, err
3535
}

lib/notification/lark.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func LarkEmit(conf LarkConf, content string) (LarkResponseEntity, error) {
4242
payload := fmt.Sprintf(Payload, timestamp, sign, content)
4343

4444
var response LarkResponseEntity
45-
resp, _, err := utils.SendRequest(http.MethodPost, conf.WebhookUrl, []byte(payload), headers)
45+
resp, _, err := utils.SendRequest(http.MethodPost, conf.WebhookUrl, []byte(payload), headers, time.Second*10)
4646
if err != nil {
4747
return response, err
4848
}

lib/notification/slack.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package notification
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
"time"
8+
9+
"github.com/keepchen/go-sail/v3/utils"
10+
)
11+
12+
// SlackConf slack配置
13+
type SlackConf struct {
14+
WebhookUrl string `yaml:"webhook_url" toml:"webhook_url" json:"webhook_url"` //webhook地址
15+
BotToken string `yaml:"bot_token" toml:"bot_token" json:"bot_token"` //bot token,注意:确保Bot Token 拥有 chat:write 权限
16+
ChannelID string `yaml:"channel_id" toml:"channel_id" json:"channel_id"` //频道
17+
}
18+
19+
// SlackResponseEntity 响应实体
20+
type SlackResponseEntity struct {
21+
OK bool `json:"ok"`
22+
Channel string `json:"channel"`
23+
Timestamp string `json:"ts"` //eg. "1640968740.000200"
24+
Message struct {
25+
Text string `json:"text"`
26+
User string `json:"user"`
27+
Timestamp string `json:"ts"` //eg. "1640968740.000200"
28+
} `json:"message"`
29+
}
30+
31+
// SlackEmit 发射slack通知
32+
func SlackEmit(conf SlackConf, content string) (SlackResponseEntity, error) {
33+
headers := map[string]string{
34+
"Content-Type": "application/json",
35+
"Authorization": fmt.Sprintf("Bearer %s", conf.BotToken),
36+
}
37+
message := map[string]interface{}{
38+
"channel": conf.ChannelID,
39+
"text": content,
40+
}
41+
payload, _ := json.Marshal(message)
42+
var response SlackResponseEntity
43+
resp, _, err := utils.SendRequest(http.MethodPost, conf.WebhookUrl, payload, headers, time.Second*10)
44+
if err != nil {
45+
return response, err
46+
}
47+
err = json.Unmarshal(resp, &response)
48+
return response, err
49+
}

0 commit comments

Comments
 (0)