2020-11-09 18:54:06 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-02-08 17:51:07 +00:00
|
|
|
"log"
|
2020-11-09 18:54:06 +00:00
|
|
|
"net/url"
|
|
|
|
|
2021-12-07 17:23:57 +00:00
|
|
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
|
|
|
)
|
2020-11-09 18:54:06 +00:00
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (a *goBlog) initTelegram() {
|
2021-06-14 19:34:29 +00:00
|
|
|
a.pPostHooks = append(a.pPostHooks, func(p *post) {
|
|
|
|
if tg := a.cfg.Blogs[p.Blog].Telegram; tg.enabled() && p.isPublishedSectionPost() {
|
2021-08-05 06:09:34 +00:00
|
|
|
if html := tg.generateHTML(p.RenderedTitle, a.fullPostURL(p), a.shortPostURL(p)); html != "" {
|
2021-12-07 17:23:57 +00:00
|
|
|
if _, err := a.send(tg, html, "HTML"); err != nil {
|
2021-06-14 19:34:29 +00:00
|
|
|
log.Printf("Failed to send post to Telegram: %v", err)
|
|
|
|
}
|
2020-11-17 21:10:13 +00:00
|
|
|
}
|
2021-06-14 19:34:29 +00:00
|
|
|
}
|
|
|
|
})
|
2020-11-17 21:10:13 +00:00
|
|
|
}
|
|
|
|
|
2021-06-14 19:34:29 +00:00
|
|
|
func (tg *configTelegram) enabled() bool {
|
2020-11-17 21:10:13 +00:00
|
|
|
if tg == nil || !tg.Enabled || tg.BotToken == "" || tg.ChatID == "" {
|
2021-06-14 19:34:29 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tg *configTelegram) generateHTML(title, fullURL, shortURL string) string {
|
|
|
|
if !tg.enabled() {
|
|
|
|
return ""
|
2020-11-09 18:54:06 +00:00
|
|
|
}
|
|
|
|
var message bytes.Buffer
|
2021-06-06 12:39:42 +00:00
|
|
|
if title != "" {
|
2021-12-07 17:23:57 +00:00
|
|
|
message.WriteString(tgbotapi.EscapeText(tgbotapi.ModeHTML, title))
|
2020-11-09 18:54:06 +00:00
|
|
|
message.WriteString("\n\n")
|
|
|
|
}
|
2021-02-08 19:33:48 +00:00
|
|
|
if tg.InstantViewHash != "" {
|
2021-06-06 12:39:42 +00:00
|
|
|
message.WriteString("<a href=\"https://t.me/iv?rhash=" + tg.InstantViewHash + "&url=" + url.QueryEscape(fullURL) + "\">")
|
2021-12-07 17:23:57 +00:00
|
|
|
message.WriteString(tgbotapi.EscapeText(tgbotapi.ModeHTML, shortURL))
|
2021-02-08 19:33:48 +00:00
|
|
|
message.WriteString("</a>")
|
|
|
|
} else {
|
2021-06-06 12:39:42 +00:00
|
|
|
message.WriteString("<a href=\"" + shortURL + "\">")
|
2021-12-07 17:23:57 +00:00
|
|
|
message.WriteString(tgbotapi.EscapeText(tgbotapi.ModeHTML, shortURL))
|
2021-02-08 19:33:48 +00:00
|
|
|
message.WriteString("</a>")
|
|
|
|
}
|
2021-06-14 19:34:29 +00:00
|
|
|
return message.String()
|
2020-11-09 18:54:06 +00:00
|
|
|
}
|
|
|
|
|
2021-12-07 17:23:57 +00:00
|
|
|
func (a *goBlog) send(tg *configTelegram, message, mode string) (int, error) {
|
2021-06-14 19:34:29 +00:00
|
|
|
if !tg.enabled() {
|
2021-12-07 17:23:57 +00:00
|
|
|
return 0, nil
|
2021-02-08 19:33:48 +00:00
|
|
|
}
|
2021-12-07 17:23:57 +00:00
|
|
|
bot, err := tgbotapi.NewBotAPIWithClient(tg.BotToken, tgbotapi.APIEndpoint, a.httpClient)
|
2020-11-09 18:54:06 +00:00
|
|
|
if err != nil {
|
2021-12-07 17:23:57 +00:00
|
|
|
return 0, err
|
2020-11-09 18:54:06 +00:00
|
|
|
}
|
2021-12-07 17:23:57 +00:00
|
|
|
msg := tgbotapi.MessageConfig{
|
|
|
|
BaseChat: tgbotapi.BaseChat{
|
|
|
|
ChannelUsername: tg.ChatID,
|
|
|
|
},
|
|
|
|
Text: message,
|
|
|
|
ParseMode: mode,
|
2021-03-31 07:29:52 +00:00
|
|
|
}
|
2021-12-07 17:23:57 +00:00
|
|
|
res, err := bot.Send(msg)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2020-11-09 18:54:06 +00:00
|
|
|
}
|
2021-12-07 17:23:57 +00:00
|
|
|
return res.MessageID, nil
|
2020-11-09 18:54:06 +00:00
|
|
|
}
|