Total Complexity | 83 |
Total Lines | 766 |
Duplicated Lines | 0 % |
Coverage | 11.96% |
Changes | 0 |
Complex classes like Request often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Request, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
89 | class Request |
||
90 | { |
||
91 | /** |
||
92 | * Telegram object |
||
93 | * |
||
94 | * @var Telegram |
||
95 | */ |
||
96 | private static $telegram; |
||
97 | |||
98 | /** |
||
99 | * URI of the Telegram API |
||
100 | * |
||
101 | * @var string |
||
102 | */ |
||
103 | private static $api_base_uri = 'https://api.telegram.org'; |
||
104 | |||
105 | /** |
||
106 | * Guzzle Client object |
||
107 | * |
||
108 | * @var Client |
||
109 | */ |
||
110 | private static $client; |
||
111 | |||
112 | /** |
||
113 | * Input value of the request |
||
114 | * |
||
115 | * @var string |
||
116 | */ |
||
117 | private static $input; |
||
118 | |||
119 | /** |
||
120 | * Request limiter |
||
121 | * |
||
122 | * @var boolean |
||
123 | */ |
||
124 | private static $limiter_enabled; |
||
125 | |||
126 | /** |
||
127 | * Request limiter's interval between checks |
||
128 | * |
||
129 | * @var float |
||
130 | */ |
||
131 | private static $limiter_interval; |
||
132 | |||
133 | /** |
||
134 | * Get the current action that is being executed |
||
135 | * |
||
136 | * @var string |
||
137 | */ |
||
138 | private static $current_action; |
||
139 | |||
140 | /** |
||
141 | * Available actions to send |
||
142 | * |
||
143 | * This is basically the list of all methods listed on the official API documentation. |
||
144 | * |
||
145 | * @link https://core.telegram.org/bots/api |
||
146 | * |
||
147 | * @var array |
||
148 | */ |
||
149 | private static $actions = [ |
||
150 | 'getUpdates', |
||
151 | 'setWebhook', |
||
152 | 'deleteWebhook', |
||
153 | 'getWebhookInfo', |
||
154 | 'getMe', |
||
155 | 'sendMessage', |
||
156 | 'forwardMessage', |
||
157 | 'sendPhoto', |
||
158 | 'sendAudio', |
||
159 | 'sendDocument', |
||
160 | 'sendSticker', |
||
161 | 'sendVideo', |
||
162 | 'sendAnimation', |
||
163 | 'sendVoice', |
||
164 | 'sendVideoNote', |
||
165 | 'sendMediaGroup', |
||
166 | 'sendLocation', |
||
167 | 'editMessageLiveLocation', |
||
168 | 'stopMessageLiveLocation', |
||
169 | 'sendVenue', |
||
170 | 'sendContact', |
||
171 | 'sendPoll', |
||
172 | 'sendChatAction', |
||
173 | 'getUserProfilePhotos', |
||
174 | 'getFile', |
||
175 | 'kickChatMember', |
||
176 | 'unbanChatMember', |
||
177 | 'restrictChatMember', |
||
178 | 'promoteChatMember', |
||
179 | 'exportChatInviteLink', |
||
180 | 'setChatPhoto', |
||
181 | 'deleteChatPhoto', |
||
182 | 'setChatTitle', |
||
183 | 'setChatDescription', |
||
184 | 'pinChatMessage', |
||
185 | 'unpinChatMessage', |
||
186 | 'leaveChat', |
||
187 | 'getChat', |
||
188 | 'getChatAdministrators', |
||
189 | 'getChatMembersCount', |
||
190 | 'getChatMember', |
||
191 | 'setChatStickerSet', |
||
192 | 'deleteChatStickerSet', |
||
193 | 'answerCallbackQuery', |
||
194 | 'answerInlineQuery', |
||
195 | 'editMessageText', |
||
196 | 'editMessageCaption', |
||
197 | 'editMessageMedia', |
||
198 | 'editMessageReplyMarkup', |
||
199 | 'stopPoll', |
||
200 | 'deleteMessage', |
||
201 | 'getStickerSet', |
||
202 | 'uploadStickerFile', |
||
203 | 'createNewStickerSet', |
||
204 | 'addStickerToSet', |
||
205 | 'setStickerPositionInSet', |
||
206 | 'deleteStickerFromSet', |
||
207 | 'sendInvoice', |
||
208 | 'answerShippingQuery', |
||
209 | 'answerPreCheckoutQuery', |
||
210 | 'setPassportDataErrors', |
||
211 | 'sendGame', |
||
212 | 'setGameScore', |
||
213 | 'getGameHighScores', |
||
214 | ]; |
||
215 | |||
216 | /** |
||
217 | * Some methods need a dummy param due to certain cURL issues. |
||
218 | * |
||
219 | * @see Request::addDummyParamIfNecessary() |
||
220 | * |
||
221 | * @var array |
||
222 | */ |
||
223 | private static $actions_need_dummy_param = [ |
||
224 | 'deleteWebhook', |
||
225 | 'getWebhookInfo', |
||
226 | 'getMe', |
||
227 | ]; |
||
228 | |||
229 | /** |
||
230 | * Available fields for InputFile helper |
||
231 | * |
||
232 | * This is basically the list of all fields that allow InputFile objects |
||
233 | * for which input can be simplified by providing local path directly as string. |
||
234 | * |
||
235 | * @var array |
||
236 | */ |
||
237 | private static $input_file_fields = [ |
||
238 | 'setWebhook' => ['certificate'], |
||
239 | 'sendPhoto' => ['photo'], |
||
240 | 'sendAudio' => ['audio', 'thumb'], |
||
241 | 'sendDocument' => ['document', 'thumb'], |
||
242 | 'sendVideo' => ['video', 'thumb'], |
||
243 | 'sendAnimation' => ['animation', 'thumb'], |
||
244 | 'sendVoice' => ['voice', 'thumb'], |
||
245 | 'sendVideoNote' => ['video_note', 'thumb'], |
||
246 | 'setChatPhoto' => ['photo'], |
||
247 | 'sendSticker' => ['sticker'], |
||
248 | 'uploadStickerFile' => ['png_sticker'], |
||
249 | 'createNewStickerSet' => ['png_sticker'], |
||
250 | 'addStickerToSet' => ['png_sticker'], |
||
251 | ]; |
||
252 | |||
253 | /** |
||
254 | * Initialize |
||
255 | * |
||
256 | * @param Telegram $telegram |
||
257 | * |
||
258 | * @throws TelegramException |
||
259 | */ |
||
260 | 30 | public static function initialize(Telegram $telegram) |
|
261 | { |
||
262 | 30 | if (!($telegram instanceof Telegram)) { |
|
|
|||
263 | throw new TelegramException('Invalid Telegram pointer!'); |
||
264 | } |
||
265 | |||
266 | 30 | self::$telegram = $telegram; |
|
267 | 30 | self::setClient(new Client(['base_uri' => self::$api_base_uri])); |
|
268 | 30 | } |
|
269 | |||
270 | /** |
||
271 | * Set a custom Guzzle HTTP Client object |
||
272 | * |
||
273 | * @param Client $client |
||
274 | * |
||
275 | * @throws TelegramException |
||
276 | */ |
||
277 | 30 | public static function setClient(Client $client) |
|
278 | { |
||
279 | 30 | if (!($client instanceof Client)) { |
|
280 | throw new TelegramException('Invalid GuzzleHttp\Client pointer!'); |
||
281 | } |
||
282 | |||
283 | 30 | self::$client = $client; |
|
284 | 30 | } |
|
285 | |||
286 | /** |
||
287 | * Set input from custom input or stdin and return it |
||
288 | * |
||
289 | * @return string |
||
290 | * @throws TelegramException |
||
291 | */ |
||
292 | public static function getInput() |
||
293 | { |
||
294 | // First check if a custom input has been set, else get the PHP input. |
||
295 | if (!($input = self::$telegram->getCustomInput())) { |
||
296 | $input = file_get_contents('php://input'); |
||
297 | } |
||
298 | |||
299 | // Make sure we have a string to work with. |
||
300 | if (!is_string($input)) { |
||
301 | throw new TelegramException('Input must be a string!'); |
||
302 | } |
||
303 | |||
304 | self::$input = $input; |
||
305 | |||
306 | TelegramLog::update(self::$input); |
||
307 | |||
308 | return self::$input; |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * Generate general fake server response |
||
313 | * |
||
314 | * @param array $data Data to add to fake response |
||
315 | * |
||
316 | * @return array Fake response data |
||
317 | */ |
||
318 | 1 | public static function generateGeneralFakeServerResponse(array $data = []) |
|
319 | { |
||
320 | //PARAM BINDED IN PHPUNIT TEST FOR TestServerResponse.php |
||
321 | //Maybe this is not the best possible implementation |
||
322 | |||
323 | //No value set in $data ie testing setWebhook |
||
324 | //Provided $data['chat_id'] ie testing sendMessage |
||
325 | |||
326 | 1 | $fake_response = ['ok' => true]; // :) |
|
327 | |||
328 | 1 | if ($data === []) { |
|
329 | 1 | $fake_response['result'] = true; |
|
330 | } |
||
331 | |||
332 | //some data to let iniatilize the class method SendMessage |
||
333 | 1 | if (isset($data['chat_id'])) { |
|
334 | 1 | $data['message_id'] = '1234'; |
|
335 | 1 | $data['date'] = '1441378360'; |
|
336 | 1 | $data['from'] = [ |
|
337 | 'id' => 123456789, |
||
338 | 'first_name' => 'botname', |
||
339 | 'username' => 'namebot', |
||
340 | ]; |
||
341 | 1 | $data['chat'] = ['id' => $data['chat_id']]; |
|
342 | |||
343 | 1 | $fake_response['result'] = $data; |
|
344 | } |
||
345 | |||
346 | 1 | return $fake_response; |
|
347 | } |
||
348 | |||
349 | /** |
||
350 | * Properly set up the request params |
||
351 | * |
||
352 | * If any item of the array is a resource, reformat it to a multipart request. |
||
353 | * Else, just return the passed data as form params. |
||
354 | * |
||
355 | * @param array $data |
||
356 | * |
||
357 | * @return array |
||
358 | * @throws TelegramException |
||
359 | */ |
||
360 | private static function setUpRequestParams(array $data) |
||
361 | { |
||
362 | $has_resource = false; |
||
363 | $multipart = []; |
||
364 | |||
365 | foreach ($data as $key => &$item) { |
||
366 | if ($key === 'media') { |
||
367 | // Magical media input helper. |
||
368 | $item = self::mediaInputHelper($item, $has_resource, $multipart); |
||
369 | } elseif (array_key_exists(self::$current_action, self::$input_file_fields) && in_array($key, self::$input_file_fields[self::$current_action], true)) { |
||
370 | // Allow absolute paths to local files. |
||
371 | if (is_string($item) && file_exists($item)) { |
||
372 | $item = new Stream(self::encodeFile($item)); |
||
373 | } |
||
374 | } elseif (is_array($item)) { |
||
375 | // Convert any nested arrays into JSON strings. |
||
376 | $item = json_encode($item); |
||
377 | } |
||
378 | |||
379 | // Reformat data array in multipart way if it contains a resource |
||
380 | $has_resource |= (is_resource($item) || $item instanceof Stream); |
||
381 | $multipart[] = ['name' => $key, 'contents' => $item]; |
||
382 | } |
||
383 | |||
384 | if ($has_resource) { |
||
385 | return ['multipart' => $multipart]; |
||
386 | } |
||
387 | |||
388 | return ['form_params' => $data]; |
||
389 | } |
||
390 | |||
391 | /** |
||
392 | * Magical input media helper to simplify passing media. |
||
393 | * |
||
394 | * This allows the following: |
||
395 | * Request::editMessageMedia([ |
||
396 | * ... |
||
397 | * 'media' => new InputMediaPhoto([ |
||
398 | * 'caption' => 'Caption!', |
||
399 | * 'media' => Request::encodeFile($local_photo), |
||
400 | * ]), |
||
401 | * ]); |
||
402 | * and |
||
403 | * Request::sendMediaGroup([ |
||
404 | * 'media' => [ |
||
405 | * new InputMediaPhoto(['media' => Request::encodeFile($local_photo_1)]), |
||
406 | * new InputMediaPhoto(['media' => Request::encodeFile($local_photo_2)]), |
||
407 | * new InputMediaVideo(['media' => Request::encodeFile($local_video_1)]), |
||
408 | * ], |
||
409 | * ]); |
||
410 | * and even |
||
411 | * Request::sendMediaGroup([ |
||
412 | * 'media' => [ |
||
413 | * new InputMediaPhoto(['media' => $local_photo_1]), |
||
414 | * new InputMediaPhoto(['media' => $local_photo_2]), |
||
415 | * new InputMediaVideo(['media' => $local_video_1]), |
||
416 | * ], |
||
417 | * ]); |
||
418 | * |
||
419 | * @param mixed $item |
||
420 | * @param bool $has_resource |
||
421 | * @param array $multipart |
||
422 | * |
||
423 | * @return mixed |
||
424 | * @throws TelegramException |
||
425 | */ |
||
426 | private static function mediaInputHelper($item, &$has_resource, array &$multipart) |
||
427 | { |
||
428 | $was_array = is_array($item); |
||
429 | $was_array || $item = [$item]; |
||
430 | |||
431 | foreach ($item as $media_item) { |
||
432 | if (!($media_item instanceof InputMedia)) { |
||
433 | continue; |
||
434 | } |
||
435 | |||
436 | $media = $media_item->getMedia(); |
||
1 ignored issue
–
show
|
|||
437 | |||
438 | // Allow absolute paths to local files. |
||
439 | if (is_string($media) && file_exists($media)) { |
||
440 | $media = new Stream(self::encodeFile($media)); |
||
441 | } |
||
442 | |||
443 | if (is_resource($media) || $media instanceof Stream) { |
||
444 | $has_resource = true; |
||
445 | $rnd_key = uniqid('media_', false); |
||
446 | $multipart[] = ['name' => $rnd_key, 'contents' => $media]; |
||
447 | |||
448 | // We're literally overwriting the passed media data! |
||
449 | $media_item->media = 'attach://' . $rnd_key; |
||
450 | $media_item->raw_data['media'] = 'attach://' . $rnd_key; |
||
451 | } |
||
452 | } |
||
453 | |||
454 | $was_array || $item = reset($item); |
||
455 | |||
456 | return json_encode($item); |
||
457 | } |
||
458 | |||
459 | /** |
||
460 | * Get the current action that's being executed |
||
461 | * |
||
462 | * @return string |
||
463 | */ |
||
464 | 7 | public static function getCurrentAction() |
|
465 | { |
||
466 | 7 | return self::$current_action; |
|
467 | } |
||
468 | |||
469 | /** |
||
470 | * Execute HTTP Request |
||
471 | * |
||
472 | * @param string $action Action to execute |
||
473 | * @param array $data Data to attach to the execution |
||
474 | * |
||
475 | * @return string Result of the HTTP Request |
||
476 | * @throws TelegramException |
||
477 | */ |
||
478 | public static function execute($action, array $data = []) |
||
479 | { |
||
480 | //Fix so that the keyboard markup is a string, not an object |
||
481 | if (isset($data['reply_markup'])) { |
||
482 | $data['reply_markup'] = json_encode($data['reply_markup']); |
||
483 | } |
||
484 | |||
485 | $result = null; |
||
486 | $request_params = self::setUpRequestParams($data); |
||
487 | $request_params['debug'] = TelegramLog::getDebugLogTempStream(); |
||
488 | |||
489 | try { |
||
490 | $response = self::$client->post( |
||
491 | '/bot' . self::$telegram->getApiKey() . '/' . $action, |
||
492 | $request_params |
||
493 | ); |
||
494 | $result = (string) $response->getBody(); |
||
495 | |||
496 | //Logging getUpdates Update |
||
497 | if ($action === 'getUpdates') { |
||
498 | TelegramLog::update($result); |
||
499 | } |
||
500 | } catch (RequestException $e) { |
||
501 | $result = $e->getResponse() ? (string) $e->getResponse()->getBody() : ''; |
||
502 | } finally { |
||
503 | //Logging verbose debug output |
||
504 | TelegramLog::endDebugLogTempStream('Verbose HTTP Request output:' . PHP_EOL . '%s' . PHP_EOL); |
||
505 | } |
||
506 | |||
507 | return $result; |
||
508 | } |
||
509 | |||
510 | /** |
||
511 | * Download file |
||
512 | * |
||
513 | * @param File $file |
||
514 | * |
||
515 | * @return boolean |
||
516 | * @throws TelegramException |
||
517 | */ |
||
518 | public static function downloadFile(File $file) |
||
519 | { |
||
520 | if (empty($download_path = self::$telegram->getDownloadPath())) { |
||
521 | throw new TelegramException('Download path not set!'); |
||
522 | } |
||
523 | |||
524 | $tg_file_path = $file->getFilePath(); |
||
525 | $file_path = $download_path . '/' . $tg_file_path; |
||
526 | |||
527 | $file_dir = dirname($file_path); |
||
528 | //For safety reasons, first try to create the directory, then check that it exists. |
||
529 | //This is in case some other process has created the folder in the meantime. |
||
530 | if (!@mkdir($file_dir, 0755, true) && !is_dir($file_dir)) { |
||
531 | throw new TelegramException('Directory ' . $file_dir . ' can\'t be created'); |
||
532 | } |
||
533 | |||
534 | $debug_handle = TelegramLog::getDebugLogTempStream(); |
||
535 | |||
536 | try { |
||
537 | self::$client->get( |
||
538 | '/file/bot' . self::$telegram->getApiKey() . '/' . $tg_file_path, |
||
539 | ['debug' => $debug_handle, 'sink' => $file_path] |
||
540 | ); |
||
541 | |||
542 | return filesize($file_path) > 0; |
||
543 | } catch (RequestException $e) { |
||
544 | return ($e->getResponse()) ? (string) $e->getResponse()->getBody() : ''; |
||
545 | } finally { |
||
546 | //Logging verbose debug output |
||
547 | TelegramLog::endDebugLogTempStream('Verbose HTTP File Download Request output:' . PHP_EOL . '%s' . PHP_EOL); |
||
548 | } |
||
549 | } |
||
550 | |||
551 | /** |
||
552 | * Encode file |
||
553 | * |
||
554 | * @param string $file |
||
555 | * |
||
556 | * @return resource |
||
557 | * @throws TelegramException |
||
558 | */ |
||
559 | public static function encodeFile($file) |
||
560 | { |
||
561 | $fp = fopen($file, 'rb'); |
||
562 | if ($fp === false) { |
||
563 | throw new TelegramException('Cannot open "' . $file . '" for reading'); |
||
564 | } |
||
565 | |||
566 | return $fp; |
||
567 | } |
||
568 | |||
569 | /** |
||
570 | * Send command |
||
571 | * |
||
572 | * @todo Fake response doesn't need json encoding? |
||
573 | * @todo Write debug entry on failure |
||
574 | * |
||
575 | * @param string $action |
||
576 | * @param array $data |
||
577 | * |
||
578 | * @return ServerResponse |
||
579 | * @throws TelegramException |
||
580 | */ |
||
581 | public static function send($action, array $data = []) |
||
582 | { |
||
583 | self::ensureValidAction($action); |
||
584 | self::addDummyParamIfNecessary($action, $data); |
||
585 | |||
586 | $bot_username = self::$telegram->getBotUsername(); |
||
587 | |||
588 | if (defined('PHPUNIT_TESTSUITE')) { |
||
589 | $fake_response = self::generateGeneralFakeServerResponse($data); |
||
590 | |||
591 | return new ServerResponse($fake_response, $bot_username); |
||
592 | } |
||
593 | |||
594 | self::ensureNonEmptyData($data); |
||
595 | |||
596 | self::limitTelegramRequests($action, $data); |
||
597 | |||
598 | // Remember which action is currently being executed. |
||
599 | self::$current_action = $action; |
||
600 | |||
601 | $raw_response = self::execute($action, $data); |
||
602 | $response = json_decode($raw_response, true); |
||
603 | |||
604 | if (null === $response) { |
||
605 | TelegramLog::debug($raw_response); |
||
606 | throw new TelegramException('Telegram returned an invalid response!'); |
||
607 | } |
||
608 | |||
609 | $response = new ServerResponse($response, $bot_username); |
||
610 | |||
611 | if (!$response->isOk() && $response->getErrorCode() === 401 && $response->getDescription() === 'Unauthorized') { |
||
612 | throw new InvalidBotTokenException(); |
||
613 | } |
||
614 | |||
615 | // Reset current action after completion. |
||
616 | self::$current_action = null; |
||
617 | |||
618 | return $response; |
||
619 | } |
||
620 | |||
621 | /** |
||
622 | * Add a dummy parameter if the passed action requires it. |
||
623 | * |
||
624 | * If a method doesn't require parameters, we need to add a dummy one anyway, |
||
625 | * because of some cURL version failed POST request without parameters. |
||
626 | * |
||
627 | * @link https://github.com/php-telegram-bot/core/pull/228 |
||
628 | * |
||
629 | * @todo Would be nice to find a better solution for this! |
||
630 | * |
||
631 | * @param string $action |
||
632 | * @param array $data |
||
633 | */ |
||
634 | protected static function addDummyParamIfNecessary($action, array &$data) |
||
635 | { |
||
636 | if (in_array($action, self::$actions_need_dummy_param, true)) { |
||
637 | // Can be anything, using a single letter to minimise request size. |
||
638 | $data = ['d']; |
||
639 | } |
||
640 | } |
||
641 | |||
642 | /** |
||
643 | * Make sure the data isn't empty, else throw an exception |
||
644 | * |
||
645 | * @param array $data |
||
646 | * |
||
647 | * @throws TelegramException |
||
648 | */ |
||
649 | private static function ensureNonEmptyData(array $data) |
||
650 | { |
||
651 | if (count($data) === 0) { |
||
652 | throw new TelegramException('Data is empty!'); |
||
653 | } |
||
654 | } |
||
655 | |||
656 | /** |
||
657 | * Make sure the action is valid, else throw an exception |
||
658 | * |
||
659 | * @param string $action |
||
660 | * |
||
661 | * @throws TelegramException |
||
662 | */ |
||
663 | private static function ensureValidAction($action) |
||
664 | { |
||
665 | if (!in_array($action, self::$actions, true)) { |
||
666 | throw new TelegramException('The action "' . $action . '" doesn\'t exist!'); |
||
667 | } |
||
668 | } |
||
669 | |||
670 | /** |
||
671 | * Use this method to send text messages. On success, the sent Message is returned |
||
672 | * |
||
673 | * @link https://core.telegram.org/bots/api#sendmessage |
||
674 | * |
||
675 | * @param array $data |
||
676 | * |
||
677 | * @return ServerResponse |
||
678 | * @throws TelegramException |
||
679 | */ |
||
680 | public static function sendMessage(array $data) |
||
681 | { |
||
682 | $text = $data['text']; |
||
683 | |||
684 | do { |
||
685 | //Chop off and send the first message |
||
686 | $data['text'] = mb_substr($text, 0, 4096); |
||
687 | $response = self::send('sendMessage', $data); |
||
688 | |||
689 | //Prepare the next message |
||
690 | $text = mb_substr($text, 4096); |
||
691 | } while (mb_strlen($text, 'UTF-8') > 0); |
||
692 | |||
693 | return $response; |
||
694 | } |
||
695 | |||
696 | /** |
||
697 | * Any statically called method should be relayed to the `send` method. |
||
698 | * |
||
699 | * @param string $action |
||
700 | * @param array $data |
||
701 | * |
||
702 | * @return ServerResponse |
||
703 | */ |
||
704 | public static function __callStatic($action, array $data) |
||
705 | { |
||
706 | // Make sure to add the action being called as the first parameter to be passed. |
||
707 | array_unshift($data, $action); |
||
708 | |||
709 | // @todo Use splat operator for unpacking when we move to PHP 5.6+ |
||
710 | return call_user_func_array('static::send', $data); |
||
711 | } |
||
712 | |||
713 | /** |
||
714 | * Return an empty Server Response |
||
715 | * |
||
716 | * No request to telegram are sent, this function is used in commands that |
||
717 | * don't need to fire a message after execution |
||
718 | * |
||
719 | * @return ServerResponse |
||
720 | */ |
||
721 | public static function emptyResponse() |
||
722 | { |
||
723 | return new ServerResponse(['ok' => true, 'result' => true], null); |
||
724 | } |
||
725 | |||
726 | /** |
||
727 | * Send message to all active chats |
||
728 | * |
||
729 | * @param string $callback_function |
||
730 | * @param array $data |
||
731 | * @param array $select_chats_params |
||
732 | * |
||
733 | * @return array |
||
734 | * @throws TelegramException |
||
735 | */ |
||
736 | public static function sendToActiveChats( |
||
737 | $callback_function, |
||
738 | array $data, |
||
739 | array $select_chats_params |
||
740 | ) { |
||
741 | self::ensureValidAction($callback_function); |
||
742 | |||
743 | $chats = DB::selectChats($select_chats_params); |
||
744 | |||
745 | $results = []; |
||
746 | if (is_array($chats)) { |
||
747 | foreach ($chats as $row) { |
||
748 | $data['chat_id'] = $row['chat_id']; |
||
749 | $results[] = self::send($callback_function, $data); |
||
750 | } |
||
751 | } |
||
752 | |||
753 | return $results; |
||
754 | } |
||
755 | |||
756 | /** |
||
757 | * Enable request limiter |
||
758 | * |
||
759 | * @param boolean $enable |
||
760 | * @param array $options |
||
761 | * |
||
762 | * @throws TelegramException |
||
763 | */ |
||
764 | public static function setLimiter($enable = true, array $options = []) |
||
779 | } |
||
780 | } |
||
781 | |||
782 | /** |
||
783 | * This functions delays API requests to prevent reaching Telegram API limits |
||
784 | * Can be disabled while in execution by 'Request::setLimiter(false)' |
||
785 | * |
||
786 | * @link https://core.telegram.org/bots/faq#my-bot-is-hitting-limits-how-do-i-avoid-this |
||
787 | * |
||
788 | * @param string $action |
||
789 | * @param array $data |
||
790 | * |
||
791 | * @throws TelegramException |
||
792 | */ |
||
793 | private static function limitTelegramRequests($action, array $data = []) |
||
794 | { |
||
795 | if (self::$limiter_enabled) { |
||
855 | } |
||
856 | } |
||
857 | } |
||
859 |