Skip to content

Commit 2f5ae35

Browse files
committed
notificator: add telegram
1 parent d1d3650 commit 2f5ae35

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

cmd/tob/main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/telkomdev/tob/notificators/discord"
1414
"github.com/telkomdev/tob/notificators/email"
1515
"github.com/telkomdev/tob/notificators/slack"
16+
"github.com/telkomdev/tob/notificators/telegram"
1617
)
1718

1819
func main() {
@@ -70,10 +71,18 @@ func main() {
7071
os.Exit(1)
7172
}
7273

74+
// telegram notificator
75+
telegramNotificator, err := telegram.NewTelegram(configs)
76+
if err != nil {
77+
fmt.Println("error: ", err)
78+
os.Exit(1)
79+
}
80+
7381
notificators := []tob.Notificator{
7482
emailNotificator,
7583
discordNotificator,
7684
slackNotificator,
85+
telegramNotificator,
7786
}
7887

7988
ctx, cancel := context.WithCancel(context.Background())

config_example.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@
8181
8282
"subject": "Tob Notification",
8383
"enable": true
84+
},
85+
86+
"telegram": {
87+
"botToken": "XXXXXXXXXX:XXXXXXXXXXX_XXXXXXXXXXXXXXXXXXXXXXX",
88+
"groupId": "-111111111",
89+
"enable": true
8490
}
8591
},
8692

notificators/telegram/telegram.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package telegram
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"github.com/telkomdev/tob/config"
7+
"github.com/telkomdev/tob/httpx"
8+
"net/url"
9+
)
10+
11+
const (
12+
// TelegramAPIURL Telegram base URL API
13+
TelegramAPIURL = "https://api.telegram.org/bot%s/sendMessage?%s"
14+
)
15+
16+
// Telegram represent Telegram notificator
17+
type Telegram struct {
18+
botToken string
19+
groupID string
20+
headers map[string]string
21+
enabled bool
22+
}
23+
24+
// NewTelegram Telegram's constructor
25+
func NewTelegram(configs config.Config) (*Telegram, error) {
26+
notificatorConfigInterface, ok := configs["notificator"]
27+
if !ok {
28+
return nil, errors.New("error: cannot find notificator field in the config file")
29+
}
30+
31+
notificatorConfig := notificatorConfigInterface.(map[string]interface{})
32+
33+
TelegramConfigInterface, ok := notificatorConfig["telegram"]
34+
if !ok {
35+
return nil, errors.New("error: cannot find Telegram field in the config file")
36+
}
37+
38+
TelegramConfig := TelegramConfigInterface.(map[string]interface{})
39+
40+
botToken, ok := TelegramConfig["botToken"].(string)
41+
if !ok {
42+
return nil, errors.New("error: cannot find Telegram botToken field in the config file")
43+
}
44+
45+
groupID, ok := TelegramConfig["groupId"].(string)
46+
if !ok {
47+
return nil, errors.New("error: cannot find Telegram groupId field in the config file")
48+
}
49+
50+
enabled, ok := TelegramConfig["enable"].(bool)
51+
if !ok {
52+
return nil, errors.New("error: cannot find Telegram enable field in the config file")
53+
}
54+
55+
headers := make(map[string]string)
56+
headers["Content-Type"] = "application/json"
57+
58+
return &Telegram{
59+
botToken: botToken,
60+
groupID: groupID,
61+
enabled: enabled,
62+
headers: headers,
63+
}, nil
64+
}
65+
66+
// Provider will return Notificator provider
67+
func (d *Telegram) Provider() string {
68+
return "telegram"
69+
}
70+
71+
// Send will send notification
72+
func (d *Telegram) Send(msg string) error {
73+
encodedMessageVal := url.Values{}
74+
encodedMessageVal.Add("text", msg)
75+
encodedMessageVal.Add("chat_id", d.groupID)
76+
encodedMessageVal.Add("disable_web_page_preview", "true")
77+
78+
telegramURL := fmt.Sprintf(TelegramAPIURL, d.botToken, encodedMessageVal.Encode())
79+
80+
_, err := httpx.HTTPGet(telegramURL, d.headers, 5)
81+
if err != nil {
82+
return err
83+
}
84+
85+
return nil
86+
}
87+
88+
// IsEnabled will return enable status
89+
func (d *Telegram) IsEnabled() bool {
90+
return d.enabled
91+
}

0 commit comments

Comments
 (0)