I’m new to making Telegram bots with PHP. I’ve got a bot that can handle commands, but I’m stuck on how to get regular text responses from users.
Here’s what I want to do:
- The bot asks ‘What’s your name?’ when the user sends /start
- The user types their name (not a command)
- The bot replies ‘Hello [name]!’
I can manage the /start command, but I’m unsure how to capture the user’s name input afterwards. Is there a way to listen for non-command messages in PHP Telegram Bot?
function initiateStart($msg) {
sendMsg($msg->getChat()->getId(), "What's your name?");
// How can I capture the next message here?
}
function greetUser($userName) {
// This function sends a greeting message
}
Any advice on how to handle non-command responses would be appreciated!
hey man, i know the struggle! i’ve been there too. what worked for me was using a simple flag in my code. like this:
$waitingForName = false;
// in your message handler
if ($waitingForName) {
greetUser($msg->getText());
$waitingForName = false;
} elseif ($msg->getText() == '/start') {
sendMsg($msg->getChat()->getId(), "What's your name?");
$waitingForName = true;
}
this way u can catch the name easily. hope it helps!
I’ve faced this exact challenge when developing my Telegram bot. The key is to use a state management system to track where each user is in the conversation flow. Here’s how I solved it:
- Store the user’s current state in a database or file.
- In your message handling function, check the user’s state before processing commands.
- If the state is ‘awaiting_name’, treat the incoming message as the name response.
Here’s a basic implementation:
function handleMessage($message) {
$userId = $message->getFrom()->getId();
$userState = getUserState($userId);
if ($userState === 'awaiting_name') {
$name = $message->getText();
greetUser($userId, $name);
setUserState($userId, 'idle');
} else {
// Handle commands or other states
}
}
function initiateStart($msg) {
$userId = $msg->getFrom()->getId();
sendMsg($userId, "What's your name?");
setUserState($userId, 'awaiting_name');
}
This approach has worked well for me in creating more complex conversation flows. Hope it helps!
Having worked on several Telegram bots, I can suggest an alternative approach using conversation handlers. This method is quite effective for managing multi-step interactions.
You can implement a ConversationHandler class that keeps track of the conversation state for each user. Here’s a basic outline:
class ConversationHandler {
private $conversations = [];
public function handleMessage($message) {
$userId = $message->getFrom()->getId();
$text = $message->getText();
if (!isset($this->conversations[$userId])) {
$this->conversations[$userId] = ['state' => 'start'];
}
switch ($this->conversations[$userId]['state']) {
case 'start':
sendMsg($userId, "What's your name?");
$this->conversations[$userId]['state'] = 'awaiting_name';
break;
case 'awaiting_name':
greetUser($userId, $text);
$this->conversations[$userId]['state'] = 'idle';
break;
}
}
}
This approach allows for more complex conversation flows and is easier to maintain as your bot grows.