Skip to content

Commit ef64fbc

Browse files
committed
fix(deps): getTelegramInlineKeyboard()
1 parent a120f80 commit ef64fbc

File tree

1 file changed

+41
-10
lines changed

1 file changed

+41
-10
lines changed

telegram/webhook_responder.go

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/json"
77
"errors"
88
"fmt"
9+
"github.com/bots-go-framework/bots-go-core/botkb"
910
"net/http"
1011
"strconv"
1112
"time"
@@ -215,20 +216,25 @@ func (r tgWebhookResponder) SendMessage(ctx context.Context, m botsfw.MessageFro
215216
return
216217
}
217218
if m.Text == "" && m.Keyboard != nil {
218-
sendable = tgbotapi.NewEditMessageReplyMarkup(chatID, messageID, inlineMessageID, m.Keyboard.(*tgbotapi.InlineKeyboardMarkup))
219+
switch keyboard := m.Keyboard.(type) {
220+
case *tgbotapi.InlineKeyboardMarkup:
221+
sendable = tgbotapi.NewEditMessageReplyMarkup(chatID, messageID, inlineMessageID, keyboard)
222+
case *botkb.MessageKeyboard:
223+
switch keyboard.KeyboardType() {
224+
case botkb.KeyboardTypeInline:
225+
kb := getTelegramInlineKeyboard(m.Keyboard)
226+
sendable = tgbotapi.NewEditMessageReplyMarkup(chatID, messageID, inlineMessageID, tgbotapi.NewInlineKeyboardMarkup(kb.InlineKeyboard...))
227+
default:
228+
err = fmt.Errorf("unknown keyboard type %T(%v)", keyboard.KeyboardType(), keyboard)
229+
return
230+
}
231+
}
219232
} else if m.Text != "" {
220233
editMessageTextConfig := tgbotapi.NewEditMessageText(chatID, messageID, inlineMessageID, m.Text)
221234
editMessageTextConfig.ParseMode = parseMode()
222235
editMessageTextConfig.DisableWebPagePreview = m.DisableWebPagePreview
223236
if m.Keyboard != nil {
224-
switch keyboard := m.Keyboard.(type) {
225-
case *tgbotapi.InlineKeyboardMarkup:
226-
editMessageTextConfig.ReplyMarkup = keyboard
227-
//case tgbotapi.ForceReply:
228-
// editMessageTextConfig.ReplyMarkup = keyboard
229-
default:
230-
panic(fmt.Sprintf("m.Keyboard has unsupported type %T", m.Keyboard))
231-
}
237+
editMessageTextConfig.ReplyMarkup = getTelegramInlineKeyboard(m.Keyboard)
232238
}
233239
sendable = editMessageTextConfig
234240
} else {
@@ -244,7 +250,7 @@ func (r tgWebhookResponder) SendMessage(ctx context.Context, m botsfw.MessageFro
244250
messageConfig.DisableWebPagePreview = m.DisableWebPagePreview
245251
messageConfig.DisableNotification = m.DisableNotification
246252
if m.Keyboard != nil {
247-
messageConfig.ReplyMarkup = m.Keyboard
253+
messageConfig.ReplyMarkup = getTelegramInlineKeyboard(m.Keyboard)
248254
}
249255

250256
messageConfig.ParseMode = parseMode()
@@ -327,6 +333,31 @@ func (r tgWebhookResponder) sendOverHttps(ctx context.Context, chattable tgbotap
327333
return
328334
}
329335

336+
func getTelegramInlineKeyboard(keyboard botkb.Keyboard) *tgbotapi.InlineKeyboardMarkup {
337+
switch kb := keyboard.(type) {
338+
case *tgbotapi.InlineKeyboardMarkup:
339+
return kb
340+
case *botkb.MessageKeyboard:
341+
tgButtons := make([][]tgbotapi.InlineKeyboardButton, len(kb.Buttons))
342+
for i, buttons := range kb.Buttons {
343+
tgButtons[i] = make([]tgbotapi.InlineKeyboardButton, len(buttons))
344+
for j, button := range buttons {
345+
switch btn := button.(type) {
346+
case botkb.DataButton:
347+
tgButtons[i][j] = tgbotapi.NewInlineKeyboardButtonData(btn.Text, btn.Data)
348+
case botkb.UrlButton:
349+
tgButtons[i][j] = tgbotapi.NewInlineKeyboardButtonURL(btn.Text, btn.URL)
350+
default:
351+
panic(fmt.Sprintf("Unknown button type at [%d][%d]: %T", i, j, btn))
352+
}
353+
}
354+
}
355+
return tgbotapi.NewInlineKeyboardMarkup(tgButtons...)
356+
default:
357+
panic(fmt.Sprintf("m.Keyboard has unsupported type %v", kb.KeyboardType()))
358+
}
359+
}
360+
330361
func GetTelegramBotAPIClient(ctx context.Context, botContext botsfw.BotContext) *tgbotapi.BotAPI {
331362
return tgbotapi.NewBotAPIWithClient(
332363
botContext.BotSettings.Token,

0 commit comments

Comments
 (0)