@@ -35,49 +35,49 @@ |
||
35 | 35 | class InlineKeyboardButton extends KeyboardButton |
36 | 36 | { |
37 | 37 | |
38 | - /** |
|
39 | - * Creates instance of InlineKeyboardButton |
|
40 | - * |
|
41 | - * @param string $string |
|
42 | - * @return InlineKeyboardButton |
|
43 | - */ |
|
44 | - public static function make(string $string): InlineKeyboardButton |
|
45 | - { |
|
46 | - return new self($string); |
|
47 | - } |
|
38 | + /** |
|
39 | + * Creates instance of InlineKeyboardButton |
|
40 | + * |
|
41 | + * @param string $string |
|
42 | + * @return InlineKeyboardButton |
|
43 | + */ |
|
44 | + public static function make(string $string): InlineKeyboardButton |
|
45 | + { |
|
46 | + return new self($string); |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * @param string $data |
|
51 | - * @return $this |
|
52 | - */ |
|
53 | - public function CallbackData(string $data): InlineKeyboardButton |
|
54 | - { |
|
55 | - $this->raw_data['callback_data'] = $data; |
|
49 | + /** |
|
50 | + * @param string $data |
|
51 | + * @return $this |
|
52 | + */ |
|
53 | + public function CallbackData(string $data): InlineKeyboardButton |
|
54 | + { |
|
55 | + $this->raw_data['callback_data'] = $data; |
|
56 | 56 | |
57 | - return $this; |
|
58 | - } |
|
57 | + return $this; |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * @param string $data |
|
62 | - * @return $this |
|
63 | - */ |
|
64 | - public function Url(string $data): InlineKeyboardButton |
|
65 | - { |
|
66 | - $this->raw_data['url'] = $data; |
|
60 | + /** |
|
61 | + * @param string $data |
|
62 | + * @return $this |
|
63 | + */ |
|
64 | + public function Url(string $data): InlineKeyboardButton |
|
65 | + { |
|
66 | + $this->raw_data['url'] = $data; |
|
67 | 67 | |
68 | - return $this; |
|
69 | - } |
|
68 | + return $this; |
|
69 | + } |
|
70 | 70 | |
71 | - /** |
|
72 | - * @param string $url |
|
73 | - * @return $this |
|
74 | - */ |
|
75 | - public function WebApp(string $url): KeyboardButton |
|
76 | - { |
|
77 | - $this->raw_data['web_app'] = new WebAppInfo(['url' => $url]); |
|
71 | + /** |
|
72 | + * @param string $url |
|
73 | + * @return $this |
|
74 | + */ |
|
75 | + public function WebApp(string $url): KeyboardButton |
|
76 | + { |
|
77 | + $this->raw_data['web_app'] = new WebAppInfo(['url' => $url]); |
|
78 | 78 | |
79 | - return $this; |
|
80 | - } |
|
79 | + return $this; |
|
80 | + } |
|
81 | 81 | |
82 | 82 | /** |
83 | 83 | * Check if the passed data array could be an InlineKeyboardButton. |
@@ -12,37 +12,37 @@ |
||
12 | 12 | class CrossData |
13 | 13 | { |
14 | 14 | |
15 | - /** |
|
16 | - * get CrossData |
|
17 | - * |
|
18 | - * @param string $key |
|
19 | - * @return string|array|bool|null |
|
20 | - */ |
|
21 | - public static function get(string $key): string|array|bool|null |
|
22 | - { |
|
23 | - $data = json_decode(getenv('TG_CROSS_DATA'), true); |
|
24 | - $block = 'TG_CROSS_DATA' . $key; |
|
15 | + /** |
|
16 | + * get CrossData |
|
17 | + * |
|
18 | + * @param string $key |
|
19 | + * @return string|array|bool|null |
|
20 | + */ |
|
21 | + public static function get(string $key): string|array|bool|null |
|
22 | + { |
|
23 | + $data = json_decode(getenv('TG_CROSS_DATA'), true); |
|
24 | + $block = 'TG_CROSS_DATA' . $key; |
|
25 | 25 | |
26 | - if (!isset($data[$key])) { |
|
27 | - return null; |
|
28 | - } |
|
26 | + if (!isset($data[$key])) { |
|
27 | + return null; |
|
28 | + } |
|
29 | 29 | |
30 | - return getenv($data[$block]); |
|
31 | - } |
|
30 | + return getenv($data[$block]); |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * put CrossData |
|
35 | - * |
|
36 | - * @param string $key |
|
37 | - * @param mixed $value |
|
38 | - * @return void |
|
39 | - */ |
|
40 | - public static function put(string $key, mixed $value): void |
|
41 | - { |
|
42 | - $data = json_decode(getenv('TG_CROSS_DATA'), true); |
|
43 | - $data[$key] = 'TG_CROSS_DATA_' . $key; |
|
44 | - putenv($data[$key] . '=' . $value); |
|
45 | - putenv(json_encode($data)); |
|
46 | - } |
|
33 | + /** |
|
34 | + * put CrossData |
|
35 | + * |
|
36 | + * @param string $key |
|
37 | + * @param mixed $value |
|
38 | + * @return void |
|
39 | + */ |
|
40 | + public static function put(string $key, mixed $value): void |
|
41 | + { |
|
42 | + $data = json_decode(getenv('TG_CROSS_DATA'), true); |
|
43 | + $data[$key] = 'TG_CROSS_DATA_' . $key; |
|
44 | + putenv($data[$key] . '=' . $value); |
|
45 | + putenv(json_encode($data)); |
|
46 | + } |
|
47 | 47 | |
48 | 48 | } |
49 | 49 | \ No newline at end of file |
@@ -18,245 +18,245 @@ |
||
18 | 18 | abstract class WebhookHandler extends Telegram |
19 | 19 | { |
20 | 20 | |
21 | - /** |
|
22 | - * @var ?Update |
|
23 | - */ |
|
24 | - protected ?Update $update; |
|
25 | - |
|
26 | - /** |
|
27 | - * @var array<Plugin> |
|
28 | - */ |
|
29 | - private array $plugins = []; |
|
30 | - |
|
31 | - /** |
|
32 | - * @var bool |
|
33 | - */ |
|
34 | - private bool $isActive = false; |
|
35 | - |
|
36 | - /** |
|
37 | - * The default configuration of the webhook. |
|
38 | - * |
|
39 | - * @var array |
|
40 | - */ |
|
41 | - private array $config = [ |
|
42 | - 'autoload_env_file' => false, |
|
43 | - 'env_file_path' => null, |
|
44 | - ]; |
|
45 | - |
|
46 | - /** |
|
47 | - * Webhook constructor. |
|
48 | - * |
|
49 | - * @param string $api_key The API key of the bot. Leave it blank for autoload from .env file. |
|
50 | - */ |
|
51 | - public function __construct(string $api_key = '') |
|
52 | - { |
|
53 | - parent::__construct($api_key); |
|
54 | - |
|
55 | - if (!Telegram::validateToken(self::getApiKey())) { |
|
56 | - throw new InvalidBotTokenException(); |
|
57 | - } |
|
58 | - } |
|
59 | - |
|
60 | - /** |
|
61 | - * Initialize the receiver. |
|
62 | - * |
|
63 | - * @return void |
|
64 | - */ |
|
65 | - public function init(): void |
|
66 | - { |
|
67 | - $this->config['env_file_path'] = $_SERVER['DOCUMENT_ROOT'] . '/.env'; |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Add a plugin to the receiver |
|
72 | - * |
|
73 | - * @param array<Plugin> $plugins |
|
74 | - */ |
|
75 | - public function addPlugin(array $plugins): void |
|
76 | - { |
|
77 | - foreach ($plugins as $plugin) { |
|
78 | - if (!is_subclass_of($plugin, Plugin::class)) { |
|
79 | - throw new \RuntimeException( |
|
80 | - sprintf('The plugin %s must be an instance of %s', get_class($plugin), Plugin::class) |
|
81 | - ); |
|
82 | - } |
|
83 | - $this->plugins[] = $plugin; |
|
84 | - } |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * Match the update with the given plugins |
|
89 | - * |
|
90 | - * @param array<Plugin> $plugins |
|
91 | - * @param ?Update $update The update to match |
|
92 | - * @return void |
|
93 | - */ |
|
94 | - public function loadPluginsWith(array $plugins, Update $update = null): void |
|
95 | - { |
|
96 | - $update = $update ?? ($this->update ?? Telegram::getUpdate()); |
|
97 | - |
|
98 | - foreach ($plugins as $plugin) { |
|
99 | - if (!is_subclass_of($plugin, Plugin::class)) { |
|
100 | - throw new \InvalidArgumentException( |
|
101 | - sprintf('The plugin %s must be an instance of %s', get_class($plugin), Plugin::class) |
|
102 | - ); |
|
103 | - } |
|
104 | - } |
|
105 | - |
|
106 | - if ($update instanceof Update) { |
|
107 | - $this->spreadUpdateWith($update, $plugins); |
|
108 | - } |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * Match the message to the plugins |
|
113 | - * |
|
114 | - * @param ?Update $update The update to match |
|
115 | - * @return void |
|
116 | - */ |
|
117 | - public function loadPlugins(Update $update = null): void |
|
118 | - { |
|
119 | - $update = $update ?? ($this->update ?? Telegram::getUpdate()); |
|
120 | - $this->loadPluginsWith($this->plugins, $update); |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * Load the environment's |
|
125 | - * |
|
126 | - * @param string $path |
|
127 | - * @retrun void |
|
128 | - */ |
|
129 | - public function loadFileOfEnv(string $path): void |
|
130 | - { |
|
131 | - DotEnv::load($path); |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * Update the configuration |
|
136 | - * |
|
137 | - * @param array $configuration |
|
138 | - * @return void |
|
139 | - */ |
|
140 | - public function updateConfiguration(array $configuration): void |
|
141 | - { |
|
142 | - $this->config = array_merge($this->config, $configuration); |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * Resolve the request. |
|
147 | - * |
|
148 | - * @param ?Update $update The custom to work with |
|
149 | - * @param array $config The configuration of the receiver |
|
150 | - * |
|
151 | - * @retrun void |
|
152 | - */ |
|
153 | - public function resolve(Update $update = null, array $config = []): void |
|
154 | - { |
|
155 | - $method = '__process'; |
|
156 | - if (!method_exists($this, $method)) { |
|
157 | - throw new \RuntimeException(sprintf('The method %s does not exist', $method)); |
|
158 | - } |
|
159 | - |
|
160 | - if (is_array($config)) $this->updateConfiguration($config); |
|
161 | - |
|
162 | - if (!empty($update)) $this->update = $update; |
|
163 | - else $this->update = Telegram::getUpdate() !== false ? Telegram::getUpdate() : null; |
|
164 | - |
|
165 | - if (empty($this->update)) { |
|
166 | - TelegramLog::error( |
|
167 | - 'The update is empty, the request is not processed' |
|
168 | - ); |
|
169 | - return; |
|
170 | - } |
|
171 | - |
|
172 | - try { |
|
173 | - |
|
174 | - Common::arrest($this, $method, $this->update); |
|
175 | - |
|
176 | - } catch (\RuntimeException $exception) { |
|
177 | - |
|
178 | - $e = $exception->getPrevious(); |
|
179 | - TelegramLog::error(($message = sprintf( |
|
180 | - '%s(%d): %s\n%s', $e->getFile(), $e->getLine(), $e->getMessage(), $e->getTraceAsString() |
|
181 | - ))); |
|
182 | - |
|
183 | - if (defined('TG_ADMIN_ID') && TG_ADMIN_ID && defined('DEBUG_MODE') && DEBUG_MODE) { |
|
184 | - file_put_contents( |
|
185 | - ($file = getcwd() . '/' . uniqid('error_') . '.log'), |
|
186 | - $message . PHP_EOL . PHP_EOL . $update->getRawData(false) |
|
187 | - ); |
|
188 | - Request::sendMessage([ |
|
189 | - 'chat_id' => TG_ADMIN_ID, |
|
190 | - 'text' => $message, |
|
191 | - ]); |
|
192 | - Request::sendDocument([ |
|
193 | - 'chat_id' => TG_ADMIN_ID, |
|
194 | - 'document' => $file, |
|
195 | - ]); |
|
196 | - unlink($file); |
|
197 | - } |
|
198 | - } |
|
199 | - } |
|
200 | - |
|
201 | - /** |
|
202 | - * This function will get update and spread it to the plugins |
|
203 | - * |
|
204 | - * @param Update $update |
|
205 | - * @param array<Plugin> $plugins |
|
206 | - * @return void |
|
207 | - */ |
|
208 | - private function spreadUpdateWith(Update $update, array $plugins): void |
|
209 | - { |
|
210 | - $this->isActive = true; |
|
211 | - |
|
212 | - foreach ($plugins as $plugin) { |
|
213 | - /** @var Plugin $plugin */ |
|
214 | - (new $plugin())->__execute($this, $update); |
|
215 | - if ($this->isActive === false) break; |
|
216 | - } |
|
217 | - |
|
218 | - $this->isActive = false; |
|
219 | - } |
|
220 | - |
|
221 | - /** |
|
222 | - * Kill the speeding process |
|
223 | - * |
|
224 | - * @return void |
|
225 | - */ |
|
226 | - public function kill(): void |
|
227 | - { |
|
228 | - $this->isActive = false; |
|
229 | - } |
|
230 | - |
|
231 | - /** |
|
232 | - * Use this method on the webhook class to get the updates |
|
233 | - * |
|
234 | - * @param Update $update |
|
235 | - * @return void |
|
236 | - */ |
|
237 | - abstract public function __process(Update $update): void; |
|
238 | - |
|
239 | - /** |
|
240 | - * put CrossData into the plugins |
|
241 | - * |
|
242 | - * @param string $key |
|
243 | - * @param mixed $value |
|
244 | - * @return void |
|
245 | - */ |
|
246 | - public function putCrossData(string $key, mixed $value): void |
|
247 | - { |
|
248 | - CrossData::put($key, $value); |
|
249 | - } |
|
250 | - |
|
251 | - /** |
|
252 | - * get CrossData from the plugins |
|
253 | - * |
|
254 | - * @param string $key |
|
255 | - * @return string|array|bool|null |
|
256 | - */ |
|
257 | - public function getCrossData(string $key): string|array|bool|null |
|
258 | - { |
|
259 | - return CrossData::get($key); |
|
260 | - } |
|
21 | + /** |
|
22 | + * @var ?Update |
|
23 | + */ |
|
24 | + protected ?Update $update; |
|
25 | + |
|
26 | + /** |
|
27 | + * @var array<Plugin> |
|
28 | + */ |
|
29 | + private array $plugins = []; |
|
30 | + |
|
31 | + /** |
|
32 | + * @var bool |
|
33 | + */ |
|
34 | + private bool $isActive = false; |
|
35 | + |
|
36 | + /** |
|
37 | + * The default configuration of the webhook. |
|
38 | + * |
|
39 | + * @var array |
|
40 | + */ |
|
41 | + private array $config = [ |
|
42 | + 'autoload_env_file' => false, |
|
43 | + 'env_file_path' => null, |
|
44 | + ]; |
|
45 | + |
|
46 | + /** |
|
47 | + * Webhook constructor. |
|
48 | + * |
|
49 | + * @param string $api_key The API key of the bot. Leave it blank for autoload from .env file. |
|
50 | + */ |
|
51 | + public function __construct(string $api_key = '') |
|
52 | + { |
|
53 | + parent::__construct($api_key); |
|
54 | + |
|
55 | + if (!Telegram::validateToken(self::getApiKey())) { |
|
56 | + throw new InvalidBotTokenException(); |
|
57 | + } |
|
58 | + } |
|
59 | + |
|
60 | + /** |
|
61 | + * Initialize the receiver. |
|
62 | + * |
|
63 | + * @return void |
|
64 | + */ |
|
65 | + public function init(): void |
|
66 | + { |
|
67 | + $this->config['env_file_path'] = $_SERVER['DOCUMENT_ROOT'] . '/.env'; |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Add a plugin to the receiver |
|
72 | + * |
|
73 | + * @param array<Plugin> $plugins |
|
74 | + */ |
|
75 | + public function addPlugin(array $plugins): void |
|
76 | + { |
|
77 | + foreach ($plugins as $plugin) { |
|
78 | + if (!is_subclass_of($plugin, Plugin::class)) { |
|
79 | + throw new \RuntimeException( |
|
80 | + sprintf('The plugin %s must be an instance of %s', get_class($plugin), Plugin::class) |
|
81 | + ); |
|
82 | + } |
|
83 | + $this->plugins[] = $plugin; |
|
84 | + } |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * Match the update with the given plugins |
|
89 | + * |
|
90 | + * @param array<Plugin> $plugins |
|
91 | + * @param ?Update $update The update to match |
|
92 | + * @return void |
|
93 | + */ |
|
94 | + public function loadPluginsWith(array $plugins, Update $update = null): void |
|
95 | + { |
|
96 | + $update = $update ?? ($this->update ?? Telegram::getUpdate()); |
|
97 | + |
|
98 | + foreach ($plugins as $plugin) { |
|
99 | + if (!is_subclass_of($plugin, Plugin::class)) { |
|
100 | + throw new \InvalidArgumentException( |
|
101 | + sprintf('The plugin %s must be an instance of %s', get_class($plugin), Plugin::class) |
|
102 | + ); |
|
103 | + } |
|
104 | + } |
|
105 | + |
|
106 | + if ($update instanceof Update) { |
|
107 | + $this->spreadUpdateWith($update, $plugins); |
|
108 | + } |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * Match the message to the plugins |
|
113 | + * |
|
114 | + * @param ?Update $update The update to match |
|
115 | + * @return void |
|
116 | + */ |
|
117 | + public function loadPlugins(Update $update = null): void |
|
118 | + { |
|
119 | + $update = $update ?? ($this->update ?? Telegram::getUpdate()); |
|
120 | + $this->loadPluginsWith($this->plugins, $update); |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * Load the environment's |
|
125 | + * |
|
126 | + * @param string $path |
|
127 | + * @retrun void |
|
128 | + */ |
|
129 | + public function loadFileOfEnv(string $path): void |
|
130 | + { |
|
131 | + DotEnv::load($path); |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * Update the configuration |
|
136 | + * |
|
137 | + * @param array $configuration |
|
138 | + * @return void |
|
139 | + */ |
|
140 | + public function updateConfiguration(array $configuration): void |
|
141 | + { |
|
142 | + $this->config = array_merge($this->config, $configuration); |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * Resolve the request. |
|
147 | + * |
|
148 | + * @param ?Update $update The custom to work with |
|
149 | + * @param array $config The configuration of the receiver |
|
150 | + * |
|
151 | + * @retrun void |
|
152 | + */ |
|
153 | + public function resolve(Update $update = null, array $config = []): void |
|
154 | + { |
|
155 | + $method = '__process'; |
|
156 | + if (!method_exists($this, $method)) { |
|
157 | + throw new \RuntimeException(sprintf('The method %s does not exist', $method)); |
|
158 | + } |
|
159 | + |
|
160 | + if (is_array($config)) $this->updateConfiguration($config); |
|
161 | + |
|
162 | + if (!empty($update)) $this->update = $update; |
|
163 | + else $this->update = Telegram::getUpdate() !== false ? Telegram::getUpdate() : null; |
|
164 | + |
|
165 | + if (empty($this->update)) { |
|
166 | + TelegramLog::error( |
|
167 | + 'The update is empty, the request is not processed' |
|
168 | + ); |
|
169 | + return; |
|
170 | + } |
|
171 | + |
|
172 | + try { |
|
173 | + |
|
174 | + Common::arrest($this, $method, $this->update); |
|
175 | + |
|
176 | + } catch (\RuntimeException $exception) { |
|
177 | + |
|
178 | + $e = $exception->getPrevious(); |
|
179 | + TelegramLog::error(($message = sprintf( |
|
180 | + '%s(%d): %s\n%s', $e->getFile(), $e->getLine(), $e->getMessage(), $e->getTraceAsString() |
|
181 | + ))); |
|
182 | + |
|
183 | + if (defined('TG_ADMIN_ID') && TG_ADMIN_ID && defined('DEBUG_MODE') && DEBUG_MODE) { |
|
184 | + file_put_contents( |
|
185 | + ($file = getcwd() . '/' . uniqid('error_') . '.log'), |
|
186 | + $message . PHP_EOL . PHP_EOL . $update->getRawData(false) |
|
187 | + ); |
|
188 | + Request::sendMessage([ |
|
189 | + 'chat_id' => TG_ADMIN_ID, |
|
190 | + 'text' => $message, |
|
191 | + ]); |
|
192 | + Request::sendDocument([ |
|
193 | + 'chat_id' => TG_ADMIN_ID, |
|
194 | + 'document' => $file, |
|
195 | + ]); |
|
196 | + unlink($file); |
|
197 | + } |
|
198 | + } |
|
199 | + } |
|
200 | + |
|
201 | + /** |
|
202 | + * This function will get update and spread it to the plugins |
|
203 | + * |
|
204 | + * @param Update $update |
|
205 | + * @param array<Plugin> $plugins |
|
206 | + * @return void |
|
207 | + */ |
|
208 | + private function spreadUpdateWith(Update $update, array $plugins): void |
|
209 | + { |
|
210 | + $this->isActive = true; |
|
211 | + |
|
212 | + foreach ($plugins as $plugin) { |
|
213 | + /** @var Plugin $plugin */ |
|
214 | + (new $plugin())->__execute($this, $update); |
|
215 | + if ($this->isActive === false) break; |
|
216 | + } |
|
217 | + |
|
218 | + $this->isActive = false; |
|
219 | + } |
|
220 | + |
|
221 | + /** |
|
222 | + * Kill the speeding process |
|
223 | + * |
|
224 | + * @return void |
|
225 | + */ |
|
226 | + public function kill(): void |
|
227 | + { |
|
228 | + $this->isActive = false; |
|
229 | + } |
|
230 | + |
|
231 | + /** |
|
232 | + * Use this method on the webhook class to get the updates |
|
233 | + * |
|
234 | + * @param Update $update |
|
235 | + * @return void |
|
236 | + */ |
|
237 | + abstract public function __process(Update $update): void; |
|
238 | + |
|
239 | + /** |
|
240 | + * put CrossData into the plugins |
|
241 | + * |
|
242 | + * @param string $key |
|
243 | + * @param mixed $value |
|
244 | + * @return void |
|
245 | + */ |
|
246 | + public function putCrossData(string $key, mixed $value): void |
|
247 | + { |
|
248 | + CrossData::put($key, $value); |
|
249 | + } |
|
250 | + |
|
251 | + /** |
|
252 | + * get CrossData from the plugins |
|
253 | + * |
|
254 | + * @param string $key |
|
255 | + * @return string|array|bool|null |
|
256 | + */ |
|
257 | + public function getCrossData(string $key): string|array|bool|null |
|
258 | + { |
|
259 | + return CrossData::get($key); |
|
260 | + } |
|
261 | 261 | |
262 | 262 | } |
263 | 263 | \ No newline at end of file |
@@ -15,129 +15,129 @@ |
||
15 | 15 | class Plugin |
16 | 16 | { |
17 | 17 | |
18 | - /** |
|
19 | - * The Update types |
|
20 | - * |
|
21 | - * @var array |
|
22 | - */ |
|
23 | - protected array $update_types = [ |
|
24 | - 'Message', |
|
25 | - 'EditedMessage', |
|
26 | - 'ChannelPost', |
|
27 | - 'EditedChannelPost', |
|
28 | - 'InlineQuery', |
|
29 | - 'ChosenInlineResult', |
|
30 | - 'CallbackQuery', |
|
31 | - 'ShippingQuery', |
|
32 | - 'PreCheckoutQuery', |
|
33 | - ]; |
|
34 | - |
|
35 | - /** |
|
36 | - * @var WebhookHandler |
|
37 | - */ |
|
38 | - protected WebhookHandler $hook; |
|
39 | - |
|
40 | - /** |
|
41 | - * @var \Generator |
|
42 | - */ |
|
43 | - protected \Generator $returns; |
|
44 | - |
|
45 | - /** |
|
46 | - * @var bool |
|
47 | - */ |
|
48 | - protected bool $kill_on_yield = false; |
|
49 | - |
|
50 | - /** |
|
51 | - * Check for the exit of the plugin. |
|
52 | - * |
|
53 | - * @param \Generator $return |
|
54 | - * @return void |
|
55 | - */ |
|
56 | - public function __checkExit(\Generator $return): void |
|
57 | - { |
|
58 | - if ($return->valid()) { |
|
59 | - if ($return->current() !== null && $this->kill_on_yield === true) { |
|
60 | - $this->kill(); |
|
61 | - } |
|
62 | - } |
|
63 | - |
|
64 | - if ($return->valid()) { |
|
65 | - $return->next(); |
|
66 | - $this->__checkExit($return); |
|
67 | - } |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Identify the update type. e.g. Message, EditedMessage, etc. |
|
72 | - * |
|
73 | - * @param Update $update |
|
74 | - * @return string|null |
|
75 | - */ |
|
76 | - public function __identify(Update $update): ?string |
|
77 | - { |
|
78 | - $type = $update->getUpdateType(); |
|
79 | - |
|
80 | - if ($type === null) { |
|
81 | - return null; |
|
82 | - } |
|
83 | - |
|
84 | - return str_replace('_', '', ucwords($type, '_')); |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * Execute the plugin. |
|
89 | - * |
|
90 | - * @param WebhookHandler $receiver |
|
91 | - * @param Update $update |
|
92 | - * @return void |
|
93 | - */ |
|
94 | - public function __execute(WebhookHandler $receiver, Update $update): void |
|
95 | - { |
|
96 | - $this->hook = $receiver; |
|
97 | - |
|
98 | - if (method_exists($this, 'onReceivedUpdate')) { |
|
99 | - $return = $this->onReceivedUpdate($update); |
|
100 | - $this->__checkExit($return); |
|
101 | - } |
|
102 | - |
|
103 | - $type = $this->__identify($update); |
|
104 | - if (method_exists($this, ($method = 'on' . $type)) && $type !== null) { |
|
105 | - $return = $this->$method($update); |
|
106 | - $this->__checkExit($return); |
|
107 | - } |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * Kill the plugin. |
|
112 | - * |
|
113 | - * @return void |
|
114 | - */ |
|
115 | - public function kill(): void |
|
116 | - { |
|
117 | - $this->hook->kill(); |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * put CrossData into the plugins |
|
122 | - * |
|
123 | - * @param string $key |
|
124 | - * @param mixed $value |
|
125 | - * @return void |
|
126 | - */ |
|
127 | - public function putCrossData(string $key, mixed $value): void |
|
128 | - { |
|
129 | - CrossData::put($key, $value); |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * get CrossData from the plugins |
|
134 | - * |
|
135 | - * @param string $key |
|
136 | - * @return string|array|bool|null |
|
137 | - */ |
|
138 | - public function getCrossData(string $key): string|array|bool|null |
|
139 | - { |
|
140 | - return CrossData::get($key); |
|
141 | - } |
|
18 | + /** |
|
19 | + * The Update types |
|
20 | + * |
|
21 | + * @var array |
|
22 | + */ |
|
23 | + protected array $update_types = [ |
|
24 | + 'Message', |
|
25 | + 'EditedMessage', |
|
26 | + 'ChannelPost', |
|
27 | + 'EditedChannelPost', |
|
28 | + 'InlineQuery', |
|
29 | + 'ChosenInlineResult', |
|
30 | + 'CallbackQuery', |
|
31 | + 'ShippingQuery', |
|
32 | + 'PreCheckoutQuery', |
|
33 | + ]; |
|
34 | + |
|
35 | + /** |
|
36 | + * @var WebhookHandler |
|
37 | + */ |
|
38 | + protected WebhookHandler $hook; |
|
39 | + |
|
40 | + /** |
|
41 | + * @var \Generator |
|
42 | + */ |
|
43 | + protected \Generator $returns; |
|
44 | + |
|
45 | + /** |
|
46 | + * @var bool |
|
47 | + */ |
|
48 | + protected bool $kill_on_yield = false; |
|
49 | + |
|
50 | + /** |
|
51 | + * Check for the exit of the plugin. |
|
52 | + * |
|
53 | + * @param \Generator $return |
|
54 | + * @return void |
|
55 | + */ |
|
56 | + public function __checkExit(\Generator $return): void |
|
57 | + { |
|
58 | + if ($return->valid()) { |
|
59 | + if ($return->current() !== null && $this->kill_on_yield === true) { |
|
60 | + $this->kill(); |
|
61 | + } |
|
62 | + } |
|
63 | + |
|
64 | + if ($return->valid()) { |
|
65 | + $return->next(); |
|
66 | + $this->__checkExit($return); |
|
67 | + } |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Identify the update type. e.g. Message, EditedMessage, etc. |
|
72 | + * |
|
73 | + * @param Update $update |
|
74 | + * @return string|null |
|
75 | + */ |
|
76 | + public function __identify(Update $update): ?string |
|
77 | + { |
|
78 | + $type = $update->getUpdateType(); |
|
79 | + |
|
80 | + if ($type === null) { |
|
81 | + return null; |
|
82 | + } |
|
83 | + |
|
84 | + return str_replace('_', '', ucwords($type, '_')); |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * Execute the plugin. |
|
89 | + * |
|
90 | + * @param WebhookHandler $receiver |
|
91 | + * @param Update $update |
|
92 | + * @return void |
|
93 | + */ |
|
94 | + public function __execute(WebhookHandler $receiver, Update $update): void |
|
95 | + { |
|
96 | + $this->hook = $receiver; |
|
97 | + |
|
98 | + if (method_exists($this, 'onReceivedUpdate')) { |
|
99 | + $return = $this->onReceivedUpdate($update); |
|
100 | + $this->__checkExit($return); |
|
101 | + } |
|
102 | + |
|
103 | + $type = $this->__identify($update); |
|
104 | + if (method_exists($this, ($method = 'on' . $type)) && $type !== null) { |
|
105 | + $return = $this->$method($update); |
|
106 | + $this->__checkExit($return); |
|
107 | + } |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * Kill the plugin. |
|
112 | + * |
|
113 | + * @return void |
|
114 | + */ |
|
115 | + public function kill(): void |
|
116 | + { |
|
117 | + $this->hook->kill(); |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * put CrossData into the plugins |
|
122 | + * |
|
123 | + * @param string $key |
|
124 | + * @param mixed $value |
|
125 | + * @return void |
|
126 | + */ |
|
127 | + public function putCrossData(string $key, mixed $value): void |
|
128 | + { |
|
129 | + CrossData::put($key, $value); |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * get CrossData from the plugins |
|
134 | + * |
|
135 | + * @param string $key |
|
136 | + * @return string|array|bool|null |
|
137 | + */ |
|
138 | + public function getCrossData(string $key): string|array|bool|null |
|
139 | + { |
|
140 | + return CrossData::get($key); |
|
141 | + } |
|
142 | 142 | |
143 | 143 | } |
144 | 144 | \ No newline at end of file |