reply.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package tg
  2. import (
  3. tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
  4. )
  5. // The type represents reply keyboards.
  6. type Reply struct {
  7. Keyboard
  8. // If true will be removed after one press.
  9. OneTime bool
  10. // If true will remove the keyboard on send.
  11. Remove bool
  12. }
  13. // Set if we should remove current keyboard on the user side
  14. // when sending the keyboard.
  15. func (kbd Reply) WithRemove(remove bool) Reply {
  16. kbd.Remove = remove
  17. return kbd
  18. }
  19. // Set if the keyboard should be hidden after
  20. // one of buttons is pressede.
  21. func (kbd Reply) WithOneTime(oneTime bool) Reply{
  22. kbd.OneTime = oneTime
  23. return kbd
  24. }
  25. // Convert the Keyboard to the Telegram API type of reply keyboard.
  26. func (kbd Reply) ToApi() any {
  27. // Shades everything.
  28. if kbd.Remove {
  29. return tgbotapi.NewRemoveKeyboard(true)
  30. }
  31. rows := [][]tgbotapi.KeyboardButton{}
  32. for _, row := range kbd.Rows {
  33. if row == nil {
  34. continue
  35. }
  36. buttons := []tgbotapi.KeyboardButton{}
  37. for _, button := range row {
  38. if !button.Valid {
  39. continue
  40. }
  41. buttons = append(buttons, button.ToTelegram())
  42. }
  43. rows = append(rows, buttons)
  44. }
  45. if kbd.OneTime {
  46. return tgbotapi.NewOneTimeReplyKeyboard(rows...)
  47. }
  48. return tgbotapi.NewReplyKeyboard(rows...)
  49. }
  50. // The type implements reply keyboard widget.
  51. type ReplyCompo struct {
  52. MessageCompo
  53. Reply
  54. }
  55. // Implementing the sendable interface.
  56. func (compo *ReplyCompo) SendConfig(
  57. sid SessionID, bot *Bot,
  58. ) (SendConfig) {
  59. sendConfig := compo.MessageCompo.SendConfig(sid, bot)
  60. msg := sendConfig.Chattable.(tgbotapi.MessageConfig)
  61. msg.ReplyMarkup = compo.Reply.ToApi()
  62. sendConfig.Chattable = msg
  63. return sendConfig
  64. }
  65. // Implementing the Server interface.
  66. func (compo *ReplyCompo) Filter(
  67. u Update,
  68. ) bool {
  69. if compo == nil || u.Message == nil {
  70. return true
  71. }
  72. _, ok := compo.ButtonMap()[u.Message.Text]
  73. if !ok {
  74. if u.Message.Location != nil {
  75. _, hasLocBtn := compo.ButtonMap().LocationButton()
  76. if !hasLocBtn {
  77. return true
  78. }
  79. } else {
  80. return true
  81. }
  82. }
  83. return false
  84. }
  85. // Implementing the UI interface.
  86. func (compo *ReplyCompo) Serve(c Context) {
  87. for u := range c.Input() {
  88. var btn Button
  89. text := u.Message.Text
  90. btns := compo.ButtonMap()
  91. btn, ok := btns[text]
  92. if !ok {
  93. if u.Message.Location != nil {
  94. locBtn, hasLocBtn := btns.LocationButton()
  95. if hasLocBtn {
  96. btn = locBtn
  97. }
  98. }
  99. }
  100. if !btn.Valid {
  101. continue
  102. }
  103. c.WithUpdate(u).Run(btn.Action)
  104. }
  105. }