Telegram bot 电报机器人:如何使用内嵌键盘和隐藏自定义键盘同时发送消息? 步骤1:向用户发送带有ReplyKeyboardMarkup的消息,只需几个按钮(例如[“是”、“否”]) 第2步:如果用户单击其中一个按钮(例如“Yes”),我想显示一条带有内联键盘的消息,并隐藏在第1步发送的按钮

Telegram bot 电报机器人:如何使用内嵌键盘和隐藏自定义键盘同时发送消息? 步骤1:向用户发送带有ReplyKeyboardMarkup的消息,只需几个按钮(例如[“是”、“否”]) 第2步:如果用户单击其中一个按钮(例如“Yes”),我想显示一条带有内联键盘的消息,并隐藏在第1步发送的按钮,telegram-bot,Telegram Bot,有可能吗?消息只有一个reply_markup属性,可以是InlinkeKeyboardMarkup或ReplyKeyboardHide。我看到的唯一方法是发送两条消息(第一条是隐藏键盘,第二条是内联键盘),但从用户体验的角度来看,这不是最好的解决方案。我可以做几个请求,但只想让用户看到一条消息 有什么想法吗?现在不可能。电报机器人API目前只允许发送一种类型的键盘:内联或简单(包括键盘隐藏和其他)。但您可以发送两条消息,第一个将发送ReplyKeyboardHide/ReplyKeyboard

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


有什么想法吗?

现在不可能。电报机器人API目前只允许发送一种类型的键盘:内联或简单(包括键盘隐藏和其他)。

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

您最好对是/否和按是或否后要显示的前一个键盘使用内联键盘。这样,您可以编辑是/否内联键盘消息并显示新键盘

// 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));
您可以通过检查inlineKeyboard的callBackQuery.Data参数发送inlineKeyboard,您可以再次编辑发送的消息并显示新消息

下面是更新消息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"
  }}
因此,当用户单击yes或no时,您将收到一条更新消息。根据上述更新消息,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;
一旦按钮被使用,它将不再显示