package helpers import ( "errors" "github.com/go-telegram/bot/models" ) var ( ErrRowLen = errors.New("row len alredy full") ) type InlineKeyboard struct { Rows []Row } type Row struct { Len int Buttons []Button } type Button struct { Data string Text string } // AddButton // Add a button to the last row of the keyboard // if the row if full and there are still pending buttons // to add, a new row will be created and buttons added to it func AddButton(ik *InlineKeyboard, text, data string) { r := &ik.Rows[len(ik.Rows)-1] if len(r.Buttons) < r.Len { button := Button{Text: text, Data: data} r.Buttons = append(r.Buttons, button) // fmt.Println("ADD BUTTON ", r, ik) }else { r = &Row{ Len: r.Len, Buttons: []Button{}, } button := Button{Text: text, Data: data} r.Buttons = append(r.Buttons, button) // fmt.Println("ADD BUTTON ", r, ik) ik.Rows = append(ik.Rows, *r) } } // AddRow // Create a new row with a len property to limit wide of kbd func AddRow(ik *InlineKeyboard, len int){ row := &Row{Len: len, Buttons: []Button{}} ik.Rows = append(ik.Rows, *row) // fmt.Println("ADD Row ", row.Len) } // CreateKeyBoard // render the structure into a models.InlineKeyboardMarkup func (ik *InlineKeyboard) CreateKeyBoard()models.InlineKeyboardMarkup{ kbd := models.InlineKeyboardMarkup{} // fmt.Println("creating keyboard ---- ", fmt.Sprintf("%#v", ik)) // fmt.Println("row 0 ", ik.Rows[0]) for _, row := range ik.Rows { r := []models.InlineKeyboardButton{} for _, button := range row.Buttons{ r = append(r, models.InlineKeyboardButton{Text: button.Text, CallbackData: button.Data}) } kbd.InlineKeyboard = append(kbd.InlineKeyboard, r) } return kbd } // KeyboardWithAcceptCancel func KeyboardWithAcceptCancel(textData [][]string, buttonSize int, up bool)models.InlineKeyboardMarkup{ kbd := &InlineKeyboard{} if up { AddRow(kbd, 2) AddButton(kbd, "Accept ✅", "operation=accept") AddButton(kbd, "Cancel ❌", "operation=cancel") } AddRow(kbd, buttonSize) for _, it := range textData{ if len(it) ==2{ AddButton(kbd, it[0], it[1]) } } if !up { AddRow(kbd, 2) AddButton(kbd, "Accept ✅", "operation=accept") AddButton(kbd, "Cancel ❌", "operation=cancel") } return kbd.CreateKeyBoard() } // KeyboardWithCancel func KeyboardWithCancel(textData [][]string, buttonSize int, up bool)models.InlineKeyboardMarkup{ kbd := &InlineKeyboard{} if up { AddRow(kbd, 2) AddButton(kbd, "Cancel ❌", "operation=cancel") } AddRow(kbd, buttonSize) for _, it := range textData{ if len(it) ==2{ AddButton(kbd, it[0], it[1]) } } if !up { AddRow(kbd, 2) AddButton(kbd, "Cancel ❌", "operation=cancel") } return kbd.CreateKeyBoard() } // KeyboardWithBackNext func KeyboardWithBackNext(textData [][]string, buttonSize int, up bool)models.InlineKeyboardMarkup{ kbd := &InlineKeyboard{} if up { AddRow(kbd, 2) AddButton(kbd, "⬅️ Back", "operation=accept") AddButton(kbd, "Next ➡️", "operation=cancel") } AddRow(kbd, buttonSize) for _, it := range textData{ if len(it) ==2{ AddButton(kbd, it[0], it[1]) } } if !up { AddRow(kbd, 2) AddButton(kbd, "⬅️ Back", "operation=accept") AddButton(kbd, "Next ➡️", "operation=cancel") } return kbd.CreateKeyBoard() }