2023-06-19 23:42:47 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2021-09-08 05:28:41 +02:00
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
package telegrambot // import "miniflux.app/v2/internal/integration/telegrambot"
|
2021-09-08 05:04:22 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
|
2023-08-11 04:46:45 +02:00
|
|
|
"miniflux.app/v2/internal/model"
|
2021-09-08 05:04:22 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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 {
|
2021-09-08 05:28:41 +02:00
|
|
|
return fmt.Errorf("telegrambot: bot creation failed: %w", err)
|
2021-09-08 05:04:22 +02:00
|
|
|
}
|
|
|
|
|
2021-09-08 05:28:41 +02:00
|
|
|
tpl, err := template.New("message").Parse("{{ .Title }}\n<a href=\"{{ .URL }}\">{{ .URL }}</a>")
|
2021-09-08 05:04:22 +02:00
|
|
|
if err != nil {
|
2021-09-08 05:28:41 +02:00
|
|
|
return fmt.Errorf("telegrambot: template parsing failed: %w", err)
|
2021-09-08 05:04:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var result bytes.Buffer
|
2021-09-08 05:28:41 +02:00
|
|
|
if err := tpl.Execute(&result, entry); err != nil {
|
|
|
|
return fmt.Errorf("telegrambot: template execution failed: %w", err)
|
2021-09-08 05:04:22 +02:00
|
|
|
}
|
|
|
|
|
2021-09-08 05:28:41 +02:00
|
|
|
chatIDInt, _ := strconv.ParseInt(chatID, 10, 64)
|
|
|
|
msg := tgbotapi.NewMessage(chatIDInt, result.String())
|
2021-09-08 05:04:22 +02:00
|
|
|
msg.ParseMode = tgbotapi.ModeHTML
|
|
|
|
msg.DisableWebPagePreview = false
|
2022-05-21 14:58:45 +02:00
|
|
|
|
|
|
|
if entry.CommentsURL != "" {
|
|
|
|
msg.ReplyMarkup = tgbotapi.NewInlineKeyboardMarkup(
|
|
|
|
tgbotapi.NewInlineKeyboardRow(
|
|
|
|
tgbotapi.NewInlineKeyboardButtonURL("Comments", entry.CommentsURL),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2021-09-08 05:04:22 +02:00
|
|
|
if _, err := bot.Send(msg); err != nil {
|
2021-09-08 05:28:41 +02:00
|
|
|
return fmt.Errorf("telegrambot: sending message failed: %w", err)
|
2021-09-08 05:04:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|