【问题标题】:VB.Net Adding items to a Reply Keyboard for Telegram.botVB.Net 将项目添加到 Telegram.bot 的回复键盘
【发布时间】:2021-07-07 03:58:38
【问题描述】:

我正在使用 VB.Net (VS2019) 和 Telegram.bot API。在 C# 示例中,它们展示了如何将项目添加到回复键盘。恐怕我根本不理解 IEnumerable 的概念,但无论如何我已经设法将 C# 转换为可工作的 VB.Net 代码。

这是代码: C#

 if (update.Message.Text.Contains("/reply"))
    {
      var keyboard = new ReplyKeyboardMarkup
      {
       Keyboard = new KeyboardButton[][]{
                  new KeyboardButton[]{
                  new KeyboardButton("Button 1"), //column 1 row 1
                  new KeyboardButton("Button 2") //column 1 row 2
                },// column 1
                  new KeyboardButton[]{
                  new KeyboardButton("Button 3") //col 2 row 1
                } // column 2
       },
       ResizeKeyboard = true
    }; ;
   bot.SendMessage(update.Message.Chat.Id, "new keyboard", replyMarkup: keyboard);
}

VB代码是

If update.Message.Text.Contains("/reply") Then
    Dim keyboard = New ReplyKeyboardMarkup With {
        .Keyboard = New KeyboardButton()() {New KeyboardButton() {New KeyboardButton("Button 1"), New KeyboardButton("Button 2")}, New KeyboardButton() {New KeyboardButton("Button 3")}},
        .ResizeKeyboard = True
    }
    bot.SendMessage(update.Message.Chat.Id, "new keyboard", replyMarkup:=keyboard)
End If

我的问题是如何即时更新它? 我从 sqlite 读取表格,我想创建按钮来保存记录的 rowids.. 所以我有

 mySQL = "select * from products;"
 Dim cs As String = "URI=file:" & DBName
Using con As New SQLiteConnection(cs)
   con.Open()
   Dim dr As SQLiteDataReader
   Using DBcmd As New SQLiteCommand(con)
       DBcmd.CommandText = mySQL
       dr = DBcmd.ExecuteReader()
       While dr.Read()
           MsgText &= String.Format("Code: *{0}* [{1}]({3}), Price: ${2}", dr("Code"), Replace(dr("ProdName"), "-", " "), dr("Price"), dr("ProdURL")) & vbCrLf
       End While
    End Using
    con.Close()
End Using

在该循环中,我想构建键盘,为每次出现的dr("code") 添加一个按钮作为按钮的文本。

请帮忙?

【问题讨论】:

  • IEnumerable(Of T) 只不过是可以在每个元素上迭代的T 序列。任何可以在序列中提供元素的东西都满足接口。 (Linq 提供了一个函数式编程接口,用于操作IEnumerable 的实现所提供的序列)。

标签: c# vb.net telegram-bot ienumerable


【解决方案1】:

一种选择是创建List(Of KeyboardButton()),即KeyboardButton 数组的通用List。您可以循环读取数据读取器,为当前记录创建KeyboardButton 数组并将其添加到List。最后,在List 上调用ToArray 以获取KeyboardButton 的锯齿状数组,您可以将其分配给Keyboard 属性。例如

Dim keyboardButtons As New List(Of KeyboardButton())

While dr.Read()
    keyboard.Add({New KeyboardButton(dr.GetString(dr.GetOrdinal("Code")))})
End While

Dim keyboard As New ReplyKeyboardMarkup With {.Keyboard = keyboardButtons.ToArray()}

【讨论】:

  • 谢谢。这样可行。不得不改变一行:到keyboardButtons.Add({New KeyboardButton("/pd " & dr.GetString(dr.GetOrdinal("Code")))})
  • 你可以像这样让它更简单:keyboard.Add({New KeyboardButton($"/pd {dr("Code")}")}).
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-06
  • 1970-01-01
  • 2019-10-11
  • 1970-01-01
  • 1970-01-01
  • 2016-12-02
相关资源
最近更新 更多