1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TelegramBot; |
4
|
|
|
|
5
|
|
|
use TelegramBot\Entities\Update; |
6
|
|
|
use TelegramBot\Exception\InvalidBotTokenException; |
7
|
|
|
use TelegramBot\Interfaces\HandlerInterface; |
8
|
|
|
use TelegramBot\Traits\HandlerTrait; |
9
|
|
|
use TelegramBot\Util\Toolkit; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* UpdateHandler class |
13
|
|
|
* |
14
|
|
|
* @link https://github.com/telegram-bot-php/core |
15
|
|
|
* @author Shahrad Elahi (https://github.com/shahradelahi) |
16
|
|
|
* @license https://github.com/telegram-bot-php/core/blob/master/LICENSE (MIT License) |
17
|
|
|
*/ |
18
|
|
|
class UpdateHandler extends Telegram implements HandlerInterface |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
use HandlerTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var ?Update |
25
|
|
|
*/ |
26
|
|
|
protected ?Update $update; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Plugin[] |
30
|
|
|
*/ |
31
|
|
|
private array $plugins = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var bool |
35
|
|
|
*/ |
36
|
|
|
private bool $active_spreader = false; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The default configuration of the webhook. |
40
|
|
|
* |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
private array $config = [ |
44
|
|
|
'autoload_env_file' => false, |
45
|
|
|
'env_file_path' => null, |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Webhook constructor. |
50
|
|
|
* |
51
|
|
|
* @param string $api_token The API key of the bot. Leave it blank for autoload from .env file. |
52
|
|
|
*/ |
53
|
|
|
public function __construct(string $api_token = '') |
54
|
|
|
{ |
55
|
|
|
parent::__construct($api_token); |
56
|
|
|
|
57
|
|
|
if (!Telegram::validateToken(self::getApiToken())) { |
|
|
|
|
58
|
|
|
throw new InvalidBotTokenException(); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Add plugins to the receiver |
64
|
|
|
* |
65
|
|
|
* @param Plugin[]|array $plugins |
66
|
|
|
* @retrun void |
67
|
|
|
*/ |
68
|
|
|
public function addPlugins(Plugin|array $plugins): UpdateHandler |
69
|
|
|
{ |
70
|
|
|
if (is_object($plugins)) { |
|
|
|
|
71
|
|
|
$plugins = [$plugins]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
foreach ($plugins as $plugin) { |
75
|
|
|
if (!is_subclass_of($plugin, Plugin::class)) { |
76
|
|
|
throw new \RuntimeException( |
77
|
|
|
sprintf('The plugin %s must be an instance of %s', get_class($plugin), Plugin::class) |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$reflection = Toolkit::reflectionClass($plugin); |
|
|
|
|
82
|
|
|
$this->plugins[] = [ |
83
|
|
|
'class' => $plugin, |
84
|
|
|
'initialized' => is_object($plugin), |
85
|
|
|
]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Match the update with the given plugins |
93
|
|
|
* |
94
|
|
|
* @param Plugin[]|array $plugins |
95
|
|
|
* @return void |
96
|
|
|
*/ |
97
|
|
|
private function loadPlugins(array $plugins): void |
98
|
|
|
{ |
99
|
|
|
$update = $update ?? ($this->update ?? Telegram::getUpdate()); |
|
|
|
|
100
|
|
|
|
101
|
|
|
foreach ($plugins as $plugin) { |
102
|
|
|
if (!is_subclass_of($plugin['class'], Plugin::class)) { |
103
|
|
|
throw new \InvalidArgumentException(sprintf( |
104
|
|
|
'The plugin %s must be an instance of %s', |
105
|
|
|
get_class($plugin['class']), Plugin::class |
106
|
|
|
)); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if (!$update instanceof Update) { |
111
|
|
|
throw new \InvalidArgumentException(sprintf( |
112
|
|
|
'The update must be an instance of %s. %s given', |
113
|
|
|
Update::class, gettype($update) |
114
|
|
|
)); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$this->spreadUpdateWith($update, $plugins); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* This function will get update and spread it to the plugins |
122
|
|
|
* |
123
|
|
|
* @param Update $update |
124
|
|
|
* @param array<Plugin> $plugins |
125
|
|
|
* @return void |
126
|
|
|
*/ |
127
|
|
|
private function spreadUpdateWith(Update $update, array $plugins): void |
128
|
|
|
{ |
129
|
|
|
$this->active_spreader = true; |
130
|
|
|
|
131
|
|
|
foreach ($plugins as $plugin) { |
132
|
|
|
if ($plugin['initialized'] === false) { |
133
|
|
|
$plugin['class'] = new $plugin['class'](); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$plugin['class']->__execute($this, $update); |
137
|
|
|
if ($this->active_spreader === false) break; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$this->active_spreader = false; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Resolve the request. |
145
|
|
|
* |
146
|
|
|
* @param ?Update $update The custom to work with |
147
|
|
|
* @param array $config The configuration of the receiver |
148
|
|
|
* |
149
|
|
|
* @retrun void |
150
|
|
|
*/ |
151
|
|
|
public function resolve(Update $update = null, array $config = []): void |
152
|
|
|
{ |
153
|
|
|
if (!method_exists($this, '__process')) { |
154
|
|
|
throw new \RuntimeException('The method __process does not exist'); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
if (is_array($config)) $this->updateConfiguration($config); |
|
|
|
|
158
|
|
|
|
159
|
|
|
if (!empty($update)) $this->update = $update; |
160
|
|
|
else $this->update = Telegram::getUpdate() !== false ? Telegram::getUpdate() : null; |
|
|
|
|
161
|
|
|
|
162
|
|
|
if (empty($this->update)) { |
163
|
|
|
TelegramLog::error( |
164
|
|
|
'The update is empty, the request is not processed' |
165
|
|
|
); |
166
|
|
|
return; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
putenv('TG_CURRENT_UPDATE=' . $this->update->getRawData(false)); |
170
|
|
|
|
171
|
|
|
$this->__process($this->update); |
172
|
|
|
$this->loadPlugins($this->plugins); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Resolve the request on single plugin. |
177
|
|
|
* |
178
|
|
|
* @param Plugin $plugin The plugin to work with |
179
|
|
|
* @param ?Update $update The custom to work with |
180
|
|
|
* @param array $config The configuration of the receiver |
181
|
|
|
* @return void |
182
|
|
|
*/ |
183
|
|
|
public static function resolveOn(Plugin $plugin, Update $update = null, array $config = []): void |
|
|
|
|
184
|
|
|
{ |
185
|
|
|
// TODO: Implement resolveOn() method. |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Update the configuration |
190
|
|
|
* |
191
|
|
|
* @param array $configuration |
192
|
|
|
* @return void |
193
|
|
|
*/ |
194
|
|
|
public function updateConfiguration(array $configuration): void |
195
|
|
|
{ |
196
|
|
|
$this->config = array_merge($this->config, $configuration); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Stop the spreader process |
201
|
|
|
* |
202
|
|
|
* @return void |
203
|
|
|
*/ |
204
|
|
|
public function stop(): void |
205
|
|
|
{ |
206
|
|
|
$this->active_spreader = false; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
} |