telegrambot.go 1.5 KB

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