I'm trying to do long polling with reactphp.
I have a function getNotifications that stay in long polling waiting a response from Telegram.
This telegram api can hold the request open until timeout or
can send a response before the end of the 50 seconds if there is a notification.
How I can recall getNotifications after a response from Telegram?
Basically I want that getNotifications is called again when there is a response.
This is my code,
thank you all
<?php
require "vendor/autoload.php";
use Clue\React\Buzz\Browser;
use Psr\Http\Message\ResponseInterface;
use React\EventLoop\LoopInterface;
$loop = React\EventLoop\Factory::create();
$browser = new Browser($loop);
$method = "getUpdates";
$timeout = 50;
$params = [
"offset" => 550495530,
"limit" => 1000,
"timeout" => $timeout
];
$bot_key = "your bot token";
$query = http_build_query($params);
$url = "https://api.telegram.org/bot" . $bot_key . "/" . $method . "?" . $query;
$browser = $browser->withOptions(array(
'timeout' => $timeout
));
function callback(){
echo "done";
}
function getNotifications($url, $browser, LoopInterface $loop){
$browser->get($url)->then(function (ResponseInterface $response) {
// response received within 50 seconds. Telegram longpolling
var_dump((string)$response->getBody());
callback();
});
}
function timer($start, LoopInterface $loop)
{
//Timer only used to count seconds
$loop->addPeriodicTimer(1.0, function ($timer) use (&$start, $loop) {
echo "tick ". $start++ . "\n";
});
}
timer(0, $loop);
getNotifications($url, $browser, $loop);
$loop->run();