如何从电报bot中的键盘输入获取响应? [英] How to get the response from the keyboard input in telegram bot?
问题描述
我想使用自定义键盘来获取选定的选项.
I want to use custom keyboard to get the selected option.
如何获取选定的选项?有例子吗?
How to get the selected option ? Is there any example?
"node-telegram-bot-api"回答了我的问题
my question is answered by "node-telegram-bot-api"
此处:如何获取键盘选择的响应?
C#有什么解决方案吗?
Is there any solution for c#?
推荐答案
要创建自定义键盘,您必须发送短信并传递IReplyMarkup
.所选选项作为消息发送,可以在OnMessage
事件中处理.将ReplyKeyboardHide
设置为回复标记时,可以隐藏自定义键盘.
To create a custom keyboard you have to sent a text message and pass a IReplyMarkup
. The selected option is sent as a message which can be handled in the OnMessage
event. You can hide the custom keyboard when you set a ReplyKeyboardHide
as reply markup.
这里是一个示例:
private const string FirstOptionText = "First option";
private const string SecondOptionText = "Second option";
private async void BotClientOnMessage(object sender, MessageEventArgs e)
{
switch (e.Message.Text)
{
case FirstOptionText:
await BotClient.SendTextMessageAsync(e.Message.Chat.Id, "You chose the first option", replyMarkup:new ReplyKeyboardHide());
break;
case SecondOptionText:
await BotClient.SendTextMessageAsync(e.Message.Chat.Id, "You chose the second option", replyMarkup:new ReplyKeyboardHide());
break;
default:
await BotClient.SendTextMessageAsync(e.Message.Chat.Id, "Hi, select an option!",
replyMarkup: new ReplyKeyboardMarkup(new[]
{
new KeyboardButton(FirstOptionText),
new KeyboardButton(SecondOptionText),
}));
break;
}
}
以下是使用自定义键盘进行的聊天:
Here is a chat with a custom keyboard:
这是我单击第一个按钮的聊天室:
Here is a chat where I clicked the first button:
我希望这会有所帮助!
这篇关于如何从电报bot中的键盘输入获取响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!