all repos — telegram-bot-api @ 5198d8b69e2b5eddd09e44f320be9f05ce544ebd

Golang bindings for the Telegram Bot API

docs/examples/keyboard.md (view raw)

 1# Keyboard
 2
 3This bot shows a numeric keyboard when you send a "open" message and hides it
 4when you send "close" message.
 5
 6```go
 7package main
 8
 9import (
10	"log"
11	"os"
12
13	tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
14)
15
16var numericKeyboard = tgbotapi.NewReplyKeyboard(
17	tgbotapi.NewKeyboardButtonRow(
18		tgbotapi.NewKeyboardButton("1"),
19		tgbotapi.NewKeyboardButton("2"),
20		tgbotapi.NewKeyboardButton("3"),
21	),
22	tgbotapi.NewKeyboardButtonRow(
23		tgbotapi.NewKeyboardButton("4"),
24		tgbotapi.NewKeyboardButton("5"),
25		tgbotapi.NewKeyboardButton("6"),
26	),
27)
28
29func main() {
30	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_APITOKEN"))
31	if err != nil {
32		log.Panic(err)
33	}
34
35	bot.Debug = true
36
37	log.Printf("Authorized on account %s", bot.Self.UserName)
38
39	u := tgbotapi.NewUpdate(0)
40	u.Timeout = 60
41
42	updates := bot.GetUpdatesChan(u)
43
44	for update := range updates {
45		if update.Message == nil { // ignore non-Message updates
46			continue
47		}
48
49		msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
50
51		switch update.Message.Text {
52		case "open":
53			msg.ReplyMarkup = numericKeyboard
54		case "close":
55			msg.ReplyMarkup = tgbotapi.NewRemoveKeyboard(true)
56		}
57
58		if _, err := bot.Send(msg); err != nil {
59			log.Panic(err)
60		}
61	}
62}
63```