Getting user contact and location data with Telegram Bot KeyboardButton parameters

I’m working on a Telegram bot and need to collect user phone numbers and location info. I’ve been looking at the KeyboardButton options but can’t get the contact and location request features working properly.

Here’s what I’ve tried so far:

$customKeyboard = array(
    'keyboard' => array(
        array(
            array(
                'text' => 'Share Phone',
                'request_contact' => true
            ),
            array(
                'text' => 'Share Location', 
                'request_location' => true
            )
        )
    ),
    'resize_keyboard' => true,
    'one_time_keyboard' => true
);

$keyboardJson = json_encode($customKeyboard);
$response = $bot->sendMessage($chatId, $messageText, null, false, null, $keyboardJson);
var_dump($response);

The buttons show up but they don’t seem to trigger the contact or location sharing. What’s the correct way to structure this keyboard markup?

Your keyboard structure looks right, but you’re probably passing the reply markup wrong. The sendMessage method wants the keyboard as the reply_markup parameter, not as a raw JSON string. Try this instead: ```php
$response = $bot->sendMessage([
‘chat_id’ => $chatId,
‘text’ => $messageText,
‘reply_markup’ => $customKeyboard
]);

If your bot wrapper needs JSON format specifically, double-check you're setting the `reply_markup` key correctly when building the request array. I ran into the same thing when I started with Telegram bots - parameter positioning gets weird depending on which PHP library you use. Also make sure your bot has the right permissions and users are actually tapping buttons instead of typing the button text.

You need to handle the incoming messages properly after users hit those buttons. When someone shares contact or location, Telegram doesn’t send regular text - it sends specific message types you’ve got to process differently. Contact sharing gives you a contact object with phone number and user details. Location sharing sends a location object with lat/long coordinates. Your message handler can’t just look for text - it needs to check for these specific types. I wasted hours debugging this exact problem because I wasn’t actually capturing the shared data on my webhook. Also double-check your bot token has the right permissions and test on mobile - these features are flaky on desktop.

Test this on your phone’s telegram app - contact/location buttons are broken on desktop. Double-check that your bot library is sending the reply_markup parameter right. Some wrappers screw up the json encoding. I had the same probelm because I was using the web version.