InlineButton became endpoint (callback handling.)

pull/108/head
Ian Byrd 7 years ago
parent d6ef68605b
commit eae6345b88
No known key found for this signature in database
GPG Key ID: 598F598CA3B8055F

@ -78,15 +78,27 @@ type Update struct {
//
// Example:
//
// tb.b.handle("/help", func (m *tb.Message) {})
// tb.b.handle(tb.OnEditedMessage, func (m *tb.Message) {})
// tb.b.handle(tb.OnQuery, func (q *tb.Query) {})
// b.handle("/help", func (m *tb.Message) {})
// b.handle(tb.OnEdited, func (m *tb.Message) {})
// b.handle(tb.OnQuery, func (q *tb.Query) {})
//
func (b *Bot) Handle(endpoint string, handler interface{}) {
b.handlers[endpoint] = handler
// // make a hook for one of your preserved (by-pointer)
// // inline buttons.
// b.handle(&inlineButton, func (c *tb.Callback) {})
//
func (b *Bot) Handle(endpoint interface{}, handler interface{}) {
switch end := endpoint.(type) {
case string:
b.handlers[end] = handler
case CallbackEndpoint:
b.handlers["\f"+end.CallbackUnique()] = handler
}
}
var cmdRx = regexp.MustCompile(`^(\/\w+)(@(\w+))?(\s|$)`)
var (
cmdRx = regexp.MustCompile(`^(\/\w+)(@(\w+))?(\s|$)`)
cbackRx = regexp.MustCompile(`^\f(\w+)\|(.+)$`)
)
func (b *Bot) handleCommand(m *Message, cmdName, cmdBot string) bool {
// Group-syntax: "/cmd@bot"
@ -217,6 +229,27 @@ func (b *Bot) Start() {
}
if upd.Callback != nil {
if upd.Callback.Data != "" {
data := upd.Callback.Data
if data[0] == '\f' {
match := cbackRx.FindAllStringSubmatch(data, -1)
if match != nil {
unique, payload := match[0][1], match[0][2]
if handler, ok := b.handlers["\f"+unique]; ok {
if handler, ok := handler.(func(*Callback)); ok {
upd.Callback.Data = payload
handler(upd.Callback)
continue
}
}
}
}
}
if handler, ok := b.handlers[OnCallback]; ok {
if handler, ok := handler.(func(*Callback)); ok {
handler(upd.Callback)
@ -245,9 +278,9 @@ func (b *Bot) handle(end string, m *Message) bool {
if handler, ok := handler.(func(*Message)); ok {
go handler(m)
return true
} else {
return false
}
return false
}
func (b *Bot) handleMedia(m *Message) bool {
@ -384,6 +417,9 @@ func (b *Bot) SendAlbum(to Recipient, a Album, options ...interface{}) ([]Messag
embedSendOptions(params, sendOpts)
respJSON, err := b.sendFiles("sendMediaGroup", files, params)
if err != nil {
return nil, err
}
var resp struct {
Ok bool

@ -1,5 +1,11 @@
package telebot
// CallbackEndpoint is an interface any element capable
// of responding to a callback `\f<unique>`.
type CallbackEndpoint interface {
CallbackUnique() string
}
// Callback object represents a query from a callback button in an
// inline keyboard.
type Callback struct {
@ -47,3 +53,22 @@ type CallbackResponse struct {
// https://telegram.me/your_bot?start=XXXX
URL string `json:"url,omitempty"`
}
// InlineButton represents a button displayed in the message.
type InlineButton struct {
// Unique slagish name for this kind of button,
// try to be as specific as possible.
//
// It will be used as a callback endpoint.
Unique string `json:"unique,omitempty"`
Text string `json:"text"`
URL string `json:"url,omitempty"`
Data string `json:"callback_data,omitempty"`
InlineQuery string `json:"switch_inline_query,omitempty"`
}
// CallbackUnique returns InlineButto.Unique
func (t *InlineButton) CallbackUnique() string {
return t.Unique
}

@ -106,12 +106,3 @@ type InlineKeyboardMarkup struct {
// an Array of KeyboardButton objects.
InlineKeyboard [][]InlineButton `json:"inline_keyboard,omitempty"`
}
// InlineButton represents a button displayed in the message.
type InlineButton struct {
Text string `json:"text"`
URL string `json:"url,omitempty"`
Data string `json:"callback_data,omitempty"`
InlineQuery string `json:"switch_inline_query,omitempty"`
}

@ -154,6 +154,18 @@ func embedSendOptions(params map[string]string, opt *SendOptions) {
}
if opt.ReplyMarkup != nil {
keys := opt.ReplyMarkup.InlineKeyboard
if len(keys) > 0 && len(keys[0]) > 0 {
for i, _ := range keys {
for j, _ := range keys[i] {
key := &keys[i][j]
if key.Unique != "" {
key.Data = "\f" + key.Unique + "|" + key.Data
}
}
}
}
replyMarkup, _ := json.Marshal(opt.ReplyMarkup)
params["reply_markup"] = string(replyMarkup)
}

Loading…
Cancel
Save