| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the TelegramBot package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Avtandil Kikabidze aka LONGMAN <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Longman\TelegramBot; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use GuzzleHttp\Client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use GuzzleHttp\ClientInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use GuzzleHttp\Exception\RequestException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use GuzzleHttp\Psr7\Stream; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Longman\TelegramBot\Entities\File; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Longman\TelegramBot\Entities\InputMedia\InputMedia; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Longman\TelegramBot\Entities\Message; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Longman\TelegramBot\Entities\ServerResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Longman\TelegramBot\Exception\InvalidBotTokenException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Longman\TelegramBot\Exception\TelegramException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use Throwable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * Class Request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * @method static ServerResponse getUpdates(array $data)                      Use this method to receive incoming updates using long polling (wiki). An Array of Update objects is returned. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * @method static ServerResponse setWebhook(array $data)                      Use this method to specify a url and receive incoming updates via an outgoing webhook. Whenever there is an update for the bot, we will send an HTTPS POST request to the specified url, containing a JSON-serialized Update. In case of an unsuccessful request, we will give up after a reasonable amount of attempts. Returns true. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  * @method static ServerResponse deleteWebhook()                              Use this method to remove webhook integration if you decide to switch back to getUpdates. Returns True on success. Requires no parameters. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  * @method static ServerResponse getWebhookInfo()                             Use this method to get current webhook status. Requires no parameters. On success, returns a WebhookInfo object. If the bot is using getUpdates, will return an object with the url field empty. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  * @method static ServerResponse getMe()                                      A simple method for testing your bot's auth token. Requires no parameters. Returns basic information about the bot in form of a User object. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  * @method static ServerResponse logOut()                                     Use this method to log out from the cloud Bot API server before launching the bot locally. Requires no parameters. Returns True on success. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  * @method static ServerResponse close()                                      Use this method to close the bot instance before moving it from one local server to another. Requires no parameters. Returns True on success. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  * @method static ServerResponse forwardMessage(array $data)                  Use this method to forward messages of any kind. On success, the sent Message is returned. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  * @method static ServerResponse copyMessage(array $data)                     Use this method to copy messages of any kind. The method is analogous to the method forwardMessages, but the copied message doesn't have a link to the original message. Returns the MessageId of the sent message on success. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  * @method static ServerResponse sendPhoto(array $data)                       Use this method to send photos. On success, the sent Message is returned. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  * @method static ServerResponse sendAudio(array $data)                       Use this method to send audio files, if you want Telegram clients to display them in the music player. Your audio must be in the .mp3 format. On success, the sent Message is returned. Bots can currently send audio files of up to 50 MB in size, this limit may be changed in the future. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  * @method static ServerResponse sendDocument(array $data)                    Use this method to send general files. On success, the sent Message is returned. Bots can currently send files of any type of up to 50 MB in size, this limit may be changed in the future. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  * @method static ServerResponse sendSticker(array $data)                     Use this method to send .webp stickers. On success, the sent Message is returned. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  * @method static ServerResponse sendVideo(array $data)                       Use this method to send video files, Telegram clients support mp4 videos (other formats may be sent as Document). On success, the sent Message is returned. Bots can currently send video files of up to 50 MB in size, this limit may be changed in the future. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  * @method static ServerResponse sendAnimation(array $data)                   Use this method to send animation files (GIF or H.264/MPEG-4 AVC video without sound). On success, the sent Message is returned. Bots can currently send animation files of up to 50 MB in size, this limit may be changed in the future. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  * @method static ServerResponse sendVoice(array $data)                       Use this method to send audio files, if you want Telegram clients to display the file as a playable voice message. For this to work, your audio must be in an .ogg file encoded with OPUS (other formats may be sent as Audio or Document). On success, the sent Message is returned. Bots can currently send voice messages of up to 50 MB in size, this limit may be changed in the future. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  * @method static ServerResponse sendVideoNote(array $data)                   Use this method to send video messages. On success, the sent Message is returned. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  * @method static ServerResponse sendMediaGroup(array $data)                  Use this method to send a group of photos or videos as an album. On success, an array of the sent Messages is returned. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  * @method static ServerResponse sendLocation(array $data)                    Use this method to send point on the map. On success, the sent Message is returned. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  * @method static ServerResponse editMessageLiveLocation(array $data)         Use this method to edit live location messages sent by the bot or via the bot (for inline bots). A location can be edited until its live_period expires or editing is explicitly disabled by a call to stopMessageLiveLocation. On success, if the edited message was sent by the bot, the edited Message is returned, otherwise True is returned. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  * @method static ServerResponse stopMessageLiveLocation(array $data)         Use this method to stop updating a live location message sent by the bot or via the bot (for inline bots) before live_period expires. On success, if the message was sent by the bot, the sent Message is returned, otherwise True is returned. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  * @method static ServerResponse sendVenue(array $data)                       Use this method to send information about a venue. On success, the sent Message is returned. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  * @method static ServerResponse sendContact(array $data)                     Use this method to send phone contacts. On success, the sent Message is returned. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  * @method static ServerResponse sendPoll(array $data)                        Use this method to send a native poll. A native poll can't be sent to a private chat. On success, the sent Message is returned. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  * @method static ServerResponse sendDice(array $data)                        Use this method to send a dice, which will have a random value from 1 to 6. On success, the sent Message is returned. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  * @method static ServerResponse sendChatAction(array $data)                  Use this method when you need to tell the user that something is happening on the bot's side. The status is set for 5 seconds or less (when a message arrives from your bot, Telegram clients clear its typing status). Returns True on success. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  * @method static ServerResponse getUserProfilePhotos(array $data)            Use this method to get a list of profile pictures for a user. Returns a UserProfilePhotos object. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  * @method static ServerResponse getFile(array $data)                         Use this method to get basic info about a file and prepare it for downloading. For the moment, bots can download files of up to 20MB in size. On success, a File object is returned. The file can then be downloaded via the link https://api.telegram.org/file/bot<token>/<file_path>, where <file_path> is taken from the response. It is guaranteed that the link will be valid for at least 1 hour. When the link expires, a new one can be requested by calling getFile again. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  * @method static ServerResponse kickChatMember(array $data)                  Use this method to kick a user from a group, a supergroup or a channel. In the case of supergroups and channels, the user will not be able to return to the group on their own using invite links, etc., unless unbanned first. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. Returns True on success. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  * @method static ServerResponse unbanChatMember(array $data)                 Use this method to unban a previously kicked user in a supergroup or channel. The user will not return to the group or channel automatically, but will be able to join via link, etc. The bot must be an administrator for this to work. Returns True on success. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  * @method static ServerResponse restrictChatMember(array $data)              Use this method to restrict a user in a supergroup. The bot must be an administrator in the supergroup for this to work and must have the appropriate admin rights. Pass True for all permissions to lift restrictions from a user. Returns True on success. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  * @method static ServerResponse promoteChatMember(array $data)               Use this method to promote or demote a user in a supergroup or a channel. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. Pass False for all boolean parameters to demote a user. Returns True on success. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  * @method static ServerResponse setChatAdministratorCustomTitle(array $data) Use this method to set a custom title for an administrator in a supergroup promoted by the bot. Returns True on success. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  * @method static ServerResponse setChatPermissions(array $data)              Use this method to set default chat permissions for all members. The bot must be an administrator in the group or a supergroup for this to work and must have the can_restrict_members admin rights. Returns True on success. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  * @method static ServerResponse exportChatInviteLink(array $data)            Use this method to generate a new invite link for a chat. Any previously generated link is revoked. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. Returns the new invite link as String on success. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  * @method static ServerResponse setChatPhoto(array $data)                    Use this method to set a new profile photo for the chat. Photos can't be changed for private chats. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. Returns True on success. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  * @method static ServerResponse deleteChatPhoto(array $data)                 Use this method to delete a chat photo. Photos can't be changed for private chats. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. Returns True on success. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  * @method static ServerResponse setChatTitle(array $data)                    Use this method to change the title of a chat. Titles can't be changed for private chats. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. Returns True on success. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  * @method static ServerResponse setChatDescription(array $data)              Use this method to change the description of a group, a supergroup or a channel. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. Returns True on success. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  * @method static ServerResponse pinChatMessage(array $data)                  Use this method to pin a message in a supergroup or a channel. The bot must be an administrator in the chat for this to work and must have the ‘can_pin_messages’ admin right in the supergroup or ‘can_edit_messages’ admin right in the channel. Returns True on success. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  * @method static ServerResponse unpinChatMessage(array $data)                Use this method to unpin a message in a supergroup or a channel. The bot must be an administrator in the chat for this to work and must have the ‘can_pin_messages’ admin right in the supergroup or ‘can_edit_messages’ admin right in the channel. Returns True on success. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  * @method static ServerResponse unpinAllChatMessages(array $data)            Use this method to clear the list of pinned messages in a chat. If the chat is not a private chat, the bot must be an administrator in the chat for this to work and must have the 'can_pin_messages' admin right in a supergroup or 'can_edit_messages' admin right in a channel. Returns True on success. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  * @method static ServerResponse leaveChat(array $data)                       Use this method for your bot to leave a group, supergroup or channel. Returns True on success. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  * @method static ServerResponse getChat(array $data)                         Use this method to get up to date information about the chat (current name of the user for one-on-one conversations, current username of a user, group or channel, etc.). Returns a Chat object on success. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  * @method static ServerResponse getChatAdministrators(array $data)           Use this method to get a list of administrators in a chat. On success, returns an Array of ChatMember objects that contains information about all chat administrators except other bots. If the chat is a group or a supergroup and no administrators were appointed, only the creator will be returned. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  * @method static ServerResponse getChatMembersCount(array $data)             Use this method to get the number of members in a chat. Returns Int on success. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  * @method static ServerResponse getChatMember(array $data)                   Use this method to get information about a member of a chat. Returns a ChatMember object on success. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  * @method static ServerResponse setChatStickerSet(array $data)               Use this method to set a new group sticker set for a supergroup. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. Use the field can_set_sticker_set optionally returned in getChat requests to check if the bot can use this method. Returns True on success. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  * @method static ServerResponse deleteChatStickerSet(array $data)            Use this method to delete a group sticker set from a supergroup. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. Use the field can_set_sticker_set optionally returned in getChat requests to check if the bot can use this method. Returns True on success. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  * @method static ServerResponse answerCallbackQuery(array $data)             Use this method to send answers to callback queries sent from inline keyboards. The answer will be displayed to the user as a notification at the top of the chat screen or as an alert. On success, True is returned. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  * @method static ServerResponse answerInlineQuery(array $data)               Use this method to send answers to an inline query. On success, True is returned. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  * @method static ServerResponse setMyCommands(array $data)                   Use this method to change the list of the bot's commands. Returns True on success. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  * @method static ServerResponse getMyCommands()                              Use this method to get the current list of the bot's commands. Requires no parameters. Returns Array of BotCommand on success. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  * @method static ServerResponse editMessageText(array $data)                 Use this method to edit text and game messages sent by the bot or via the bot (for inline bots). On success, if edited message is sent by the bot, the edited Message is returned, otherwise True is returned. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  * @method static ServerResponse editMessageCaption(array $data)              Use this method to edit captions of messages sent by the bot or via the bot (for inline bots). On success, if edited message is sent by the bot, the edited Message is returned, otherwise True is returned. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  * @method static ServerResponse editMessageMedia(array $data)                Use this method to edit audio, document, photo, or video messages. On success, if the edited message was sent by the bot, the edited Message is returned, otherwise True is returned. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  * @method static ServerResponse editMessageReplyMarkup(array $data)          Use this method to edit only the reply markup of messages sent by the bot or via the bot (for inline bots). On success, if edited message is sent by the bot, the edited Message is returned, otherwise True is returned. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  * @method static ServerResponse stopPoll(array $data)                        Use this method to stop a poll which was sent by the bot. On success, the stopped Poll with the final results is returned. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  * @method static ServerResponse deleteMessage(array $data)                   Use this method to delete a message, including service messages, with certain limitations. Returns True on success. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  * @method static ServerResponse getStickerSet(array $data)                   Use this method to get a sticker set. On success, a StickerSet object is returned. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  * @method static ServerResponse uploadStickerFile(array $data)               Use this method to upload a .png file with a sticker for later use in createNewStickerSet and addStickerToSet methods (can be used multiple times). Returns the uploaded File on success. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  * @method static ServerResponse createNewStickerSet(array $data)             Use this method to create new sticker set owned by a user. The bot will be able to edit the created sticker set. Returns True on success. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  * @method static ServerResponse addStickerToSet(array $data)                 Use this method to add a new sticker to a set created by the bot. Returns True on success. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  * @method static ServerResponse setStickerPositionInSet(array $data)         Use this method to move a sticker in a set created by the bot to a specific position. Returns True on success. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  * @method static ServerResponse deleteStickerFromSet(array $data)            Use this method to delete a sticker from a set created by the bot. Returns True on success. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  * @method static ServerResponse setStickerSetThumb(array $data)              Use this method to set the thumbnail of a sticker set. Animated thumbnails can be set for animated sticker sets only. Returns True on success. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  * @method static ServerResponse sendInvoice(array $data)                     Use this method to send invoices. On success, the sent Message is returned. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  * @method static ServerResponse answerShippingQuery(array $data)             If you sent an invoice requesting a shipping address and the parameter is_flexible was specified, the Bot API will send an Update with a shipping_query field to the bot. Use this method to reply to shipping queries. On success, True is returned. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  * @method static ServerResponse answerPreCheckoutQuery(array $data)          Once the user has confirmed their payment and shipping details, the Bot API sends the final confirmation in the form of an Update with the field pre_checkout_query. Use this method to respond to such pre-checkout queries. On success, True is returned. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  * @method static ServerResponse setPassportDataErrors(array $data)           Informs a user that some of the Telegram Passport elements they provided contains errors. The user will not be able to re-submit their Passport to you until the errors are fixed (the contents of the field for which you returned the error must change). Returns True on success. Use this if the data submitted by the user doesn't satisfy the standards your service requires for any reason. For example, if a birthday date seems invalid, a submitted document is blurry, a scan shows evidence of tampering, etc. Supply some details in the error message to make sure the user knows how to correct the issues. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  * @method static ServerResponse sendGame(array $data)                        Use this method to send a game. On success, the sent Message is returned. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  * @method static ServerResponse setGameScore(array $data)                    Use this method to set the score of the specified user in a game. On success, if the message was sent by the bot, returns the edited Message, otherwise returns True. Returns an error, if the new score is not greater than the user's current score in the chat and force is False. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  * @method static ServerResponse getGameHighScores(array $data)               Use this method to get data for high score tables. Will return the score of the specified user and several of his neighbors in a game. On success, returns an Array of GameHighScore objects. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | class Request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * Telegram object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * @var Telegram | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     private static $telegram; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * URI of the Telegram API | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     private static $api_base_uri = 'https://api.telegram.org'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * Guzzle Client object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * @var ClientInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     private static $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      * Request limiter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * @var bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     private static $limiter_enabled; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      * Request limiter's interval between checks | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      * @var float | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     private static $limiter_interval; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      * The current action that is being executed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     private static $current_action = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      * Available actions to send | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * This is basically the list of all methods listed on the official API documentation. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      * @link https://core.telegram.org/bots/api | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     private static $actions = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         'getUpdates', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         'setWebhook', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         'deleteWebhook', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         'getWebhookInfo', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         'getMe', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         'logOut', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         'close', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         'sendMessage', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         'forwardMessage', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         'copyMessage', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         'sendPhoto', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |         'sendAudio', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |         'sendDocument', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         'sendSticker', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |         'sendVideo', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         'sendAnimation', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |         'sendVoice', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |         'sendVideoNote', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |         'sendMediaGroup', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |         'sendLocation', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |         'editMessageLiveLocation', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |         'stopMessageLiveLocation', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |         'sendVenue', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |         'sendContact', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |         'sendPoll', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |         'sendDice', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |         'sendChatAction', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |         'getUserProfilePhotos', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |         'getFile', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |         'kickChatMember', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |         'unbanChatMember', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |         'restrictChatMember', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |         'promoteChatMember', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |         'setChatAdministratorCustomTitle', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |         'setChatPermissions', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |         'exportChatInviteLink', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |         'setChatPhoto', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |         'deleteChatPhoto', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |         'setChatTitle', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |         'setChatDescription', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |         'pinChatMessage', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |         'unpinChatMessage', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |         'unpinAllChatMessages', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |         'leaveChat', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |         'getChat', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |         'getChatAdministrators', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |         'getChatMembersCount', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |         'getChatMember', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |         'setChatStickerSet', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |         'deleteChatStickerSet', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |         'answerCallbackQuery', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |         'answerInlineQuery', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |         'setMyCommands', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |         'getMyCommands', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |         'editMessageText', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |         'editMessageCaption', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |         'editMessageMedia', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |         'editMessageReplyMarkup', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |         'stopPoll', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |         'deleteMessage', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |         'getStickerSet', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |         'uploadStickerFile', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |         'createNewStickerSet', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  |         'addStickerToSet', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  |         'setStickerPositionInSet', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |         'deleteStickerFromSet', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |         'setStickerSetThumb', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |         'sendInvoice', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |         'answerShippingQuery', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |         'answerPreCheckoutQuery', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  |         'setPassportDataErrors', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  |         'sendGame', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |         'setGameScore', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  |         'getGameHighScores', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |      * Some methods need a dummy param due to certain cURL issues. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  |      * @see Request::addDummyParamIfNecessary() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  |     private static $actions_need_dummy_param = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  |         'deleteWebhook', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  |         'getWebhookInfo', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  |         'getMe', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  |         'logOut', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 |  |  |         'close', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  |         'getMyCommands', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  |      * Available fields for InputFile helper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  |      * This is basically the list of all fields that allow InputFile objects | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  |      * for which input can be simplified by providing local path directly  as string. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  |     private static $input_file_fields = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  |         'setWebhook'          => ['certificate'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  |         'sendPhoto'           => ['photo'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  |         'sendAudio'           => ['audio', 'thumb'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  |         'sendDocument'        => ['document', 'thumb'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  |         'sendVideo'           => ['video', 'thumb'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  |         'sendAnimation'       => ['animation', 'thumb'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  |         'sendVoice'           => ['voice', 'thumb'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 |  |  |         'sendVideoNote'       => ['video_note', 'thumb'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  |         'setChatPhoto'        => ['photo'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 |  |  |         'sendSticker'         => ['sticker'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  |         'uploadStickerFile'   => ['png_sticker'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  |         'createNewStickerSet' => ['png_sticker', 'tgs_sticker'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  |         'addStickerToSet'     => ['png_sticker', 'tgs_sticker'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 |  |  |         'setStickerSetThumb'  => ['thumb'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 |  |  |      * Initialize | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 |  |  |      * @param Telegram $telegram | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 | 31 |  |     public static function initialize(Telegram $telegram): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 | 31 |  |         self::$telegram = $telegram; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 | 31 |  |         self::setClient(self::$client ?: new Client(['base_uri' => self::$api_base_uri])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 | 31 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 |  |  |      * Set a custom Guzzle HTTP Client object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 |  |  |      * @param ClientInterface $client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 | 31 |  |     public static function setClient(ClientInterface $client): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 | 31 |  |         self::$client = $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 | 31 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 |  |  |      * Get input from custom input or stdin and return it | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 299 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 |  |  |     public static function getInput(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 302 |  |  |         // First check if a custom input has been set, else get the PHP input. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 303 |  |  |         return self::$telegram->getCustomInput() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 304 |  |  |             ?: file_get_contents('php://input'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 305 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 306 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 307 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 308 |  |  |      * Generate general fake server response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 309 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 310 |  |  |      * @param array $data Data to add to fake response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 311 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 312 |  |  |      * @return array Fake response data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 313 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 314 | 1 |  |     public static function generateGeneralFakeServerResponse(array $data = []): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 315 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 316 |  |  |         //PARAM BINDED IN PHPUNIT TEST FOR TestServerResponse.php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 317 |  |  |         //Maybe this is not the best possible implementation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 318 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 319 |  |  |         //No value set in $data ie testing setWebhook | 
            
                                                                                                            
                            
            
                                    
            
            
                | 320 |  |  |         //Provided $data['chat_id'] ie testing sendMessage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 321 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 322 | 1 |  |         $fake_response = ['ok' => true]; // :) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 323 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 324 | 1 |  |         if ($data === []) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 325 | 1 |  |             $fake_response['result'] = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 326 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 327 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 328 |  |  |         //some data to initialize the class method SendMessage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 329 | 1 |  |         if (isset($data['chat_id'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 330 | 1 |  |             $data['message_id'] = '1234'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 331 | 1 |  |             $data['date']       = '1441378360'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 332 | 1 |  |             $data['from']       = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 333 |  |  |                 'id'         => 123456789, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 334 |  |  |                 'first_name' => 'botname', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 335 |  |  |                 'username'   => 'namebot', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 336 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 337 | 1 |  |             $data['chat']       = ['id' => $data['chat_id']]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 338 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 339 | 1 |  |             $fake_response['result'] = $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 340 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 341 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 342 | 1 |  |         return $fake_response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 343 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 344 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 345 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 346 |  |  |      * Properly set up the request params | 
            
                                                                                                            
                            
            
                                    
            
            
                | 347 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 348 |  |  |      * If any item of the array is a resource, reformat it to a multipart request. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 349 |  |  |      * Else, just return the passed data as form params. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 350 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 351 |  |  |      * @param array $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 352 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 353 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 354 |  |  |      * @throws TelegramException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 355 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 356 |  |  |     private static function setUpRequestParams(array $data): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 357 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 358 |  |  |         $has_resource = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 359 |  |  |         $multipart    = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 360 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 361 |  |  |         foreach ($data as $key => &$item) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 362 |  |  |             if ($key === 'media') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 363 |  |  |                 // Magical media input helper. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 364 |  |  |                 $item = self::mediaInputHelper($item, $has_resource, $multipart); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 365 |  |  |             } elseif (array_key_exists(self::$current_action, self::$input_file_fields) && in_array($key, self::$input_file_fields[self::$current_action], true)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 366 |  |  |                 // Allow absolute paths to local files. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 367 |  |  |                 if (is_string($item) && file_exists($item)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 368 |  |  |                     $item = new Stream(self::encodeFile($item)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 369 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 370 |  |  |             } elseif (is_array($item) || is_object($item)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 371 |  |  |                 // Convert any nested arrays or objects into JSON strings. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 372 |  |  |                 $item = json_encode($item); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 373 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 374 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 375 |  |  |             // Reformat data array in multipart way if it contains a resource | 
            
                                                                                                            
                            
            
                                    
            
            
                | 376 |  |  |             $has_resource = $has_resource || is_resource($item) || $item instanceof Stream; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 377 |  |  |             $multipart[]  = ['name' => $key, 'contents' => $item]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 378 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 379 |  |  |         unset($item); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 380 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 381 |  |  |         if ($has_resource) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 382 |  |  |             return ['multipart' => $multipart]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 383 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 384 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 385 |  |  |         return ['form_params' => $data]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 386 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 387 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 388 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 389 |  |  |      * Magical input media helper to simplify passing media. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 390 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 391 |  |  |      * This allows the following: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 392 |  |  |      * Request::editMessageMedia([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 393 |  |  |      *     ... | 
            
                                                                                                            
                            
            
                                    
            
            
                | 394 |  |  |      *     'media' => new InputMediaPhoto([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 395 |  |  |      *         'caption' => 'Caption!', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 396 |  |  |      *         'media'   => Request::encodeFile($local_photo), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 397 |  |  |      *     ]), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 398 |  |  |      * ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 399 |  |  |      * and | 
            
                                                                                                            
                            
            
                                    
            
            
                | 400 |  |  |      * Request::sendMediaGroup([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 401 |  |  |      *     'media'   => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 402 |  |  |      *         new InputMediaPhoto(['media' => Request::encodeFile($local_photo_1)]), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 403 |  |  |      *         new InputMediaPhoto(['media' => Request::encodeFile($local_photo_2)]), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 404 |  |  |      *         new InputMediaVideo(['media' => Request::encodeFile($local_video_1)]), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 405 |  |  |      *     ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 406 |  |  |      * ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 407 |  |  |      * and even | 
            
                                                                                                            
                            
            
                                    
            
            
                | 408 |  |  |      * Request::sendMediaGroup([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 409 |  |  |      *     'media'   => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 410 |  |  |      *         new InputMediaPhoto(['media' => $local_photo_1]), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 411 |  |  |      *         new InputMediaPhoto(['media' => $local_photo_2]), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 412 |  |  |      *         new InputMediaVideo(['media' => $local_video_1]), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 413 |  |  |      *     ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 414 |  |  |      * ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 415 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 416 |  |  |      * @param mixed $item | 
            
                                                                                                            
                            
            
                                    
            
            
                | 417 |  |  |      * @param bool  $has_resource | 
            
                                                                                                            
                            
            
                                    
            
            
                | 418 |  |  |      * @param array $multipart | 
            
                                                                                                            
                            
            
                                    
            
            
                | 419 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 420 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 421 |  |  |      * @throws TelegramException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 422 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 423 |  |  |     private static function mediaInputHelper($item, bool &$has_resource, array &$multipart) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 424 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 425 |  |  |         $was_array = is_array($item); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 426 |  |  |         $was_array || $item = [$item]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 427 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 428 |  |  |         /** @var InputMedia|null $media_item */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 429 |  |  |         foreach ($item as $media_item) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 430 |  |  |             if (!($media_item instanceof InputMedia)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 431 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 432 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 433 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 434 |  |  |             // Make a list of all possible media that can be handled by the helper. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 435 |  |  |             $possible_medias = array_filter([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 436 |  |  |                 'media' => $media_item->getMedia(), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 437 |  |  |                 'thumb' => $media_item->getThumb(), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 438 |  |  |             ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 439 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 440 |  |  |             foreach ($possible_medias as $type => $media) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 441 |  |  |                 // Allow absolute paths to local files. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 442 |  |  |                 if (is_string($media) && strpos($media, 'attach://') !== 0 && file_exists($media)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 443 |  |  |                     $media = new Stream(self::encodeFile($media)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 444 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 445 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 446 |  |  |                 if (is_resource($media) || $media instanceof Stream) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 447 |  |  |                     $has_resource = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 448 |  |  |                     $unique_key   = uniqid($type . '_', false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 449 |  |  |                     $multipart[]  = ['name' => $unique_key, 'contents' => $media]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 450 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 451 |  |  |                     // We're literally overwriting the passed media type data! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 452 |  |  |                     $media_item->$type           = 'attach://' . $unique_key; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 453 |  |  |                     $media_item->raw_data[$type] = 'attach://' . $unique_key; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 454 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 455 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 456 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 457 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 458 |  |  |         $was_array || $item = reset($item); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 459 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 460 |  |  |         return json_encode($item); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 461 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 462 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 463 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 464 |  |  |      * Get the current action that's being executed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 465 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 466 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 467 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 468 | 7 |  |     public static function getCurrentAction(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 469 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 470 | 7 |  |         return self::$current_action; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 471 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 472 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 473 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 474 |  |  |      * Execute HTTP Request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 475 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 476 |  |  |      * @param string $action Action to execute | 
            
                                                                                                            
                            
            
                                    
            
            
                | 477 |  |  |      * @param array  $data   Data to attach to the execution | 
            
                                                                                                            
                            
            
                                    
            
            
                | 478 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 479 |  |  |      * @return string Result of the HTTP Request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 480 |  |  |      * @throws TelegramException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 481 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 482 |  |  |     public static function execute(string $action, array $data = []): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 483 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 484 |  |  |         $request_params          = self::setUpRequestParams($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 485 |  |  |         $request_params['debug'] = TelegramLog::getDebugLogTempStream(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 486 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 487 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 488 |  |  |             $response = self::$client->post( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 489 |  |  |                 '/bot' . self::$telegram->getApiKey() . '/' . $action, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 490 |  |  |                 $request_params | 
            
                                                                                                            
                            
            
                                    
            
            
                | 491 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 492 |  |  |             $result   = (string) $response->getBody(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 493 |  |  |         } catch (RequestException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 494 |  |  |             $response = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 495 |  |  |             $result   = $e->getResponse() ? (string) $e->getResponse()->getBody() : ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 496 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 497 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 506 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 507 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 508 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 509 |  |  |      * Download file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 510 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 511 |  |  |      * @param File $file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 512 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 513 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 514 |  |  |      * @throws TelegramException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 515 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 516 |  |  |     public static function downloadFile(File $file): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 517 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 518 |  |  |         if (empty($download_path = self::$telegram->getDownloadPath())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 519 |  |  |             throw new TelegramException('Download path not set!'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 520 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 521 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 522 |  |  |         $tg_file_path = $file->getFilePath(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 523 |  |  |         $file_path    = $download_path . '/' . $tg_file_path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 524 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 525 |  |  |         $file_dir = dirname($file_path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 526 |  |  |         //For safety reasons, first try to create the directory, then check that it exists. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 527 |  |  |         //This is in case some other process has created the folder in the meantime. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 528 |  |  |         if (!@mkdir($file_dir, 0755, true) && !is_dir($file_dir)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 529 |  |  |             throw new TelegramException('Directory ' . $file_dir . ' can\'t be created'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 530 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 531 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 532 |  |  |         $debug_handle = TelegramLog::getDebugLogTempStream(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 533 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 534 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 535 |  |  |             self::$client->get( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 536 |  |  |                 '/file/bot' . self::$telegram->getApiKey() . '/' . $tg_file_path, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 537 |  |  |                 ['debug' => $debug_handle, 'sink' => $file_path] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 538 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 539 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 540 |  |  |             return filesize($file_path) > 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 541 |  |  |         } catch (Throwable $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 542 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 543 |  |  |         } finally { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 544 |  |  |             //Logging verbose debug output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 545 |  |  |             TelegramLog::endDebugLogTempStream('Verbose HTTP File Download Request output:' . PHP_EOL . '%s' . PHP_EOL); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 546 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 547 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 548 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 549 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 550 |  |  |      * Encode file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 551 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 552 |  |  |      * @param string $file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 553 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 554 |  |  |      * @return resource | 
            
                                                                                                            
                            
            
                                    
            
            
                | 555 |  |  |      * @throws TelegramException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 556 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 557 |  |  |     public static function encodeFile(string $file) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 558 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 559 |  |  |         $fp = fopen($file, 'rb'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 560 |  |  |         if ($fp === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 561 |  |  |             throw new TelegramException('Cannot open "' . $file . '" for reading'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 562 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 563 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 564 |  |  |         return $fp; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 565 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 566 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 567 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 568 |  |  |      * Send command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 569 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 570 |  |  |      * @todo Fake response doesn't need json encoding? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 571 |  |  |      * @todo Write debug entry on failure | 
            
                                                                                                            
                            
            
                                    
            
            
                | 572 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 573 |  |  |      * @param string $action | 
            
                                                                                                            
                            
            
                                    
            
            
                | 574 |  |  |      * @param array  $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 575 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 576 |  |  |      * @return ServerResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 577 |  |  |      * @throws TelegramException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 578 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 579 |  |  |     public static function send(string $action, array $data = []): ServerResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 580 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 581 |  |  |         self::ensureValidAction($action); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 582 |  |  |         self::addDummyParamIfNecessary($action, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 583 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 584 |  |  |         $bot_username = self::$telegram->getBotUsername(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 585 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 586 |  |  |         if (defined('PHPUNIT_TESTSUITE')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 587 |  |  |             $fake_response = self::generateGeneralFakeServerResponse($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 588 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 589 |  |  |             return new ServerResponse($fake_response, $bot_username); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 590 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 591 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 592 |  |  |         self::ensureNonEmptyData($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 593 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 594 |  |  |         self::limitTelegramRequests($action, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 595 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 596 |  |  |         // Remember which action is currently being executed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 597 |  |  |         self::$current_action = $action; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 598 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 599 |  |  |         $raw_response = self::execute($action, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 600 |  |  |         $response     = json_decode($raw_response, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 601 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 602 |  |  |         if (null === $response) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 603 |  |  |             TelegramLog::debug($raw_response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 604 |  |  |             throw new TelegramException('Telegram returned an invalid response!'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 605 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 606 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 607 |  |  |         $response = new ServerResponse($response, $bot_username); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 608 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 609 |  |  |         if (!$response->isOk() && $response->getErrorCode() === 401 && $response->getDescription() === 'Unauthorized') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 610 |  |  |             throw new InvalidBotTokenException(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 611 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 612 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 613 |  |  |         // Special case for sent polls, which need to be saved specially. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 614 |  |  |         // @todo Take into account if DB gets extracted into separate module. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 615 |  |  |         if ($response->isOk() && ($message = $response->getResult()) && ($message instanceof Message) && $poll = $message->getPoll()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 616 |  |  |             DB::insertPollRequest($poll); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 617 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 618 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 619 |  |  |         // Reset current action after completion. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 620 |  |  |         self::$current_action = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 621 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 622 |  |  |         return $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 623 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 624 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 625 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 626 |  |  |      * Add a dummy parameter if the passed action requires it. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 627 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 628 |  |  |      * If a method doesn't require parameters, we need to add a dummy one anyway, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 629 |  |  |      * because of some cURL version failed POST request without parameters. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 630 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 631 |  |  |      * @link https://github.com/php-telegram-bot/core/pull/228 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 632 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 633 |  |  |      * @todo Would be nice to find a better solution for this! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 634 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 635 |  |  |      * @param string $action | 
            
                                                                                                            
                            
            
                                    
            
            
                | 636 |  |  |      * @param array  $data | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 637 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 638 |  |  |     protected static function addDummyParamIfNecessary(string $action, array &$data): void | 
            
                                                                        
                            
            
                                    
            
            
                | 639 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 640 |  |  |         if (in_array($action, self::$actions_need_dummy_param, true)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 641 |  |  |             // Can be anything, using a single letter to minimise request size. | 
            
                                                                        
                            
            
                                    
            
            
                | 642 |  |  |             $data = ['d']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 643 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 644 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 645 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 646 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 647 |  |  |      * Make sure the data isn't empty, else throw an exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 648 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 649 |  |  |      * @param array $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 650 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 651 |  |  |      * @throws TelegramException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 652 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 653 |  |  |     private static function ensureNonEmptyData(array $data): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 654 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 655 |  |  |         if (count($data) === 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 656 |  |  |             throw new TelegramException('Data is empty!'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 657 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 658 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 659 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 660 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 661 |  |  |      * Make sure the action is valid, else throw an exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 662 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 663 |  |  |      * @param string $action | 
            
                                                                                                            
                            
            
                                    
            
            
                | 664 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 665 |  |  |      * @throws TelegramException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 666 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 667 |  |  |     private static function ensureValidAction(string $action): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 668 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 669 |  |  |         if (!in_array($action, self::$actions, true)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 670 |  |  |             throw new TelegramException('The action "' . $action . '" doesn\'t exist!'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 671 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 672 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 673 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 674 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 675 |  |  |      * Use this method to send text messages. On success, the last sent Message is returned | 
            
                                                                                                            
                            
            
                                    
            
            
                | 676 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 677 |  |  |      * All message responses are saved in `$extras['responses']`. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 678 |  |  |      * Custom encoding can be defined in `$extras['encoding']` (default: `mb_internal_encoding()`) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 679 |  |  |      * Custom splitting can be defined in `$extras['split']` (default: 4096) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 680 |  |  |      *     `$extras['split'] = null;` // force to not split message at all! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 681 |  |  |      *     `$extras['split'] = 200;`  // split message into 200 character chunks | 
            
                                                                                                            
                            
            
                                    
            
            
                | 682 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 683 |  |  |      * @link https://core.telegram.org/bots/api#sendmessage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 684 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 685 |  |  |      * @todo Splitting formatted text may break the message. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 686 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 687 |  |  |      * @param array      $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 688 |  |  |      * @param array|null $extras | 
            
                                                                                                            
                            
            
                                    
            
            
                | 689 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 690 |  |  |      * @return ServerResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 691 |  |  |      * @throws TelegramException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 692 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 693 |  |  |     public static function sendMessage(array $data, ?array &$extras = []): ServerResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 694 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 695 |  |  |         $extras = array_merge([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 696 |  |  |             'split'    => 4096, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 697 |  |  |             'encoding' => mb_internal_encoding(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 698 |  |  |         ], (array) $extras); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 699 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 700 |  |  |         $text       = $data['text']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 701 |  |  |         $encoding   = $extras['encoding']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 702 |  |  |         $max_length = $extras['split'] ?: mb_strlen($text, $encoding); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 703 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 704 |  |  |         $responses = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 705 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 706 |  |  |         do { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 707 |  |  |             // Chop off and send the first message. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 708 |  |  |             $data['text'] = mb_substr($text, 0, $max_length, $encoding); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 709 |  |  |             $responses[]  = self::send('sendMessage', $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 710 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 711 |  |  |             // Prepare the next message. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 712 |  |  |             $text = mb_substr($text, $max_length, null, $encoding); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 713 |  |  |         } while ($text !== ''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 714 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 715 |  |  |         // Add all response objects to referenced variable. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 716 |  |  |         $extras['responses'] = $responses; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 717 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 718 |  |  |         return end($responses); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 719 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 720 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 721 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 722 |  |  |      * Any statically called method should be relayed to the `send` method. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 723 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 724 |  |  |      * @param string $action | 
            
                                                                                                            
                            
            
                                    
            
            
                | 725 |  |  |      * @param array  $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 726 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 727 |  |  |      * @return ServerResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 728 |  |  |      * @throws TelegramException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 729 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 730 |  |  |     public static function __callStatic(string $action, array $data): ServerResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 731 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 732 |  |  |         // Only argument should be the data array, ignore any others. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 733 |  |  |         return static::send($action, reset($data) ?: []); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 734 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 735 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 736 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 737 |  |  |      * Return an empty Server Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 738 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 739 |  |  |      * No request is sent to Telegram. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 740 |  |  |      * This function is used in commands that don't need to fire a message after execution | 
            
                                                                                                            
                            
            
                                    
            
            
                | 741 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 742 |  |  |      * @return ServerResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 743 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 744 |  |  |     public static function emptyResponse(): ServerResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 745 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 746 |  |  |         return new ServerResponse(['ok' => true, 'result' => true]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 747 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 748 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 749 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 750 |  |  |      * Send message to all active chats | 
            
                                                                                                            
                            
            
                                    
            
            
                | 751 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 752 |  |  |      * @param string $callback_function | 
            
                                                                                                            
                            
            
                                    
            
            
                | 753 |  |  |      * @param array  $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 754 |  |  |      * @param array  $select_chats_params | 
            
                                                                                                            
                            
            
                                    
            
            
                | 755 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 756 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 757 |  |  |      * @throws TelegramException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 758 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 759 |  |  |     public static function sendToActiveChats( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 760 |  |  |         string $callback_function, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 761 |  |  |         array $data, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 762 |  |  |         array $select_chats_params | 
            
                                                                                                            
                            
            
                                    
            
            
                | 763 |  |  |     ): array { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 764 |  |  |         self::ensureValidAction($callback_function); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 765 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 766 |  |  |         $chats = DB::selectChats($select_chats_params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 767 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 768 |  |  |         $results = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 769 |  |  |         if (is_array($chats)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 770 |  |  |             foreach ($chats as $row) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 771 |  |  |                 $data['chat_id'] = $row['chat_id']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 772 |  |  |                 $results[]       = self::send($callback_function, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 773 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 774 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 775 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 776 |  |  |         return $results; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 777 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 778 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 779 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 780 |  |  |      * Enable request limiter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 781 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 782 |  |  |      * @param bool  $enable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 783 |  |  |      * @param array $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 784 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 785 |  |  |      * @throws TelegramException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 786 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 787 |  |  |     public static function setLimiter(bool $enable = true, array $options = []): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 788 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 789 |  |  |         if (DB::isDbConnected()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 790 |  |  |             $options_default = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 791 |  |  |                 'interval' => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 792 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 793 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 794 |  |  |             $options = array_merge($options_default, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 795 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 796 |  |  |             if (!is_numeric($options['interval']) || $options['interval'] <= 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 797 |  |  |                 throw new TelegramException('Interval must be a number and must be greater than zero!'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 798 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 799 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 800 |  |  |             self::$limiter_interval = $options['interval']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 801 |  |  |             self::$limiter_enabled  = $enable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 802 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 803 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 804 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 805 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 806 |  |  |      * This functions delays API requests to prevent reaching Telegram API limits | 
            
                                                                                                            
                            
            
                                    
            
            
                | 807 |  |  |      *  Can be disabled while in execution by 'Request::setLimiter(false)' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 808 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 809 |  |  |      * @link https://core.telegram.org/bots/faq#my-bot-is-hitting-limits-how-do-i-avoid-this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 810 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 811 |  |  |      * @param string $action | 
            
                                                                                                            
                            
            
                                    
            
            
                | 812 |  |  |      * @param array  $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 813 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 814 |  |  |      * @throws TelegramException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 815 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 816 |  |  |     private static function limitTelegramRequests(string $action, array $data = []): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 817 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 818 |  |  |         if (self::$limiter_enabled) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 819 |  |  |             $limited_methods = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 820 |  |  |                 'sendMessage', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 821 |  |  |                 'forwardMessage', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 822 |  |  |                 'copyMessage', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 823 |  |  |                 'sendPhoto', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 824 |  |  |                 'sendAudio', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 825 |  |  |                 'sendDocument', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 826 |  |  |                 'sendSticker', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 827 |  |  |                 'sendVideo', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 828 |  |  |                 'sendAnimation', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 829 |  |  |                 'sendVoice', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 830 |  |  |                 'sendVideoNote', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 831 |  |  |                 'sendMediaGroup', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 832 |  |  |                 'sendLocation', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 833 |  |  |                 'editMessageLiveLocation', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 834 |  |  |                 'stopMessageLiveLocation', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 835 |  |  |                 'sendVenue', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 836 |  |  |                 'sendContact', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 837 |  |  |                 'sendPoll', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 838 |  |  |                 'sendDice', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 839 |  |  |                 'sendInvoice', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 840 |  |  |                 'sendGame', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 841 |  |  |                 'setGameScore', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 842 |  |  |                 'setMyCommands', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 843 |  |  |                 'editMessageText', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 844 |  |  |                 'editMessageCaption', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 845 |  |  |                 'editMessageMedia', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 846 |  |  |                 'editMessageReplyMarkup', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 847 |  |  |                 'stopPoll', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 848 |  |  |                 'setChatTitle', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 849 |  |  |                 'setChatDescription', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 850 |  |  |                 'setChatStickerSet', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 851 |  |  |                 'deleteChatStickerSet', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 852 |  |  |                 'setPassportDataErrors', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 853 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 854 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 855 |  |  |             $chat_id           = $data['chat_id'] ?? null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 856 |  |  |             $inline_message_id = $data['inline_message_id'] ?? null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 857 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 858 |  |  |             if (($chat_id || $inline_message_id) && in_array($action, $limited_methods, true)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 859 |  |  |                 $timeout = 60; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 860 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 861 |  |  |                 while (true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 862 |  |  |                     if ($timeout <= 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 863 |  |  |                         throw new TelegramException('Timed out while waiting for a request spot!'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 864 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 865 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 866 |  |  |                     if (!($requests = DB::getTelegramRequestCount($chat_id, $inline_message_id))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 867 |  |  |                         break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 868 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 869 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 870 |  |  |                     // Make sure we're handling integers here. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 871 |  |  |                     $requests = array_map('intval', $requests); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 872 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 873 |  |  |                     $chat_per_second   = ($requests['LIMIT_PER_SEC'] === 0);    // No more than one message per second inside a particular chat | 
            
                                                                                                            
                            
            
                                    
            
            
                | 874 |  |  |                     $global_per_second = ($requests['LIMIT_PER_SEC_ALL'] < 30); // No more than 30 messages per second to different chats | 
            
                                                                                                            
                            
            
                                    
            
            
                | 875 |  |  |                     $groups_per_minute = (((is_numeric($chat_id) && $chat_id > 0) || $inline_message_id !== null) || ((!is_numeric($chat_id) || $chat_id < 0) && $requests['LIMIT_PER_MINUTE'] < 20));    // No more than 20 messages per minute in groups and channels | 
            
                                                                                                            
                            
            
                                    
            
            
                | 876 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 877 |  |  |                     if ($chat_per_second && $global_per_second && $groups_per_minute) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 878 |  |  |                         break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 879 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 880 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 881 |  |  |                     $timeout--; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 882 |  |  |                     usleep((int) (self::$limiter_interval * 1000000)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 883 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 884 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 885 |  |  |                 DB::insertTelegramRequest($action, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 886 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 887 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 888 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 889 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 890 |  |  |  |