1 | <?php |
||
2 | |||
3 | namespace BPT\receiver; |
||
4 | |||
5 | use BPT\telegram\telegram; |
||
6 | use BPT\BPT; |
||
7 | use BPT\constants\loggerTypes; |
||
8 | use BPT\lock; |
||
9 | use BPT\logger; |
||
10 | use BPT\settings; |
||
11 | use BPT\types\update; |
||
12 | use JetBrains\PhpStorm\NoReturn; |
||
13 | |||
14 | /** |
||
15 | * getUpdates class , For receiving updates by polling methods |
||
16 | */ |
||
17 | class getUpdates extends receiver { |
||
18 | #[NoReturn] |
||
19 | public static function init () { |
||
20 | $last_update_id = self::loadData(); |
||
21 | lock::set('getUpdateHook'); |
||
22 | while(true) { |
||
23 | if (!lock::exist('getUpdateHook')) { |
||
24 | logger::write('getUpdateHook deleted, loop ended.',loggerTypes::INFO); |
||
25 | break; |
||
26 | } |
||
27 | $updates = telegram::getUpdates($last_update_id,allowed_updates: settings::$allowed_updates); |
||
28 | if (!telegram::$status) { |
||
29 | logger::write("There is some problem happened , telegram response : \n".json_encode($updates),loggerTypes::ERROR); |
||
30 | BPT::exit(print_r($updates,true)); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
31 | } |
||
32 | self::handleUpdates($updates); |
||
33 | $last_update_id = BPT::$update->update_id+1; |
||
34 | lock::save('getUpdate',$last_update_id); |
||
35 | } |
||
36 | } |
||
37 | |||
38 | private static function loadData(): bool|int|string { |
||
39 | if (lock::exist('getUpdate')) { |
||
40 | return lock::read('getUpdate'); |
||
41 | } |
||
42 | self::deleteOldLocks(); |
||
43 | telegram::deleteWebhook(); |
||
44 | lock::save('getUpdate',0); |
||
45 | return 0; |
||
46 | } |
||
47 | |||
48 | private static function deleteOldLocks() { |
||
49 | lock::deleteIfExist(['BPT-HOOK', 'BPT-MULTI-EXEC', 'BPT-MULTI-CURL']); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @param update[] $updates |
||
54 | */ |
||
55 | private static function handleUpdates(array $updates) { |
||
56 | foreach ($updates as $update) { |
||
57 | receiver::processUpdate($update); |
||
58 | } |
||
59 | } |
||
60 | } |