@@ -14,89 +14,89 @@ |
||
14 | 14 | class Plugin |
15 | 15 | { |
16 | 16 | |
17 | - /** |
|
18 | - * @var WebhookHandler |
|
19 | - */ |
|
20 | - protected WebhookHandler $hook; |
|
21 | - |
|
22 | - /** |
|
23 | - * @var \Generator |
|
24 | - */ |
|
25 | - protected \Generator $returns; |
|
26 | - |
|
27 | - /** |
|
28 | - * @var bool |
|
29 | - */ |
|
30 | - protected bool $kill_on_yield = true; |
|
31 | - |
|
32 | - /** |
|
33 | - * Check for the exit of the plugin. |
|
34 | - * |
|
35 | - * @param \Generator $return |
|
36 | - * @return void |
|
37 | - */ |
|
38 | - public function __checkExit(\Generator $return): void |
|
39 | - { |
|
40 | - if ($return->valid()) { |
|
41 | - if ($return->current() !== null && $this->kill_on_yield === true) { |
|
42 | - $this->kill(); |
|
43 | - } |
|
44 | - } |
|
45 | - |
|
46 | - if ($return->valid()) { |
|
47 | - $return->next(); |
|
48 | - $this->__checkExit($return); |
|
49 | - } |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * Identify the update type. e.g. Message, EditedMessage, etc. |
|
54 | - * |
|
55 | - * @param Update $update |
|
56 | - * @return string|null |
|
57 | - */ |
|
58 | - public function __identify(Update $update): ?string |
|
59 | - { |
|
60 | - $type = $update->getUpdateType(); |
|
61 | - |
|
62 | - if ($type === null) { |
|
63 | - return null; |
|
64 | - } |
|
65 | - |
|
66 | - return str_replace('_', '', ucwords($type, '_')); |
|
67 | - } |
|
68 | - |
|
69 | - /** |
|
70 | - * Execute the plugin. |
|
71 | - * |
|
72 | - * @param WebhookHandler $receiver |
|
73 | - * @param Update $update |
|
74 | - * @return void |
|
75 | - */ |
|
76 | - public function __execute(WebhookHandler $receiver, Update $update): void |
|
77 | - { |
|
78 | - $this->hook = $receiver; |
|
79 | - |
|
80 | - if (method_exists($this, 'onReceivedUpdate')) { |
|
81 | - $return = $this->onReceivedUpdate($update); |
|
82 | - $this->__checkExit($return); |
|
83 | - } |
|
84 | - |
|
85 | - $type = $this->__identify($update); |
|
86 | - if (method_exists($this, ($method = 'on' . $type)) && $type !== null) { |
|
87 | - $return = $this->$method($update); |
|
88 | - $this->__checkExit($return); |
|
89 | - } |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * Kill the plugin. |
|
94 | - * |
|
95 | - * @return void |
|
96 | - */ |
|
97 | - public function kill(): void |
|
98 | - { |
|
99 | - $this->hook->kill(); |
|
100 | - } |
|
17 | + /** |
|
18 | + * @var WebhookHandler |
|
19 | + */ |
|
20 | + protected WebhookHandler $hook; |
|
21 | + |
|
22 | + /** |
|
23 | + * @var \Generator |
|
24 | + */ |
|
25 | + protected \Generator $returns; |
|
26 | + |
|
27 | + /** |
|
28 | + * @var bool |
|
29 | + */ |
|
30 | + protected bool $kill_on_yield = true; |
|
31 | + |
|
32 | + /** |
|
33 | + * Check for the exit of the plugin. |
|
34 | + * |
|
35 | + * @param \Generator $return |
|
36 | + * @return void |
|
37 | + */ |
|
38 | + public function __checkExit(\Generator $return): void |
|
39 | + { |
|
40 | + if ($return->valid()) { |
|
41 | + if ($return->current() !== null && $this->kill_on_yield === true) { |
|
42 | + $this->kill(); |
|
43 | + } |
|
44 | + } |
|
45 | + |
|
46 | + if ($return->valid()) { |
|
47 | + $return->next(); |
|
48 | + $this->__checkExit($return); |
|
49 | + } |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * Identify the update type. e.g. Message, EditedMessage, etc. |
|
54 | + * |
|
55 | + * @param Update $update |
|
56 | + * @return string|null |
|
57 | + */ |
|
58 | + public function __identify(Update $update): ?string |
|
59 | + { |
|
60 | + $type = $update->getUpdateType(); |
|
61 | + |
|
62 | + if ($type === null) { |
|
63 | + return null; |
|
64 | + } |
|
65 | + |
|
66 | + return str_replace('_', '', ucwords($type, '_')); |
|
67 | + } |
|
68 | + |
|
69 | + /** |
|
70 | + * Execute the plugin. |
|
71 | + * |
|
72 | + * @param WebhookHandler $receiver |
|
73 | + * @param Update $update |
|
74 | + * @return void |
|
75 | + */ |
|
76 | + public function __execute(WebhookHandler $receiver, Update $update): void |
|
77 | + { |
|
78 | + $this->hook = $receiver; |
|
79 | + |
|
80 | + if (method_exists($this, 'onReceivedUpdate')) { |
|
81 | + $return = $this->onReceivedUpdate($update); |
|
82 | + $this->__checkExit($return); |
|
83 | + } |
|
84 | + |
|
85 | + $type = $this->__identify($update); |
|
86 | + if (method_exists($this, ($method = 'on' . $type)) && $type !== null) { |
|
87 | + $return = $this->$method($update); |
|
88 | + $this->__checkExit($return); |
|
89 | + } |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * Kill the plugin. |
|
94 | + * |
|
95 | + * @return void |
|
96 | + */ |
|
97 | + public function kill(): void |
|
98 | + { |
|
99 | + $this->hook->kill(); |
|
100 | + } |
|
101 | 101 | |
102 | 102 | } |
103 | 103 | \ No newline at end of file |
@@ -16,197 +16,197 @@ |
||
16 | 16 | abstract class WebhookHandler extends Telegram |
17 | 17 | { |
18 | 18 | |
19 | - /** |
|
20 | - * @var ?Update |
|
21 | - */ |
|
22 | - protected ?Update $update; |
|
23 | - |
|
24 | - /** |
|
25 | - * @var array<Plugin> |
|
26 | - */ |
|
27 | - private array $plugins = []; |
|
28 | - |
|
29 | - /** |
|
30 | - * @var bool |
|
31 | - */ |
|
32 | - private bool $isActive = false; |
|
33 | - |
|
34 | - /** |
|
35 | - * The default configuration of the webhook. |
|
36 | - * |
|
37 | - * @var array |
|
38 | - */ |
|
39 | - private array $config = [ |
|
40 | - 'autoload_env_file' => false, |
|
41 | - 'env_file_path' => null, |
|
42 | - ]; |
|
43 | - |
|
44 | - /** |
|
45 | - * Webhook constructor. |
|
46 | - * |
|
47 | - * @param string $api_key The API key of the bot. Leave it blank for autoload from .env file. |
|
48 | - */ |
|
49 | - public function __construct(string $api_key = '') |
|
50 | - { |
|
51 | - parent::__construct($api_key); |
|
52 | - |
|
53 | - if (!Telegram::validateToken(self::getApiKey())) { |
|
54 | - throw new InvalidBotTokenException(); |
|
55 | - } |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * Initialize the receiver. |
|
60 | - * |
|
61 | - * @return void |
|
62 | - */ |
|
63 | - public function init(): void |
|
64 | - { |
|
65 | - $this->config['env_file_path'] = $_SERVER['DOCUMENT_ROOT'] . '/.env'; |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * Add a plugin to the receiver |
|
70 | - * |
|
71 | - * @param array<Plugin> $plugins |
|
72 | - */ |
|
73 | - public function addPlugin(array $plugins): void |
|
74 | - { |
|
75 | - foreach ($plugins as $plugin) { |
|
76 | - if (!is_subclass_of($plugin, Plugin::class)) { |
|
77 | - throw new \RuntimeException( |
|
78 | - sprintf('The plugin %s must be an instance of %s', get_class($plugin), Plugin::class) |
|
79 | - ); |
|
80 | - } |
|
81 | - $this->plugins[] = $plugin; |
|
82 | - } |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * Match the update with the given plugins |
|
87 | - * |
|
88 | - * @param array<Plugin> $plugins |
|
89 | - * @param ?Update $update The update to match |
|
90 | - * @return void |
|
91 | - */ |
|
92 | - public function loadPluginsWith(array $plugins, Update $update = null): void |
|
93 | - { |
|
94 | - $update = $update ?? ($this->update ?? Telegram::getUpdate()); |
|
95 | - |
|
96 | - foreach ($plugins as $plugin) { |
|
97 | - if (!is_subclass_of($plugin, Plugin::class)) { |
|
98 | - throw new \InvalidArgumentException( |
|
99 | - sprintf('The plugin %s must be an instance of %s', get_class($plugin), Plugin::class) |
|
100 | - ); |
|
101 | - } |
|
102 | - } |
|
103 | - |
|
104 | - if ($update instanceof Update) { |
|
105 | - $this->spreadUpdateWith($update, $plugins); |
|
106 | - } |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * Match the message to the plugins |
|
111 | - * |
|
112 | - * @param ?Update $update The update to match |
|
113 | - * @return void |
|
114 | - */ |
|
115 | - public function loadPlugins(Update $update = null): void |
|
116 | - { |
|
117 | - $update = $update ?? ($this->update ?? Telegram::getUpdate()); |
|
118 | - $this->loadPluginsWith($this->plugins, $update); |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * Load the environment's |
|
123 | - * |
|
124 | - * @param string $path |
|
125 | - * @retrun void |
|
126 | - */ |
|
127 | - public function loadFileOfEnv(string $path): void |
|
128 | - { |
|
129 | - DotEnv::load($path); |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Update the configuration |
|
134 | - * |
|
135 | - * @param array $configuration |
|
136 | - * @return void |
|
137 | - */ |
|
138 | - public function updateConfiguration(array $configuration): void |
|
139 | - { |
|
140 | - $this->config = array_merge($this->config, $configuration); |
|
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 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * This function will get update and spread it to the plugins |
|
176 | - * |
|
177 | - * @param Update $update |
|
178 | - * @param array<Plugin> $plugins |
|
179 | - * @return void |
|
180 | - */ |
|
181 | - private function spreadUpdateWith(Update $update, array $plugins): void |
|
182 | - { |
|
183 | - $this->isActive = true; |
|
184 | - |
|
185 | - foreach ($plugins as $plugin) { |
|
186 | - /** @var Plugin $plugin */ |
|
187 | - (new $plugin())->__execute($this, $update); |
|
188 | - if ($this->isActive === false) break; |
|
189 | - } |
|
190 | - |
|
191 | - $this->isActive = false; |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * Kill the speeding process |
|
196 | - * |
|
197 | - * @return void |
|
198 | - */ |
|
199 | - public function kill(): void |
|
200 | - { |
|
201 | - $this->isActive = false; |
|
202 | - } |
|
203 | - |
|
204 | - /** |
|
205 | - * Use this method on the webhook class to get the updates |
|
206 | - * |
|
207 | - * @param Update $update |
|
208 | - * @return void |
|
209 | - */ |
|
210 | - abstract public function __process(Update $update): void; |
|
19 | + /** |
|
20 | + * @var ?Update |
|
21 | + */ |
|
22 | + protected ?Update $update; |
|
23 | + |
|
24 | + /** |
|
25 | + * @var array<Plugin> |
|
26 | + */ |
|
27 | + private array $plugins = []; |
|
28 | + |
|
29 | + /** |
|
30 | + * @var bool |
|
31 | + */ |
|
32 | + private bool $isActive = false; |
|
33 | + |
|
34 | + /** |
|
35 | + * The default configuration of the webhook. |
|
36 | + * |
|
37 | + * @var array |
|
38 | + */ |
|
39 | + private array $config = [ |
|
40 | + 'autoload_env_file' => false, |
|
41 | + 'env_file_path' => null, |
|
42 | + ]; |
|
43 | + |
|
44 | + /** |
|
45 | + * Webhook constructor. |
|
46 | + * |
|
47 | + * @param string $api_key The API key of the bot. Leave it blank for autoload from .env file. |
|
48 | + */ |
|
49 | + public function __construct(string $api_key = '') |
|
50 | + { |
|
51 | + parent::__construct($api_key); |
|
52 | + |
|
53 | + if (!Telegram::validateToken(self::getApiKey())) { |
|
54 | + throw new InvalidBotTokenException(); |
|
55 | + } |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * Initialize the receiver. |
|
60 | + * |
|
61 | + * @return void |
|
62 | + */ |
|
63 | + public function init(): void |
|
64 | + { |
|
65 | + $this->config['env_file_path'] = $_SERVER['DOCUMENT_ROOT'] . '/.env'; |
|
66 | + } |
|
67 | + |
|
68 | + /** |
|
69 | + * Add a plugin to the receiver |
|
70 | + * |
|
71 | + * @param array<Plugin> $plugins |
|
72 | + */ |
|
73 | + public function addPlugin(array $plugins): void |
|
74 | + { |
|
75 | + foreach ($plugins as $plugin) { |
|
76 | + if (!is_subclass_of($plugin, Plugin::class)) { |
|
77 | + throw new \RuntimeException( |
|
78 | + sprintf('The plugin %s must be an instance of %s', get_class($plugin), Plugin::class) |
|
79 | + ); |
|
80 | + } |
|
81 | + $this->plugins[] = $plugin; |
|
82 | + } |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * Match the update with the given plugins |
|
87 | + * |
|
88 | + * @param array<Plugin> $plugins |
|
89 | + * @param ?Update $update The update to match |
|
90 | + * @return void |
|
91 | + */ |
|
92 | + public function loadPluginsWith(array $plugins, Update $update = null): void |
|
93 | + { |
|
94 | + $update = $update ?? ($this->update ?? Telegram::getUpdate()); |
|
95 | + |
|
96 | + foreach ($plugins as $plugin) { |
|
97 | + if (!is_subclass_of($plugin, Plugin::class)) { |
|
98 | + throw new \InvalidArgumentException( |
|
99 | + sprintf('The plugin %s must be an instance of %s', get_class($plugin), Plugin::class) |
|
100 | + ); |
|
101 | + } |
|
102 | + } |
|
103 | + |
|
104 | + if ($update instanceof Update) { |
|
105 | + $this->spreadUpdateWith($update, $plugins); |
|
106 | + } |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * Match the message to the plugins |
|
111 | + * |
|
112 | + * @param ?Update $update The update to match |
|
113 | + * @return void |
|
114 | + */ |
|
115 | + public function loadPlugins(Update $update = null): void |
|
116 | + { |
|
117 | + $update = $update ?? ($this->update ?? Telegram::getUpdate()); |
|
118 | + $this->loadPluginsWith($this->plugins, $update); |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * Load the environment's |
|
123 | + * |
|
124 | + * @param string $path |
|
125 | + * @retrun void |
|
126 | + */ |
|
127 | + public function loadFileOfEnv(string $path): void |
|
128 | + { |
|
129 | + DotEnv::load($path); |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Update the configuration |
|
134 | + * |
|
135 | + * @param array $configuration |
|
136 | + * @return void |
|
137 | + */ |
|
138 | + public function updateConfiguration(array $configuration): void |
|
139 | + { |
|
140 | + $this->config = array_merge($this->config, $configuration); |
|
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 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * This function will get update and spread it to the plugins |
|
176 | + * |
|
177 | + * @param Update $update |
|
178 | + * @param array<Plugin> $plugins |
|
179 | + * @return void |
|
180 | + */ |
|
181 | + private function spreadUpdateWith(Update $update, array $plugins): void |
|
182 | + { |
|
183 | + $this->isActive = true; |
|
184 | + |
|
185 | + foreach ($plugins as $plugin) { |
|
186 | + /** @var Plugin $plugin */ |
|
187 | + (new $plugin())->__execute($this, $update); |
|
188 | + if ($this->isActive === false) break; |
|
189 | + } |
|
190 | + |
|
191 | + $this->isActive = false; |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * Kill the speeding process |
|
196 | + * |
|
197 | + * @return void |
|
198 | + */ |
|
199 | + public function kill(): void |
|
200 | + { |
|
201 | + $this->isActive = false; |
|
202 | + } |
|
203 | + |
|
204 | + /** |
|
205 | + * Use this method on the webhook class to get the updates |
|
206 | + * |
|
207 | + * @param Update $update |
|
208 | + * @return void |
|
209 | + */ |
|
210 | + abstract public function __process(Update $update): void; |
|
211 | 211 | |
212 | 212 | } |
213 | 213 | \ No newline at end of file |
@@ -154,10 +154,15 @@ discard block |
||
154 | 154 | throw new \RuntimeException('The method __process does not exist'); |
155 | 155 | } |
156 | 156 | |
157 | - if (is_array($config)) $this->updateConfiguration($config); |
|
157 | + if (is_array($config)) { |
|
158 | + $this->updateConfiguration($config); |
|
159 | + } |
|
158 | 160 | |
159 | - if (!empty($update)) $this->update = $update; |
|
160 | - else $this->update = Telegram::getUpdate() !== false ? Telegram::getUpdate() : null; |
|
161 | + if (!empty($update)) { |
|
162 | + $this->update = $update; |
|
163 | + } else { |
|
164 | + $this->update = Telegram::getUpdate() !== false ? Telegram::getUpdate() : null; |
|
165 | + } |
|
161 | 166 | |
162 | 167 | if (empty($this->update)) { |
163 | 168 | TelegramLog::error( |
@@ -185,7 +190,9 @@ discard block |
||
185 | 190 | foreach ($plugins as $plugin) { |
186 | 191 | /** @var Plugin $plugin */ |
187 | 192 | (new $plugin())->__execute($this, $update); |
188 | - if ($this->isActive === false) break; |
|
193 | + if ($this->isActive === false) { |
|
194 | + break; |
|
195 | + } |
|
189 | 196 | } |
190 | 197 | |
191 | 198 | $this->isActive = false; |
@@ -143,227 +143,227 @@ |
||
143 | 143 | class Request |
144 | 144 | { |
145 | 145 | |
146 | - /** |
|
147 | - * Available fields for InputFile helper |
|
148 | - * |
|
149 | - * This is basically the list of all fields that allow InputFile objects |
|
150 | - * for which input can be simplified by providing local path directly as string. |
|
151 | - * |
|
152 | - * @var array |
|
153 | - */ |
|
154 | - private static array $input_file_fields = [ |
|
155 | - 'setWebhook' => ['certificate'], |
|
156 | - 'sendPhoto' => ['photo'], |
|
157 | - 'sendAudio' => ['audio', 'thumb'], |
|
158 | - 'sendDocument' => ['document', 'thumb'], |
|
159 | - 'sendVideo' => ['video', 'thumb'], |
|
160 | - 'sendAnimation' => ['animation', 'thumb'], |
|
161 | - 'sendVoice' => ['voice', 'thumb'], |
|
162 | - 'sendVideoNote' => ['video_note', 'thumb'], |
|
163 | - 'setChatPhoto' => ['photo'], |
|
164 | - 'sendSticker' => ['sticker'], |
|
165 | - 'uploadStickerFile' => ['png_sticker'], |
|
166 | - 'createNewStickerSet' => ['png_sticker', 'tgs_sticker', 'webm_sticker'], |
|
167 | - 'addStickerToSet' => ['png_sticker', 'tgs_sticker', 'webm_sticker'], |
|
168 | - 'setStickerSetThumb' => ['thumb'], |
|
169 | - ]; |
|
170 | - |
|
171 | - /** |
|
172 | - * URI of the Telegram API |
|
173 | - * |
|
174 | - * @var string |
|
175 | - */ |
|
176 | - private static string $api_base_uri = 'https://api.telegram.org'; |
|
177 | - |
|
178 | - /** |
|
179 | - * URI of the Telegram API for downloading files (relative to $api_base_url or absolute) |
|
180 | - * |
|
181 | - * @var string |
|
182 | - */ |
|
183 | - private static string $api_base_download_uri = '/file/bot{API_KEY}'; |
|
184 | - |
|
185 | - /** |
|
186 | - * The current action that is being executed |
|
187 | - * |
|
188 | - * @var string |
|
189 | - */ |
|
190 | - private static string $current_action = ''; |
|
191 | - |
|
192 | - /** |
|
193 | - * Get the Telegram API path |
|
194 | - * |
|
195 | - * @return string |
|
196 | - */ |
|
197 | - public static function getApiPath(): string |
|
198 | - { |
|
199 | - return self::$api_base_uri . '/bot' . Telegram::getApiKey() . '/'; |
|
200 | - } |
|
201 | - |
|
202 | - /** |
|
203 | - * Initialize a http client |
|
204 | - * |
|
205 | - * @return Client |
|
206 | - */ |
|
207 | - private static function getClient(): Client |
|
208 | - { |
|
209 | - return new Client(); |
|
210 | - } |
|
211 | - |
|
212 | - /** |
|
213 | - * Use this method to get stats of given user in a supergroup or channel. |
|
214 | - * |
|
215 | - * @param int $user_id User identifier |
|
216 | - * @param int $chat_id Identifier of the chat to get stats for |
|
217 | - * |
|
218 | - * @return string [left, member, administrator, creator] |
|
219 | - */ |
|
220 | - public static function getChatMemberStatus(int $user_id, int $chat_id): string |
|
221 | - { |
|
222 | - $response = self::getChatMember([ |
|
223 | - 'user_id' => $user_id, |
|
224 | - 'chat_id' => $chat_id, |
|
225 | - ]); |
|
226 | - |
|
227 | - return $response->getResult()->status ?? "left"; |
|
228 | - } |
|
229 | - |
|
230 | - /** |
|
231 | - * Properly set up the request params |
|
232 | - * |
|
233 | - * If any item of the array is a resource, reformat it to a multipart request. |
|
234 | - * Else, just return the passed data as form params. |
|
235 | - * |
|
236 | - * @param array $data |
|
237 | - * @return array |
|
238 | - */ |
|
239 | - private static function setUpRequestParams(array $data): array |
|
240 | - { |
|
241 | - $multipart = []; |
|
242 | - $has_resource = false; |
|
243 | - |
|
244 | - $options = [ |
|
245 | - 'headers' => [ |
|
246 | - 'Content-Type' => 'application/json', |
|
247 | - 'Accept' => 'application/json', |
|
248 | - 'User-Agent' => 'TelegramBot-PHP/' . Telegram::$VERSION |
|
249 | - ] |
|
250 | - ]; |
|
251 | - |
|
252 | - foreach ($data as $key => &$item) { |
|
253 | - if (array_key_exists(self::$current_action, self::$input_file_fields) && in_array($key, self::$input_file_fields[self::$current_action], true)) { |
|
254 | - |
|
255 | - if (is_string($item) && file_exists($item)) { |
|
256 | - $has_resource = true; |
|
257 | - |
|
258 | - } elseif (filter_var($item, FILTER_VALIDATE_URL)) { |
|
259 | - $has_resource = false; |
|
260 | - continue; |
|
261 | - |
|
262 | - } else { |
|
263 | - throw new TelegramException( |
|
264 | - 'Invalid file path or URL: ' . $item . ' for ' . self::$current_action . ' action' |
|
265 | - ); |
|
266 | - } |
|
267 | - |
|
268 | - $multipart[$key] = $item; |
|
269 | - continue; |
|
270 | - } |
|
271 | - |
|
272 | - if ($item instanceof Entity) { |
|
273 | - $item = $item->getRawData(); |
|
274 | - } |
|
275 | - |
|
276 | - if (is_array($item)) { |
|
277 | - $item = json_encode($item); |
|
278 | - } |
|
279 | - |
|
280 | - $options['query'][$key] = $item; |
|
281 | - } |
|
282 | - unset($item); |
|
283 | - |
|
284 | - if ($has_resource) { |
|
285 | - $options['multipart'] = FormData::create($multipart); |
|
286 | - } |
|
287 | - |
|
288 | - return $options; |
|
289 | - } |
|
290 | - |
|
291 | - /** |
|
292 | - * Create a Http Request |
|
293 | - * |
|
294 | - * @param string $action Action to execute |
|
295 | - * @param array $data Data to attach to the execution |
|
296 | - * |
|
297 | - * @return array An array of the setUpRequestParams and the url |
|
298 | - */ |
|
299 | - public static function create(string $action, array $data): array |
|
300 | - { |
|
301 | - return [ |
|
302 | - 'url' => self::getApiPath() . $action, |
|
303 | - 'options' => self::setUpRequestParams($data) |
|
304 | - ]; |
|
305 | - } |
|
306 | - |
|
307 | - /** |
|
308 | - * Execute HTTP Request |
|
309 | - * |
|
310 | - * @param string $action Action to execute |
|
311 | - * @param array $data Data to attach to the execution |
|
312 | - * |
|
313 | - * @return string Result of the HTTP Request |
|
314 | - */ |
|
315 | - private static function execute(string $action, array $data): string |
|
316 | - { |
|
317 | - $request = self::create($action, $data); |
|
318 | - |
|
319 | - $response = self::getClient()->get($request['url'], $request['options']); |
|
320 | - |
|
321 | - return $response->getBody(); |
|
322 | - } |
|
323 | - |
|
324 | - /** |
|
325 | - * Send command |
|
326 | - * |
|
327 | - * @param string $action |
|
328 | - * @param array $data |
|
329 | - * |
|
330 | - * @return Response |
|
331 | - * @throws TelegramException |
|
332 | - */ |
|
333 | - public static function send(string $action, array $data = []): Response |
|
334 | - { |
|
335 | - self::$current_action = $action; |
|
336 | - |
|
337 | - $raw_response = self::execute($action, $data); |
|
338 | - |
|
339 | - if (!Common::isJson($raw_response)) { |
|
340 | - TelegramLog::debug($raw_response); |
|
341 | - throw new TelegramException('Invalid response from API'); |
|
342 | - } |
|
343 | - |
|
344 | - $response = new Response(json_decode($raw_response, true)); |
|
345 | - |
|
346 | - if (!$response->isOk() && $response->getErrorCode() === 401 && $response->getDescription() === 'Unauthorized') { |
|
347 | - throw new InvalidBotTokenException(); |
|
348 | - } |
|
349 | - |
|
350 | - self::$current_action = ''; |
|
351 | - |
|
352 | - return $response; |
|
353 | - } |
|
354 | - |
|
355 | - /** |
|
356 | - * Any statically called method should be relayed to the `send` method. |
|
357 | - * |
|
358 | - * @param string $action |
|
359 | - * @param array $data |
|
360 | - * |
|
361 | - * @return Response |
|
362 | - * @throws TelegramException |
|
363 | - */ |
|
364 | - public static function __callStatic(string $action, array $data): Response |
|
365 | - { |
|
366 | - return static::send($action, reset($data) ?: []); |
|
367 | - } |
|
146 | + /** |
|
147 | + * Available fields for InputFile helper |
|
148 | + * |
|
149 | + * This is basically the list of all fields that allow InputFile objects |
|
150 | + * for which input can be simplified by providing local path directly as string. |
|
151 | + * |
|
152 | + * @var array |
|
153 | + */ |
|
154 | + private static array $input_file_fields = [ |
|
155 | + 'setWebhook' => ['certificate'], |
|
156 | + 'sendPhoto' => ['photo'], |
|
157 | + 'sendAudio' => ['audio', 'thumb'], |
|
158 | + 'sendDocument' => ['document', 'thumb'], |
|
159 | + 'sendVideo' => ['video', 'thumb'], |
|
160 | + 'sendAnimation' => ['animation', 'thumb'], |
|
161 | + 'sendVoice' => ['voice', 'thumb'], |
|
162 | + 'sendVideoNote' => ['video_note', 'thumb'], |
|
163 | + 'setChatPhoto' => ['photo'], |
|
164 | + 'sendSticker' => ['sticker'], |
|
165 | + 'uploadStickerFile' => ['png_sticker'], |
|
166 | + 'createNewStickerSet' => ['png_sticker', 'tgs_sticker', 'webm_sticker'], |
|
167 | + 'addStickerToSet' => ['png_sticker', 'tgs_sticker', 'webm_sticker'], |
|
168 | + 'setStickerSetThumb' => ['thumb'], |
|
169 | + ]; |
|
170 | + |
|
171 | + /** |
|
172 | + * URI of the Telegram API |
|
173 | + * |
|
174 | + * @var string |
|
175 | + */ |
|
176 | + private static string $api_base_uri = 'https://api.telegram.org'; |
|
177 | + |
|
178 | + /** |
|
179 | + * URI of the Telegram API for downloading files (relative to $api_base_url or absolute) |
|
180 | + * |
|
181 | + * @var string |
|
182 | + */ |
|
183 | + private static string $api_base_download_uri = '/file/bot{API_KEY}'; |
|
184 | + |
|
185 | + /** |
|
186 | + * The current action that is being executed |
|
187 | + * |
|
188 | + * @var string |
|
189 | + */ |
|
190 | + private static string $current_action = ''; |
|
191 | + |
|
192 | + /** |
|
193 | + * Get the Telegram API path |
|
194 | + * |
|
195 | + * @return string |
|
196 | + */ |
|
197 | + public static function getApiPath(): string |
|
198 | + { |
|
199 | + return self::$api_base_uri . '/bot' . Telegram::getApiKey() . '/'; |
|
200 | + } |
|
201 | + |
|
202 | + /** |
|
203 | + * Initialize a http client |
|
204 | + * |
|
205 | + * @return Client |
|
206 | + */ |
|
207 | + private static function getClient(): Client |
|
208 | + { |
|
209 | + return new Client(); |
|
210 | + } |
|
211 | + |
|
212 | + /** |
|
213 | + * Use this method to get stats of given user in a supergroup or channel. |
|
214 | + * |
|
215 | + * @param int $user_id User identifier |
|
216 | + * @param int $chat_id Identifier of the chat to get stats for |
|
217 | + * |
|
218 | + * @return string [left, member, administrator, creator] |
|
219 | + */ |
|
220 | + public static function getChatMemberStatus(int $user_id, int $chat_id): string |
|
221 | + { |
|
222 | + $response = self::getChatMember([ |
|
223 | + 'user_id' => $user_id, |
|
224 | + 'chat_id' => $chat_id, |
|
225 | + ]); |
|
226 | + |
|
227 | + return $response->getResult()->status ?? "left"; |
|
228 | + } |
|
229 | + |
|
230 | + /** |
|
231 | + * Properly set up the request params |
|
232 | + * |
|
233 | + * If any item of the array is a resource, reformat it to a multipart request. |
|
234 | + * Else, just return the passed data as form params. |
|
235 | + * |
|
236 | + * @param array $data |
|
237 | + * @return array |
|
238 | + */ |
|
239 | + private static function setUpRequestParams(array $data): array |
|
240 | + { |
|
241 | + $multipart = []; |
|
242 | + $has_resource = false; |
|
243 | + |
|
244 | + $options = [ |
|
245 | + 'headers' => [ |
|
246 | + 'Content-Type' => 'application/json', |
|
247 | + 'Accept' => 'application/json', |
|
248 | + 'User-Agent' => 'TelegramBot-PHP/' . Telegram::$VERSION |
|
249 | + ] |
|
250 | + ]; |
|
251 | + |
|
252 | + foreach ($data as $key => &$item) { |
|
253 | + if (array_key_exists(self::$current_action, self::$input_file_fields) && in_array($key, self::$input_file_fields[self::$current_action], true)) { |
|
254 | + |
|
255 | + if (is_string($item) && file_exists($item)) { |
|
256 | + $has_resource = true; |
|
257 | + |
|
258 | + } elseif (filter_var($item, FILTER_VALIDATE_URL)) { |
|
259 | + $has_resource = false; |
|
260 | + continue; |
|
261 | + |
|
262 | + } else { |
|
263 | + throw new TelegramException( |
|
264 | + 'Invalid file path or URL: ' . $item . ' for ' . self::$current_action . ' action' |
|
265 | + ); |
|
266 | + } |
|
267 | + |
|
268 | + $multipart[$key] = $item; |
|
269 | + continue; |
|
270 | + } |
|
271 | + |
|
272 | + if ($item instanceof Entity) { |
|
273 | + $item = $item->getRawData(); |
|
274 | + } |
|
275 | + |
|
276 | + if (is_array($item)) { |
|
277 | + $item = json_encode($item); |
|
278 | + } |
|
279 | + |
|
280 | + $options['query'][$key] = $item; |
|
281 | + } |
|
282 | + unset($item); |
|
283 | + |
|
284 | + if ($has_resource) { |
|
285 | + $options['multipart'] = FormData::create($multipart); |
|
286 | + } |
|
287 | + |
|
288 | + return $options; |
|
289 | + } |
|
290 | + |
|
291 | + /** |
|
292 | + * Create a Http Request |
|
293 | + * |
|
294 | + * @param string $action Action to execute |
|
295 | + * @param array $data Data to attach to the execution |
|
296 | + * |
|
297 | + * @return array An array of the setUpRequestParams and the url |
|
298 | + */ |
|
299 | + public static function create(string $action, array $data): array |
|
300 | + { |
|
301 | + return [ |
|
302 | + 'url' => self::getApiPath() . $action, |
|
303 | + 'options' => self::setUpRequestParams($data) |
|
304 | + ]; |
|
305 | + } |
|
306 | + |
|
307 | + /** |
|
308 | + * Execute HTTP Request |
|
309 | + * |
|
310 | + * @param string $action Action to execute |
|
311 | + * @param array $data Data to attach to the execution |
|
312 | + * |
|
313 | + * @return string Result of the HTTP Request |
|
314 | + */ |
|
315 | + private static function execute(string $action, array $data): string |
|
316 | + { |
|
317 | + $request = self::create($action, $data); |
|
318 | + |
|
319 | + $response = self::getClient()->get($request['url'], $request['options']); |
|
320 | + |
|
321 | + return $response->getBody(); |
|
322 | + } |
|
323 | + |
|
324 | + /** |
|
325 | + * Send command |
|
326 | + * |
|
327 | + * @param string $action |
|
328 | + * @param array $data |
|
329 | + * |
|
330 | + * @return Response |
|
331 | + * @throws TelegramException |
|
332 | + */ |
|
333 | + public static function send(string $action, array $data = []): Response |
|
334 | + { |
|
335 | + self::$current_action = $action; |
|
336 | + |
|
337 | + $raw_response = self::execute($action, $data); |
|
338 | + |
|
339 | + if (!Common::isJson($raw_response)) { |
|
340 | + TelegramLog::debug($raw_response); |
|
341 | + throw new TelegramException('Invalid response from API'); |
|
342 | + } |
|
343 | + |
|
344 | + $response = new Response(json_decode($raw_response, true)); |
|
345 | + |
|
346 | + if (!$response->isOk() && $response->getErrorCode() === 401 && $response->getDescription() === 'Unauthorized') { |
|
347 | + throw new InvalidBotTokenException(); |
|
348 | + } |
|
349 | + |
|
350 | + self::$current_action = ''; |
|
351 | + |
|
352 | + return $response; |
|
353 | + } |
|
354 | + |
|
355 | + /** |
|
356 | + * Any statically called method should be relayed to the `send` method. |
|
357 | + * |
|
358 | + * @param string $action |
|
359 | + * @param array $data |
|
360 | + * |
|
361 | + * @return Response |
|
362 | + * @throws TelegramException |
|
363 | + */ |
|
364 | + public static function __callStatic(string $action, array $data): Response |
|
365 | + { |
|
366 | + return static::send($action, reset($data) ?: []); |
|
367 | + } |
|
368 | 368 | |
369 | 369 | } |