telegrambot.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package telegrambot // import "miniflux.app/v2/internal/integration/telegrambot"
  4. import (
  5. "fmt"
  6. "log/slog"
  7. "strconv"
  8. "miniflux.app/v2/internal/config"
  9. "miniflux.app/v2/internal/model"
  10. "miniflux.app/v2/internal/urllib"
  11. )
  12. func PushEntry(feed *model.Feed, entry *model.Entry, botToken, chatID string, topicID *int64, disableWebPagePreview, disableNotification bool, disableButtons bool) error {
  13. formattedText := fmt.Sprintf(
  14. `<b>%s</b> - <a href=%q>%s</a>`,
  15. feed.Title,
  16. entry.URL,
  17. entry.Title,
  18. )
  19. message := &MessageRequest{
  20. ChatID: chatID,
  21. Text: formattedText,
  22. ParseMode: HTMLFormatting,
  23. DisableWebPagePreview: disableWebPagePreview,
  24. DisableNotification: disableNotification,
  25. }
  26. if topicID != nil {
  27. message.MessageThreadID = *topicID
  28. }
  29. if !disableButtons {
  30. var markupRow []*InlineKeyboardButton
  31. baseURL := config.Opts.BaseURL()
  32. entryPath := "/unread/entry/" + strconv.FormatInt(entry.ID, 10)
  33. minifluxEntryURL, err := urllib.JoinBaseURLAndPath(baseURL, entryPath)
  34. if err != nil {
  35. slog.Error("Unable to create Miniflux entry URL", slog.Any("error", err))
  36. } else {
  37. minifluxEntryURLButton := InlineKeyboardButton{Text: "Go to Miniflux", URL: minifluxEntryURL}
  38. markupRow = append(markupRow, &minifluxEntryURLButton)
  39. }
  40. articleURLButton := InlineKeyboardButton{Text: "Go to article", URL: entry.URL}
  41. markupRow = append(markupRow, &articleURLButton)
  42. if entry.CommentsURL != "" {
  43. commentURLButton := InlineKeyboardButton{Text: "Comments", URL: entry.CommentsURL}
  44. markupRow = append(markupRow, &commentURLButton)
  45. }
  46. message.ReplyMarkup = &InlineKeyboard{}
  47. message.ReplyMarkup.InlineKeyboard = append(message.ReplyMarkup.InlineKeyboard, markupRow)
  48. }
  49. client := NewClient(botToken, chatID)
  50. _, err := client.SendMessage(message)
  51. return err
  52. }