Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like BotApi 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 BotApi, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class BotApi |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * HTTP codes |
||
| 24 | * |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | public static $codes = [ |
||
| 28 | // Informational 1xx |
||
| 29 | 100 => 'Continue', |
||
| 30 | 101 => 'Switching Protocols', |
||
| 31 | 102 => 'Processing', // RFC2518 |
||
| 32 | // Success 2xx |
||
| 33 | 200 => 'OK', |
||
| 34 | 201 => 'Created', |
||
| 35 | 202 => 'Accepted', |
||
| 36 | 203 => 'Non-Authoritative Information', |
||
| 37 | 204 => 'No Content', |
||
| 38 | 205 => 'Reset Content', |
||
| 39 | 206 => 'Partial Content', |
||
| 40 | 207 => 'Multi-Status', // RFC4918 |
||
| 41 | 208 => 'Already Reported', // RFC5842 |
||
| 42 | 226 => 'IM Used', // RFC3229 |
||
| 43 | // Redirection 3xx |
||
| 44 | 300 => 'Multiple Choices', |
||
| 45 | 301 => 'Moved Permanently', |
||
| 46 | 302 => 'Found', // 1.1 |
||
| 47 | 303 => 'See Other', |
||
| 48 | 304 => 'Not Modified', |
||
| 49 | 305 => 'Use Proxy', |
||
| 50 | // 306 is deprecated but reserved |
||
| 51 | 307 => 'Temporary Redirect', |
||
| 52 | 308 => 'Permanent Redirect', // RFC7238 |
||
| 53 | // Client Error 4xx |
||
| 54 | 400 => 'Bad Request', |
||
| 55 | 401 => 'Unauthorized', |
||
| 56 | 402 => 'Payment Required', |
||
| 57 | 403 => 'Forbidden', |
||
| 58 | 404 => 'Not Found', |
||
| 59 | 405 => 'Method Not Allowed', |
||
| 60 | 406 => 'Not Acceptable', |
||
| 61 | 407 => 'Proxy Authentication Required', |
||
| 62 | 408 => 'Request Timeout', |
||
| 63 | 409 => 'Conflict', |
||
| 64 | 410 => 'Gone', |
||
| 65 | 411 => 'Length Required', |
||
| 66 | 412 => 'Precondition Failed', |
||
| 67 | 413 => 'Payload Too Large', |
||
| 68 | 414 => 'URI Too Long', |
||
| 69 | 415 => 'Unsupported Media Type', |
||
| 70 | 416 => 'Range Not Satisfiable', |
||
| 71 | 417 => 'Expectation Failed', |
||
| 72 | 422 => 'Unprocessable Entity', // RFC4918 |
||
| 73 | 423 => 'Locked', // RFC4918 |
||
| 74 | 424 => 'Failed Dependency', // RFC4918 |
||
| 75 | 425 => 'Reserved for WebDAV advanced collections expired proposal', // RFC2817 |
||
| 76 | 426 => 'Upgrade Required', // RFC2817 |
||
| 77 | 428 => 'Precondition Required', // RFC6585 |
||
| 78 | 429 => 'Too Many Requests', // RFC6585 |
||
| 79 | 431 => 'Request Header Fields Too Large', // RFC6585 |
||
| 80 | // Server Error 5xx |
||
| 81 | 500 => 'Internal Server Error', |
||
| 82 | 501 => 'Not Implemented', |
||
| 83 | 502 => 'Bad Gateway', |
||
| 84 | 503 => 'Service Unavailable', |
||
| 85 | 504 => 'Gateway Timeout', |
||
| 86 | 505 => 'HTTP Version Not Supported', |
||
| 87 | 506 => 'Variant Also Negotiates (Experimental)', // RFC2295 |
||
| 88 | 507 => 'Insufficient Storage', // RFC4918 |
||
| 89 | 508 => 'Loop Detected', // RFC5842 |
||
| 90 | 510 => 'Not Extended', // RFC2774 |
||
| 91 | 511 => 'Network Authentication Required', // RFC6585 |
||
| 92 | ]; |
||
| 93 | |||
| 94 | |||
| 95 | /** |
||
| 96 | * Default http status code |
||
| 97 | */ |
||
| 98 | const DEFAULT_STATUS_CODE = 200; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Not Modified http status code |
||
| 102 | */ |
||
| 103 | const NOT_MODIFIED_STATUS_CODE = 304; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Limits for tracked ids |
||
| 107 | */ |
||
| 108 | const MAX_TRACKED_EVENTS = 200; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Url prefixes |
||
| 112 | */ |
||
| 113 | const URL_PREFIX = 'https://api.telegram.org/bot'; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Url prefix for files |
||
| 117 | */ |
||
| 118 | const FILE_URL_PREFIX = 'https://api.telegram.org/file/bot'; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * CURL object |
||
| 122 | * |
||
| 123 | * @var |
||
| 124 | */ |
||
| 125 | protected $curl; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Bot token |
||
| 129 | * |
||
| 130 | * @var string |
||
| 131 | */ |
||
| 132 | protected $token; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Botan tracker |
||
| 136 | * |
||
| 137 | * @var \TelegramBot\Api\Botan |
||
| 138 | */ |
||
| 139 | protected $tracker; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * list of event ids |
||
| 143 | * |
||
| 144 | * @var array |
||
| 145 | */ |
||
| 146 | protected $trackedEvents = []; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Check whether return associative array |
||
| 150 | * |
||
| 151 | * @var bool |
||
| 152 | */ |
||
| 153 | protected $returnArray = true; |
||
| 154 | |||
| 155 | |||
| 156 | /** |
||
| 157 | * Constructor |
||
| 158 | * |
||
| 159 | * @param string $token Telegram Bot API token |
||
| 160 | * @param string|null $trackerToken Yandex AppMetrica application api_key |
||
| 161 | */ |
||
| 162 | 9 | public function __construct($token, $trackerToken = null) |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Set return array |
||
| 174 | * |
||
| 175 | * @param bool $mode |
||
| 176 | * |
||
| 177 | * @return $this |
||
| 178 | */ |
||
| 179 | public function setModeObject($mode = true) |
||
| 185 | |||
| 186 | |||
| 187 | /** |
||
| 188 | * Call method |
||
| 189 | * |
||
| 190 | * @param string $method |
||
| 191 | * @param array|null $data |
||
| 192 | * |
||
| 193 | * @return mixed |
||
| 194 | * @throws \TelegramBot\Api\Exception |
||
| 195 | * @throws \TelegramBot\Api\HttpException |
||
| 196 | * @throws \TelegramBot\Api\InvalidJsonException |
||
| 197 | */ |
||
| 198 | public function call($method, array $data = null) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * curl_exec wrapper for response validation |
||
| 231 | * |
||
| 232 | * @param array $options |
||
| 233 | * |
||
| 234 | * @return string |
||
| 235 | * |
||
| 236 | * @throws \TelegramBot\Api\HttpException |
||
| 237 | */ |
||
| 238 | protected function executeCurl(array $options) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Response validation |
||
| 253 | * |
||
| 254 | * @param resource $curl |
||
| 255 | * @param string $response |
||
| 256 | * @throws HttpException |
||
| 257 | */ |
||
| 258 | public static function curlValidate($curl, $response = null) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * JSON validation |
||
| 272 | * |
||
| 273 | * @param string $jsonString |
||
| 274 | * @param boolean $asArray |
||
| 275 | * |
||
| 276 | * @return object|array |
||
| 277 | * @throws \TelegramBot\Api\InvalidJsonException |
||
| 278 | */ |
||
| 279 | public static function jsonValidate($jsonString, $asArray) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Use this method to send text messages. On success, the sent \TelegramBot\Api\Types\Message is returned. |
||
| 292 | * |
||
| 293 | * @param int|string $chatId |
||
| 294 | * @param string $text |
||
| 295 | * @param string|null $parseMode |
||
| 296 | * @param bool $disablePreview |
||
| 297 | * @param int|null $replyToMessageId |
||
| 298 | * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|null $replyMarkup |
||
| 299 | * @param bool $disableNotification |
||
| 300 | * |
||
| 301 | * @return \TelegramBot\Api\Types\Message |
||
| 302 | * @throws \TelegramBot\Api\InvalidArgumentException |
||
| 303 | * @throws \TelegramBot\Api\Exception |
||
| 304 | */ |
||
| 305 | public function sendMessage( |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Use this method to send phone contacts |
||
| 327 | * |
||
| 328 | * @param int|string $chatId chat_id or @channel_name |
||
| 329 | * @param string $phoneNumber |
||
| 330 | * @param string $firstName |
||
| 331 | * @param string $lastName |
||
| 332 | * @param int|null $replyToMessageId |
||
| 333 | * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|null $replyMarkup |
||
| 334 | * @param bool $disableNotification |
||
| 335 | * |
||
| 336 | * @return \TelegramBot\Api\Types\Message |
||
| 337 | * @throws \TelegramBot\Api\Exception |
||
| 338 | */ |
||
| 339 | public function sendContact( |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Use this method when you need to tell the user that something is happening on the bot's side. |
||
| 361 | * The status is set for 5 seconds or less (when a message arrives from your bot, |
||
| 362 | * Telegram clients clear its typing status). |
||
| 363 | * |
||
| 364 | * We only recommend using this method when a response from the bot will take a noticeable amount of time to arrive. |
||
| 365 | * |
||
| 366 | * Type of action to broadcast. Choose one, depending on what the user is about to receive: |
||
| 367 | * `typing` for text messages, `upload_photo` for photos, `record_video` or `upload_video` for videos, |
||
| 368 | * `record_audio` or upload_audio for audio files, `upload_document` for general files, |
||
| 369 | * `find_location` for location data. |
||
| 370 | * |
||
| 371 | * @param int $chatId |
||
| 372 | * @param string $action |
||
| 373 | * |
||
| 374 | * @return bool |
||
| 375 | * @throws \TelegramBot\Api\Exception |
||
| 376 | */ |
||
| 377 | public function sendChatAction($chatId, $action) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Use this method to get a list of profile pictures for a user. |
||
| 387 | * |
||
| 388 | * @param int $userId |
||
| 389 | * @param int $offset |
||
| 390 | * @param int $limit |
||
| 391 | * |
||
| 392 | * @return \TelegramBot\Api\Types\UserProfilePhotos |
||
| 393 | * @throws \TelegramBot\Api\Exception |
||
| 394 | */ |
||
| 395 | public function getUserProfilePhotos($userId, $offset = 0, $limit = 100) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Use this method to specify a url and receive incoming updates via an outgoing webhook. |
||
| 406 | * Whenever there is an update for the bot, we will send an HTTPS POST request to the specified url, |
||
| 407 | * containing a JSON-serialized Update. |
||
| 408 | * In case of an unsuccessful request, we will give up after a reasonable amount of attempts. |
||
| 409 | * |
||
| 410 | * @param string $url HTTPS url to send updates to. Use an empty string to remove webhook integration |
||
| 411 | * @param \CURLFile|string $certificate Upload your public key certificate |
||
| 412 | * so that the root certificate in use can be checked |
||
| 413 | * |
||
| 414 | * @return string |
||
| 415 | * |
||
| 416 | * @throws \TelegramBot\Api\Exception |
||
| 417 | */ |
||
| 418 | public function setWebhook($url = '', $certificate = null) |
||
| 422 | |||
| 423 | |||
| 424 | /** |
||
| 425 | * Use this method to clear webhook and use getUpdates again! |
||
| 426 | * |
||
| 427 | * @return mixed |
||
| 428 | * |
||
| 429 | * @throws \TelegramBot\Api\Exception |
||
| 430 | */ |
||
| 431 | public function deleteWebhook() |
||
| 435 | |||
| 436 | /** |
||
| 437 | * A simple method for testing your bot's auth token.Requires no parameters. |
||
| 438 | * Returns basic information about the bot in form of a User object. |
||
| 439 | * |
||
| 440 | * @return \TelegramBot\Api\Types\User |
||
| 441 | * @throws \TelegramBot\Api\Exception |
||
| 442 | * @throws \TelegramBot\Api\InvalidArgumentException |
||
| 443 | */ |
||
| 444 | public function getMe() |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Use this method to receive incoming updates using long polling. |
||
| 451 | * An Array of Update objects is returned. |
||
| 452 | * |
||
| 453 | * Notes |
||
| 454 | * 1. This method will not work if an outgoing webhook is set up. |
||
| 455 | * 2. In order to avoid getting duplicate updates, recalculate offset after each server response. |
||
| 456 | * |
||
| 457 | * @param int $offset |
||
| 458 | * @param int $limit |
||
| 459 | * @param int $timeout |
||
| 460 | * |
||
| 461 | * @return Update[] |
||
| 462 | * @throws \TelegramBot\Api\Exception |
||
| 463 | * @throws \TelegramBot\Api\InvalidArgumentException |
||
| 464 | */ |
||
| 465 | 2 | public function getUpdates($offset = 0, $limit = 100, $timeout = 0) |
|
| 481 | |||
| 482 | /** |
||
| 483 | * Use this method to send point on the map. On success, the sent Message is returned. |
||
| 484 | * |
||
| 485 | * @param int|string $chatId |
||
| 486 | * @param float $latitude |
||
| 487 | * @param float $longitude |
||
| 488 | * @param int|null $replyToMessageId |
||
| 489 | * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|null $replyMarkup |
||
| 490 | * @param bool $disableNotification |
||
| 491 | * |
||
| 492 | * @param null|int $livePeriod |
||
| 493 | * @return \TelegramBot\Api\Types\Message |
||
| 494 | */ |
||
| 495 | public function sendLocation( |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Use this method to edit live location messages sent by the bot or via the bot (for inline bots). |
||
| 517 | * |
||
| 518 | * @param int|string $chatId |
||
| 519 | * @param int $messageId |
||
| 520 | * @param string $inlineMessageId |
||
| 521 | * @param float $latitude |
||
| 522 | * @param float $longitude |
||
| 523 | * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|null $replyMarkup |
||
| 524 | * @return \TelegramBot\Api\Types\Message |
||
| 525 | */ |
||
| 526 | public function editMessageLiveLocation( |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Use this method to stop updating a live location message sent by the bot or via the bot (for inline bots) before |
||
| 546 | * live_period expires. |
||
| 547 | * |
||
| 548 | * @param int|string $chatId |
||
| 549 | * @param int $messageId |
||
| 550 | * @param string $inlineMessageId |
||
| 551 | * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|null $replyMarkup |
||
| 552 | * @return \TelegramBot\Api\Types\Message |
||
| 553 | */ |
||
| 554 | public function stopMessageLiveLocation( |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Use this method to send information about a venue. On success, the sent Message is returned. |
||
| 570 | * |
||
| 571 | * @param int|string $chatId chat_id or @channel_name |
||
| 572 | * @param float $latitude |
||
| 573 | * @param float $longitude |
||
| 574 | * @param string $title |
||
| 575 | * @param string $address |
||
| 576 | * @param string|null $foursquareId |
||
| 577 | * @param int|null $replyToMessageId |
||
| 578 | * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|null $replyMarkup |
||
| 579 | * @param bool $disableNotification |
||
| 580 | * |
||
| 581 | * @return \TelegramBot\Api\Types\Message |
||
| 582 | * @throws \TelegramBot\Api\Exception |
||
| 583 | */ |
||
| 584 | public function sendVenue( |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Use this method to send .webp stickers. On success, the sent Message is returned. |
||
| 610 | * |
||
| 611 | * @param int|string $chatId chat_id or @channel_name |
||
| 612 | * @param \CURLFile|string $sticker |
||
| 613 | * @param int|null $replyToMessageId |
||
| 614 | * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|null $replyMarkup |
||
| 615 | * @param bool $disableNotification |
||
| 616 | * |
||
| 617 | * @return \TelegramBot\Api\Types\Message |
||
| 618 | * @throws \TelegramBot\Api\InvalidArgumentException |
||
| 619 | * @throws \TelegramBot\Api\Exception |
||
| 620 | */ |
||
| 621 | View Code Duplication | public function sendSticker( |
|
| 636 | |||
| 637 | /** |
||
| 638 | * Use this method to send video files, |
||
| 639 | * Telegram clients support mp4 videos (other formats may be sent as Document). |
||
| 640 | * On success, the sent Message is returned. |
||
| 641 | * |
||
| 642 | * @param int|string $chatId chat_id or @channel_name |
||
| 643 | * @param \CURLFile|string $video |
||
| 644 | * @param int|null $duration |
||
| 645 | * @param string|null $caption |
||
| 646 | * @param int|null $replyToMessageId |
||
| 647 | * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|null $replyMarkup |
||
| 648 | * @param bool $disableNotification |
||
| 649 | * |
||
| 650 | * @return \TelegramBot\Api\Types\Message |
||
| 651 | * @throws \TelegramBot\Api\InvalidArgumentException |
||
| 652 | * @throws \TelegramBot\Api\Exception |
||
| 653 | */ |
||
| 654 | public function sendVideo( |
||
| 673 | |||
| 674 | /** |
||
| 675 | * Use this method to send audio files, |
||
| 676 | * if you want Telegram clients to display the file as a playable voice message. |
||
| 677 | * For this to work, your audio must be in an .ogg file encoded with OPUS |
||
| 678 | * (other formats may be sent as Audio or Document). |
||
| 679 | * On success, the sent Message is returned. |
||
| 680 | * Bots can currently send voice messages of up to 50 MB in size, this limit may be changed in the future. |
||
| 681 | * |
||
| 682 | * @param int|string $chatId chat_id or @channel_name |
||
| 683 | * @param \CURLFile|string $voice |
||
| 684 | * @param int|null $duration |
||
| 685 | * @param int|null $replyToMessageId |
||
| 686 | * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|null $replyMarkup |
||
| 687 | * @param bool $disableNotification |
||
| 688 | * |
||
| 689 | * @return \TelegramBot\Api\Types\Message |
||
| 690 | * @throws \TelegramBot\Api\InvalidArgumentException |
||
| 691 | * @throws \TelegramBot\Api\Exception |
||
| 692 | */ |
||
| 693 | View Code Duplication | public function sendVoice( |
|
| 710 | |||
| 711 | /** |
||
| 712 | * Use this method to forward messages of any kind. On success, the sent Message is returned. |
||
| 713 | * |
||
| 714 | * @param int|string $chatId chat_id or @channel_name |
||
| 715 | * @param int $fromChatId |
||
| 716 | * @param int $messageId |
||
| 717 | * @param bool $disableNotification |
||
| 718 | * |
||
| 719 | * @return \TelegramBot\Api\Types\Message |
||
| 720 | * @throws \TelegramBot\Api\InvalidArgumentException |
||
| 721 | * @throws \TelegramBot\Api\Exception |
||
| 722 | */ |
||
| 723 | public function forwardMessage($chatId, $fromChatId, $messageId, $disableNotification = false) |
||
| 732 | |||
| 733 | /** |
||
| 734 | * Use this method to send audio files, |
||
| 735 | * if you want Telegram clients to display them in the music player. |
||
| 736 | * Your audio must be in the .mp3 format. |
||
| 737 | * On success, the sent Message is returned. |
||
| 738 | * Bots can currently send audio files of up to 50 MB in size, this limit may be changed in the future. |
||
| 739 | * |
||
| 740 | * For backward compatibility, when the fields title and performer are both empty |
||
| 741 | * and the mime-type of the file to be sent is not audio/mpeg, the file will be sent as a playable voice message. |
||
| 742 | * For this to work, the audio must be in an .ogg file encoded with OPUS. |
||
| 743 | * This behavior will be phased out in the future. For sending voice messages, use the sendVoice method instead. |
||
| 744 | * |
||
| 745 | * @deprecated since 20th February. Removed backward compatibility from the method sendAudio. |
||
| 746 | * Voice messages now must be sent using the method sendVoice. |
||
| 747 | * There is no more need to specify a non-empty title or performer while sending the audio by file_id. |
||
| 748 | * |
||
| 749 | * @param int|string $chatId chat_id or @channel_name |
||
| 750 | * @param \CURLFile|string $audio |
||
| 751 | * @param int|null $duration |
||
| 752 | * @param string|null $performer |
||
| 753 | * @param string|null $title |
||
| 754 | * @param int|null $replyToMessageId |
||
| 755 | * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|null $replyMarkup |
||
| 756 | * @param bool $disableNotification |
||
| 757 | * |
||
| 758 | * @return \TelegramBot\Api\Types\Message |
||
| 759 | * @throws \TelegramBot\Api\InvalidArgumentException |
||
| 760 | * @throws \TelegramBot\Api\Exception |
||
| 761 | */ |
||
| 762 | public function sendAudio( |
||
| 783 | |||
| 784 | /** |
||
| 785 | * Use this method to send photos. On success, the sent Message is returned. |
||
| 786 | * |
||
| 787 | * @param int|string $chatId chat_id or @channel_name |
||
| 788 | * @param \CURLFile|string $photo |
||
| 789 | * @param string|null $caption |
||
| 790 | * @param int|null $replyToMessageId |
||
| 791 | * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|null $replyMarkup |
||
| 792 | * @param bool $disableNotification |
||
| 793 | * |
||
| 794 | * @return \TelegramBot\Api\Types\Message |
||
| 795 | * @throws \TelegramBot\Api\InvalidArgumentException |
||
| 796 | * @throws \TelegramBot\Api\Exception |
||
| 797 | */ |
||
| 798 | View Code Duplication | public function sendPhoto( |
|
| 815 | |||
| 816 | /** |
||
| 817 | * Use this method to send general files. On success, the sent Message is returned. |
||
| 818 | * Bots can currently send files of any type of up to 50 MB in size, this limit may be changed in the future. |
||
| 819 | * |
||
| 820 | * @param int|string $chatId chat_id or @channel_name |
||
| 821 | * @param \CURLFile|string $document |
||
| 822 | * @param string|null $caption |
||
| 823 | * @param int|null $replyToMessageId |
||
| 824 | * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|null $replyMarkup |
||
| 825 | * @param bool $disableNotification |
||
| 826 | * |
||
| 827 | * @return \TelegramBot\Api\Types\Message |
||
| 828 | * @throws \TelegramBot\Api\InvalidArgumentException |
||
| 829 | * @throws \TelegramBot\Api\Exception |
||
| 830 | */ |
||
| 831 | View Code Duplication | public function sendDocument( |
|
| 848 | |||
| 849 | /** |
||
| 850 | * Use this method to get basic info about a file and prepare it for downloading. |
||
| 851 | * For the moment, bots can download files of up to 20MB in size. |
||
| 852 | * On success, a File object is returned. |
||
| 853 | * The file can then be downloaded via the link https://api.telegram.org/file/bot<token>/<file_path>, |
||
| 854 | * where <file_path> is taken from the response. |
||
| 855 | * It is guaranteed that the link will be valid for at least 1 hour. |
||
| 856 | * When the link expires, a new one can be requested by calling getFile again. |
||
| 857 | * |
||
| 858 | * @param $fileId |
||
| 859 | * |
||
| 860 | * @return \TelegramBot\Api\Types\File |
||
| 861 | * @throws \TelegramBot\Api\InvalidArgumentException |
||
| 862 | * @throws \TelegramBot\Api\Exception |
||
| 863 | */ |
||
| 864 | public function getFile($fileId) |
||
| 868 | |||
| 869 | /** |
||
| 870 | * Get file contents via cURL |
||
| 871 | * |
||
| 872 | * @param $fileId |
||
| 873 | * |
||
| 874 | * @return string |
||
| 875 | * |
||
| 876 | * @throws \TelegramBot\Api\HttpException |
||
| 877 | */ |
||
| 878 | public function downloadFile($fileId) |
||
| 890 | |||
| 891 | /** |
||
| 892 | * Use this method to send answers to an inline query. On success, True is returned. |
||
| 893 | * No more than 50 results per query are allowed. |
||
| 894 | * |
||
| 895 | * @param string $inlineQueryId |
||
| 896 | * @param AbstractInlineQueryResult[] $results |
||
| 897 | * @param int $cacheTime |
||
| 898 | * @param bool $isPersonal |
||
| 899 | * @param string $nextOffset |
||
| 900 | * @param string $switchPmText |
||
| 901 | * @param string $switchPmParameter |
||
| 902 | * |
||
| 903 | * @return mixed |
||
| 904 | * @throws Exception |
||
| 905 | */ |
||
| 906 | public function answerInlineQuery( |
||
| 930 | |||
| 931 | /** |
||
| 932 | * Use this method to kick a user from a group or a supergroup. |
||
| 933 | * In the case of supergroups, the user will not be able to return to the group |
||
| 934 | * on their own using invite links, etc., unless unbanned first. |
||
| 935 | * The bot must be an administrator in the group for this to work. Returns True on success. |
||
| 936 | * |
||
| 937 | * @param int|string $chatId Unique identifier for the target group |
||
| 938 | * or username of the target supergroup (in the format @supergroupusername) |
||
| 939 | * @param int $userId Unique identifier of the target user |
||
| 940 | * @param null|int $untilDate Date when the user will be unbanned, unix time. |
||
| 941 | * If user is banned for more than 366 days or less than 30 seconds from the current time |
||
| 942 | * they are considered to be banned forever |
||
| 943 | * |
||
| 944 | * @return bool |
||
| 945 | */ |
||
| 946 | public function kickChatMember($chatId, $userId, $untilDate = null) |
||
| 954 | |||
| 955 | /** |
||
| 956 | * Use this method to unban a previously kicked user in a supergroup. |
||
| 957 | * The user will not return to the group automatically, but will be able to join via link, etc. |
||
| 958 | * The bot must be an administrator in the group for this to work. Returns True on success. |
||
| 959 | * |
||
| 960 | * @param int|string $chatId Unique identifier for the target group |
||
| 961 | * or username of the target supergroup (in the format @supergroupusername) |
||
| 962 | * @param int $userId Unique identifier of the target user |
||
| 963 | * |
||
| 964 | * @return bool |
||
| 965 | */ |
||
| 966 | public function unbanChatMember($chatId, $userId) |
||
| 973 | |||
| 974 | /** |
||
| 975 | * Use this method to send answers to callback queries sent from inline keyboards. |
||
| 976 | * The answer will be displayed to the user as a notification at the top of the chat screen or as an alert. |
||
| 977 | * |
||
| 978 | * @param $callbackQueryId |
||
| 979 | * @param null $text |
||
| 980 | * @param bool $showAlert |
||
| 981 | * |
||
| 982 | * @return bool |
||
| 983 | */ |
||
| 984 | public function answerCallbackQuery($callbackQueryId, $text = null, $showAlert = false) |
||
| 992 | |||
| 993 | |||
| 994 | /** |
||
| 995 | * Use this method to edit text messages sent by the bot or via the bot |
||
| 996 | * |
||
| 997 | * @param int|string $chatId |
||
| 998 | * @param int $messageId |
||
| 999 | * @param string $text |
||
| 1000 | * @param string $inlineMessageId |
||
| 1001 | * @param string|null $parseMode |
||
| 1002 | * @param bool $disablePreview |
||
| 1003 | * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|null $replyMarkup |
||
| 1004 | * @return Message |
||
| 1005 | */ |
||
| 1006 | public function editMessageText( |
||
| 1025 | |||
| 1026 | /** |
||
| 1027 | * Use this method to edit text messages sent by the bot or via the bot |
||
| 1028 | * |
||
| 1029 | * @param int|string $chatId |
||
| 1030 | * @param int $messageId |
||
| 1031 | * @param string|null $caption |
||
| 1032 | * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|null $replyMarkup |
||
| 1033 | * @param string $inlineMessageId |
||
| 1034 | * |
||
| 1035 | * @return \TelegramBot\Api\Types\Message |
||
| 1036 | * @throws \TelegramBot\Api\InvalidArgumentException |
||
| 1037 | * @throws \TelegramBot\Api\Exception |
||
| 1038 | */ |
||
| 1039 | View Code Duplication | public function editMessageCaption( |
|
| 1054 | |||
| 1055 | /** |
||
| 1056 | * Use this method to edit only the reply markup of messages sent by the bot or via the bot |
||
| 1057 | * |
||
| 1058 | * @param int|string $chatId |
||
| 1059 | * @param int $messageId |
||
| 1060 | * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|null $replyMarkup |
||
| 1061 | * @param string $inlineMessageId |
||
| 1062 | * |
||
| 1063 | * @return Message |
||
| 1064 | */ |
||
| 1065 | public function editMessageReplyMarkup( |
||
| 1078 | |||
| 1079 | /** |
||
| 1080 | * Use this method to delete a message, including service messages, with the following limitations: |
||
| 1081 | * - A message can only be deleted if it was sent less than 48 hours ago. |
||
| 1082 | * - Bots can delete outgoing messages in groups and supergroups. |
||
| 1083 | * - Bots granted can_post_messages permissions can delete outgoing messages in channels. |
||
| 1084 | * - If the bot is an administrator of a group, it can delete any message there. |
||
| 1085 | * - If the bot has can_delete_messages permission in a supergroup or a channel, it can delete any message there. |
||
| 1086 | * |
||
| 1087 | * @param int|string $chatId |
||
| 1088 | * @param int $messageId |
||
| 1089 | * |
||
| 1090 | * @return bool |
||
| 1091 | */ |
||
| 1092 | public function deleteMessage($chatId, $messageId) |
||
| 1099 | |||
| 1100 | /** |
||
| 1101 | * Close curl |
||
| 1102 | */ |
||
| 1103 | 9 | public function __destruct() |
|
| 1107 | |||
| 1108 | /** |
||
| 1109 | * @return string |
||
| 1110 | */ |
||
| 1111 | public function getUrl() |
||
| 1115 | |||
| 1116 | /** |
||
| 1117 | * @return string |
||
| 1118 | */ |
||
| 1119 | public function getFileUrl() |
||
| 1123 | |||
| 1124 | /** |
||
| 1125 | * @param \TelegramBot\Api\Types\Update $update |
||
| 1126 | * @param string $eventName |
||
| 1127 | * |
||
| 1128 | * @throws \TelegramBot\Api\Exception |
||
| 1129 | */ |
||
| 1130 | public function trackUpdate(Update $update, $eventName = 'Message') |
||
| 1142 | |||
| 1143 | /** |
||
| 1144 | * Wrapper for tracker |
||
| 1145 | * |
||
| 1146 | * @param \TelegramBot\Api\Types\Message $message |
||
| 1147 | * @param string $eventName |
||
| 1148 | * |
||
| 1149 | * @throws \TelegramBot\Api\Exception |
||
| 1150 | */ |
||
| 1151 | public function track(Message $message, $eventName = 'Message') |
||
| 1157 | |||
| 1158 | /** |
||
| 1159 | * Use this method to send invoices. On success, the sent Message is returned. |
||
| 1160 | * |
||
| 1161 | * @param int|string $chatId |
||
| 1162 | * @param string $title |
||
| 1163 | * @param string $description |
||
| 1164 | * @param string $payload |
||
| 1165 | * @param string $providerToken |
||
| 1166 | * @param string $startParameter |
||
| 1167 | * @param string $currency |
||
| 1168 | * @param array $prices |
||
| 1169 | * @param string|null $photoUrl |
||
| 1170 | * @param int|null $photoSize |
||
| 1171 | * @param int|null $photoWidth |
||
| 1172 | * @param int|null $photoHeight |
||
| 1173 | * @param bool $needName |
||
| 1174 | * @param bool $needPhoneNumber |
||
| 1175 | * @param bool $needEmail |
||
| 1176 | * @param bool $needShippingAddress |
||
| 1177 | * @param bool $isFlexible |
||
| 1178 | * @param int|null $replyToMessageId |
||
| 1179 | * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|null $replyMarkup |
||
| 1180 | * @param bool $disableNotification |
||
| 1181 | * |
||
| 1182 | * @return Message |
||
| 1183 | */ |
||
| 1184 | public function sendInvoice( |
||
| 1229 | |||
| 1230 | /** |
||
| 1231 | * If you sent an invoice requesting a shipping address and the parameter is_flexible was specified, the Bot API |
||
| 1232 | * will send an Update with a shipping_query field to the bot. Use this method to reply to shipping queries. |
||
| 1233 | * On success, True is returned. |
||
| 1234 | * |
||
| 1235 | * @param string $shippingQueryId |
||
| 1236 | * @param bool $ok |
||
| 1237 | * @param array $shipping_options |
||
| 1238 | * @param null|string $errorMessage |
||
| 1239 | * |
||
| 1240 | * @return bool |
||
| 1241 | * |
||
| 1242 | */ |
||
| 1243 | public function answerShippingQuery($shippingQueryId, $ok = true, $shipping_options = [], $errorMessage = null) |
||
| 1252 | |||
| 1253 | /** |
||
| 1254 | * Use this method to respond to such pre-checkout queries. On success, True is returned. |
||
| 1255 | * Note: The Bot API must receive an answer within 10 seconds after the pre-checkout query was sent. |
||
| 1256 | * |
||
| 1257 | * @param string $preCheckoutQueryId |
||
| 1258 | * @param bool $ok |
||
| 1259 | * @param null|string $errorMessage |
||
| 1260 | * |
||
| 1261 | * @return mixed |
||
| 1262 | */ |
||
| 1263 | public function answerPreCheckoutQuery($preCheckoutQueryId, $ok = true, $errorMessage = null) |
||
| 1271 | |||
| 1272 | /** |
||
| 1273 | * Use this method to restrict a user in a supergroup. |
||
| 1274 | * The bot must be an administrator in the supergroup for this to work and must have the appropriate admin rights. |
||
| 1275 | * Pass True for all boolean parameters to lift restrictions from a user. |
||
| 1276 | * |
||
| 1277 | * @param string|int $chatId Unique identifier for the target chat or username of the target supergroup |
||
| 1278 | * (in the format @supergroupusername) |
||
| 1279 | * @param int $userId Unique identifier of the target user |
||
| 1280 | * @param null|integer $untilDate Date when restrictions will be lifted for the user, unix time. |
||
| 1281 | * If user is restricted for more than 366 days or less than 30 seconds from the current time, |
||
| 1282 | * they are considered to be restricted forever |
||
| 1283 | * @param bool $canSendMessages Pass True, if the user can send text messages, contacts, locations and venues |
||
| 1284 | * @param bool $canSendMediaMessages No Pass True, if the user can send audios, documents, photos, videos, |
||
| 1285 | * video notes and voice notes, implies can_send_messages |
||
| 1286 | * @param bool $canSendOtherMessages Pass True, if the user can send animations, games, stickers and |
||
| 1287 | * use inline bots, implies can_send_media_messages |
||
| 1288 | * @param bool $canAddWebPagePreviews Pass True, if the user may add web page previews to their messages, |
||
| 1289 | * implies can_send_media_messages |
||
| 1290 | * |
||
| 1291 | * @return bool |
||
| 1292 | */ |
||
| 1293 | public function restrictChatMember( |
||
| 1312 | |||
| 1313 | /** |
||
| 1314 | * Use this method to promote or demote a user in a supergroup or a channel. |
||
| 1315 | * The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. |
||
| 1316 | * Pass False for all boolean parameters to demote a user. |
||
| 1317 | * |
||
| 1318 | * @param string|int $chatId Unique identifier for the target chat or username of the target supergroup |
||
| 1319 | * (in the format @supergroupusername) |
||
| 1320 | * @param int $userId Unique identifier of the target user |
||
| 1321 | * @param bool $canChangeInfo Pass True, if the administrator can change chat title, photo and other settings |
||
| 1322 | * @param bool $canPostMessages Pass True, if the administrator can create channel posts, channels only |
||
| 1323 | * @param bool $canEditMessages Pass True, if the administrator can edit messages of other users, channels only |
||
| 1324 | * @param bool $canDeleteMessages Pass True, if the administrator can delete messages of other users |
||
| 1325 | * @param bool $canInviteUsers Pass True, if the administrator can invite new users to the chat |
||
| 1326 | * @param bool $canRestrictMembers Pass True, if the administrator can restrict, ban or unban chat members |
||
| 1327 | * @param bool $canPinMessages Pass True, if the administrator can pin messages, supergroups only |
||
| 1328 | * @param bool $canPromoteMembers Pass True, if the administrator can add new administrators with a subset of his |
||
| 1329 | * own privileges or demote administrators that he has promoted,directly or |
||
| 1330 | * indirectly (promoted by administrators that were appointed by him) |
||
| 1331 | * |
||
| 1332 | * @return bool |
||
| 1333 | */ |
||
| 1334 | public function promoteChatMember( |
||
| 1359 | |||
| 1360 | /** |
||
| 1361 | * Use this method to export an invite link to a supergroup or a channel. |
||
| 1362 | * The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. |
||
| 1363 | * |
||
| 1364 | * @param string|int $chatId Unique identifier for the target chat or username of the target channel |
||
| 1365 | * (in the format @channelusername) |
||
| 1366 | * @return string |
||
| 1367 | */ |
||
| 1368 | public function exportChatInviteLink($chatId) |
||
| 1374 | |||
| 1375 | /** |
||
| 1376 | * Use this method to set a new profile photo for the chat. Photos can't be changed for private chats. |
||
| 1377 | * The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. |
||
| 1378 | * |
||
| 1379 | * @param string|int $chatId Unique identifier for the target chat or username of the target channel |
||
| 1380 | * (in the format @channelusername) |
||
| 1381 | * @param \CURLFile|string $photo New chat photo, uploaded using multipart/form-data |
||
| 1382 | * |
||
| 1383 | * @return bool |
||
| 1384 | */ |
||
| 1385 | public function setChatPhoto($chatId, $photo) |
||
| 1392 | |||
| 1393 | /** |
||
| 1394 | * Use this method to delete a chat photo. Photos can't be changed for private chats. |
||
| 1395 | * The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. |
||
| 1396 | * |
||
| 1397 | * @param string|int $chatId Unique identifier for the target chat or username of the target channel |
||
| 1398 | * (in the format @channelusername) |
||
| 1399 | * |
||
| 1400 | * @return bool |
||
| 1401 | */ |
||
| 1402 | public function deleteChatPhoto($chatId) |
||
| 1408 | |||
| 1409 | /** |
||
| 1410 | * Use this method to change the title of a chat. Titles can't be changed for private chats. |
||
| 1411 | * The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. |
||
| 1412 | * |
||
| 1413 | * @param string|int $chatId Unique identifier for the target chat or username of the target channel |
||
| 1414 | * (in the format @channelusername) |
||
| 1415 | * @param string $title New chat title, 1-255 characters |
||
| 1416 | * |
||
| 1417 | * @return bool |
||
| 1418 | */ |
||
| 1419 | public function setChatTitle($chatId, $title) |
||
| 1426 | |||
| 1427 | /** |
||
| 1428 | * Use this method to change the description of a supergroup or a channel. |
||
| 1429 | * The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. |
||
| 1430 | * |
||
| 1431 | * @param string|int $chatId Unique identifier for the target chat or username of the target channel |
||
| 1432 | * (in the format @channelusername) |
||
| 1433 | * @param string|null $description New chat description, 0-255 characters |
||
| 1434 | * |
||
| 1435 | * @return bool |
||
| 1436 | */ |
||
| 1437 | public function setChatDescription($chatId, $description = null) |
||
| 1444 | |||
| 1445 | /** |
||
| 1446 | * Use this method to pin a message in a supergroup. |
||
| 1447 | * The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. |
||
| 1448 | * |
||
| 1449 | * @param string|int $chatId Unique identifier for the target chat or username of the target channel |
||
| 1450 | * (in the format @channelusername) |
||
| 1451 | * @param int $messageId Identifier of a message to pin |
||
| 1452 | * @param bool $disableNotification |
||
| 1453 | * |
||
| 1454 | * @return bool |
||
| 1455 | */ |
||
| 1456 | public function pinChatMessage($chatId, $messageId, $disableNotification = false) |
||
| 1464 | |||
| 1465 | /** |
||
| 1466 | * Use this method to unpin a message in a supergroup chat. |
||
| 1467 | * The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. |
||
| 1468 | * |
||
| 1469 | * @param string|int $chatId Unique identifier for the target chat or username of the target channel |
||
| 1470 | * (in the format @channelusername) |
||
| 1471 | * |
||
| 1472 | * @return bool |
||
| 1473 | */ |
||
| 1474 | public function unpinChatMessage($chatId) |
||
| 1480 | |||
| 1481 | /** |
||
| 1482 | * Use this method to get up to date information about the chat |
||
| 1483 | * (current name of the user for one-on-one conversations, current username of a user, group or channel, etc.). |
||
| 1484 | * |
||
| 1485 | * @param string|int $chatId Unique identifier for the target chat or username of the target channel |
||
| 1486 | * (in the format @channelusername) |
||
| 1487 | * |
||
| 1488 | * @return Chat |
||
| 1489 | */ |
||
| 1490 | public function getChat($chatId) |
||
| 1496 | |||
| 1497 | /** |
||
| 1498 | * Use this method to get information about a member of a chat. |
||
| 1499 | * |
||
| 1500 | * @param string|int $chatId Unique identifier for the target chat or username of the target channel |
||
| 1501 | * (in the format @channelusername) |
||
| 1502 | * @param int $userId |
||
| 1503 | * |
||
| 1504 | * @return ChatMember |
||
| 1505 | */ |
||
| 1506 | public function getChatMember($chatId, $userId) |
||
| 1513 | |||
| 1514 | /** |
||
| 1515 | * Use this method for your bot to leave a group, supergroup or channel. |
||
| 1516 | * |
||
| 1517 | * @param string|int $chatId Unique identifier for the target chat or username of the target channel |
||
| 1518 | * (in the format @channelusername) |
||
| 1519 | * |
||
| 1520 | * @return bool |
||
| 1521 | */ |
||
| 1522 | public function leaveChat($chatId) |
||
| 1528 | |||
| 1529 | /** |
||
| 1530 | * Use this method to get the number of members in a chat. |
||
| 1531 | * |
||
| 1532 | * @param string|int $chatId Unique identifier for the target chat or username of the target channel |
||
| 1533 | * (in the format @channelusername) |
||
| 1534 | * |
||
| 1535 | * @return int |
||
| 1536 | */ |
||
| 1537 | public function getChatMembersCount($chatId) |
||
| 1546 | |||
| 1547 | /** |
||
| 1548 | * Use this method to get a list of administrators in a chat. |
||
| 1549 | * |
||
| 1550 | * @param string|int $chatId Unique identifier for the target chat or username of the target channel |
||
| 1551 | * (in the format @channelusername) |
||
| 1552 | * |
||
| 1553 | * @return array |
||
| 1554 | */ |
||
| 1555 | public function getChatAdministrators($chatId) |
||
| 1566 | } |
||
| 1567 |