Skip to content

Commit

Permalink
Merge pull request #11 from bulletmys/keyboard
Browse files Browse the repository at this point in the history
Added Button Functionality
  • Loading branch information
George Gabolaev authored Apr 13, 2020
2 parents 857bf63 + ef67618 commit e713f2b
Show file tree
Hide file tree
Showing 10 changed files with 741 additions and 322 deletions.
12 changes: 12 additions & 0 deletions api_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,18 @@ func (h *MockHandler) GetEvents(w http.ResponseWriter) {
"lastName": "SurName"
}
}
},
{
"eventId": 8,
"payload": {
"callbackData": "echo",
"from": {
"firstName": "Name",
"userId": "1234567890"
},
"queryId": "SVR:123456"
},
"type": "callbackQuery"
}
]
}
Expand Down
24 changes: 23 additions & 1 deletion bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ 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 {
return &Message{
client: b.client,
Chat: Chat{ID: chatID},
Text: text,
ContentType: Text,
InlineKeyboard: keyboard,
}
}

// NewFileMessage returns new file message
func (b *Bot) NewFileMessage(chatID string, file *os.File) *Message {
return &Message{
Expand All @@ -83,7 +94,7 @@ func (b *Bot) NewFileMessageByFileID(chatID, fileID string) *Message {
}
}

// NewFileMessage returns new voice message
// NewVoiceMessage returns new voice message
func (b *Bot) NewVoiceMessage(chatID string, file *os.File) *Message {
return &Message{
client: b.client,
Expand Down Expand Up @@ -114,6 +125,17 @@ func (b *Bot) NewMessageFromPart(message PartMessage) *Message {
}
}

// NewButtonResponse returns new ButtonResponse
func (b *Bot) NewButtonResponse(queryID, url, text string, showAlert bool) *ButtonResponse {
return &ButtonResponse{
client: b.client,
QueryID: queryID,
Text: text,
URL: url,
ShowAlert: showAlert,
}
}

func (b *Bot) NewChat(id string) *Chat {
return &Chat{
client: b.client,
Expand Down
59 changes: 59 additions & 0 deletions button.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package botgolang

//go:generate easyjson -all button.go

// Button represents a button in inline keyboard
type Button struct {
// Button text
Text string `json:"text"`

// URL to be opened
// You can't use it with CallbackData
URL string `json:"url,omitempty"`

// Data that identify the button
// You can't use it with URL
CallbackData string `json:"callbackData,omitempty"`
}

// NewURLButton returns new button with URL field
func NewURLButton(text string, url string) Button {
return Button{
Text: text,
URL: url,
}
}

// NewCallbackButton returns new button with CallbackData field
func NewCallbackButton(text string, callbackData string) Button {
return Button{
Text: text,
CallbackData: callbackData,
}
}

// ButtonResponse represents a data that is returned when a button is clicked
type ButtonResponse struct {
client *Client

// Id of the query
QueryID string `json:"queryId"`

// Text of the response message
Text string `json:"text"`

// Display alert?
ShowAlert bool `json:"showAlert"`

// URL to be opened
URL string `json:"url"`

// CallbackData of the query (id of the pressed button).
CallbackData string `json:"callbackData"`
}

// Send method sends your response message.
// Make sure you have QueryID in your ButtonResponse.
func (cl *ButtonResponse) Send() error {
return cl.client.SendAnswerCallbackQuery(cl)
}
193 changes: 193 additions & 0 deletions button_easyjson.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e713f2b

Please sign in to comment.