1
bot/examples/inline_keyboard/main.go
2024-02-27 11:10:23 +03:00

66 lines
1.8 KiB
Go

package main
import (
"context"
"os"
"os/signal"
"github.com/go-telegram/bot"
"github.com/go-telegram/bot/models"
)
// Send any text message to the bot after the bot has been started
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
opts := []bot.Option{
bot.WithDefaultHandler(defaultHandler),
bot.WithCallbackQueryDataHandler("button", 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,
})
b.SendMessage(ctx, &bot.SendMessageParams{
ChatID: update.CallbackQuery.Message.Message.Chat.ID,
Text: "You selected the button: " + update.CallbackQuery.Data,
})
}
func defaultHandler(ctx context.Context, b *bot.Bot, update *models.Update) {
kb := &models.InlineKeyboardMarkup{
InlineKeyboard: [][]models.InlineKeyboardButton{
{
{Text: "Button 1", CallbackData: "button_1"},
{Text: "Button 2", CallbackData: "button_2"},
}, {
{Text: "Button 3", CallbackData: "button_3"},
},
},
}
b.SendMessage(ctx, &bot.SendMessageParams{
ChatID: update.Message.Chat.ID,
Text: "Click by button",
ReplyMarkup: kb,
})
}