How to make ReplyKeyboardMarkup with telegram-bot-api Golang

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

How to make ReplyKeyboardMarkup with telegram-bot-api Golang

问题

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

itemsSel := [][]string{}
itemsSel = append(itemsSel, []string{item1.Caption, item2.Caption, item3.Caption})

msg := tgbotapi.NewMessage(update.Message.Chat.ID, "选择项目")
msg.ReplyMarkup = &tgbotapi.ReplyKeyboardMarkup{
    Keyboard:       itemsSel,
    ResizeKeyboard: true,
    OneTimeKeyboard: false,
    Selective:       false,
}

log.Println(msg.ReplyMarkup)

msg.ReplyToMessageID = update.Message.MessageID
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

itemsSel := [][]string{}
itemsSel = append(itemsSel, []string{item1.Caption, item2.Caption, item3.Caption})

msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Select item")
msg.ReplyMarkup = &tgbotapi.ReplyKeyboardMarkup{
    Keyboard:       itemsSel,
    ResizeKeyboard: true,
    OneTimeKeyboard: false,
    Selective:       false,
}

log.Println(msg.ReplyMarkup)

msg.ReplyToMessageID = update.Message.MessageID
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. 在代码中定义你的回复键盘,包括所有的行和按钮

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

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

    msg.ReplyMarkup = mainMenu
    
  4. 发送消息

    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

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

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

    msg.ReplyMarkup = mainMenu
    
  4. Send it

    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:

确定