|
5 | 5 |
|
6 | 6 |
|
7 | 7 | use Illuminate\Console\Command; |
| 8 | +use Longman\TelegramBot\Entities\Update; |
8 | 9 | use Longman\TelegramBot\Exception\TelegramException; |
9 | 10 | use Longman\TelegramBot\Telegram; |
10 | 11 | use Symfony\Component\Console\Command\SignalableCommandInterface; |
11 | 12 |
|
12 | 13 | class TelegramFetchCommand extends Command implements SignalableCommandInterface |
13 | 14 | { |
14 | 15 |
|
15 | | - protected $signature = 'telegram:fetch'; |
| 16 | + protected $signature = 'telegram:fetch |
| 17 | + {--a|all-update-types : Explicitly allow all updates (including "chat_member")} |
| 18 | + {--allowed-updates= : Define allowed updates (comma-seperated)}'; |
16 | 19 |
|
17 | 20 | protected $description = 'Fetches Telegram updates periodically'; |
18 | 21 |
|
19 | 22 | protected bool $shallExit = false; |
20 | 23 |
|
| 24 | + protected ?int $childPid = null; |
| 25 | + |
21 | 26 | public function handle(Telegram $bot) |
22 | 27 | { |
23 | 28 | $this->callSilent('telegram:delete-webhook'); |
24 | 29 |
|
25 | 30 | $options = [ |
26 | | - 'timeout' => 5 |
| 31 | + 'timeout' => 30 |
27 | 32 | ]; |
28 | 33 |
|
29 | | - $this->info("Start fetching updates...\n<comment>(Exit with Ctrl + C. This can take a few seconds.)</comment>"); |
30 | | - while (true) { |
31 | | - if ($this->shallExit) { |
32 | | - break; |
| 34 | + // allowed_updates |
| 35 | + if ($this->option('all-update-types')) { |
| 36 | + $options['allowed_updates'] = Update::getUpdateTypes(); |
| 37 | + } elseif ($allowedUpdates = $this->option('allowed-updates')) { |
| 38 | + $options['allowed_updates'] = str($allowedUpdates)->explode(','); |
| 39 | + } |
| 40 | + |
| 41 | + $this->info("Start fetching updates...\n<comment>(Exit with Ctrl + C.)</comment>"); |
| 42 | + |
| 43 | + if ($this->childPid = pcntl_fork()) { |
| 44 | + // Parent process |
| 45 | + |
| 46 | + while (true) { |
| 47 | + |
| 48 | + if ($this->shallExit) { |
| 49 | + exec('kill -9 ' . $this->childPid); |
| 50 | + break; |
| 51 | + } |
| 52 | + |
33 | 53 | } |
34 | 54 |
|
35 | | - try { |
36 | | - $bot->handleGetUpdates($options); |
37 | | - } catch (TelegramException $e) { |
38 | | - // Only print message |
39 | | - $this->error($e->getMessage()); |
| 55 | + } else { |
| 56 | + // Child process |
| 57 | + |
| 58 | + while (true) { |
| 59 | + |
| 60 | + $response = rescue(fn() => $bot->handleGetUpdates($options)); |
| 61 | + |
| 62 | + if ($response !== null && ! $response->isOk()) { |
| 63 | + $this->error($response->getDescription()); |
| 64 | + } |
| 65 | + |
40 | 66 | } |
| 67 | + |
41 | 68 | } |
42 | 69 | } |
43 | 70 |
|
|
0 commit comments