I’ve set up a Telegram bot using @botfather and now I want to add custom commands that run from my server. I’ve created a BotHandler.php
file in my root directory to handle incoming messages:
$incomingMessage = json_decode(file_get_contents('php://input'), true);
$userId = $incomingMessage['message']['from']['id'];
$messageText = $incomingMessage['message']['text'];
$response = '';
if ($messageText === 'Hello') {
$response = 'Hey there!';
} elseif ($messageText === 'What\'s your name?') {
$response = 'I\'m ChatBot';
}
$botToken = 'your_bot_token_here';
$apiUrl = "https://api.telegram.org/bot{$botToken}/sendMessage";
$params = [
'chat_id' => $userId,
'text' => $response ?: 'I got your message!'
];
file_get_contents($apiUrl . '?' . http_build_query($params));
I’ve set the webhook using the Telegram API, and I got a success message. But when I try to use these commands in my Telegram chat with the bot, nothing happens. What am I missing? How can I make sure the commands are running from my server? Any help would be great!
Hey Ethan, I’ve been through this exact scenario with my Telegram bot. First off, double-check your webhook setup. Sometimes it looks good but isn’t quite right. Try sending a test message to your bot and see if anything shows up in your server logs.
One thing that tripped me up was server permissions. Make sure your PHP script has the right permissions to execute and write logs if needed. Also, confirm your server’s firewall isn’t blocking incoming requests from Telegram’s IPs.
For command handling, I’d suggest tweaking your code to specifically look for the ‘/’ at the start of messages. Something like:
if (strpos($messageText, ‘/’) === 0) {
$command = substr($messageText, 1);
switch ($command) {
case ‘start’:
$response = ‘Bot started!’;
break;
// more commands…
}
}
This approach worked wonders for me. Keep at it, and you’ll get those custom commands running smoothly!
yo dude, sounds like ur webhook might not be set up right. check if ur server’s actually gettin the messages from telegram. try addin some logging to ur php script to see if its running.
also, make sure ur server can be reached from the internet. if its behind a firewall or smthn, telegram cant hit it. test ur webhook url in a browser to see if it works.
hope this helps!
I encountered a similar issue when setting up my Telegram bot. One crucial step you might have missed is verifying that your webhook is actually receiving updates from Telegram. Try adding some logging to your PHP script to confirm it’s being executed when you send commands.
Also, make sure your server is publicly accessible. If it’s behind a firewall or on a local network, Telegram won’t be able to reach it. You can test this by trying to access your webhook URL from a browser or using a tool like curl.
For handling commands specifically, you’ll want to modify your script to check if the message starts with a ‘/’. Something like:
if (strpos($messageText, ‘/’) === 0) {
// Handle commands
$command = strtolower(substr($messageText, 1));
switch ($command) {
case ‘start’:
$response = ‘Welcome!’;
break;
// Add more commands here
}
}
This should help you get those custom commands working. Let me know if you need any more assistance!