GoBlog/telegram.go

236 lines
6.2 KiB
Go
Raw Permalink Normal View History

2020-11-09 18:54:06 +00:00
package main
import (
2021-12-07 18:05:43 +00:00
"errors"
2020-11-09 18:54:06 +00:00
"net/url"
2021-12-07 18:05:43 +00:00
"strconv"
2020-11-09 18:54:06 +00:00
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"go.goblog.app/app/pkgs/builderpool"
)
2020-11-09 18:54:06 +00:00
func (a *goBlog) initTelegram() {
2023-12-27 10:37:58 +00:00
a.pPostHooks = append(a.pPostHooks, func(p *post) { a.tgPost(p, false) })
2022-03-31 14:18:49 +00:00
a.pUpdateHooks = append(a.pUpdateHooks, a.tgUpdate)
a.pDeleteHooks = append(a.pDeleteHooks, a.tgDelete)
2023-12-27 10:37:58 +00:00
a.pUndeleteHooks = append(a.pUndeleteHooks, func(p *post) { a.tgPost(p, true) })
2022-03-31 14:18:49 +00:00
}
func (tg *configTelegram) enabled() bool {
if tg == nil || !tg.Enabled || tg.BotToken == "" || tg.ChatID == "" {
return false
}
return true
}
2023-12-27 10:37:58 +00:00
func (a *goBlog) tgPost(p *post, silent bool) {
if tg := a.getBlogFromPost(p).Telegram; tg.enabled() && p.isPublicPublishedSectionPost() {
tgChat := p.firstParameter("telegramchat")
tgMsg := p.firstParameter("telegrammsg")
if tgChat != "" && tgMsg != "" {
// Already posted
return
}
// Generate HTML
html := tg.generateHTML(p.RenderedTitle, a.fullPostURL(p), a.shortPostURL(p))
if html == "" {
return
}
// Send message
chatId, msgId, err := a.sendTelegram(tg, html, tgbotapi.ModeHTML, silent)
if err != nil {
a.error("Failed to send post to Telegram", "err", err)
return
}
if chatId == 0 || msgId == 0 {
// Not sent
return
}
// Save chat and message id to post
err = a.db.replacePostParam(p.Path, "telegramchat", []string{strconv.FormatInt(chatId, 10)})
if err != nil {
a.error("Failed to save Telegram chat id", "err", err)
}
err = a.db.replacePostParam(p.Path, "telegrammsg", []string{strconv.Itoa(msgId)})
if err != nil {
a.error("Failed to save Telegram message id", "err", err)
2021-12-07 18:05:43 +00:00
}
2022-03-31 14:18:49 +00:00
}
}
func (a *goBlog) tgUpdate(p *post) {
2022-12-14 15:03:54 +00:00
if tg := a.getBlogFromPost(p).Telegram; tg.enabled() {
2022-03-31 14:18:49 +00:00
tgChat := p.firstParameter("telegramchat")
tgMsg := p.firstParameter("telegrammsg")
if tgChat == "" || tgMsg == "" {
// Not send to Telegram
return
}
2022-03-31 14:18:49 +00:00
// Parse tgChat to int64
chatId, err := strconv.ParseInt(tgChat, 10, 64)
if err != nil {
2023-12-27 10:37:58 +00:00
a.error("Failed to parse Telegram chat ID", "err", err)
2022-03-31 14:18:49 +00:00
return
}
2022-03-31 14:18:49 +00:00
// Parse tgMsg to int
messageId, err := strconv.Atoi(tgMsg)
if err != nil {
2023-12-27 10:37:58 +00:00
a.error("Failed to parse Telegram message ID", "err", err)
2022-03-31 14:18:49 +00:00
return
}
// Generate HTML
html := tg.generateHTML(p.RenderedTitle, a.fullPostURL(p), a.shortPostURL(p))
if html == "" {
return
}
// Send update
err = a.updateTelegram(tg, chatId, messageId, html, "HTML")
if err != nil {
2023-12-27 10:37:58 +00:00
a.error("Failed to send update to Telegram", "err", err)
2022-03-31 14:18:49 +00:00
}
}
2020-11-17 21:10:13 +00:00
}
2022-03-31 14:18:49 +00:00
func (a *goBlog) tgDelete(p *post) {
2022-12-14 15:03:54 +00:00
if tg := a.getBlogFromPost(p).Telegram; tg.enabled() {
2022-03-31 14:18:49 +00:00
tgChat := p.firstParameter("telegramchat")
tgMsg := p.firstParameter("telegrammsg")
if tgChat == "" || tgMsg == "" {
// Not send to Telegram
return
}
// Parse tgChat to int64
chatId, err := strconv.ParseInt(tgChat, 10, 64)
if err != nil {
2023-12-27 10:37:58 +00:00
a.error("Failed to parse Telegram chat ID", "err", err)
2022-03-31 14:18:49 +00:00
return
}
// Parse tgMsg to int
messageId, err := strconv.Atoi(tgMsg)
if err != nil {
2023-12-27 10:37:58 +00:00
a.error("Failed to parse Telegram message ID", "err", err)
2022-03-31 14:18:49 +00:00
return
}
// Delete message
err = a.deleteTelegram(tg, chatId, messageId)
if err != nil {
2023-12-27 10:37:58 +00:00
a.error("Failed to delete Telegram message", "err", err)
2022-03-31 14:18:49 +00:00
}
// Delete chat and message id from post
err = a.db.replacePostParam(p.Path, "telegramchat", []string{})
if err != nil {
2023-12-27 10:37:58 +00:00
a.error("Failed to remove Telegram chat id", "err", err)
2022-03-31 14:18:49 +00:00
}
err = a.db.replacePostParam(p.Path, "telegrammsg", []string{})
if err != nil {
2023-12-27 10:37:58 +00:00
a.error("Failed to remove Telegram message id", "err", err)
2022-03-31 14:18:49 +00:00
}
}
}
2022-02-16 12:02:08 +00:00
func (tg *configTelegram) generateHTML(title, fullURL, shortURL string) (html string) {
if !tg.enabled() {
return ""
2020-11-09 18:54:06 +00:00
}
message := builderpool.Get()
defer builderpool.Put(message)
if title != "" {
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 != "" {
message.WriteString("<a href=\"https://t.me/iv?rhash=" + tg.InstantViewHash + "&url=" + url.QueryEscape(fullURL) + "\">")
message.WriteString(tgbotapi.EscapeText(tgbotapi.ModeHTML, shortURL))
2021-02-08 19:33:48 +00:00
message.WriteString("</a>")
} else {
message.WriteString("<a href=\"" + shortURL + "\">")
message.WriteString(tgbotapi.EscapeText(tgbotapi.ModeHTML, shortURL))
2021-02-08 19:33:48 +00:00
message.WriteString("</a>")
}
2022-02-16 12:02:08 +00:00
html = message.String()
return
2020-11-09 18:54:06 +00:00
}
2022-03-31 14:18:49 +00:00
func (a *goBlog) sendTelegram(tg *configTelegram, message, mode string, silent bool) (int64, int, error) {
if !tg.enabled() {
2021-12-07 18:05:43 +00:00
return 0, 0, nil
2021-02-08 19:33:48 +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 18:05:43 +00:00
return 0, 0, err
2020-11-09 18:54:06 +00:00
}
msg := tgbotapi.MessageConfig{
BaseChat: tgbotapi.BaseChat{
2022-03-31 14:18:49 +00:00
ChannelUsername: tg.ChatID,
DisableNotification: silent,
},
Text: message,
ParseMode: mode,
2021-03-31 07:29:52 +00:00
}
res, err := bot.Send(msg)
if err != nil {
2021-12-07 18:05:43 +00:00
return 0, 0, err
}
return res.Chat.ID, res.MessageID, nil
}
func (a *goBlog) updateTelegram(tg *configTelegram, chatId int64, messageId int, message, mode string) error {
2021-12-07 18:05:43 +00:00
if !tg.enabled() {
return nil
}
bot, err := tgbotapi.NewBotAPIWithClient(tg.BotToken, tgbotapi.APIEndpoint, a.httpClient)
if err != nil {
return err
}
// Check if chat is still the configured one
2021-12-07 18:05:43 +00:00
chat, err := bot.GetChat(tgbotapi.ChatInfoConfig{
ChatConfig: tgbotapi.ChatConfig{
SuperGroupUsername: tg.ChatID,
},
})
if err != nil {
return err
}
if chat.ID != chatId {
return errors.New("chat id mismatch")
}
// Send update
2021-12-07 18:05:43 +00:00
msg := tgbotapi.EditMessageTextConfig{
BaseEdit: tgbotapi.BaseEdit{
ChatID: chatId,
MessageID: messageId,
},
Text: message,
ParseMode: mode,
}
_, err = bot.Send(msg)
return err
}
func (a *goBlog) deleteTelegram(tg *configTelegram, chatId int64, messageId int) error {
if !tg.enabled() {
return nil
}
bot, err := tgbotapi.NewBotAPIWithClient(tg.BotToken, tgbotapi.APIEndpoint, a.httpClient)
2021-12-07 18:05:43 +00:00
if err != nil {
return err
2020-11-09 18:54:06 +00:00
}
chat, err := bot.GetChat(tgbotapi.ChatInfoConfig{
ChatConfig: tgbotapi.ChatConfig{
SuperGroupUsername: tg.ChatID,
},
})
if err != nil {
return err
}
if chat.ID != chatId {
return errors.New("chat id mismatch")
}
msg := tgbotapi.DeleteMessageConfig{
ChatID: chatId,
MessageID: messageId,
}
_, err = bot.Send(msg)
return err
2020-11-09 18:54:06 +00:00
}