reply.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 == nil {
  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. sendConfig.Message.ReplyMarkup = compo.Reply.ToApi()
  61. return sendConfig
  62. }
  63. // Implementing the Server interface.
  64. func (compo ReplyCompo) Filter(
  65. u *Update,
  66. ) bool {
  67. if compo == nil || u.Message == nil {
  68. return true
  69. }
  70. _, ok := compo.ButtonMap()[u.Message.Text]
  71. if !ok {
  72. if u.Message.Location != nil {
  73. locBtn := compo.ButtonMap().LocationButton()
  74. if locBtn == nil {
  75. return true
  76. }
  77. } else {
  78. return true
  79. }
  80. }
  81. return false
  82. }
  83. // Implementing the UI interface.
  84. func (compo ReplyCompo) Serve(c *Context) {
  85. for u := range c.Input() {
  86. var btn *Button
  87. text := u.Message.Text
  88. btns := compo.ButtonMap()
  89. btn, ok := btns[text]
  90. if !ok {
  91. if u.Message.Location != nil {
  92. btn = btns.LocationButton()
  93. }
  94. }
  95. if btn != nil {
  96. c.WithUpdate(u).Run(btn.Action)
  97. }
  98. }
  99. }