generated from yiisoft/package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetUpdatesCommand.php
More file actions
70 lines (61 loc) · 2.4 KB
/
GetUpdatesCommand.php
File metadata and controls
70 lines (61 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
declare(strict_types=1);
namespace Botasis\Runtime\Console;
use Botasis\Client\Telegram\Client\ClientInterface;
use Botasis\Client\Telegram\Request\TelegramRequest;
use Botasis\Runtime\Application;
use Botasis\Runtime\Update\Update;
use Botasis\Runtime\Update\UpdateFactory;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Throwable;
#[AsCommand(name: 'botasis:telegram:updates', description: 'Get updates from the bot and process them')]
final class GetUpdatesCommand extends Command
{
public function __construct(
private readonly ClientInterface $client,
private readonly Application $application,
private readonly UpdateFactory $updateFactory,
private readonly LoggerInterface $logger = new NullLogger(),
?string $name = null,
) {
parent::__construct($name);
}
protected function configure(): void
{
$this->addOption(
name: 'allowed-updates',
shortcut: 'u',
mode: InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
description: 'Allowed updates type. For a complete list of available update types see https://core.telegram.org/bots/api#update',
default: ['message', 'callback_query']
);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$update = null;
$data = ['allowed_updates' => $input->getOption('allowed-updates')];
$request = new TelegramRequest('getUpdates', $data);
/** @var array $update */
foreach ($this->client->send($request)['result'] ?? [] as $update) {
try {
$update = $this->updateFactory->create($update);
$this->application->handle($update);
} catch (Throwable $exception) {
// TODO Add an error handler? I.e. to send messages to Sentry.
$this->logger->error($exception);
}
}
/** @var Update|null $update */
if ($update !== null) {
$data['offset'] = $update->id->value + 1;
$this->client->send(new TelegramRequest('getUpdates', $data));
}
return 0;
}
}