Skip to content

Commit ef53ad0

Browse files
committed
feat: Keyboard interface introduced
1 parent 8f5d687 commit ef53ad0

File tree

4 files changed

+69
-14
lines changed

4 files changed

+69
-14
lines changed

tgbotapi/configs.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,11 @@ type Fileable interface {
111111

112112
// BaseChat is a base type for all chat config types.
113113
type BaseChat struct {
114-
ChatID int64 `json:"chat_id,omitempty"`
115-
ChannelUsername string `json:"channel_username,omitempty"`
116-
ReplyToMessageID int `json:"reply_to_message_id,omitempty"`
117-
ReplyMarkup interface{} `json:"reply_markup,omitempty"`
118-
DisableNotification bool `json:"disable_notification,omitempty"`
114+
ChatID int64 `json:"chat_id,omitempty"`
115+
ChannelUsername string `json:"channel_username,omitempty"`
116+
ReplyToMessageID int `json:"reply_to_message_id,omitempty"`
117+
ReplyMarkup any `json:"reply_markup,omitempty"`
118+
DisableNotification bool `json:"disable_notification,omitempty"`
119119

120120
ProtectContent bool `json:"protect_content,omitempty"` // Protects the contents of the sent message from forwarding and saving
121121

tgbotapi/keyboard.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package tgbotapi
2+
3+
import "github.com/bots-go-framework/bots-go-core/botkb"
4+
5+
type keyboardType string
6+
7+
type Keyboard interface {
8+
botkb.Keyboard
9+
telegramKeyboardType() keyboardType
10+
}

tgbotapi/message.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package tgbotapi
33
//go:generate ffjson $GOFILE
44

55
import (
6+
"strconv"
67
"strings"
78
"time"
89
)
@@ -95,6 +96,10 @@ type Message struct {
9596
UniqueGift *UniqueGiftInfo `json:"unique_gift,omitempty"`
9697
}
9798

99+
func (m *Message) GetMessageID() string {
100+
return strconv.Itoa(m.MessageID)
101+
}
102+
98103
// Time converts the message timestamp into a Time.
99104
func (m *Message) Time() time.Time {
100105
return time.Unix(int64(m.Date), 0)

tgbotapi/types.go

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -295,21 +295,51 @@ func (f *File) Link(token string) string {
295295
return fmt.Sprintf(FileEndpoint, token, f.FilePath)
296296
}
297297

298+
var _ botkb.Keyboard = (*ReplyKeyboardMarkup)(nil)
299+
var _ Keyboard = (*ReplyKeyboardMarkup)(nil)
300+
298301
// ReplyKeyboardMarkup allows the Bot to set a custom keyboard.
299302
type ReplyKeyboardMarkup struct {
300-
Keyboard [][]KeyboardButton `json:"keyboard"`
301-
ResizeKeyboard bool `json:"resize_keyboard,omitempty"` // optional
302-
OneTimeKeyboard bool `json:"one_time_keyboard,omitempty"` // optional
303-
Selective bool `json:"selective,omitempty"` // optional
303+
// Array of button rows, each represented by an Array of KeyboardButton objects
304+
Keyboard [][]KeyboardButton `json:"keyboard"`
305+
306+
// Optional. Requests clients to always show the keyboard when the regular keyboard is hidden.
307+
// Defaults to false, in which case the custom keyboard can be hidden and opened with a keyboard icon.
308+
IsPersistent bool `json:"is_persistent,omitempty"`
309+
310+
// Optional. Requests clients to resize the keyboard vertically for optimal fit
311+
// (e.g., make the keyboard smaller if there are just two rows of buttons).
312+
// Defaults to false, in which case the custom keyboard is always of the same height as the app's standard keyboard.
313+
ResizeKeyboard bool `json:"resize_keyboard,omitempty"` // optional
314+
315+
// Optional. Requests clients to hide the keyboard as soon as it's been used.
316+
// The keyboard will still be available, but clients will automatically display the usual letter-keyboard
317+
// in the chat - the user can press a special button in the input field to see the custom keyboard again.
318+
//Defaults to false.
319+
OneTimeKeyboard bool `json:"one_time_keyboard,omitempty"` // optional
320+
321+
// Optional. The placeholder to be shown in the input field when the keyboard is active; 1-64 characters
322+
InputFieldPlaceholder string `json:"input_field_placeholder,omitempty"`
323+
324+
// Optional. Use this parameter if you want to show the keyboard to specific users only.
325+
// Targets:
326+
// 1) users that are @mentioned in the text of the Message object;
327+
// 2) if the bot's message is a reply to a message in the same chat and forum topic, sender of the original message.
328+
//
329+
// Example: A user requests to change the bot's language, bot replies to the request with a keyboard to select the new language.
330+
// Other users in the group don't see the keyboard.
331+
Selective bool `json:"selective,omitempty"` // optional
332+
}
333+
334+
func (m *ReplyKeyboardMarkup) telegramKeyboardType() keyboardType {
335+
return "reply_keyboard"
304336
}
305337

306338
// KeyboardType returns KeyboardTypeBottom
307339
func (*ReplyKeyboardMarkup) KeyboardType() botkb.KeyboardType {
308340
return botkb.KeyboardTypeBottom
309341
}
310342

311-
var _ botkb.Keyboard = (*ReplyKeyboardMarkup)(nil)
312-
313343
// KeyboardButtonRequestUsers represents a request from the bot to send users
314344
// https://core.telegram.org/bots/api#keyboardbuttonrequestusers
315345
type KeyboardButtonRequestUsers struct {
@@ -435,24 +465,36 @@ func (v WebAppInfo) Validate() error {
435465
return nil
436466
}
437467

468+
var _ botkb.Keyboard = (*ReplyKeyboardHide)(nil)
469+
var _ Keyboard = (*ReplyKeyboardHide)(nil)
470+
438471
// ReplyKeyboardHide allows the Bot to hide a custom keyboard.
439472
type ReplyKeyboardHide struct {
440473
HideKeyboard bool `json:"hide_keyboard"`
441474
Selective bool `json:"selective,omitempty"` // optional
442475
}
443476

477+
func (h *ReplyKeyboardHide) telegramKeyboardType() keyboardType {
478+
return "ReplyKeyboardHide"
479+
}
480+
444481
// KeyboardType returns KeyboardTypeHide
445482
func (*ReplyKeyboardHide) KeyboardType() botkb.KeyboardType {
446483
return botkb.KeyboardTypeHide
447484
}
448485

449-
var _ botkb.Keyboard = (*ReplyKeyboardHide)(nil)
486+
var _ botkb.Keyboard = (*InlineKeyboardMarkup)(nil)
487+
var _ Keyboard = (*InlineKeyboardMarkup)(nil)
450488

451489
// InlineKeyboardMarkup is a custom keyboard presented for an inline bot.
452490
type InlineKeyboardMarkup struct {
453491
InlineKeyboard [][]InlineKeyboardButton `json:"inline_keyboard"`
454492
}
455493

494+
func (v *InlineKeyboardMarkup) telegramKeyboardType() keyboardType {
495+
return "InlineKeyboardMarkup"
496+
}
497+
456498
func (v *InlineKeyboardMarkup) Validate() error {
457499
for _, row := range v.InlineKeyboard {
458500
for _, button := range row {
@@ -469,8 +511,6 @@ func (*InlineKeyboardMarkup) KeyboardType() botkb.KeyboardType {
469511
return botkb.KeyboardTypeInline
470512
}
471513

472-
var _ botkb.Keyboard = (*InlineKeyboardMarkup)(nil)
473-
474514
// LoginUrl represents a parameter of the inline keyboard button used to automatically authorize a user.
475515
// https://core.telegram.org/bots/api#loginurl
476516
type LoginUrl struct {

0 commit comments

Comments
 (0)