telegrambot.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package telegrambot // import "miniflux.app/integration/telegrambot"
  4. import (
  5. "bytes"
  6. "fmt"
  7. "html/template"
  8. "strconv"
  9. tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
  10. "miniflux.app/model"
  11. )
  12. // PushEntry pushes entry to telegram chat using integration settings provided
  13. func PushEntry(entry *model.Entry, botToken, chatID string) error {
  14. bot, err := tgbotapi.NewBotAPI(botToken)
  15. if err != nil {
  16. return fmt.Errorf("telegrambot: bot creation failed: %w", err)
  17. }
  18. tpl, err := template.New("message").Parse("{{ .Title }}\n<a href=\"{{ .URL }}\">{{ .URL }}</a>")
  19. if err != nil {
  20. return fmt.Errorf("telegrambot: template parsing failed: %w", err)
  21. }
  22. var result bytes.Buffer
  23. if err := tpl.Execute(&result, entry); err != nil {
  24. return fmt.Errorf("telegrambot: template execution failed: %w", err)
  25. }
  26. chatIDInt, _ := strconv.ParseInt(chatID, 10, 64)
  27. msg := tgbotapi.NewMessage(chatIDInt, result.String())
  28. msg.ParseMode = tgbotapi.ModeHTML
  29. msg.DisableWebPagePreview = false
  30. if entry.CommentsURL != "" {
  31. msg.ReplyMarkup = tgbotapi.NewInlineKeyboardMarkup(
  32. tgbotapi.NewInlineKeyboardRow(
  33. tgbotapi.NewInlineKeyboardButtonURL("Comments", entry.CommentsURL),
  34. ))
  35. }
  36. if _, err := bot.Send(msg); err != nil {
  37. return fmt.Errorf("telegrambot: sending message failed: %w", err)
  38. }
  39. return nil
  40. }