diff --git a/config.go b/config.go index 0d8e311..3e07003 100644 --- a/config.go +++ b/config.go @@ -56,6 +56,7 @@ type configBlog struct { Photos *photos `mapstructure:"photos"` DefaultSection string `mapstructure:"defaultsection"` CustomPages []*customPage `mapstructure:"custompages"` + Telegram *configTelegram `mapstructure:"telegram"` } type section struct { diff --git a/hooks.go b/hooks.go index 3f70098..e99b5bf 100644 --- a/hooks.go +++ b/hooks.go @@ -27,6 +27,7 @@ func (p *post) postPostHooks() { }(p, cmdTmplString) } go p.apPost() + go p.tgPost() go p.sendWebmentions() } diff --git a/notifications.go b/notifications.go index 4b24a9e..96c83de 100644 --- a/notifications.go +++ b/notifications.go @@ -1,37 +1,15 @@ package main import ( - "errors" "log" - "net/http" - "net/url" ) func sendNotification(text string) { log.Println("Notification:", text) if appConfig.Notifications.Telegram.Enabled { - err := sendTelegramMessage(text) + err := sendTelegramMessage(text, appConfig.Notifications.Telegram.BotToken, appConfig.Notifications.Telegram.ChatID) if err != nil { - log.Println("Failed to send Telegram message:", err.Error()) + log.Println("Failed to send Telegram notification:", err.Error()) } } } - -const telegramBaseURL = "https://api.telegram.org/bot" - -func sendTelegramMessage(text string) error { - params := url.Values{} - params.Add("chat_id", appConfig.Notifications.Telegram.ChatID) - params.Add("text", text) - tgURL, err := url.Parse(telegramBaseURL + appConfig.Notifications.Telegram.BotToken + "/sendMessage") - if err != nil { - return errors.New("failed to create Telegram request") - } - tgURL.RawQuery = params.Encode() - req, _ := http.NewRequest(http.MethodPost, tgURL.String(), nil) - resp, err := http.DefaultClient.Do(req) - if err != nil || resp.StatusCode != http.StatusOK { - return errors.New("failed to send Telegram message") - } - return nil -} diff --git a/telegram.go b/telegram.go new file mode 100644 index 0000000..2ee5930 --- /dev/null +++ b/telegram.go @@ -0,0 +1,40 @@ +package main + +import ( + "bytes" + "errors" + "net/http" + "net/url" +) + +const telegramBaseURL = "https://api.telegram.org/bot" + +func (p *post) tgPost() { + if !appConfig.Blogs[p.Blog].Telegram.Enabled { + return + } + var message bytes.Buffer + if title := p.title(); title != "" { + message.WriteString(title) + message.WriteString("\n\n") + } + message.WriteString(appConfig.Server.PublicAddress + p.Path) + sendTelegramMessage(message.String(), appConfig.Blogs[p.Blog].Telegram.BotToken, appConfig.Blogs[p.Blog].Telegram.ChatID) +} + +func sendTelegramMessage(text, bottoken, chatID string) error { + params := url.Values{} + params.Add("chat_id", chatID) + params.Add("text", text) + tgURL, err := url.Parse(telegramBaseURL + bottoken + "/sendMessage") + if err != nil { + return errors.New("failed to create Telegram request") + } + tgURL.RawQuery = params.Encode() + req, _ := http.NewRequest(http.MethodPost, tgURL.String(), nil) + resp, err := http.DefaultClient.Do(req) + if err != nil || resp.StatusCode != http.StatusOK { + return errors.New("failed to send Telegram message") + } + return nil +}