From 8cc35a7b4472fcc05c39bf8aa5e5898e159d623c Mon Sep 17 00:00:00 2001 From: Jonathan Toledo Date: Wed, 2 Oct 2019 14:28:14 -0500 Subject: [PATCH] Telegram Notifications --- Config.md | 11 ++++ README.md | 1 + notify/notify.go | 1 + notify/telegram_notify.go | 109 ++++++++++++++++++++++++++++++++++++++ sample_config.json | 4 ++ 5 files changed, 126 insertions(+) create mode 100644 notify/telegram_notify.go diff --git a/Config.md b/Config.md index 6c536fa..d013357 100644 --- a/Config.md +++ b/Config.md @@ -186,6 +186,17 @@ To recieve notifications to any Dingding add below block to your config file wit "Content-Type":"application/json" } } +``` +### Telegram + +To recieve notifications to your Telegram Channel/Chat, add below block to your config file with your telegram details + +``` +"telegram":{ + "botToken":"bot token", + "chatID":"chat id" +} + ``` [Dingding Dev Document](https://open-doc.dingtalk.com/docs/doc.htm?spm=a219a.7629140.0.0.Tvbh61&treeId=257&articleId=105735&docType=1) diff --git a/README.md b/README.md index 9298419..91324e2 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,7 @@ Notifications will be triggered when mean response time is below given response 3. [Mailgun](https://github.com/sanathp/statusok/blob/master/Config.md#mailgun) 4. [Http EndPoint](https://github.com/sanathp/statusok/blob/master/Config.md#http-endpoint) 5. [Dingding](https://github.com/sanathp/statusok/blob/master/Config.md#dingding) +6. [Telegram](https://github.com/sanathp/statusok/blob/master/Config.md#telegram) Adding support to other clients is simple.[view details](https://github.com/sanathp/statusok/blob/master/Config.md#write-your-own-notification-client) diff --git a/notify/notify.go b/notify/notify.go index b2f9671..b965bc9 100644 --- a/notify/notify.go +++ b/notify/notify.go @@ -13,6 +13,7 @@ type NotificationTypes struct { MailNotify MailNotify `json:"mail"` Mailgun MailgunNotify `json:"mailGun"` Slack SlackNotify `json:"slack"` + Telegram TelegramNotify `json:"telegram"` Http HttpNotify `json:"httpEndPoint"` Dingding DingdingNotify `json:"dingding"` Pagerduty PagerdutyNotify `json:"pagerduty"` diff --git a/notify/telegram_notify.go b/notify/telegram_notify.go new file mode 100644 index 0000000..7070182 --- /dev/null +++ b/notify/telegram_notify.go @@ -0,0 +1,109 @@ +package notify + +import ( + "bytes" + "encoding/json" + "errors" + "io" + "net/http" + "strconv" + "strings" + "fmt" +) + +type TelegramNotify struct { + BotToken string `json:"botToken"` + ChatID string `json:"chatID"` +} + +type telegramPostMessage struct { + ChatID string `json:"chat_id"` + Text string `json:"text,omitempty"` +} + +func (telegramNotify TelegramNotify) GetClientName() string { + return "Telegram" +} + +func (telegramNotify TelegramNotify) Initialize() error { + + if len(strings.TrimSpace(telegramNotify.BotToken)) == 0 { + return errors.New("Telegram: botToken is a required field") + } + + if len(strings.TrimSpace(telegramNotify.ChatID)) == 0 { + return errors.New("Telegram: chatID is a required field") + } + + return nil +} + +func (telegramNotify TelegramNotify) SendResponseTimeNotification(responseTimeNotification ResponseTimeNotification) error { + + message := getMessageFromResponseTimeNotification(responseTimeNotification) + + payload, jsonErr := telegramNotify.getJsonParamBody(message) + + if jsonErr != nil { + return jsonErr + } + + requestURL := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", telegramNotify.BotToken) + + getResponse, respErr := http.Post(requestURL, "application/json", payload) + + if respErr != nil { + return respErr + } + + defer getResponse.Body.Close() + + if getResponse.StatusCode != http.StatusOK { + return errors.New("Telegram : Send notifaction failed. Response code " + strconv.Itoa(getResponse.StatusCode)) + } + + return nil +} + +func (telegramNotify TelegramNotify) SendErrorNotification(errorNotification ErrorNotification) error { + + message := getMessageFromErrorNotification(errorNotification) + + payload, jsonErr := telegramNotify.getJsonParamBody(message) + + if jsonErr != nil { + return jsonErr + } + + requestURL := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", telegramNotify.BotToken) + + getResponse, respErr := http.Post(requestURL, "application/json", payload) + + if respErr != nil { + return respErr + } + + defer getResponse.Body.Close() + + if getResponse.StatusCode != http.StatusOK { + return errors.New("Telegram : Send notifaction failed. Response code " + strconv.Itoa(getResponse.StatusCode)) + } + + return nil +} + +func (telegramNotify TelegramNotify) getJsonParamBody(message string) (io.Reader, error) { + + data, jsonErr := json.Marshal(telegramPostMessage{telegramNotify.ChatID, + message, + }) + + if jsonErr != nil { + + jsonErr = errors.New("Invalid Parameters for Content-Type application/json : " + jsonErr.Error()) + + return nil, jsonErr + } + + return bytes.NewBuffer(data), nil +} diff --git a/sample_config.json b/sample_config.json index 04b5d68..fc04970 100644 --- a/sample_config.json +++ b/sample_config.json @@ -29,6 +29,10 @@ "username":"statusok", "channelWebhookURL":"https://hooks.slack.com/services/T09ZQZhET2E5Tl7" }, + "telegram":{ + "botToken":"xxxxxx:xxxx-xxxxxxxxxxxxxxxx", + "chatID":"-1001027884121" + }, "mailGun":{ "email":"statusok@gmail.com", "apiKey":"key-a8215497fc0",