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