2019-12-23 09:21:21 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-03-26 10:53:12 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
2019-12-23 09:21:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type NotificationService interface {
|
|
|
|
Post(message string)
|
|
|
|
}
|
|
|
|
|
|
|
|
type NotificationServices []NotificationService
|
|
|
|
|
|
|
|
// Post to all Notification Services
|
|
|
|
func (notificationServices *NotificationServices) Post(message string) {
|
|
|
|
for _, notificationService := range *notificationServices {
|
|
|
|
notificationService.Post(message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Telegram
|
|
|
|
type Telegram struct {
|
2020-03-26 10:53:12 +00:00
|
|
|
userId int
|
2019-12-23 09:21:21 +00:00
|
|
|
botToken string
|
|
|
|
}
|
|
|
|
|
2020-03-26 10:53:12 +00:00
|
|
|
var telegramBaseUrl = "https://api.telegram.org/bot"
|
|
|
|
|
2019-12-23 09:21:21 +00:00
|
|
|
func (t *Telegram) Post(message string) {
|
2020-03-26 10:53:12 +00:00
|
|
|
params := url.Values{}
|
|
|
|
params.Add("chat_id", strconv.Itoa(t.userId))
|
|
|
|
params.Add("text", message)
|
|
|
|
tgUrl, err := url.Parse(telegramBaseUrl + t.botToken + "/sendMessage")
|
2019-12-23 09:21:21 +00:00
|
|
|
if err != nil {
|
2020-03-26 10:53:12 +00:00
|
|
|
fmt.Println("Failed to create Telegram request")
|
2019-12-23 09:21:21 +00:00
|
|
|
return
|
|
|
|
}
|
2020-03-26 10:53:12 +00:00
|
|
|
tgUrl.RawQuery = params.Encode()
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, tgUrl.String(), nil)
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil || resp.StatusCode != 200 {
|
2019-12-23 09:21:21 +00:00
|
|
|
fmt.Println("Failed to send Telegram message")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|