keyboard.go 2.3 KB

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