Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Telegram Notifications #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions notify/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
109 changes: 109 additions & 0 deletions notify/telegram_notify.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 4 additions & 0 deletions sample_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down