How to implement contact sharing button in Telegram bot?

I’m having trouble setting up a contact sharing feature in my Telegram bot. Here’s the PHP code I’m using:

$keyboard = [
    [
        ['text' => 'Share Contact', 'request_contact' => true]
    ]
];

$reply_markup = [
    'keyboard' => $keyboard,
    'one_time_keyboard' => true,
    'resize_keyboard' => true
];

$response = [
    'chat_id' => $chat_id,
    'text' => 'Would you like to share your phone number?',
    'reply_markup' => json_encode($reply_markup)
];

apiRequest('sendMessage', $response);

The button shows up but doesn’t trigger the contact sharing prompt when clicked. Any ideas what might be wrong? I’m pretty new to Telegram bot development so I might be missing something obvious. Thanks for any help!

I’ve encountered this issue before while working on Telegram bots. Although your code appears to be correct, the problem might lie elsewhere. It may be that your bot does not have the required permissions to request user data, which needs enabling via BotFather. It is also important to test on a mobile device because contact sharing often does not work on desktop clients. Additionally, make sure your apiRequest function is working as expected. You could also try adding ‘parse_mode’ => ‘HTML’ to the response array to see if that helps with formatting.

Your code structure looks correct, but there are a few things to consider. First, ensure you’re testing on a mobile device, as contact sharing typically doesn’t work on desktop Telegram clients. Also, double-check that your bot has the necessary permissions set through BotFather. Another potential issue could be with your apiRequest function - make sure it’s properly sending the data to Telegram’s API. If these don’t resolve the problem, try adding some error logging to pinpoint where the process is failing. Lastly, remember that some users might have privacy settings that prevent contact sharing, so always provide an alternative input method for phone numbers.

hey there! i’ve run into this before. make sure ur testing on mobile, cuz contact sharing can be wonky on desktop. also, double check ur bot permissions in BotFather. if that dont work, try adding some debug logs to see where its breaking. good luck with ur bot!