1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TelegramBot; |
4
|
|
|
|
5
|
|
|
use TelegramBot\Entities\Update; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Plugin |
9
|
|
|
* |
10
|
|
|
* @link https://github.com/telegram-bot-php/core |
11
|
|
|
* @author Shahrad Elahi (https://github.com/shahradelahi) |
12
|
|
|
* @license https://github.com/telegram-bot-php/core/blob/master/LICENSE (MIT License) |
13
|
|
|
*/ |
14
|
|
|
class Plugin |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The Update types |
19
|
|
|
* |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected array $update_types = [ |
23
|
|
|
'Message', |
24
|
|
|
'EditedMessage', |
25
|
|
|
'ChannelPost', |
26
|
|
|
'EditedChannelPost', |
27
|
|
|
'InlineQuery', |
28
|
|
|
'ChosenInlineResult', |
29
|
|
|
'CallbackQuery', |
30
|
|
|
'ShippingQuery', |
31
|
|
|
'PreCheckoutQuery', |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var WebhookHandler |
36
|
|
|
*/ |
37
|
|
|
protected WebhookHandler $hook; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var \Generator |
41
|
|
|
*/ |
42
|
|
|
protected \Generator $returns; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var bool |
46
|
|
|
*/ |
47
|
|
|
protected bool $kill_on_yield = false; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Check for the exit of the plugin. |
51
|
|
|
* |
52
|
|
|
* @param \Generator $return |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
|
|
public function __checkExit(\Generator $return): void |
56
|
|
|
{ |
57
|
|
|
if ($return->valid()) { |
58
|
|
|
if ($return->current() !== null && $this->kill_on_yield === true) { |
59
|
|
|
$this->kill(); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if ($return->valid()) { |
64
|
|
|
$return->next(); |
65
|
|
|
$this->__checkExit($return); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Identify the update type. e.g. Message, EditedMessage, etc. |
71
|
|
|
* |
72
|
|
|
* @param Update $update |
73
|
|
|
* @return string|null |
74
|
|
|
*/ |
75
|
|
|
public function __identify(Update $update): ?string |
76
|
|
|
{ |
77
|
|
|
$type = $update->getUpdateType(); |
78
|
|
|
|
79
|
|
|
if ($type === null) { |
80
|
|
|
return null; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return str_replace('_', '', ucwords($type, '_')); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Execute the plugin. |
88
|
|
|
* |
89
|
|
|
* @param WebhookHandler $receiver |
90
|
|
|
* @param Update $update |
91
|
|
|
* @return void |
92
|
|
|
*/ |
93
|
|
|
public function __execute(WebhookHandler $receiver, Update $update): void |
94
|
|
|
{ |
95
|
|
|
$this->hook = $receiver; |
96
|
|
|
|
97
|
|
|
if (method_exists($this, 'onReceivedUpdate')) { |
98
|
|
|
$return = $this->onReceivedUpdate($update); |
99
|
|
|
$this->__checkExit($return); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$type = $this->__identify($update); |
103
|
|
|
if (method_exists($this, ($method = 'on' . $type)) && $type !== null) { |
104
|
|
|
$return = $this->$method($update); |
105
|
|
|
$this->__checkExit($return); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Kill the plugin. |
111
|
|
|
* |
112
|
|
|
* @return void |
113
|
|
|
*/ |
114
|
|
|
public function kill(): void |
115
|
|
|
{ |
116
|
|
|
$this->hook->kill(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
} |