How to make ReplyKeyboardMarkup with telegram-bot-api Golang

huangapple go评论138阅读模式
英文:

How to make ReplyKeyboardMarkup with telegram-bot-api Golang

问题

我找到的唯一信息是2015年的一个关闭的线程,其中涉及到库的问题。代码片段如下:

  1. itemsSel := [][]string{}
  2. itemsSel = append(itemsSel, []string{item1.Caption, item2.Caption, item3.Caption})
  3. msg := tgbotapi.NewMessage(update.Message.Chat.ID, "选择项目")
  4. msg.ReplyMarkup = &tgbotapi.ReplyKeyboardMarkup{
  5. Keyboard: itemsSel,
  6. ResizeKeyboard: true,
  7. OneTimeKeyboard: false,
  8. Selective: false,
  9. }
  10. log.Println(msg.ReplyMarkup)
  11. msg.ReplyToMessageID = update.Message.MessageID
  12. bot.Send(msg)

在https://core.telegram.org/bots/api#replykeyboardmarkup上有一些关于tgbotapi.ReplyKeyboardMarkup对象的描述,但是我不知道如何创建这个对象。帮帮我吧 :3

英文:

Only one information I found is closed thread from 2015 with problem in library. There was a piece of code

  1. itemsSel := [][]string{}
  2. itemsSel = append(itemsSel, []string{item1.Caption, item2.Caption, item3.Caption})
  3. msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Select item")
  4. msg.ReplyMarkup = &tgbotapi.ReplyKeyboardMarkup{
  5. Keyboard: itemsSel,
  6. ResizeKeyboard: true,
  7. OneTimeKeyboard: false,
  8. Selective: false,
  9. }
  10. log.Println(msg.ReplyMarkup)
  11. msg.ReplyToMessageID = update.Message.MessageID
  12. bot.Send(msg)

There is some description of tgbotapi.ReplyKeyboardMarkup object, but there is a matrix of KeyboardButton objects as it is written on https://core.telegram.org/bots/api#replykeyboardmarkup. How to make this object, I don't know. Help me :3

答案1

得分: 1

你好,这是我是如何做的:

  1. 在代码中定义你的回复键盘,包括所有的行和按钮

    1. var itemsSel = tgbotapi.NewReplyKeyboard(
    2. tgbotapi.NewKeyboardButtonRow(
    3. tgbotapi.NewKeyboardButton("按钮1"),
    4. tgbotapi.NewKeyboardButton("按钮2")),
    5. )
  2. 然后定义你的消息配置

    1. msg := tgbotapi.NewMessage(update.Message.Chat.ID, "主菜单")
  3. 将你的回复键盘分配给消息的回复标记,像这样

    1. msg.ReplyMarkup = mainMenu
  4. 发送消息

    1. bot.Send(msg)

我是从一个乌克兰的 YouTuber 那里学到的。这是视频链接:https://www.youtube.com/watch?v=yJkzK1-fLf4

英文:

Hello this is how I did it

  1. Define your reply keyboard with all its rows and buttons in rows

    1. var itemsSel = tgbotapi.NewReplyKeyboard(
    2. tgbotapi.NewKeyboardButtonRow(
    3. tgbotapi.NewKeyboardButton("button1"),
    4. tgbotapi.NewKeyboardButton("button2")),
    5. )
  2. then you define your message config

    1. msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Main Menu")
  3. assign your Reply Keyboard to the Message Reply Markup like this

    1. msg.ReplyMarkup = mainMenu
  4. Send it

    1. bot.Send(msg)

I learned all that from some Ukrainian youtuber
Here is the video https://www.youtube.com/watch?v=yJkzK1-fLf4

huangapple
  • 本文由 发表于 2022年8月25日 04:39:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/73479223.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定