telegram-bot-api/docs/examples/keyboard.md

64 lines
1.3 KiB
Markdown
Raw Permalink Normal View History

2020-07-27 00:22:16 +02:00
# Keyboard
This bot shows a numeric keyboard when you send a "open" message and hides it
when you send "close" message.
```go
package main
import (
2021-03-10 22:36:15 +01:00
"log"
"os"
2020-07-27 00:22:16 +02:00
2021-03-10 22:36:15 +01:00
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
2020-07-27 00:22:16 +02:00
)
var numericKeyboard = tgbotapi.NewReplyKeyboard(
2021-03-10 22:36:15 +01:00
tgbotapi.NewKeyboardButtonRow(
tgbotapi.NewKeyboardButton("1"),
tgbotapi.NewKeyboardButton("2"),
tgbotapi.NewKeyboardButton("3"),
),
tgbotapi.NewKeyboardButtonRow(
tgbotapi.NewKeyboardButton("4"),
tgbotapi.NewKeyboardButton("5"),
tgbotapi.NewKeyboardButton("6"),
),
2020-07-27 00:22:16 +02:00
)
func main() {
2021-03-10 22:36:15 +01:00
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_APITOKEN"))
if err != nil {
log.Panic(err)
}
2020-07-27 00:22:16 +02:00
2021-03-10 22:36:15 +01:00
bot.Debug = true
2020-07-27 00:22:16 +02:00
2021-03-10 22:36:15 +01:00
log.Printf("Authorized on account %s", bot.Self.UserName)
2020-07-27 00:22:16 +02:00
2021-03-10 22:36:15 +01:00
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
2020-07-27 00:22:16 +02:00
2021-03-10 22:36:15 +01:00
updates := bot.GetUpdatesChan(u)
2020-07-27 00:22:16 +02:00
2021-03-10 22:36:15 +01:00
for update := range updates {
if update.Message == nil { // ignore non-Message updates
continue
}
2020-07-27 00:22:16 +02:00
2021-03-10 22:36:15 +01:00
msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
2020-07-27 00:22:16 +02:00
2021-03-10 22:36:15 +01:00
switch update.Message.Text {
case "open":
msg.ReplyMarkup = numericKeyboard
case "close":
msg.ReplyMarkup = tgbotapi.NewRemoveKeyboard(true)
}
2020-07-27 00:22:16 +02:00
2021-03-10 22:36:15 +01:00
if _, err := bot.Send(msg); err != nil {
log.Panic(err)
}
}
2020-07-27 00:22:16 +02:00
}
```