inline.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 == nil {
  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. if len(compo.Inline.Rows) > 0 {
  39. sendConfig.Message.ReplyMarkup = compo.Inline.ToApi()
  40. }
  41. return sendConfig
  42. }
  43. // Update the component on the client side.
  44. // Requires exactly the pointer but not the value
  45. // cause it changes insides of the structure.
  46. func (compo *InlineCompo) Update(c Context) {
  47. if compo.Message != nil {
  48. var edit tgbotapi.Chattable
  49. markup := compo.Inline.ToApi()
  50. ln := len(markup.InlineKeyboard)
  51. if ln == 0 || compo.Inline.Rows == nil {
  52. edit = tgbotapi.NewEditMessageText(
  53. c.Session.Id.ToApi(),
  54. compo.Message.MessageID,
  55. compo.Text,
  56. )
  57. } else {
  58. edit = tgbotapi.NewEditMessageTextAndMarkup(
  59. c.Session.Id.ToApi(),
  60. compo.Message.MessageID,
  61. compo.Text,
  62. markup,
  63. )
  64. }
  65. msg, _ := c.Bot.Api.Send(edit)
  66. compo.Message = &msg
  67. }
  68. compo.buttonMap = compo.MakeButtonMap()
  69. }
  70. // Implementing the Filterer interface.
  71. func (compo InlineCompo) Filter(u Update) bool {
  72. if compo == nil || u.CallbackQuery == nil {
  73. return true
  74. }
  75. if u.CallbackQuery.Message.MessageID !=
  76. compo.Message.MessageID {
  77. return true
  78. }
  79. return false
  80. }
  81. // Implementing the Server interface.
  82. func (compo InlineCompo) Serve(c Context) {
  83. for u := range c.Input() {
  84. compo.OnOneUpdate(c, u)
  85. }
  86. }
  87. func (compo *InlineCompo) OnOneUpdate(c Context, u Update) {
  88. var act Action
  89. btns := compo.ButtonMap()
  90. cb := tgbotapi.NewCallback(
  91. u.CallbackQuery.ID,
  92. u.CallbackQuery.Data,
  93. )
  94. data := u.CallbackQuery.Data
  95. _, err := c.Bot.Api.Request(cb)
  96. if err != nil {
  97. return
  98. }
  99. btn, ok := btns[data]
  100. if !ok {
  101. return
  102. }
  103. if btn != nil {
  104. act = btn.Action
  105. } else if compo.Action != nil {
  106. act = compo.Action
  107. }
  108. c.WithUpdate(u).Run(act)
  109. }