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