Alexander Trakhimenok
Alexander Trakhimenok

Reputation: 6268

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

Is it possible to do? The message has just single reply_markup property and can be either InlinkeKeyboardMarkup or ReplyKeyboardHide. The only way to do that I see is to send 2 messages (first to hide keyboard and 2nd with inline keyboard) but that would not be best solution from user experience point of view. I'm OK to do couple of request but want to have just 1 message visible to user.

Any thoughts?

Upvotes: 13

Views: 38061

Answers (6)

user8876021
user8876021

Reputation:

Just set the OneTimeKeyboard Property to true,

Button.OneTimeKeyboard = true;

Once button is used it's never showing again

Upvotes: 2

Emad Armoun
Emad Armoun

Reputation: 2099

There isn't any logical solution. but there's a trick. you can send a message to remove the previous keyboard, then remove this message, finally send the next message with it's keyboard.

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

Upvotes: 6

Mayer Spitz
Mayer Spitz

Reputation: 2525

I guess you want the button should disappear once it's pressed:

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

You can even make it more responsive by adding this:

MyButton.ResizeKeyboard = true;

Upvotes: 3

user2228544
user2228544

Reputation: 31

you better use inlinekeyboard for both yes/no and the preceding keyboard which you want to show after pressing yes or no. that way, you can edit the yes/no inlinekeyboard message and show new keyboard.

you can send the inlineKeyboard and by checking it's callBackQuery.Data parameter you can edit the sent message again and show your new message instead.

below is a sample update message 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"
  }}

so, when the user clicks on yes or no, you will get an update message. based on above update message, the chatid and messageid are known so using the c# Telegram.Bot library the edit code is like:

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

Upvotes: 1

Kirill
Kirill

Reputation: 131

But you can send two messages, the first one would send ReplyKeyboardHide/ReplyKeyboardRemove and the second would send the inline keyboard

Upvotes: 3

ihoru
ihoru

Reputation: 1980

It's impossible right now. Telegram Bot API currently allows sending only one type of keyboard: inline or simple (including KeyboardHide and other).

Upvotes: 10

Related Questions