mirror of
https://github.com/StackExchange/dnscontrol.git
synced 2025-12-17 22:28:29 +08:00
New feature: Telegram Notifier (#2503)
Co-authored-by: Tom Limoncelli <tlimoncelli@stackoverflow.com>
This commit is contained in:
parent
8bf996bb8e
commit
e26afaf7e0
2 changed files with 62 additions and 0 deletions
|
|
@ -82,6 +82,12 @@ Please see the [Teams documentation](https://docs.microsoft.com/en-us/microsoftt
|
||||||
|
|
||||||
Configure `teams_url` to this webhook.
|
Configure `teams_url` to this webhook.
|
||||||
|
|
||||||
|
### Telegram
|
||||||
|
|
||||||
|
If you want to use the [Telegram](https://telegram.org/) integration, you need to create a Telegram bot and obtain a Bot Token, as well as a Chat ID. Get a Bot Token by contacting [@BotFather](https://telegram.me/botfather), and a Chat ID by contacting [@myidbot](https://telegram.me/myidbot).
|
||||||
|
|
||||||
|
Configure `telegram_bot_token` and `telegram_chat_id` to these values.
|
||||||
|
|
||||||
### Bonfire
|
### Bonfire
|
||||||
|
|
||||||
This is Stack Overflow's built in chat system. This is probably not useful for most people.
|
This is Stack Overflow's built in chat system. This is probably not useful for most people.
|
||||||
|
|
|
||||||
56
pkg/notifications/telegram.go
Normal file
56
pkg/notifications/telegram.go
Normal file
|
|
@ -0,0 +1,56 @@
|
||||||
|
package notifications
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
initers = append(initers, func(cfg map[string]string) Notifier {
|
||||||
|
if bot_token, ok := cfg["telegram_bot_token"]; ok {
|
||||||
|
if chat_id, ok := cfg["telegram_chat_id"]; ok {
|
||||||
|
notifier := &telegramNotifier{
|
||||||
|
BOT_TOKEN: bot_token,
|
||||||
|
CHAT_ID: chat_id,
|
||||||
|
}
|
||||||
|
return notifier
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// telegramNotifier sends notifications to Telegram
|
||||||
|
type telegramNotifier struct {
|
||||||
|
BOT_TOKEN string
|
||||||
|
CHAT_ID string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *telegramNotifier) Notify(domain, provider, msg string, err error, preview bool) {
|
||||||
|
var payload struct {
|
||||||
|
ChatID int64 `json:"chat_id"`
|
||||||
|
Text string `json:"text"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var url = fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", s.BOT_TOKEN)
|
||||||
|
|
||||||
|
payload.ChatID, _ = strconv.ParseInt(s.CHAT_ID, 10, 64)
|
||||||
|
|
||||||
|
if preview {
|
||||||
|
payload.Text = fmt.Sprintf(`DNSControl preview: %s[%s] -** %s`, domain, provider, msg)
|
||||||
|
} else if err != nil {
|
||||||
|
payload.Text = fmt.Sprintf(`DNSControl ERROR running correction on %s[%s] -** (%s) Error: %s`, domain, provider, msg, err)
|
||||||
|
} else {
|
||||||
|
payload.Text = fmt.Sprintf(`DNSControl successfully ran correction for **%s[%s]** - %s`, domain, provider, msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
marshaled_payload, _ := json.Marshal(payload)
|
||||||
|
|
||||||
|
_, _ = http.Post(url, "application/json", bytes.NewBuffer(marshaled_payload))
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *telegramNotifier) Done() {}
|
||||||
Loading…
Add table
Reference in a new issue