Homeyra Mahmoudi
Homeyra Mahmoudi

Reputation: 23

How i can request the user's location in inline keyboard within telegram?

hi everyone I'm kinda new in telegram bot coding and I'm building a Geodata collector bot. I want to retrieve the location of the user in a button using the inline keyboard. there is just request_location param that can be used in the keyboard, not in the inline keyboard. I want also to use a callback for one of my buttons to gets back to the previous menu and I wasn't able to find a correct callback query for the keyboard. can sb help me that how I would be able to request the location in an inline keyboard?

//my buttons for acquiring the location

bot.action("POS-PCdF", (ctx) => {
  ctx.deleteMessage();
  console.log(ctx.from);
  bot.telegram.sendMessage(ctx.chat.id, "Can we access your location?", {
    reply_markup: {
      keyboard: [
        [
          {
            text: "Access my location",
            request_location: true,
            callback_data: "Access_my_loc",
          },
          { text: "Cancel", callback_data: "PCdF" },
        ],
      ],
    },
  });
});

//the menu that I want to use in my callback

bot.action("PCdF", (ctx) => {
  ctx.deleteMessage();
  ctx.telegram.sendMessage(
    ctx.chat.id,
    "Share with us the problems about *******!",
    {
      reply_markup: {
        inline_keyboard: [
          [
            { text: "Pictures", callback_data: "PIC-PCdF" },
            { text: "Location", callback_data: "POS-PCdF" },
          ],
          [
            { text: "write to us", callback_data: "TEXT-PCdF" },
            { text: "Go back Home", callback_data: "go-back" },
          ],
        ],
      },
    }
  );
});

thanks in advance.

Upvotes: 2

Views: 4181

Answers (1)

Parsa Showkati
Parsa Showkati

Reputation: 98

As explained in the Official Telegram Bot API, InlineKeyboardMarkupButton should contain exactly one of the following optional feilds: callback_data, login_url, switch_inline_query, switch_inline_query_current_chat, callback_game, pay

This means that an inline keyboard cannot have a button requesting for user's location AKA request_location.

Also, there's no built in button or solution for returning to the last keyboard (whether it's a ReplyKeyboard or a InlineKeyboard). You have to implement a return button with a proper text (callback_data if it's an InlineKeyboardButton) specifically declared so that your code can recognize it and return to the last keyboard.

Upvotes: 2

Related Questions