jlelse
/
hugo-micropub
Archived
1
Fork 0
This repository has been archived on 2020-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
hugo-micropub/notification.go

48 lines
1.0 KiB
Go

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