【问题标题】:Telegram bot: How do I send message with inline keyboard and hide custom keyboard simultaneously?Telegram bot:如何使用内联键盘发送消息并同时隐藏自定义键盘?
【发布时间】:2016-10-17 21:06:58
【问题描述】:
  • 第 1 步:使用几个按钮向用户发送带有 ReplyKeyboardMarkup 的消息(例如 ["Yes", "No"]
  • 第 2 步:如果用户单击其中一个按钮(例如Yes"”),我想使用内嵌键盘显示一条消息并隐藏第 1 步发送的按钮。

有可能吗?该消息只有一个reply_markup 属性,可以是InlinkeKeyboardMarkupReplyKeyboardHide。我看到的唯一方法是发送 2 条消息(首先隐藏键盘,第二条使用内联键盘),但从用户体验的角度来看,这不是最佳解决方案。我可以做几个请求,但只想让用户看到 1 条消息。

有什么想法吗?

【问题讨论】:

    标签: telegram-bot


    【解决方案1】:

    现在是不可能的。 Telegram Bot API 目前只允许发送一种类型的键盘:内联或简单(包括 KeyboardHide 和其他)。

    【讨论】:

      【解决方案2】:

      但是你可以发送两条消息,第一条发送ReplyKeyboardHide/ReplyKeyboardRemove,第二条发送内联键盘

      【讨论】:

        【解决方案3】:

        您最好将 inlinekeyboard 用于“是/否”和您想要在按下“是”或“否”后显示的前一个键盘。这样,您可以编辑是/否内联键盘消息并显示新键盘。

        您可以发送 inlineKeyboard 并通过检查它的 callBackQuery.Data 参数,您可以再次编辑发送的消息并显示您的新消息。

        下面是一个示例更新消息 json:

          {"update_id":14603298,
          "callback_query": 
          {
            "id": "479899181065761766",
            "from": {
              "id": 111735238,
              "first_name": "eric",
              "username": "...."
            },
            "message": {
              "message_id": 22,
              "from": {
                "id": 3576731383,
                "first_name": "the_bot_name",
                "username": "mybot_bot"
              },
              "chat": {
                "id": 111745258,
                "first_name": "eric",
                "username": "....",
                "type": "private"
              },
              "date": 1492113810,
              "text": "sent message"
            },
            "chat_instance": "5419183837652256438",
            "data": "yes"
          }}
        

        因此,当用户单击是或否时,您将收到一条更新消息。基于上述更新消息,chatid 和 messageid 是已知的,因此使用 c# Telegram.Bot 库编辑代码如下:

            var chatid= 111745258;
            var messageid=22;        
            TelegramBotClient api = new TelegramBotClient("YOUR_TOKEN");
        
        var new_keyboard = new InlineKeyboardMarkup(
                        new[]
                             {
                              new[]
                                 {
                                  new InlineKeyboardButton("step_1","step1") ,
                                  },
                              new[]
                                  {
                                  new InlineKeyboardButton("step_2","step2"),
                                  new InlineKeyboardButton("step_3","step3"),
                                   },
                              new[]
                                   {
                                  new InlineKeyboardButton("step_4","step4"),
                                   }
                          });
            api.EditMessageReplyMarkupAsync(chatid, messageid, replyMarkup: new_keyboard);
        

        【讨论】:

          【解决方案4】:

          我猜你希望按钮在按下后消失:

          ReplyKeyboardMarkup MyButton = new ReplyKeyboardMarkup();
          MyButton.OneTimeKeyboard = true;
          

          您甚至可以通过添加以下内容使其更具响应性:

          MyButton.ResizeKeyboard = true;
          

          【讨论】:

            【解决方案5】:

            没有任何合乎逻辑的解决方案。但有一个窍门。您可以发送一条消息删除上一个键盘,然后删除这条消息,最后用它的键盘发送下一条消息。

            // send a fake message
            Message sentMsg = bot.SendTextMessageAsync(chatID, ".", replyKeyboardMarkup: new ReplyKeyboardRemove()).Result;
            
            // remove the fake message
            bot.DeleteMessageAsync(chatID, sentMsg.MessageId);
            
            // send the main message with it's keyboard
            bot.SendTextMessageAsync(chatID, "the next message", replyKeyboardMarkup: new ReplyKeyboardMarkup(keyboardData));
            

            【讨论】:

              【解决方案6】:

              只需将 OneTimeKeyboard 属性设置为 true,

              Button.OneTimeKeyboard = true;
              

              一旦按钮被使用,它就再也不会显示了

              【讨论】:

                猜你喜欢
                • 2016-10-20
                • 1970-01-01
                • 2016-11-24
                • 1970-01-01
                • 2019-08-14
                • 2018-03-31
                • 2015-09-14
                • 2016-04-26
                • 2020-03-01
                相关资源
                最近更新 更多