【问题标题】:How to get the response from the keyboard input in telegram bot?如何从电报机器人中的键盘输入获得响应?
【发布时间】:2017-05-17 07:32:22
【问题描述】:

我想使用自定义键盘来获取选定的选项。

如何获得选中的选项?有例子吗?

“node-telegram-bot-api”回答了我的问题

这里:How to get the response of the keyboard selection?

c#有什么解决办法吗?

【问题讨论】:

  • 您使用哪个库来访问 Telegram Bot API?
  • 我使用电报.bot

标签: c# bots telegram-bot


【解决方案1】:

当您调用SendTextMessageAsync 时,您传递了一个指定“自定义回复键盘”的IReplyMarkup 对象。我对 Telegram Bot API 了解不多,但这看起来与您链接的 GitHub 问题所指的功能相同。

似乎有several implementations listed in the API documentation。我怀疑InlineKeyboardMarkupReplyKeyboardMarkup 是你要找的。​​p>

【讨论】:

  • 我正在寻找ReplyKeyboardMarkup
【解决方案2】:

要创建自定义键盘,您必须发送短信并传递IReplyMarkup。选定的选项作为消息发送,可以在OnMessage 事件中处理。当您将ReplyKeyboardHide 设置为回复标记时,您可以隐藏自定义键盘。

这是一个例子:

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;
    }
}

这是一个使用自定义键盘的聊天:

这是我点击第一个按钮的聊天:

我希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2018-03-23
    • 2016-03-22
    • 2020-10-08
    • 1970-01-01
    • 2017-04-11
    • 2022-10-05
    • 2018-09-25
    • 2018-06-10
    • 2017-12-10
    相关资源
    最近更新 更多