0

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)

Ameer
  • 1
  • 2

1 Answers1

1

You have a few issues here.

First, this outer loop will never do anything, because you set $i to zero and then run the loop only while $i is greater than zero... which it never is:

for ($i= 0; $i >0; $i++) {

Then you use the same variable for another loop inside that outer loop:

for ($i= 0; $i >0; $i++) {
    for ($i = 0; $i < $telegram->UpdateCount(); $i++) {

So you've got $i trying to do two different things at once. If you're using nested loops, you want to use a different variable for each loop:

for ($i = 0; $i < 100; $i++) {
    for ($j = 0; $j < 100; $j++) {

If you want to wrap your whole script in a never-ending loop, you can do that most simply with:

while (true) {
    // your code here
}

There's no need to run the import each time, so I would put this wrapper just before the getUpdates() call:

while (true) {
    $req = $telegram->getUpdates();
    for ($i = 0; $i < $telegram->UpdateCount(); $i++) {
        // ...
    }
}

Note this means your script will never exit. You'll have to hit control-c or kill its process. If you want the script to exit on some trigger, you can just break at the end of the loop:

while (true) {
    $req = $telegram->getUpdates();
    for ($i = 0; $i < $telegram->UpdateCount(); $i++) {
        // ...
    }
    if ($someCondition) {
        break;
    }
}
Alex Howansky
  • 50,515
  • 8
  • 78
  • 98
  • It really healed my code like a healing elixir. As for the while loop, if for some reason a function couldn't complete it's work like file_get_contents(), due to network issues. The code will still exit itself. – Ameer Jan 16 '23 at 18:28