inline.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package tg
  2. import (
  3. tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
  4. )
  5. // The type represents keyboard to be emdedded
  6. // into the messages (inline in Telegram terms).
  7. type Inline struct {
  8. Keyboard
  9. }
  10. // Convert the inline keyboard to markup for the tgbotapi.
  11. func (kbd Inline) ToAPI() tgbotapi.InlineKeyboardMarkup {
  12. rows := [][]tgbotapi.InlineKeyboardButton{}
  13. for _, row := range kbd.Rows {
  14. if row == nil {
  15. continue
  16. }
  17. buttons := []tgbotapi.InlineKeyboardButton{}
  18. for _, button := range row {
  19. if !button.Valid {
  20. continue
  21. }
  22. buttons = append(buttons, button.ToTelegramInline())
  23. }
  24. rows = append(rows, buttons)
  25. }
  26. return tgbotapi.NewInlineKeyboardMarkup(rows...)
  27. }
  28. // The type implements message with an inline keyboard.
  29. type InlineCompo struct {
  30. MessageCompo
  31. Inline
  32. }
  33. // Implementing the Sendable interface.
  34. func (compo *InlineCompo) SendConfig(
  35. sid SessionID, bot *Bot,
  36. ) (SendConfig) {
  37. sendConfig := compo.MessageCompo.SendConfig(sid, bot)
  38. msg := sendConfig.Chattable.(tgbotapi.MessageConfig)
  39. if len(compo.Inline.Rows) > 0 {
  40. msg.ReplyMarkup = compo.Inline.ToAPI()
  41. }
  42. sendConfig.Chattable = msg
  43. return sendConfig
  44. }
  45. // Update the component on the client side.
  46. // Requires exactly the pointer but not the value
  47. // cause it changes insides of the structure.
  48. func (compo *InlineCompo) Update(c Context) error {
  49. if compo.Message != nil {
  50. var edit tgbotapi.Chattable
  51. markup := compo.Inline.ToAPI()
  52. ln := len(markup.InlineKeyboard)
  53. if ln == 0 || compo.Inline.Rows == nil {
  54. edit = tgbotapi.NewEditMessageText(
  55. c.SessionID().ToAPI(),
  56. compo.Message.MessageID,
  57. compo.Text,
  58. )
  59. } else {
  60. edit = tgbotapi.NewEditMessageTextAndMarkup(
  61. c.SessionID().ToAPI(),
  62. compo.Message.MessageID,
  63. compo.Text,
  64. markup,
  65. )
  66. }
  67. msg, err := c.Bot().API().Send(edit)
  68. if err != nil {
  69. return err
  70. }
  71. compo.Message = &msg
  72. }
  73. return nil
  74. }
  75. // Implementing the Filterer interface.
  76. func (compo *InlineCompo) Filter(u Update) bool {
  77. if u.CallbackQuery == nil {
  78. return true
  79. }
  80. if u.CallbackQuery.Message.MessageID !=
  81. compo.Message.MessageID {
  82. return true
  83. }
  84. return false
  85. }
  86. // Implementing the Server interface.
  87. func (compo *InlineCompo) Serve(c Context) {
  88. for u := range c.Input() {
  89. compo.OnOneUpdate(c, u)
  90. }
  91. }
  92. func (compo *InlineCompo) OnOneUpdate(c Context, u Update) error {
  93. var act Action
  94. btns := compo.ButtonMap()
  95. cb := tgbotapi.NewCallback(
  96. u.CallbackQuery.ID,
  97. u.CallbackQuery.Data,
  98. )
  99. data := u.CallbackQuery.Data
  100. _, err := c.Bot().API().Request(cb)
  101. if err != nil {
  102. return err
  103. }
  104. btn, ok := btns[data]
  105. if !ok {
  106. return nil
  107. }
  108. if btn.Action != nil {
  109. act = btn.Action
  110. } else if compo.Action != nil {
  111. act = compo.Action
  112. }
  113. c.WithUpdate(u).Run(act)
  114. return nil
  115. }