-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add example inline_keyboard_multiselect
- Loading branch information
Showing
1 changed file
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
}) | ||
} |