Hey everyone! I’m working on a Telegram bot using PHP and I’ve added a ‘Contact Us’ button. Now I’m stuck trying to figure out how to capture the user’s message after they click this button. Does anyone have experience with this?
Here’s a quick example of what I’ve got so far:
$keyboard = [
['Contact Us']
];
$reply_markup = [
'keyboard' => $keyboard,
'resize_keyboard' => true,
'one_time_keyboard' => true
];
$telegramApi->sendMessage([
'chat_id' => $chatId,
'text' => 'How can we help?',
'reply_markup' => json_encode($reply_markup)
]);
But I’m not sure how to handle the user’s response after they click the button. Any tips or code snippets would be super helpful! Thanks in advance!
I’ve dealt with a similar situation in my Telegram bot project. After the user clicks ‘Contact Us’, you’ll want to set a flag in your database or session to indicate they’re in ‘contact mode’. Then, in your message handling function, check for this flag.
If it’s set, treat the next message as their contact query. You could do something like:
if ($message == 'Contact Us') {
setUserState($chatId, 'awaiting_contact');
// Send a message asking for their query
} elseif (getUserState($chatId) == 'awaiting_contact') {
// Process their message as a contact query
processContactQuery($chatId, $message);
setUserState($chatId, 'normal');
}
This approach allows you to handle the contact flow seamlessly. Remember to clear the state after processing to avoid issues with future messages. Hope this helps!
hey there! after the user hits ‘Contact Us’, you gotta set up a listener for their next msg. use a database or session to track their state. when they send somethin, check if theyre in ‘contact mode’ and handle it accordingly. dont forget to reset the state after!