*/ class Telegram { /** * Constant for type Inline Query. */ const INLINE_QUERY = 'inline_query'; /** * Constant for type Callback Query. */ const CALLBACK_QUERY = 'callback_query'; /** * Constant for type Edited Message. */ const EDITED_MESSAGE = 'edited_message'; /** * Constant for type Reply. */ const REPLY = 'reply'; /** * Constant for type Message. */ const MESSAGE = 'message'; /** * Constant for type Photo. */ const PHOTO = 'photo'; /** * Constant for type Video. */ const VIDEO = 'video'; /** * Constant for type Audio. */ const AUDIO = 'audio'; /** * Constant for type Voice. */ const VOICE = 'voice'; /** * Constant for type animation. */ const ANIMATION = 'animation'; /** * Constant for type sticker. */ const STICKER = 'sticker'; /** * Constant for type Document. */ const DOCUMENT = 'document'; /** * Constant for type Location. */ const LOCATION = 'location'; /** * Constant for type Contact. */ const CONTACT = 'contact'; /** * Constant for type Channel Post. */ const CHANNEL_POST = 'channel_post'; /** * Constant for type New Chat Member. */ const NEW_CHAT_MEMBER = 'new_chat_member'; /** * Constant for type Left Chat Member. */ const LEFT_CHAT_MEMBER = 'left_chat_member'; /** * Constant for type My Chat Member. */ const MY_CHAT_MEMBER = 'my_chat_member'; private $bot_token = ''; private $data = []; private $updates = []; private $log_errors; private $proxy; private $update_type; /// Class constructor /** * Create a Telegram instance from the bot token * \param $bot_token the bot token * \param $log_errors enable or disable the logging * \param $proxy array with the proxy configuration (url, port, type, auth) * \return an instance of the class. */ public function __construct($bot_token, $log_errors = true, array $proxy = []) { $this->bot_token = $bot_token; $this->data = $this->getData(); $this->log_errors = $log_errors; $this->proxy = $proxy; } /// Do requests to Telegram Bot API /** * Contacts the various API's endpoints * \param $api the API endpoint * \param $content the request parameters as array * \param $post boolean tells if $content needs to be sends * \return the JSON Telegram's reply. */ public function endpoint($api, array $content, $post = true) { $url = 'https://api.telegram.org/bot'.$this->bot_token.'/'.$api; if ($post) { $reply = $this->sendAPIRequest($url, $content); } else { $reply = $this->sendAPIRequest($url, [], false); } return json_decode($reply, true); } /// A method for testing your bot. /** * See getMe * \return the JSON Telegram's reply. */ public function getMe() { return $this->endpoint('getMe', [], false); } /** * See logOut * \return the JSON Telegram's reply. */ public function logOut() { return $this->endpoint('logOut', [], false); } /** * See close * \return the JSON Telegram's reply. */ public function close() { return $this->endpoint('close', [], false); } /// A method for responding http to Telegram. /** * \return the HTTP 200 to Telegram. */ public function respondSuccess() { http_response_code(200); return json_encode(['status' => 'success']); } /// Send a message /** * See sendMessage for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function sendMessage(array $content) { return $this->endpoint('sendMessage', $content); } /// Copy a message /** * See copyMessage for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function copyMessage(array $content) { return $this->endpoint('copyMessage', $content); } /// Forward a message /** * See forwardMessage for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function forwardMessage(array $content) { return $this->endpoint('forwardMessage', $content); } /// Send a photo /** * See sendPhoto for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function sendPhoto(array $content) { return $this->endpoint('sendPhoto', $content); } /// Send an audio /** * See sendAudio for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function sendAudio(array $content) { return $this->endpoint('sendAudio', $content); } /// Send a document /** * See sendDocument for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function sendDocument(array $content) { return $this->endpoint('sendDocument', $content); } /// Send an animation /** * See sendAnimation for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function sendAnimation(array $content) { return $this->endpoint('sendAnimation', $content); } /// Send a sticker /** * See sendSticker for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function sendSticker(array $content) { return $this->endpoint('sendSticker', $content); } /// Send a video /** * See sendVideo for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function sendVideo(array $content) { return $this->endpoint('sendVideo', $content); } /// Send a voice message /** * See sendVoice for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function sendVoice(array $content) { return $this->endpoint('sendVoice', $content); } /// Send a location /** * See sendLocation for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function sendLocation(array $content) { return $this->endpoint('sendLocation', $content); } /// Edit Message Live Location /** * See editMessageLiveLocation for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function editMessageLiveLocation(array $content) { return $this->endpoint('editMessageLiveLocation', $content); } /// Stop Message Live Location /** * See stopMessageLiveLocation for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function stopMessageLiveLocation(array $content) { return $this->endpoint('stopMessageLiveLocation', $content); } /// Set Chat Sticker Set /** * See setChatStickerSet for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function setChatStickerSet(array $content) { return $this->endpoint('setChatStickerSet', $content); } /// Delete Chat Sticker Set /** * See deleteChatStickerSet for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function deleteChatStickerSet(array $content) { return $this->endpoint('deleteChatStickerSet', $content); } /// Send Media Group /** * See sendMediaGroup for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function sendMediaGroup(array $content) { return $this->endpoint('sendMediaGroup', $content); } /// Send Venue /** * See sendVenue for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function sendVenue(array $content) { return $this->endpoint('sendVenue', $content); } //Send contact /** * See sendContact for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function sendContact(array $content) { return $this->endpoint('sendContact', $content); } //Send a poll /** * See sendPoll for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function sendPoll(array $content) { return $this->endpoint('sendPoll', $content); } //Send a dice /** * See sendDice for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function sendDice(array $content) { return $this->endpoint('sendDice', $content); } /// Send a chat action /** * See sendChatAction for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function sendChatAction(array $content) { return $this->endpoint('sendChatAction', $content); } /// Get a list of profile pictures for a user /** * See getUserProfilePhotos for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function getUserProfilePhotos(array $content) { return $this->endpoint('getUserProfilePhotos', $content); } /// Use this method to get basic info about a file and prepare it for downloading /** * 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/, where 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. * \param $file_id String File identifier to get info about * \return the JSON Telegram's reply. */ public function getFile($file_id) { $content = ['file_id' => $file_id]; return $this->endpoint('getFile', $content); } /// Kick Chat Member /** * Deprecated * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function kickChatMember(array $content) { return $this->endpoint('kickChatMember', $content); } /// Leave Chat /** * See leaveChat for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function leaveChat(array $content) { return $this->endpoint('leaveChat', $content); } /// Ban Chat Member /** * See banChatMember for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function banChatMember(array $content) { return $this->endpoint('banChatMember', $content); } /// Unban Chat Member /** * See unbanChatMember for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function unbanChatMember(array $content) { return $this->endpoint('unbanChatMember', $content); } /// Get Chat Information /** * See getChat for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function getChat(array $content) { return $this->endpoint('getChat', $content); } /// Get chat Administrators /** * See getChatAdministrators for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function getChatAdministrators(array $content) { return $this->endpoint('getChatAdministrators', $content); } /// Get chat member count /** * See getChatMemberCount for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function getChatMemberCount(array $content) { return $this->endpoint('getChatMemberCount', $content); } /** * For retrocompatibility * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function getChatMembersCount(array $content) { return $this->getChatMemberCount($content); } /** * See getChatMember for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function getChatMember(array $content) { return $this->endpoint('getChatMember', $content); } /** * See answerInlineQuery for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function answerInlineQuery(array $content) { return $this->endpoint('answerInlineQuery', $content); } /// Set Game Score /** * See setGameScore for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function setGameScore(array $content) { return $this->endpoint('setGameScore', $content); } /// Get Game Hi Scores /** * See getGameHighScores for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function getGameHighScores(array $content) { return $this->endpoint('getGameHighScores', $content); } /// Answer a callback Query /** * See answerCallbackQuery for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function answerCallbackQuery(array $content) { return $this->endpoint('answerCallbackQuery', $content); } /// Set the list of the bot commands /** * See setMyCommands for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function setMyCommands(array $content) { return $this->endpoint('setMyCommands', $content); } /// Delete the list of the bot commands /** * See deleteMyCommands for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function deleteMyCommands(array $content) { return $this->endpoint('deleteMyCommands', $content); } /// Get the list of the bot commands /** * See getMyCommands for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function getMyCommands(array $content) { return $this->endpoint('getMyCommands', $content); } /// Set the chat menu button /** * See setChatMenuButton for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function setChatMenuButton(array $content) { return $this->endpoint('setChatMenuButton', $content); } /// Get the chat menu button /** * See getChatMenuButton for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function getChatMenuButton(array $content) { return $this->endpoint('getChatMenuButton', $content); } /// Set the default aministrator rights /** * See setMyDefaultAdministratorRights for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function setMyDefaultAdministratorRights(array $content) { return $this->endpoint('setMyDefaultAdministratorRights', $content); } /// Get the default aministrator rights /** * See getMyDefaultAdministratorRights for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function getMyDefaultAdministratorRights(array $content) { return $this->endpoint('getMyDefaultAdministratorRights', $content); } /** * See editMessageText for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function editMessageText(array $content) { return $this->endpoint('editMessageText', $content); } /** * See editMessageCaption for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function editMessageCaption(array $content) { return $this->endpoint('editMessageCaption', $content); } /** * See editMessageMedia for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function editMessageMedia(array $content) { return $this->endpoint('editMessageMedia', $content); } /** * See editMessageReplyMarkup for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function editMessageReplyMarkup(array $content) { return $this->endpoint('editMessageReplyMarkup', $content); } /** * See stopPoll for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function stopPoll(array $content) { return $this->endpoint('stopPoll', $content); } /// Use this method to download a file /** * Use this method to to download a file from the Telegram servers. * \param $telegram_file_path String File path on Telegram servers * \param $local_file_path String File path where save the file. */ public function downloadFile($telegram_file_path, $local_file_path) { $file_url = 'https://api.telegram.org/file/bot'.$this->bot_token.'/'.$telegram_file_path; $in = fopen($file_url, 'rb'); $out = fopen($local_file_path, 'wb'); while ($chunk = fread($in, 8192)) { fwrite($out, $chunk, 8192); } fclose($in); fclose($out); } /// Set a WebHook for the bot /** * 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. * * If you'd like to make sure that the Webhook request comes from Telegram, we recommend using a secret path in the URL, e.g. https://www.example.com/. Since nobody else knows your bot‘s token, you can be pretty sure it’s us. * \param $url String HTTPS url to send updates to. Use an empty string to remove webhook integration * \param $certificate InputFile Upload your public key certificate so that the root certificate in use can be checked * \return the JSON Telegram's reply. */ public function setWebhook($url, $certificate = '') { if ($certificate == '') { $requestBody = ['url' => $url]; } else { $requestBody = ['url' => $url, 'certificate' => "@$certificate"]; } return $this->endpoint('setWebhook', $requestBody, true); } /// Delete the WebHook for the bot /** * Use this method to remove webhook integration if you decide to switch back to getUpdates. Returns True on success. Requires no parameters. * \return the JSON Telegram's reply. */ public function deleteWebhook() { return $this->endpoint('deleteWebhook', [], false); } /// Get the data of the current message /** Get the POST request of a user in a Webhook or the message actually processed in a getUpdates() enviroment. * \return the JSON users's message. */ public function getData() { if (empty($this->data)) { $rawData = file_get_contents('php://input'); return json_decode($rawData, true); } else { return $this->data; } } /// Set the data currently used public function setData(array $data) { $this->data = $data; } /// Get the text of the current message /** * \return the String users's text. */ public function Text() { $type = $this->getUpdateType(); if ($type == self::CALLBACK_QUERY) { return @$this->data['callback_query']['data']; } if ($type == self::CHANNEL_POST) { return @$this->data['channel_post']['text']; } if ($type == self::EDITED_MESSAGE) { return @$this->data['edited_message']['text']; } return @$this->data['message']['text']; } public function Caption() { $type = $this->getUpdateType(); if ($type == self::CHANNEL_POST) { return @$this->data['channel_post']['caption']; } return @$this->data['message']['caption']; } /// Get the chat_id of the current message /** * \return the String users's chat_id. */ public function ChatID() { $chat = $this->Chat(); return $chat['id']; } /** * \return the Array chat. */ public function Chat() { $type = $this->getUpdateType(); if ($type == self::CALLBACK_QUERY) { return @$this->data['callback_query']['message']['chat']; } if ($type == self::CHANNEL_POST) { return @$this->data['channel_post']['chat']; } if ($type == self::EDITED_MESSAGE) { return @$this->data['edited_message']['chat']; } if ($type == self::INLINE_QUERY) { return @$this->data['inline_query']['from']; } if ($type == self::MY_CHAT_MEMBER) { return @$this->data['my_chat_member']['chat']; } return $this->data['message']['chat']; } /// Get the message_id of the current message /** * \return the String message_id. */ public function MessageID() { $type = $this->getUpdateType(); if ($type == self::CALLBACK_QUERY) { return @$this->data['callback_query']['message']['message_id']; } if ($type == self::CHANNEL_POST) { return @$this->data['channel_post']['message_id']; } if ($type == self::EDITED_MESSAGE) { return @$this->data['edited_message']['message_id']; } return $this->data['message']['message_id']; } /// Get the reply_to_message message_id of the current message /** * \return the String reply_to_message message_id. */ public function ReplyToMessageID() { return $this->data['message']['reply_to_message']['message_id']; } /// Get the reply_to_message forward_from user_id of the current message /** * \return the String reply_to_message forward_from user_id. */ public function ReplyToMessageFromUserID() { return $this->data['message']['reply_to_message']['forward_from']['id']; } /// Get the inline_query of the current update /** * \return the Array inline_query. */ public function Inline_Query() { return $this->data['inline_query']; } /// Get the callback_query of the current update /** * \return the String callback_query. */ public function Callback_Query() { return $this->data['callback_query']; } /// Get the callback_query id of the current update /** * \return the String callback_query id. */ public function Callback_ID() { return $this->data['callback_query']['id']; } /// Get the Get the data of the current callback /** * \deprecated Use Text() instead * \return the String callback_data. */ public function Callback_Data() { return $this->data['callback_query']['data']; } /// Get the Get the message of the current callback /** * \return the Message. */ public function Callback_Message() { return $this->data['callback_query']['message']; } /// Get the Get the chat_id of the current callback /** * \deprecated Use ChatId() instead * \return the String callback_query. */ public function Callback_ChatID() { return $this->data['callback_query']['message']['chat']['id']; } /// Get the Get the from_id of the current callback /** * \return the String callback_query from_id. */ public function Callback_FromID() { return $this->data['callback_query']['from']['id']; } /// Get the date of the current message /** * \return the String message's date. */ public function Date() { return $this->data['message']['date']; } /// Get the first name of the user public function FirstName() { $type = $this->getUpdateType(); if ($type == self::CALLBACK_QUERY) { return @$this->data['callback_query']['from']['first_name']; } if ($type == self::CHANNEL_POST) { return @$this->data['channel_post']['from']['first_name']; } if ($type == self::EDITED_MESSAGE) { return @$this->data['edited_message']['from']['first_name']; } return @$this->data['message']['from']['first_name']; } /// Get the last name of the user public function LastName() { $type = $this->getUpdateType(); if ($type == self::CALLBACK_QUERY) { return @$this->data['callback_query']['from']['last_name']; } if ($type == self::CHANNEL_POST) { return @$this->data['channel_post']['from']['last_name']; } if ($type == self::EDITED_MESSAGE) { return @$this->data['edited_message']['from']['last_name']; } if ($type == self::MESSAGE) { return @$this->data['message']['from']['last_name']; } return ''; } /// Get the username of the user public function Username() { $type = $this->getUpdateType(); if ($type == self::CALLBACK_QUERY) { return @$this->data['callback_query']['from']['username']; } if ($type == self::CHANNEL_POST) { return @$this->data['channel_post']['from']['username']; } if ($type == self::EDITED_MESSAGE) { return @$this->data['edited_message']['from']['username']; } return @$this->data['message']['from']['username']; } /// Get the location in the message public function Location() { return $this->data['message']['location']; } /// Get the update_id of the message public function UpdateID() { return $this->data['update_id']; } /// Get the number of updates public function UpdateCount() { return count($this->updates['result']); } /// Get user's id of current message public function UserID() { $type = $this->getUpdateType(); if ($type == self::CALLBACK_QUERY) { return $this->data['callback_query']['from']['id']; } if ($type == self::CHANNEL_POST) { return $this->data['channel_post']['from']['id']; } if ($type == self::EDITED_MESSAGE) { return @$this->data['edited_message']['from']['id']; } if ($type == self::INLINE_QUERY) { return @$this->data['inline_query']['from']['id']; } return $this->data['message']['from']['id']; } /// Get user's id of current forwarded message public function FromID() { return $this->data['message']['forward_from']['id']; } /// Get chat's id where current message forwarded from public function FromChatID() { return $this->data['message']['forward_from_chat']['id']; } /// Tell if a message is from a group or user chat /** * \return BOOLEAN true if the message is from a Group chat, false otherwise. */ public function messageFromGroup() { if ($this->data['message']['chat']['type'] == 'private') { return false; } return true; } /// Get the contact phone number /** * \return a String of the contact phone number. */ public function getContactPhoneNumber() { if ($this->getUpdateType() == self::CONTACT) { return $this->data['message']['contact']['phone_number']; } return ''; } /// Get the title of the group chat /** * \return a String of the title chat. */ public function messageFromGroupTitle() { if ($this->data['message']['chat']['type'] != 'private') { return $this->data['message']['chat']['title']; } return ''; } /// Set a custom keyboard /** This object represents a custom keyboard with reply options * \param $options Array of Array of String; Array of button rows, each represented by an Array of Strings * \param $onetime Boolean Requests clients to hide the keyboard as soon as it's been used. Defaults to false. * \param $resize Boolean Requests clients to resize the keyboard vertically for optimal fit (e.g., make the keyboard smaller if there are just two rows of buttons). Defaults to false, in which case the custom keyboard is always of the same height as the app's standard keyboard. * \param $selective Boolean Use this parameter if you want to show the keyboard to specific users only. Targets: 1) users that are @mentioned in the text of the Message object; 2) if the bot's message is a reply (has reply_to_message_id), sender of the original message. * \return the requested keyboard as Json. */ public function buildKeyBoard(array $options, $onetime = false, $resize = false, $selective = true) { $replyMarkup = [ 'keyboard' => $options, 'one_time_keyboard' => $onetime, 'resize_keyboard' => $resize, 'selective' => $selective, ]; $encodedMarkup = json_encode($replyMarkup, true); return $encodedMarkup; } /// Set an InlineKeyBoard /** This object represents an inline keyboard that appears right next to the message it belongs to. * \param $options Array of Array of InlineKeyboardButton; Array of button rows, each represented by an Array of InlineKeyboardButton * \return the requested keyboard as Json. */ public function buildInlineKeyBoard(array $options) { $replyMarkup = [ 'inline_keyboard' => $options, ]; $encodedMarkup = json_encode($replyMarkup, true); return $encodedMarkup; } /// Create an InlineKeyboardButton /** This object represents one button of an inline keyboard. You must use exactly one of the optional fields. * \param $text String; Array of button rows, each represented by an Array of Strings * \param $url String Optional. HTTP url to be opened when button is pressed * \param $callback_data String Optional. Data to be sent in a callback query to the bot when button is pressed * \param $switch_inline_query String Optional. If set, pressing the button will prompt the user to select one of their chats, open that chat and insert the bot‘s username and the specified inline query in the input field. Can be empty, in which case just the bot’s username will be inserted. * \param $switch_inline_query_current_chat String Optional. Optional. If set, pressing the button will insert the bot‘s username and the specified inline query in the current chat's input field. Can be empty, in which case only the bot’s username will be inserted. * \param $callback_game String Optional. Description of the game that will be launched when the user presses the button. * \param $pay Boolean Optional. Specify True, to send a Pay button. * \return the requested button as Array. */ public function buildInlineKeyboardButton( $text, $url = '', $callback_data = '', $switch_inline_query = null, $switch_inline_query_current_chat = null, $callback_game = '', $pay = '' ) { $replyMarkup = [ 'text' => $text, ]; if ($url != '') { $replyMarkup['url'] = $url; } elseif ($callback_data != '') { $replyMarkup['callback_data'] = $callback_data; } elseif (!is_null($switch_inline_query)) { $replyMarkup['switch_inline_query'] = $switch_inline_query; } elseif (!is_null($switch_inline_query_current_chat)) { $replyMarkup['switch_inline_query_current_chat'] = $switch_inline_query_current_chat; } elseif ($callback_game != '') { $replyMarkup['callback_game'] = $callback_game; } elseif ($pay != '') { $replyMarkup['pay'] = $pay; } return $replyMarkup; } /// Create a KeyboardButton /** This object represents one button of an inline keyboard. You must use exactly one of the optional fields. * \param $text String; Array of button rows, each represented by an Array of Strings * \param $request_contact Boolean Optional. If True, the user's phone number will be sent as a contact when the button is pressed. Available in private chats only * \param $request_location Boolean Optional. If True, the user's current location will be sent when the button is pressed. Available in private chats only * \return the requested button as Array. */ public function buildKeyboardButton($text, $request_contact = false, $request_location = false) { $replyMarkup = [ 'text' => $text, 'request_contact' => $request_contact, 'request_location' => $request_location, ]; return $replyMarkup; } /// Hide a custom keyboard /** Upon receiving a message with this object, Telegram clients will hide the current custom keyboard and display the default letter-keyboard. By default, custom keyboards are displayed until a new keyboard is sent by a bot. An exception is made for one-time keyboards that are hidden immediately after the user presses a button. * \param $selective Boolean Use this parameter if you want to show the keyboard to specific users only. Targets: 1) users that are @mentioned in the text of the Message object; 2) if the bot's message is a reply (has reply_to_message_id), sender of the original message. * \return the requested keyboard hide as Array. */ public function buildKeyBoardHide($selective = true) { $replyMarkup = [ 'remove_keyboard' => true, 'selective' => $selective, ]; $encodedMarkup = json_encode($replyMarkup, true); return $encodedMarkup; } /// Display a reply interface to the user /* Upon receiving a message with this object, Telegram clients will display a reply interface to the user (act as if the user has selected the bot‘s message and tapped ’Reply'). This can be extremely useful if you want to create user-friendly step-by-step interfaces without having to sacrifice privacy mode. * \param $selective Boolean Use this parameter if you want to show the keyboard to specific users only. Targets: 1) users that are @mentioned in the text of the Message object; 2) if the bot's message is a reply (has reply_to_message_id), sender of the original message. * \return the requested force reply as Array */ public function buildForceReply($selective = true) { $replyMarkup = [ 'force_reply' => true, 'selective' => $selective, ]; $encodedMarkup = json_encode($replyMarkup, true); return $encodedMarkup; } // Payments /// Send an invoice /** * See sendInvoice for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function sendInvoice(array $content) { return $this->endpoint('sendInvoice', $content); } /// Answer a shipping query /** * See answerShippingQuery for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function answerShippingQuery(array $content) { return $this->endpoint('answerShippingQuery', $content); } /// Answer a PreCheckout query /** * See answerPreCheckoutQuery for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function answerPreCheckoutQuery(array $content) { return $this->endpoint('answerPreCheckoutQuery', $content); } /// Set Passport data errors /** * See setPassportDataErrors for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function setPassportDataErrors(array $content) { return $this->endpoint('setPassportDataErrors', $content); } /// Send a Game /** * See sendGame for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function sendGame(array $content) { return $this->endpoint('sendGame', $content); } /// Send a video note /** * See sendVideoNote for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function sendVideoNote(array $content) { return $this->endpoint('sendVideoNote', $content); } /// Restrict Chat Member /** * See restrictChatMember for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function restrictChatMember(array $content) { return $this->endpoint('restrictChatMember', $content); } /// Promote Chat Member /** * See promoteChatMember for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function promoteChatMember(array $content) { return $this->endpoint('promoteChatMember', $content); } /// Set chat Administrator custom title /** * See setChatAdministratorCustomTitle for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function setChatAdministratorCustomTitle(array $content) { return $this->endpoint('setChatAdministratorCustomTitle', $content); } /// Ban a channel chat in a super group or channel /** * See banChatSenderChat for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function banChatSenderChat(array $content) { return $this->endpoint('banChatSenderChat', $content); } /// Unban a channel chat in a super group or channel /** * See unbanChatSenderChat for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function unbanChatSenderChat(array $content) { return $this->endpoint('unbanChatSenderChat', $content); } /// Set default chat permission for all members /** * See setChatPermissions for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function setChatPermissions(array $content) { return $this->endpoint('setChatPermissions', $content); } //// Export Chat Invite Link /** * See exportChatInviteLink for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function exportChatInviteLink(array $content) { return $this->endpoint('exportChatInviteLink', $content); } //// Create Chat Invite Link /** * See createChatInviteLink for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function createChatInviteLink(array $content) { return $this->endpoint('createChatInviteLink', $content); } //// Edit Chat Invite Link /** * See editChatInviteLink for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function editChatInviteLink(array $content) { return $this->endpoint('editChatInviteLink', $content); } //// Revoke Chat Invite Link /** * See revokeChatInviteLink for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function revokeChatInviteLink(array $content) { return $this->endpoint('revokeChatInviteLink', $content); } //// Approve chat join request /** * See approveChatJoinRequest for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function approveChatJoinRequest(array $content) { return $this->endpoint('approveChatJoinRequest', $content); } //// Decline chat join request /** * See declineChatJoinRequest for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function declineChatJoinRequest(array $content) { return $this->endpoint('declineChatJoinRequest', $content); } /// Set Chat Photo /** * See setChatPhoto for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function setChatPhoto(array $content) { return $this->endpoint('setChatPhoto', $content); } /// Delete Chat Photo /** * See deleteChatPhoto for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function deleteChatPhoto(array $content) { return $this->endpoint('deleteChatPhoto', $content); } /// Set Chat Title /** * See setChatTitle for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function setChatTitle(array $content) { return $this->endpoint('setChatTitle', $content); } /// Set Chat Description /** * See setChatDescription for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function setChatDescription(array $content) { return $this->endpoint('setChatDescription', $content); } /// Pin Chat Message /** * See pinChatMessage for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function pinChatMessage(array $content) { return $this->endpoint('pinChatMessage', $content); } /// Unpin Chat Message /** * See unpinChatMessage for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function unpinChatMessage(array $content) { return $this->endpoint('unpinChatMessage', $content); } /// Unpin All Chat Messages /** * See unpinAllChatMessages for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function unpinAllChatMessages(array $content) { return $this->endpoint('unpinAllChatMessages', $content); } /// Get Sticker Set /** * See getStickerSet for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function getStickerSet(array $content) { return $this->endpoint('getStickerSet', $content); } /// Upload Sticker File /** * See uploadStickerFile for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function uploadStickerFile(array $content) { return $this->endpoint('uploadStickerFile', $content); } /// Create New Sticker Set /** * See createNewStickerSet for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function createNewStickerSet(array $content) { return $this->endpoint('createNewStickerSet', $content); } /// Add Sticker To Set /** * See addStickerToSet for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function addStickerToSet(array $content) { return $this->endpoint('addStickerToSet', $content); } /// Set Sticker Position In Set /** * See setStickerPositionInSet for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function setStickerPositionInSet(array $content) { return $this->endpoint('setStickerPositionInSet', $content); } /// Delete Sticker From Set /** * See deleteStickerFromSet for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function deleteStickerFromSet(array $content) { return $this->endpoint('deleteStickerFromSet', $content); } /// Set Sticker Thumb From Set /** * See setStickerSetThumb for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function setStickerSetThumb(array $content) { return $this->endpoint('setStickerSetThumb', $content); } /// Delete a message /** * See deleteMessage for the input values * \param $content the request parameters as array * \return the JSON Telegram's reply. */ public function deleteMessage(array $content) { return $this->endpoint('deleteMessage', $content); } /// Receive incoming messages using polling /** Use this method to receive incoming updates using long polling. * \param $offset Integer Identifier of the first update to be returned. Must be greater by one than the highest among the identifiers of previously received updates. By default, updates starting with the earliest unconfirmed update are returned. An update is considered confirmed as soon as getUpdates is called with an offset higher than its update_id. * \param $limit Integer Limits the number of updates to be retrieved. Values between 1—100 are accepted. Defaults to 100 * \param $timeout Integer Timeout in seconds for long polling. Defaults to 0, i.e. usual short polling * \param $update Boolean If true updates the pending message list to the last update received. Default to true. * \return the updates as Array. */ public function getUpdates($offset = 0, $limit = 100, $timeout = 0, $update = true) { $content = ['offset' => $offset, 'limit' => $limit, 'timeout' => $timeout]; $this->updates = $this->endpoint('getUpdates', $content); if ($update) { if (array_key_exists('result', $this->updates) && is_array($this->updates['result']) && count($this->updates['result']) >= 1) { //for CLI working. $last_element_id = $this->updates['result'][count($this->updates['result']) - 1]['update_id'] + 1; $content = ['offset' => $last_element_id, 'limit' => '1', 'timeout' => $timeout]; $this->endpoint('getUpdates', $content); } } return $this->updates; } /// Serve an update /** Use this method to use the bultin function like Text() or Username() on a specific update. * \param $update Integer The index of the update in the updates array. */ public function serveUpdate($update) { $this->data = $this->updates['result'][$update]; } /// Return current update type /** * Return current update type `False` on failure. * * @return bool|string */ public function getUpdateType() { if ($this->update_type) { return $this->update_type; } $update = $this->data; if (isset($update['inline_query'])) { $this->update_type = self::INLINE_QUERY; return $this->update_type; } if (isset($update['callback_query'])) { $this->update_type = self::CALLBACK_QUERY; return $this->update_type; } if (isset($update['edited_message'])) { $this->update_type = self::EDITED_MESSAGE; return $this->update_type; } if (isset($update['message']['text'])) { $this->update_type = self::MESSAGE; return $this->update_type; } if (isset($update['message']['photo'])) { $this->update_type = self::PHOTO; return $this->update_type; } if (isset($update['message']['video'])) { $this->update_type = self::VIDEO; return $this->update_type; } if (isset($update['message']['audio'])) { $this->update_type = self::AUDIO; return $this->update_type; } if (isset($update['message']['voice'])) { $this->update_type = self::VOICE; return $this->update_type; } if (isset($update['message']['contact'])) { $this->update_type = self::CONTACT; return $this->update_type; } if (isset($update['message']['location'])) { $this->update_type = self::LOCATION; return $this->update_type; } if (isset($update['message']['reply_to_message'])) { $this->update_type = self::REPLY; return $this->update_type; } if (isset($update['message']['animation'])) { $this->update_type = self::ANIMATION; return $this->update_type; } if (isset($update['message']['sticker'])) { $this->update_type = self::STICKER; return $this->update_type; } if (isset($update['message']['document'])) { $this->update_type = self::DOCUMENT; return $this->update_type; } if (isset($update['message']['new_chat_member'])) { $this->update_type = self::NEW_CHAT_MEMBER; return $this->update_type; } if (isset($update['message']['left_chat_member'])) { $this->update_type = self::LEFT_CHAT_MEMBER; return $this->update_type; } if (isset($update['my_chat_member'])) { $this->update_type = self::MY_CHAT_MEMBER; return $this->update_type; } if (isset($update['channel_post'])) { $this->update_type = self::CHANNEL_POST; return $this->update_type; } return false; } private function sendAPIRequest($url, array $content, $post = true) { if (isset($content['chat_id'])) { $url = $url.'?chat_id='.$content['chat_id']; unset($content['chat_id']); } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); if ($post) { curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $content); } // echo "inside curl if"; if (!empty($this->proxy)) { // echo "inside proxy if"; if (array_key_exists('type', $this->proxy)) { curl_setopt($ch, CURLOPT_PROXYTYPE, $this->proxy['type']); } if (array_key_exists('auth', $this->proxy)) { curl_setopt($ch, CURLOPT_PROXYUSERPWD, $this->proxy['auth']); } if (array_key_exists('url', $this->proxy)) { // echo "Proxy Url"; curl_setopt($ch, CURLOPT_PROXY, $this->proxy['url']); } if (array_key_exists('port', $this->proxy)) { // echo "Proxy port"; curl_setopt($ch, CURLOPT_PROXYPORT, $this->proxy['port']); } } curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $result = curl_exec($ch); if ($result === false) { $result = json_encode( ['ok' => false, 'curl_error_code' => curl_errno($ch), 'curl_error' => curl_error($ch)] ); } curl_close($ch); if ($this->log_errors) { if (class_exists('TelegramErrorLogger')) { $loggerArray = ($this->getData() == null) ? [$content] : [$this->getData(), $content]; TelegramErrorLogger::log(json_decode($result, true), $loggerArray); } } return $result; } } // Helper for Uploading file using CURL if (!function_exists('curl_file_create')) { function curl_file_create($filename, $mimetype = '', $postname = '') { return "@$filename;filename=" .($postname ?: basename($filename)) .($mimetype ? ";type=$mimetype" : ''); } }