keyboard.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package tg
  2. import (
  3. //tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
  4. )
  5. // The general keyboard type used both in Reply and Inline.
  6. type Keyboard struct {
  7. // The action is called if there is no
  8. // defined action for the button.
  9. Action Action
  10. Rows []ButtonRow
  11. buttonMap ButtonMap
  12. }
  13. // Returns the new keyboard with specified rows.
  14. func NewKeyboard(rows ...ButtonRow) Keyboard {
  15. ret := &Keyboard{}
  16. for _, row := range rows {
  17. if row != nil && len(row) > 0 {
  18. ret.Rows = append(ret.Rows, row)
  19. }
  20. }
  21. return ret
  22. }
  23. func (kbd Keyboard) RowNum() int {
  24. return len(kbd.Rows)
  25. }
  26. func (kbd *Keyboard) RemoveRow(i int) {
  27. if i<0 || i > len(kbd.Rows) - 1 {
  28. return
  29. }
  30. kbd.Rows = append(kbd.Rows[:i], kbd.Rows[i+1:]...)
  31. }
  32. // Adds a new button row to the current keyboard.
  33. func (kbd Keyboard) Row(btns ...Button) Keyboard {
  34. // For empty row. We do not need that.
  35. if len(btns) < 1 {
  36. return kbd
  37. }
  38. retBtns := []*Button{}
  39. for _, btn := range btns {
  40. if btn == nil {
  41. continue
  42. }
  43. retBtns = append(retBtns, btn)
  44. }
  45. // Add only if there is something to add.
  46. if len(retBtns) > 0 {
  47. kbd.Rows = append(kbd.Rows, retBtns)
  48. }
  49. return kbd
  50. }
  51. // Adds buttons as one column list.
  52. func (kbd Keyboard) List(btns ...Button) Keyboard {
  53. for _, btn := range btns {
  54. if btn == nil {
  55. continue
  56. }
  57. kbd.Rows = append(kbd.Rows, ButtonRow{btn})
  58. }
  59. return kbd
  60. }
  61. // Set the default action when no button provides
  62. // key to the data we got.
  63. func (kbd Keyboard) WithAction(a Action) Keyboard {
  64. kbd.Action = a
  65. return kbd
  66. }
  67. // Returns the map of buttons. Where the key
  68. // is button data and the value is Action.
  69. func (kbd Keyboard) ButtonMap() ButtonMap {
  70. if kbd.buttonMap == nil {
  71. kbd.buttonMap = kbd.MakeButtonMap()
  72. }
  73. return kbd.buttonMap
  74. }
  75. // Returns the map of buttons on the most fresh version of the keyboard.
  76. func (kbd Keyboard) MakeButtonMap() ButtonMap {
  77. ret := make(ButtonMap)
  78. for _, vi := range kbd.Rows {
  79. for _, vj := range vi {
  80. ret[vj.Key()] = vj
  81. }
  82. }
  83. kbd.buttonMap = ret
  84. return ret
  85. }
  86. // Convert the keyboard to the more specific inline one.
  87. func (kbd Keyboard) Inline() Inline {
  88. ret := Inline{}
  89. ret.Keyboard = kbd
  90. return ret
  91. }
  92. // Convert the keyboard to the more specific reply one.
  93. func (kbd Keyboard) Reply() Reply {
  94. ret := &Reply{}
  95. ret.Keyboard = kbd
  96. // it is used more often than not once.
  97. ret.OneTime = true
  98. return ret
  99. }