Add PollingBot class to handle Telegram polling updates
This commit is contained in:
parent
374e3907c4
commit
da553c3bc9
1 changed files with 109 additions and 0 deletions
109
src/TgBotLib/PollingBot.php
Normal file
109
src/TgBotLib/PollingBot.php
Normal file
|
@ -0,0 +1,109 @@
|
|||
<?php
|
||||
|
||||
namespace TgBotLib;
|
||||
|
||||
use TgBotLib\Abstracts\UpdateEvent;
|
||||
use TgBotLib\Classes\Utilities;
|
||||
use TgBotLib\Enums\EventType;
|
||||
use TgBotLib\Objects\Update;
|
||||
|
||||
class PollingBot extends Bot
|
||||
{
|
||||
private ?int $offset;
|
||||
private int $limit;
|
||||
private int $timeout;
|
||||
private array $allowedUpdates;
|
||||
|
||||
/**
|
||||
* Constructor for the class, initializing with a Bot instance.
|
||||
*
|
||||
* @param string $token
|
||||
* @param string $endpoint
|
||||
*/
|
||||
public function __construct(string $token, string $endpoint='https://api.telegram.org')
|
||||
{
|
||||
parent::__construct($token, $endpoint);
|
||||
$this->offset = null;
|
||||
$this->limit = 100;
|
||||
$this->timeout = 0;
|
||||
$this->allowedUpdates = [];
|
||||
}
|
||||
|
||||
public function setOffset(int $offset): void
|
||||
{
|
||||
$this->offset = $offset;
|
||||
}
|
||||
|
||||
public function getOffset(): int
|
||||
{
|
||||
return $this->offset;
|
||||
}
|
||||
|
||||
public function setLimit(int $limit): void
|
||||
{
|
||||
$this->limit = $limit;
|
||||
}
|
||||
|
||||
public function getLimit(): int
|
||||
{
|
||||
return $this->limit;
|
||||
}
|
||||
|
||||
public function setTimeout(int $timeout): void
|
||||
{
|
||||
$this->timeout = $timeout;
|
||||
}
|
||||
|
||||
public function getTimeout(): int
|
||||
{
|
||||
return $this->timeout;
|
||||
}
|
||||
|
||||
public function setAllowedUpdates(array $allowedUpdates): void
|
||||
{
|
||||
$this->allowedUpdates = $allowedUpdates;
|
||||
}
|
||||
|
||||
public function getAllowedUpdates(): array
|
||||
{
|
||||
return $this->allowedUpdates;
|
||||
}
|
||||
|
||||
public function addAllowedUpdate(string $allowedUpdate): void
|
||||
{
|
||||
if(in_array($allowedUpdate, $this->allowedUpdates))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
$this->allowedUpdates[] = $allowedUpdate;
|
||||
}
|
||||
|
||||
public function handleUpdates(): void
|
||||
{
|
||||
foreach($this->getUpdates(offset: ($this->offset ?: 0), limit: $this->limit, timeout: $this->timeout, allowed_updates: $this->retrieveAllowedUpdates()) as $update)
|
||||
{
|
||||
// Update the last offset if the current Update ID is greater than the offset ID
|
||||
if($update->getUpdateId() > $this->offset)
|
||||
{
|
||||
$this->offset = $update->getUpdateId() + 1;
|
||||
}
|
||||
|
||||
/** @var UpdateEvent $eventHandler */
|
||||
foreach($this->getEventHandlersByType(Utilities::determineEventType($update)) as $eventHandler)
|
||||
{
|
||||
(new $eventHandler($update))->handle($this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function retrieveAllowedUpdates(): ?array
|
||||
{
|
||||
if(count($this->allowedUpdates) === 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->allowedUpdates;
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue