Merge pull request #47 from zabawaba99/inline-keyboard-button

Adding support for inline keyboard buttons
pull/32/merge
Ian Byrd 8 years ago committed by GitHub
commit 15ae62790e

@ -111,8 +111,9 @@ func embedSendOptions(params *url.Values, options *SendOptions) {
{ {
forceReply := options.ReplyMarkup.ForceReply forceReply := options.ReplyMarkup.ForceReply
customKeyboard := (options.ReplyMarkup.CustomKeyboard != nil) customKeyboard := (options.ReplyMarkup.CustomKeyboard != nil)
inlineKeyboard := options.ReplyMarkup.InlineKeyboard != nil
hiddenKeyboard := options.ReplyMarkup.HideCustomKeyboard hiddenKeyboard := options.ReplyMarkup.HideCustomKeyboard
if forceReply || customKeyboard || hiddenKeyboard { if forceReply || customKeyboard || hiddenKeyboard || inlineKeyboard {
replyMarkup, _ := json.Marshal(options.ReplyMarkup) replyMarkup, _ := json.Marshal(options.ReplyMarkup)
params.Set("reply_markup", string(replyMarkup)) params.Set("reply_markup", string(replyMarkup))
} }

@ -11,10 +11,11 @@ import (
// Bot represents a separate Telegram bot instance. // Bot represents a separate Telegram bot instance.
type Bot struct { type Bot struct {
Token string Token string
Identity User Identity User
Messages chan Message Messages chan Message
Queries chan Query Queries chan Query
Callbacks chan Callback
} }
// NewBot does try to build a Bot with token `token`, which // NewBot does try to build a Bot with token `token`, which
@ -34,18 +35,19 @@ func NewBot(token string) (*Bot, error) {
// Listen periodically looks for updates and delivers new messages // Listen periodically looks for updates and delivers new messages
// to the subscription channel. // to the subscription channel.
func (b *Bot) Listen(subscription chan Message, timeout time.Duration) { func (b *Bot) Listen(subscription chan Message, timeout time.Duration) {
go b.poll(subscription, nil, timeout) go b.poll(subscription, nil, nil, timeout)
} }
// Start periodically polls messages and/or updates to corresponding channels // Start periodically polls messages and/or updates to corresponding channels
// from the bot object. // from the bot object.
func (b *Bot) Start(timeout time.Duration) { func (b *Bot) Start(timeout time.Duration) {
b.poll(b.Messages, b.Queries, timeout) b.poll(b.Messages, b.Queries, b.Callbacks, timeout)
} }
func (b *Bot) poll( func (b *Bot) poll(
messages chan Message, messages chan Message,
queries chan Query, queries chan Query,
callbacks chan Callback,
timeout time.Duration, timeout time.Duration,
) { ) {
latestUpdate := 0 latestUpdate := 0
@ -62,18 +64,24 @@ func (b *Bot) poll(
} }
for _, update := range updates { for _, update := range updates {
if update.Query == nil /* if message */ { if update.Payload != nil /* if message */ {
if messages == nil { if messages == nil {
continue continue
} }
messages <- update.Payload messages <- *update.Payload
} else /* if query */ { } else if update.Query != nil /* if query */ {
if queries == nil { if queries == nil {
continue continue
} }
queries <- *update.Query queries <- *update.Query
} else if update.Callback != nil {
if callbacks == nil {
continue
}
callbacks <- *update.Callback
} }
latestUpdate = update.ID latestUpdate = update.ID

@ -40,6 +40,9 @@ type ReplyMarkup struct {
// //
// Note: you don't need to set HideCustomKeyboard field to show custom keyboard. // Note: you don't need to set HideCustomKeyboard field to show custom keyboard.
CustomKeyboard [][]string `json:"keyboard,omitempty"` CustomKeyboard [][]string `json:"keyboard,omitempty"`
InlineKeyboard [][]KeyboardButton `json:"inline_keyboard,omitempty"`
// Requests clients to resize the keyboard vertically for optimal fit // Requests clients to resize the keyboard vertically for optimal fit
// (e.g., make the keyboard smaller if there are just two rows of buttons). // (e.g., make the keyboard smaller if there are just two rows of buttons).
// Defaults to false, in which case the custom keyboard is always of the // Defaults to false, in which case the custom keyboard is always of the

@ -54,11 +54,12 @@ func (c Chat) IsGroupChat() bool {
// Update object represents an incoming update. // Update object represents an incoming update.
type Update struct { type Update struct {
ID int `json:"update_id"` ID int `json:"update_id"`
Payload Message `json:"message"` Payload *Message `json:"message"`
// optional // optional
Query *Query `json:"inline_query"` Callback *Callback `json:"callback_query"`
Query *Query `json:"inline_query"`
} }
// Thumbnail object represents an image/sticker of a particular size. // Thumbnail object represents an image/sticker of a particular size.
@ -129,6 +130,14 @@ type Video struct {
Preview Thumbnail `json:"thumb"` Preview Thumbnail `json:"thumb"`
} }
// KeyboardButton represents a button displayed on in a message.
type KeyboardButton struct {
Text string `json:"text"`
URL string `json:"url,omitempty"`
Data string `json:"callback_data,omitempty"`
InlineQuery string `json:"switch_inline_query,omitempty"`
}
// Contact object represents a contact to Telegram user // Contact object represents a contact to Telegram user
type Contact struct { type Contact struct {
UserID int `json:"user_id"` UserID int `json:"user_id"`
@ -142,3 +151,21 @@ type Location struct {
Longitude float32 `json:"longitude"` Longitude float32 `json:"longitude"`
Latitude float32 `json:"latitude"` Latitude float32 `json:"latitude"`
} }
// Callback object represents a query from a callback button in an
// inline keyboard.
type Callback struct {
ID string `json:"id"`
// For message sent to channels, Sender may be empty
Sender User `json:"from"`
// Message will be set if the button that originated the query
// was attached to a message sent by a bot.
Message Message `json:"message"`
// MessageID will be set if the button was attached to a message
// sent via the bot in inline mode.
MessageID string `json:"inline_message_id"`
Data string `json:"data"`
}

Loading…
Cancel
Save