1
0
mirror of https://github.com/taigrr/godns synced 2025-01-18 04:03:25 -08:00

add slack support for notification

This commit is contained in:
Kent Hua 2020-07-02 06:14:57 +00:00
parent b1e94fda52
commit eec9e9a881
3 changed files with 96 additions and 0 deletions

View File

@ -405,6 +405,24 @@ Update config file and provide your Telegram options, a notification message wil
```
Markdown is supported in message template, and use `%0A` for newline.
### Slack notification support
Update config file and provide your Slack options, a notification message will be sent to your slack channel once the IP is changed and updated.
```json
"notify": {
"slack": {
"enabled": true,
"bot_api_token": "xoxb-xxx",
"channel": "your_channel",
"message_template": "Domain *{{ .Domain }}* is updated to \n{{ .CurrentIP }}",
"use_proxy": false
},
}
```
Markdown is supported in message template, and use `\n` for newline.
### SOCKS5 proxy support
You can also use SOCKS5 proxy, just fill SOCKS5 address to the ```socks5_proxy``` item:

View File

@ -12,6 +12,15 @@ type Domain struct {
SubDomains []string `json:"sub_domains"`
}
// Notify struct for slack notification
type SlackNotify struct {
Enabled bool `json:"enabled"`
BotApiToken string `json:"bot_api_token"`
Channel string `json:"channel"`
MsgTemplate string `json:"message_template"`
UseProxy bool `json:"use_proxy"`
}
// Notify struct for telegram notification
type TelegramNotify struct {
Enabled bool `json:"enabled"`
@ -35,6 +44,7 @@ type MailNotify struct {
type Notify struct {
Telegram TelegramNotify `json:"telegram"`
Mail MailNotify `json:"mail"`
Slack SlackNotify `json:"slack"`
}
// Settings struct

View File

@ -10,6 +10,7 @@ import (
"log"
"net"
"net/http"
"net/url"
"strings"
dnsResolver "github.com/TimothyYe/godns/resolver"
@ -321,6 +322,69 @@ func SendMailNotify(configuration *Settings, domain, currentIP string) error {
return nil
}
// SendSlack sends slack if IP is changed
func SendSlackNotify(configuration *Settings, domain, currentIP string) error {
if !configuration.Notify.Slack.Enabled {
return nil
}
if configuration.Notify.Slack.BotApiToken == "" {
return errors.New("bot api token cannot be empty")
}
if configuration.Notify.Slack.Channel == "" {
return errors.New("channel cannot be empty")
}
client := GetHttpClient(configuration, configuration.Notify.Slack.UseProxy)
tpl := configuration.Notify.Slack.MsgTemplate
if tpl == "" {
tpl = "_Your IP address is changed to_\n\n*{{ .CurrentIP }}*\n\nDomain *{{ .Domain }}* is updated"
}
msg := buildTemplate(currentIP, domain, tpl)
var response *http.Response
var err error
formData := url.Values{
"token": {configuration.Notify.Slack.BotApiToken},
"channel": {configuration.Notify.Slack.Channel},
"text": {msg},
}
response, err = client.PostForm("https://slack.com/api/chat.postMessage", formData)
if err != nil {
return err
}
defer response.Body.Close()
body, _ := ioutil.ReadAll(response.Body)
type ResponseParameters struct {
MigrateToChatID int64 `json:"migrate_to_chat_id"` // optional
RetryAfter int `json:"retry_after"` // optional
}
type APIResponse struct {
Ok bool `json:"ok"`
Result json.RawMessage `json:"result"`
ErrorCode int `json:"error_code"`
Description string `json:"description"`
Parameters *ResponseParameters `json:"parameters"`
}
var resp APIResponse
err = json.Unmarshal([]byte(body), &resp)
if err != nil {
fmt.Println("error:", err)
return errors.New("Failed to parse response")
}
if !resp.Ok {
return errors.New(resp.Description)
}
return nil
}
// SendNotify sends notify if IP is changed
func SendNotify(configuration *Settings, domain, currentIP string) error {
err := SendTelegramNotify(configuration, domain, currentIP)
@ -331,6 +395,10 @@ func SendNotify(configuration *Settings, domain, currentIP string) error {
if err != nil {
log.Println("Send email notification with error:", err.Error())
}
err = SendSlackNotify(configuration, domain, currentIP)
if err != nil {
log.Println("Send slack notification with error:", err.Error())
}
return nil
}