Am writing a simple telegram bot in php using getUpdates method for receiving messages from bot.
So after executing php bot.php
in terminal, it works and reply only once then the script auto exit.
bot.php
<?php
for ($i= 0; $i >0; $i++) {
/**
* Telegram Bot whitout WebHook.
* It uses getUpdates Telegram's API
*/
include 'Telegram.php'; //am using a php telegram bot Library
$bot_token = 'xyz';
$telegram = new Telegram($bot_token);
// Get all the new updates and set the new correct update_id
$req = $telegram->getUpdates();
for ($i = 0; $i < $telegram->UpdateCount(); $i++) {
// You NEED to call serveUpdate before accessing the values of message in Telegram Class
$telegram->serveUpdate($i);
$text = $telegram->Text();
$chat_id = $telegram->ChatID();
if ($text == '/start') {
$reply = 'Working';
$content = ['chat_id' => $chat_id, 'text' => $reply];
$telegram->sendMessage($content);
}
if ($text == '/test') {
if ($telegram->messageFromGroup()) {
$reply = 'Chat Group';
} else {
$reply = 'Private Chat';
}
if ($text == '/git') {
$reply = 'Check me on GitHub: https://github.com/Eleirbag89/TelegramBotPHP';
// Build the reply array
$content = ['chat_id' => $chat_id, 'text' => $reply];
$telegram->sendMessage($content);
}
}
}
I tried setting set_time_limit(0)
but it doesn't works, then I wrapped the entire code in a for loop. But it still exit after running once.
I want the script to remain running and reply all incoming messages, untill I exit script myself or exit terminal)