调出键盘和使用 /command 命令

image.png

  1. package main
  2. import (
  3. "log"
  4. tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
  5. )
  6. var numericKeyboard = tgbotapi.NewReplyKeyboard(
  7. tgbotapi.NewKeyboardButtonRow(
  8. tgbotapi.NewKeyboardButton("1"),
  9. tgbotapi.NewKeyboardButton("2"),
  10. tgbotapi.NewKeyboardButton("3"),
  11. ),
  12. tgbotapi.NewKeyboardButtonRow(
  13. tgbotapi.NewKeyboardButton("4"),
  14. tgbotapi.NewKeyboardButton("5"),
  15. tgbotapi.NewKeyboardButton("6"),
  16. ),
  17. )
  18. func main() {
  19. bot, err := tgbotapi.NewBotAPI("YourTokenForBot")
  20. if err != nil {
  21. log.Panic(err)
  22. }
  23. bot.Debug = true
  24. log.Printf("Authorized on account %s", bot.Self.UserName)
  25. u := tgbotapi.NewUpdate(0)
  26. u.Timeout = 60
  27. updates, err := bot.GetUpdatesChan(u)
  28. for update := range updates {
  29. if update.Message == nil { // ignore non-Message updates
  30. continue
  31. }
  32. msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
  33. switch update.Message.Text {
  34. case "open":
  35. msg.ReplyMarkup = numericKeyboard
  36. case "close":
  37. msg.ReplyMarkup = tgbotapi.NewRemoveKeyboard(true)
  38. }
  39. if _, err := bot.Send(msg); err != nil {
  40. log.Panic(err)
  41. }
  42. }
  43. }