1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-08-26 18:21:01 +00:00

Add Telegram integration

This commit is contained in:
三三 2021-09-08 11:04:22 +08:00 committed by GitHub
parent 93596c1218
commit 34dd358eb0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 173 additions and 4 deletions

View file

@ -10,6 +10,7 @@ import (
"miniflux.app/integration/nunuxkeeper"
"miniflux.app/integration/pinboard"
"miniflux.app/integration/pocket"
"miniflux.app/integration/telegrambot"
"miniflux.app/integration/wallabag"
"miniflux.app/logger"
"miniflux.app/model"
@ -70,3 +71,15 @@ func SendEntry(entry *model.Entry, integration *model.Integration) {
}
}
}
// PushEntry pushes new entry to the activated providers.
// This function should be wrapped in a goroutine to avoid block of program execution.
func PushEntry(entry *model.Entry, integration *model.Integration) {
if integration.TelegramBotEnabled {
logger.Debug("[Integration] Sending Entry #%d for User #%d to telegram", entry.ID, integration.UserID)
err := telegrambot.PushEntry(entry, integration.TelegramBotToken, integration.TelegramBotChatID)
if err != nil {
logger.Error("[Integration] push entry to telegram bot failed: %v", err)
}
}
}

View file

@ -0,0 +1,2 @@
// Package telegrambot provides a simple entry-to-telegram push
package telegrambot

View file

@ -0,0 +1,41 @@
package telegrambot
import (
"bytes"
"fmt"
"html/template"
"strconv"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"miniflux.app/model"
)
// PushEntry pushes entry to telegram chat using integration settings provided
func PushEntry(entry *model.Entry, botToken, chatID string) error {
bot, err := tgbotapi.NewBotAPI(botToken)
if err != nil {
return fmt.Errorf("telegrambot: create bot failed: %w", err)
}
t, err := template.New("message").Parse("{{ .Title }}\n<a href=\"{{ .URL }}\">{{ .URL }}</a>")
if err != nil {
return fmt.Errorf("telegrambot: parse template failed: %w", err)
}
var result bytes.Buffer
err = t.Execute(&result, entry)
if err != nil {
return fmt.Errorf("telegrambot: execute template failed: %w", err)
}
chatId, _ := strconv.ParseInt(chatID, 10, 64)
msg := tgbotapi.NewMessage(chatId, result.String())
msg.ParseMode = tgbotapi.ModeHTML
msg.DisableWebPagePreview = false
if _, err := bot.Send(msg); err != nil {
return fmt.Errorf("telegrambot: send message failed: %w", err)
}
return nil
}