Skip to content

Commit

Permalink
add example inline_keyboard_multiselect
Browse files Browse the repository at this point in the history
  • Loading branch information
negasus committed Apr 11, 2024
1 parent a8e8c13 commit f46cf7b
Showing 1 changed file with 102 additions and 0 deletions.
102 changes: 102 additions & 0 deletions examples/inline_keyboard_multiselect/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package main

import (
"context"
"fmt"
"os"
"os/signal"

"github.com/go-telegram/bot"
"github.com/go-telegram/bot/models"
)

// Send /select command to the bot to see the example in action.

var currentOptions = []bool{false, false, false}

func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()

opts := []bot.Option{
bot.WithMessageTextHandler("/select", bot.MatchTypeExact, commandHandler),
bot.WithCallbackQueryDataHandler("btn_", bot.MatchTypePrefix, callbackHandler),
}

b, err := bot.New(os.Getenv("EXAMPLE_TELEGRAM_BOT_TOKEN"), opts...)
if nil != err {
// panics for the sake of simplicity.
// you should handle this error properly in your code.
panic(err)
}

b.Start(ctx)
}

func callbackHandler(ctx context.Context, b *bot.Bot, update *models.Update) {
// answering callback query first to let Telegram know that we received the callback query,
// and we're handling it. Otherwise, Telegram might retry sending the update repetitively
// as it thinks the callback query doesn't reach to our application. learn more by
// reading the footnote of the https://core.telegram.org/bots/api#callbackquery type.
b.AnswerCallbackQuery(ctx, &bot.AnswerCallbackQueryParams{
CallbackQueryID: update.CallbackQuery.ID,
ShowAlert: false,
})

switch update.CallbackQuery.Data {
case "btn_opt1":
currentOptions[0] = !currentOptions[0]
case "btn_opt2":
currentOptions[1] = !currentOptions[1]
case "btn_opt3":
currentOptions[2] = !currentOptions[2]
case "btn_select":
b.DeleteMessage(ctx, &bot.DeleteMessageParams{
ChatID: update.CallbackQuery.Message.Message.Chat.ID,
MessageID: update.CallbackQuery.Message.Message.ID,
})
b.SendMessage(ctx, &bot.SendMessageParams{
ChatID: update.CallbackQuery.Message.Message.Chat.ID,
Text: fmt.Sprintf("Selected options: %v", currentOptions),
})
return
}

b.EditMessageReplyMarkup(ctx, &bot.EditMessageReplyMarkupParams{
ChatID: update.CallbackQuery.Message.Message.Chat.ID,
MessageID: update.CallbackQuery.Message.Message.ID,
ReplyMarkup: buildKeyboard(),
})
}

func buildKeyboard() models.ReplyMarkup {
kb := &models.InlineKeyboardMarkup{
InlineKeyboard: [][]models.InlineKeyboardButton{
{
{Text: buttonText("Option 1", currentOptions[0]), CallbackData: "btn_opt1"},
{Text: buttonText("Option 1", currentOptions[1]), CallbackData: "btn_opt2"},
{Text: buttonText("Option 1", currentOptions[2]), CallbackData: "btn_opt3"},
}, {
{Text: "Select", CallbackData: "btn_select"},
},
},
}

return kb
}

func buttonText(text string, opt bool) string {
if opt {
return "✅ " + text
}

return "❌ " + text
}

func commandHandler(ctx context.Context, b *bot.Bot, update *models.Update) {
b.SendMessage(ctx, &bot.SendMessageParams{
ChatID: update.Message.Chat.ID,
Text: "Select multiple options",
ReplyMarkup: buildKeyboard(),
})
}

0 comments on commit f46cf7b

Please sign in to comment.