-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
52 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |