telegrambot.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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 _, err := bot.Send(msg); err != nil {
  32. return fmt.Errorf("telegrambot: sending message failed: %w", err)
  33. }
  34. return nil
  35. }