Telegram add hook for undelete

This commit is contained in:
Jan-Lukas Else 2022-03-31 16:18:49 +02:00
parent 9035f9bf46
commit 2a867e7c3f
3 changed files with 89 additions and 80 deletions

View File

@ -34,7 +34,7 @@ func (a *goBlog) sendNotification(text string) {
if err := a.sendNtfy(cfg.Ntfy, n.Text); err != nil {
log.Println("Failed to send notification to Ntfy:", err.Error())
}
if _, _, err := a.sendTelegram(cfg.Telegram, n.Text, ""); err != nil {
if _, _, err := a.sendTelegram(cfg.Telegram, n.Text, "", false); err != nil {
log.Println("Failed to send notification to Telegram:", err.Error())
}
}

View File

@ -11,7 +11,21 @@ import (
)
func (a *goBlog) initTelegram() {
a.pPostHooks = append(a.pPostHooks, func(p *post) {
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) {
if tg := a.cfg.Blogs[p.Blog].Telegram; tg.enabled() && p.isPublishedSectionPost() {
tgChat := p.firstParameter("telegramchat")
tgMsg := p.firstParameter("telegrammsg")
@ -25,7 +39,7 @@ func (a *goBlog) initTelegram() {
return
}
// Send message
chatId, msgId, err := a.sendTelegram(tg, html, tgbotapi.ModeHTML)
chatId, msgId, err := a.sendTelegram(tg, html, tgbotapi.ModeHTML, silent)
if err != nil {
log.Printf("Failed to send post to Telegram: %v", err)
return
@ -44,8 +58,10 @@ func (a *goBlog) initTelegram() {
log.Printf("Failed to save Telegram message id: %v", err)
}
}
})
a.pUpdateHooks = append(a.pUpdateHooks, func(p *post) {
}
}
func (a *goBlog) tgUpdate(p *post) {
if tg := a.cfg.Blogs[p.Blog].Telegram; tg.enabled() {
tgChat := p.firstParameter("telegramchat")
tgMsg := p.firstParameter("telegrammsg")
@ -76,8 +92,9 @@ func (a *goBlog) initTelegram() {
log.Printf("Failed to send update to Telegram: %v", err)
}
}
})
a.pDeleteHooks = append(a.pDeleteHooks, func(p *post) {
}
func (a *goBlog) tgDelete(p *post) {
if tg := a.cfg.Blogs[p.Blog].Telegram; tg.enabled() {
tgChat := p.firstParameter("telegramchat")
tgMsg := p.firstParameter("telegrammsg")
@ -112,15 +129,6 @@ func (a *goBlog) initTelegram() {
log.Printf("Failed to remove Telegram message id: %v", err)
}
}
})
// TODO: Handle undelete
}
func (tg *configTelegram) enabled() bool {
if tg == nil || !tg.Enabled || tg.BotToken == "" || tg.ChatID == "" {
return false
}
return true
}
func (tg *configTelegram) generateHTML(title, fullURL, shortURL string) (html string) {
@ -146,7 +154,7 @@ func (tg *configTelegram) generateHTML(title, fullURL, shortURL string) (html st
return
}
func (a *goBlog) sendTelegram(tg *configTelegram, message, mode string) (int64, int, error) {
func (a *goBlog) sendTelegram(tg *configTelegram, message, mode string, silent bool) (int64, int, error) {
if !tg.enabled() {
return 0, 0, nil
}
@ -157,6 +165,7 @@ func (a *goBlog) sendTelegram(tg *configTelegram, message, mode string) (int64,
msg := tgbotapi.MessageConfig{
BaseChat: tgbotapi.BaseChat{
ChannelUsername: tg.ChatID,
DisableNotification: silent,
},
Text: message,
ParseMode: mode,

View File

@ -67,7 +67,7 @@ func Test_configTelegram_send(t *testing.T) {
httpClient: fakeClient.Client,
}
chatId, msgId, err := app.sendTelegram(tg, "Message", "HTML")
chatId, msgId, err := app.sendTelegram(tg, "Message", "HTML", false)
require.Nil(t, err)
assert.Equal(t, 123, msgId)