I’m trying to create a Telegram bot that can ask users for their phone number and location. I found some info about KeyboardButton objects but I’m having trouble getting it to work. Here’s what I’ve tried so far:
The buttons show up but nothing happens when I tap them. Am I missing something? How can I properly set up these special request buttons? Any help would be great!
hey mate, ur code looks good but might be a webhook issue. make sure ur server’s set up to receive telegram updates. also, check ur bots privacy settings in botfather - sometimes they block contact/location sharing.
if ur still stuck, try printin out the raw update data to see whats comin through. good luck!
I’ve dealt with this exact issue before. Your code looks fine, but the problem might be in how you’re handling incoming messages. Make sure you’ve implemented a proper update handler to process the contact and location data when users share it.
Here’s a snippet that worked for me:
$update = json_decode(file_get_contents('php://input'), true);
if (isset($update['message']['contact'])) {
$phoneNumber = $update['message']['contact']['phone_number'];
// Process phone number
} elseif (isset($update['message']['location'])) {
$latitude = $update['message']['location']['latitude'];
$longitude = $update['message']['location']['longitude'];
// Process location
}
Also, double-check your bot’s privacy settings with BotFather. Sometimes these need to be adjusted to allow contact and location sharing. Hope this helps!
I’ve implemented similar functionality in my Telegram bots before. The code you’ve shared looks correct, but there might be an issue with how you’re handling the bot’s response. Make sure you’ve set up a webhook or are using long polling to receive updates from Telegram.
Also, check that your bot has the necessary permissions to request contact and location data. You can configure this through BotFather when setting up your bot.
If you’re still having trouble, try debugging by logging the incoming updates from Telegram. This can help you see if the button presses are being registered correctly.
Lastly, ensure you’re testing on a mobile device or Telegram desktop app, as some web versions of Telegram don’t fully support these features.