Skip to content

Commit 0d1bc8c

Browse files
author
Syfaro
committed
Add helpers for regular and inline keyboards.
1 parent 0252f39 commit 0d1bc8c

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

helpers.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,3 +427,113 @@ func NewEditMessageReplyMarkup(chatID int64, messageID int, replyMarkup InlineKe
427427
ReplyMarkup: &replyMarkup,
428428
}
429429
}
430+
431+
// NewHideKeyboard hides the keyboard, with the option for being selective
432+
// or hiding for everyone.
433+
func NewHideKeyboard(selective bool) ReplyKeyboardHide {
434+
return ReplyKeyboardHide{
435+
HideKeyboard: true,
436+
Selective: selective,
437+
}
438+
}
439+
440+
// NewKeyboardButton creates a regular keyboard button.
441+
func NewKeyboardButton(text string) KeyboardButton {
442+
return KeyboardButton{
443+
Text: text,
444+
}
445+
}
446+
447+
// NewKeyboardButtonContact creates a keyboard button that requests
448+
// user contact information upon click.
449+
func NewKeyboardButtonContact(text string) KeyboardButton {
450+
return KeyboardButton{
451+
Text: text,
452+
RequestContact: true,
453+
}
454+
}
455+
456+
// NewKeyboardButtonLocation creates a keyboard button that requests
457+
// user location information upon click.
458+
func NewKeyboardButtonLocation(text string) KeyboardButton {
459+
return KeyboardButton{
460+
Text: text,
461+
RequestLocation: true,
462+
}
463+
}
464+
465+
// NewKeyboardButtonRow creates a row of keyboard buttons.
466+
func NewKeyboardButtonRow(buttons ...KeyboardButton) []KeyboardButton {
467+
var row []KeyboardButton
468+
469+
for _, button := range buttons {
470+
row = append(row, button)
471+
}
472+
473+
return row
474+
}
475+
476+
// NewReplyKeyboard creates a new regular keyboard with sane defaults.
477+
func NewReplyKeyboard(rows ...[]KeyboardButton) ReplyKeyboardMarkup {
478+
var keyboard [][]KeyboardButton
479+
480+
for _, row := range rows {
481+
keyboard = append(keyboard, row)
482+
}
483+
484+
return ReplyKeyboardMarkup{
485+
ResizeKeyboard: true,
486+
Keyboard: keyboard,
487+
}
488+
}
489+
490+
// NewInlineKeyboardButtonData creates an inline keyboard button with text
491+
// and data for a callback.
492+
func NewInlineKeyboardButtonData(text, data string) InlineKeyboardButton {
493+
return InlineKeyboardButton{
494+
Text: text,
495+
CallbackData: &data,
496+
}
497+
}
498+
499+
// NewInlineKeyboardButtonURL creates an inline keyboard button with text
500+
// which goes to a URL.
501+
func NewInlineKeyboardButtonURL(text, url string) InlineKeyboardButton {
502+
return InlineKeyboardButton{
503+
Text: text,
504+
URL: &url,
505+
}
506+
}
507+
508+
// NewInlineKeyboardButtonSwitch creates an inline keyboard button with
509+
// text which allows the user to switch to a chat or return to a chat.
510+
func NewInlineKeyboardButtonSwitch(text, sw string) InlineKeyboardButton {
511+
return InlineKeyboardButton{
512+
Text: text,
513+
SwitchInlineQuery: &sw,
514+
}
515+
}
516+
517+
// NewInlineKeyboardRow creates an inline keyboard row with buttons.
518+
func NewInlineKeyboardRow(buttons ...InlineKeyboardButton) []InlineKeyboardButton {
519+
var row []InlineKeyboardButton
520+
521+
for _, button := range buttons {
522+
row = append(row, button)
523+
}
524+
525+
return row
526+
}
527+
528+
// NewInlineKeyboardMarkup creates a new inline keyboard.
529+
func NewInlineKeyboardMarkup(rows ...[]InlineKeyboardButton) InlineKeyboardMarkup {
530+
var keyboard [][]InlineKeyboardButton
531+
532+
for _, row := range rows {
533+
keyboard = append(keyboard, row)
534+
}
535+
536+
return InlineKeyboardMarkup{
537+
InlineKeyboard: keyboard,
538+
}
539+
}

0 commit comments

Comments
 (0)