hugo-micropub/notification.go

48 lines
1.0 KiB
Go
Raw Normal View History

package main
import (
"fmt"
"net/http"
"net/url"
"strconv"
)
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 {
userId int
botToken string
}
var telegramBaseUrl = "https://api.telegram.org/bot"
func (t *Telegram) Post(message string) {
params := url.Values{}
params.Add("chat_id", strconv.Itoa(t.userId))
params.Add("text", message)
tgUrl, err := url.Parse(telegramBaseUrl + t.botToken + "/sendMessage")
if err != nil {
fmt.Println("Failed to create Telegram request")
return
}
tgUrl.RawQuery = params.Encode()
req, _ := http.NewRequest(http.MethodPost, tgUrl.String(), nil)
resp, err := http.DefaultClient.Do(req)
if err != nil || resp.StatusCode != 200 {
fmt.Println("Failed to send Telegram message")
return
}
}