Telegram bot:如何使用内联键盘发送消息并同时隐藏自定义键盘?

Telegram bot: How do I send message with inline keyboard and hide custom keyboard simultaneously?

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

有什么想法吗?

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

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

你最好对 yes/no 和你想在按是或否后显示的前面的键盘使用内联键盘。这样,您就可以编辑 yes/no 内联键盘消息并显示新键盘。

您可以发送 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);

我想您希望按钮在按下后消失:

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

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

MyButton.ResizeKeyboard = true;

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

// 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));

只需将 OneTimeKeyboard 属性 设置为 true,

Button.OneTimeKeyboard = true;

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