Skip to content

Commit

Permalink
Merge pull request #12 from bulletmys/keyboard-improvements
Browse files Browse the repository at this point in the history
Keyboard improvements
  • Loading branch information
George Gabolaev authored Apr 16, 2021
2 parents 7f899fb + df19408 commit ed140b0
Show file tree
Hide file tree
Showing 8 changed files with 606 additions and 68 deletions.
4 changes: 2 additions & 2 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,13 @@ func (b *Bot) NewTextMessage(chatID, text string) *Message {
}

// NewInlineKeyboardMessage returns new text message with inline keyboard
func (b *Bot) NewInlineKeyboardMessage(chatID, text string, keyboard [][]Button) *Message {
func (b *Bot) NewInlineKeyboardMessage(chatID, text string, keyboard Keyboard) *Message {
return &Message{
client: b.client,
Chat: Chat{ID: chatID},
Text: text,
ContentType: Text,
InlineKeyboard: keyboard,
InlineKeyboard: &keyboard,
}
}

Expand Down
1 change: 1 addition & 0 deletions button.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package botgolang
//go:generate easyjson -all button.go

// Button represents a button in inline keyboard
// Make sure you have URL or CallbackData in your Button.
type Button struct {
// Button text
Text string `json:"text"`
Expand Down
12 changes: 6 additions & 6 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ func (c *Client) SendTextMessage(message *Message) error {
}

if message.InlineKeyboard != nil {
data, err := json.Marshal(message.InlineKeyboard)
data, err := json.Marshal(message.InlineKeyboard.GetKeyboard())
if err != nil {
return fmt.Errorf("cannot marshal inline keyboard markup: %s", err)
}
Expand Down Expand Up @@ -384,7 +384,7 @@ func (c *Client) EditMessage(message *Message) error {
}

if message.InlineKeyboard != nil {
data, err := json.Marshal(message.InlineKeyboard)
data, err := json.Marshal(message.InlineKeyboard.GetKeyboard())
if err != nil {
return fmt.Errorf("cannot marshal inline keyboard markup: %s", err)
}
Expand Down Expand Up @@ -434,7 +434,7 @@ func (c *Client) SendFileMessage(message *Message) error {
}

if message.InlineKeyboard != nil {
data, err := json.Marshal(message.InlineKeyboard)
data, err := json.Marshal(message.InlineKeyboard.GetKeyboard())
if err != nil {
return fmt.Errorf("cannot marshal inline keyboard markup: %s", err)
}
Expand Down Expand Up @@ -471,7 +471,7 @@ func (c *Client) SendVoiceMessage(message *Message) error {
}

if message.InlineKeyboard != nil {
data, err := json.Marshal(message.InlineKeyboard)
data, err := json.Marshal(message.InlineKeyboard.GetKeyboard())
if err != nil {
return fmt.Errorf("cannot marshal inline keyboard markup: %s", err)
}
Expand All @@ -498,7 +498,7 @@ func (c *Client) UploadFile(message *Message) error {
}

if message.InlineKeyboard != nil {
data, err := json.Marshal(message.InlineKeyboard)
data, err := json.Marshal(message.InlineKeyboard.GetKeyboard())
if err != nil {
return fmt.Errorf("cannot marshal inline keyboard markup: %s", err)
}
Expand All @@ -525,7 +525,7 @@ func (c *Client) UploadVoice(message *Message) error {
}

if message.InlineKeyboard != nil {
data, err := json.Marshal(message.InlineKeyboard)
data, err := json.Marshal(message.InlineKeyboard.GetKeyboard())
if err != nil {
return fmt.Errorf("cannot marshal inline keyboard markup: %s", err)
}
Expand Down
6 changes: 5 additions & 1 deletion example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ func main() {

helloBtn := botgolang.NewCallbackButton("Hello", "echo")
goBtn := botgolang.NewURLButton("go", "https://golang.org/")
message.AttachInlineKeyboard([][]botgolang.Button{{helloBtn, goBtn}})

keyboard := botgolang.NewKeyboard()
keyboard.AddRow(helloBtn, goBtn)

message.AttachInlineKeyboard(keyboard)

if err := message.Send(); err != nil {
log.Printf("failed to send message: %s", err)
Expand Down
112 changes: 112 additions & 0 deletions keyboard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package botgolang

import (
"fmt"
)

// Keyboard represents an inline keyboard markup
// Call the NewKeyboard() func to get a keyboard instance
type Keyboard struct {
Rows [][]Button
}

// NewKeyboard returns a new keyboard instance
func NewKeyboard() Keyboard {
return Keyboard{
Rows: make([][]Button, 0),
}
}

// AddRows adds a row to the keyboard
func (k *Keyboard) AddRow(row ...Button) {
k.Rows = append(k.Rows, row)
}

// AddButton adds a button to the end of the row
func (k *Keyboard) AddButton(rowIndex int, button Button) error {
if ok := k.checkRow(rowIndex); !ok {
return fmt.Errorf("no such row: %d", rowIndex)
}

k.Rows[rowIndex] = append(k.Rows[rowIndex], button)
return nil
}

// DeleteRow removes the row from the keyboard
func (k *Keyboard) DeleteRow(index int) error {
if ok := k.checkRow(index); !ok {
return fmt.Errorf("no such row: %d", index)
}

k.Rows = append(k.Rows[:index], k.Rows[index+1:]...)
return nil
}

// DeleteButton removes the button from the row.
// Note - at least one button should remain in a row,
// if you want to delete all buttons, use the DeleteRow function
func (k *Keyboard) DeleteButton(rowIndex, buttonIndex int) error {
if ok := k.checkButton(rowIndex, buttonIndex); !ok {
return fmt.Errorf("no button at index %d or row %d", buttonIndex, rowIndex)
}

if k.RowSize(rowIndex) < 2 {
return fmt.Errorf("can't delete button: at least one should remain in a row")
}

row := &k.Rows[rowIndex]
*row = append((*row)[:buttonIndex], (*row)[buttonIndex+1:]...)
return nil
}

// ChangeButton changes the button to a new one at the specified position
func (k *Keyboard) ChangeButton(rowIndex, buttonIndex int, newButton Button) error {
if ok := k.checkButton(rowIndex, buttonIndex); !ok {
return fmt.Errorf("no button at index %d or row %d", buttonIndex, rowIndex)
}

k.Rows[rowIndex][buttonIndex] = newButton
return nil
}

// SwapRows swaps two rows in keyboard
func (k *Keyboard) SwapRows(first, second int) error {
if ok := k.checkRow(first); !ok {
return fmt.Errorf("no such index (first): %d", first)
}
if ok := k.checkRow(second); !ok {
return fmt.Errorf("no such index (second): %d", second)
}

k.Rows[first], k.Rows[second] = k.Rows[second], k.Rows[first]
return nil
}

// RowsCount returns the number of rows
func (k *Keyboard) RowsCount() int {
return len(k.Rows)
}

// RowSize returns the number of buttons in a row.
// If there is no such row, then returns -1
func (k *Keyboard) RowSize(row int) int {
if ok := k.checkRow(row); !ok {
return -1
}
return len(k.Rows[row])
}

// GetKeyboard returns an array of button rows
func (k *Keyboard) GetKeyboard() [][]Button {
return k.Rows
}

// checkRow checks that the index of row doesnt go beyond the bounds of the array
func (k *Keyboard) checkRow(i int) bool {
return i >= 0 && i < len(k.Rows)
}

// checkButton checks that the button and row indexes doesnt go beyond the bounds of the array
func (k *Keyboard) checkButton(row, button int) bool {
return k.checkRow(row) && button >= 0 && button < len(k.Rows[row])
}
Loading

0 comments on commit ed140b0

Please sign in to comment.