Skip to content

Commit d2c4f96

Browse files
committed
Add loop interval as parameter.
1 parent 0e53389 commit d2c4f96

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

src/BotManager.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ public function handleRequest(): self
250250
{
251251
if (empty($this->params->getBotParam('webhook'))) {
252252
if ($loop_time = $this->getLoopTime()) {
253-
$this->handleGetUpdatesLoop($loop_time);
253+
$this->handleGetUpdatesLoop($loop_time, $this->getLoopInterval());
254254
} else {
255255
$this->handleGetUpdates();
256256
}
@@ -281,14 +281,32 @@ public function getLoopTime(): int
281281
return max(0, (int)$loop_time);
282282
}
283283

284+
/**
285+
* Get the number of seconds the script should wait after each getUpdates request.
286+
*
287+
* @return int
288+
*/
289+
public function getLoopInterval(): int
290+
{
291+
$interval_time = $this->params->getScriptParam('i');
292+
293+
if (null === $interval_time || (is_string($interval_time) && '' === trim($interval_time))) {
294+
return 2;
295+
}
296+
297+
// Minimum interval is 1 second.
298+
return max(1, (int)$interval_time);
299+
}
300+
284301
/**
285302
* Loop the getUpdates method for the passed amount of seconds.
286303
*
287304
* @param int $loop_time_in_seconds
305+
* @param int $loop_interval_in_seconds
288306
*
289307
* @return \NPM\TelegramBotManager\BotManager
290308
*/
291-
public function handleGetUpdatesLoop(int $loop_time_in_seconds): self
309+
public function handleGetUpdatesLoop(int $loop_time_in_seconds, int $loop_interval_in_seconds = 2): self
292310
{
293311
// Remember the time we started this loop.
294312
$now = time();
@@ -299,7 +317,7 @@ public function handleGetUpdatesLoop(int $loop_time_in_seconds): self
299317
$this->handleGetUpdates();
300318

301319
// Chill a bit.
302-
sleep(2);
320+
sleep($loop_interval_in_seconds);
303321
}
304322

305323
return $this;

src/Params.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class Params
1919
's',
2020
'a',
2121
'l',
22+
'i',
2223
];
2324

2425
/**

tests/TelegramBotManager/Tests/BotManagerTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,36 @@ public function testGetLoopTime()
288288
self::assertSame(12345, (new BotManager(ParamsTest::$demo_vital_params))->getLoopTime());
289289
}
290290

291+
public function testGetLoopInterval()
292+
{
293+
// Parameter not set.
294+
self::assertSame(2, (new BotManager(ParamsTest::$demo_vital_params))->getLoopInterval());
295+
296+
$_GET = ['i' => ''];
297+
self::assertSame(2, (new BotManager(ParamsTest::$demo_vital_params))->getLoopInterval());
298+
299+
$_GET = ['i' => ' '];
300+
self::assertSame(2, (new BotManager(ParamsTest::$demo_vital_params))->getLoopInterval());
301+
302+
$_GET = ['i' => 'text-string'];
303+
self::assertSame(1, (new BotManager(ParamsTest::$demo_vital_params))->getLoopInterval());
304+
305+
$_GET = ['i' => 0];
306+
self::assertSame(1, (new BotManager(ParamsTest::$demo_vital_params))->getLoopInterval());
307+
308+
$_GET = ['i' => -12345];
309+
self::assertSame(1, (new BotManager(ParamsTest::$demo_vital_params))->getLoopInterval());
310+
311+
$_GET = ['i' => 12345];
312+
self::assertSame(12345, (new BotManager(ParamsTest::$demo_vital_params))->getLoopInterval());
313+
314+
$_GET = ['i' => '-12345'];
315+
self::assertSame(1, (new BotManager(ParamsTest::$demo_vital_params))->getLoopInterval());
316+
317+
$_GET = ['i' => '12345'];
318+
self::assertSame(12345, (new BotManager(ParamsTest::$demo_vital_params))->getLoopInterval());
319+
}
320+
291321
public function testSetBotExtras()
292322
{
293323
$extras = [

0 commit comments

Comments
 (0)