I am using VB.Net (VS2019) and the Telegram.bot API. In the C# sample, they show how to add items to the replykeyboard. I'm afraid I don't understand the IEnumerable
concept at all, but I have managed to translate the C# to working VB.Net code anyway.
This is the code: 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);
}
The VB Code is
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
My question is how I can update that on the fly? I an reading a table from sqlite and I want to create the buttons to hold the rowids of the records.. So I have
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
In that loop I would like to build the keyboard adding one button for each occurrence of the dr("code")
as the text of the button.
Help please?
One option is to create a List(Of KeyboardButton())
, i.e. a generic List
of KeyboardButton
arrays. You can read from your data reader in a loop, create an array of KeyboardButton
for the current record and add it to the List
. Finally, call ToArray
on the List
to get a jagged array of KeyboardButton
that you can assign to the Keyboard
property. E.g.
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()}