|
| 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