diff --git a/.idea/php.xml b/.idea/php.xml index 05fa7cb..61cd929 100644 --- a/.idea/php.xml +++ b/.idea/php.xml @@ -13,6 +13,7 @@ + diff --git a/src/TgBotLib/Bot.php b/src/TgBotLib/Bot.php index 0bb2e5a..3989ad6 100644 --- a/src/TgBotLib/Bot.php +++ b/src/TgBotLib/Bot.php @@ -5,6 +5,7 @@ namespace TgBotLib; use TgBotLib\Exceptions\TelegramException; + use TgBotLib\Objects\Telegram\Message; use TgBotLib\Objects\Telegram\Update; use TgBotLib\Objects\Telegram\User; use TgBotLib\Objects\Telegram\WebhookInfo; @@ -113,24 +114,24 @@ * @return array * @throws TelegramException */ - public function sendRequest(string $method, array $params = []): array + public function sendRequest(string $method, array $params=[]): array { $ch = curl_init(); curl_setopt_array($ch, [ CURLOPT_URL => $this->getMethodUrl($method), CURLOPT_POST => true, - CURLOPT_POSTFIELDS => http_build_query($params), + CURLOPT_POSTFIELDS => $params, CURLOPT_RETURNTRANSFER => true, - CURLOPT_FOLLOWLOCATION => true, - CURLOPT_AUTOREFERER => true, - CURLOPT_HEADER => false, - CURLOPT_CONNECTTIMEOUT => 10, - CURLOPT_TIMEOUT => 30, + CURLOPT_HTTPHEADER => [ + 'Content-Type: multipart/form-data' + ] ]); + $response = curl_exec($ch); + print_r($response); if ($response === false) - throw new TelegramException('Curl error: ' . curl_error($ch), curl_errno($ch)); + throw new TelegramException('curl error: ' . curl_error($ch), curl_errno($ch)); curl_close($ch); $parsed = json_decode($response, true); @@ -144,9 +145,11 @@ /** * Use this method to receive incoming updates using long polling (wiki). Returns an Array of Update objects. * - * @param array $options + * @param array $options Optional parameters * @return Update[] * @throws TelegramException + * @link https://core.telegram.org/bots/api#getupdates + * @see https://en.wikipedia.org/wiki/Push_technology#Long_polling */ public function getUpdates(array $options=[]): array { @@ -175,9 +178,10 @@ * secret token as content. * * @param string $url HTTPS URL to send updates to. Use an empty string to remove webhook integration - * @param array $options + * @param array $options Optional parameters * @return bool * @throws TelegramException + * @link https://core.telegram.org/bots/api#setwebhook */ public function setWebhook(string $url, array $options=[]): bool { @@ -191,9 +195,10 @@ * Use this method to remove webhook integration if you decide to switch back to getUpdates. * Returns True on success. * - * @param bool $drop_pending_updates + * @param bool $drop_pending_updates Pass True to drop all pending updates * @return bool * @throws TelegramException + * @link https://core.telegram.org/bots/api#deletewebhook */ public function deleteWebhook(bool $drop_pending_updates=false): bool { @@ -210,6 +215,7 @@ * * @return WebhookInfo * @throws TelegramException + * @link https://core.telegram.org/bots/api#getwebhookinfo */ public function getWebhookInfo(): WebHookInfo { @@ -222,9 +228,224 @@ * * @return User * @throws TelegramException + * @link https://core.telegram.org/bots/api#getme */ public function getMe(): User { return User::fromArray($this->sendRequest('getMe')); } + + /** + * Use this method to log out from the cloud Bot API server before launching the bot locally. You must log out + * the bot before running it locally, otherwise there is no guarantee that the bot will receive updates. After + * a successful call, you can immediately log in on a local server, but will not be able to log in back to the + * cloud Bot API server for 10 minutes. Returns True on success. Requires no parameters. + * + * @return bool + * @throws TelegramException + * @link https://core.telegram.org/bots/api#logout + */ + public function logout(): bool + { + $this->sendRequest('logout'); + return true; + } + + /** + * Use this method to close the bot instance before moving it from one local server to another. You need to + * delete the webhook before calling this method to ensure that the bot isn't launched again after server + * restart. The method will return error 429 in the first 10 minutes after the bot is launched. Returns True on + * success. Requires no parameters. + * + * @return bool + * @throws TelegramException + * @link https://core.telegram.org/bots/api#close + */ + public function close(): bool + { + $this->sendRequest('close'); + return true; + } + + /** + * Use this method to send text messages. On success, the sent Message is returned. + * + * @param string $chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername) + * @param string $text Text of the message to be sent, 1-4096 characters after entities parsing + * @param array $options Optional parameters + * @return Message + * @throws TelegramException + * @link https://core.telegram.org/bots/api#sendmessage + */ + public function sendMessage(string $chat_id, string $text, array $options=[]): Message + { + return Message::fromArray($this->sendRequest('sendMessage', array_merge($options, [ + 'chat_id' => $chat_id, + 'text' => $text + ]))); + } + + /** + * Use this method to forward messages of any kind. Service messages can't be forwarded. On success, the sent + * Message is returned. + * + * @param string $chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername) + * @param string $from_chat_id Unique identifier for the chat where the original message was sent (or channel username in the format @channelusername) + * @param int $message_id Message identifier in the chat specified in from_chat_id + * @param array $options Optional parameters + * @return Message + * @throws TelegramException + * @link https://core.telegram.org/bots/api#forwardmessage + */ + public function forwardMessage(string $chat_id, string $from_chat_id, int $message_id, array $options=[]): Message + { + return Message::fromArray($this->sendRequest('forwardMessage', array_merge($options, [ + 'chat_id' => $chat_id, + 'from_chat_id' => $from_chat_id, + 'message_id' => $message_id + ]))); + } + + /** + * Use this method to copy messages of any kind. Service messages and invoice messages can't be copied. A quiz + * poll can be copied only if the value of the field correct_option_id is known to the bot. The method is + * analogous to the method forwardMessage, but the copied message doesn't have a link to the original message. + * Returns the MessageId of the sent message on success. + * + * @param string $chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername) + * @param string $from_chat_id Unique identifier for the chat where the original message was sent (or channel username in the format @channelusername) + * @param int $message_id Message identifier in the chat specified in from_chat_id + * @param array $options Optional parameters + * @return Message + * @throws TelegramException + * @link https://core.telegram.org/bots/api#copymessage + */ + public function copyMessage(string $chat_id, string $from_chat_id, int $message_id, array $options=[]): Message + { + return Message::fromArray($this->sendRequest('copyMessage', array_merge($options, [ + 'chat_id' => $chat_id, + 'from_chat_id' => $from_chat_id, + 'message_id' => $message_id + ]))); + } + + /** + * Use this method to send photos. On success, the sent Message is returned. + * + * @param string $chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername) + * @param string $photo Photo to send. Pass a file_id as String to send a photo that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get a photo from the Internet, or upload a new photo using multipart/form-data. The photo must be at most 10 MB in size. The photo's width and height must not exceed 10000 in total. Width and height ratio must be at most 20. + * @param array $options Optional parameters + * @return Message + * @throws TelegramException + * @link https://core.telegram.org/bots/api#sendphoto + */ + public function sendPhoto(string $chat_id, string $photo, array $options=[]): Message + { + return Message::fromArray($this->sendRequest('sendPhoto', array_merge($options, [ + 'chat_id' => $chat_id, + 'photo' => (file_exists($photo) ? "@{$photo}" : $photo) + ]))); + } + + /** + * 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 or .M4A 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. + * + * For sending voice messages, use the sendVoice method instead. + * + * @param string $chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername) + * @param string $audio Audio file to send. Pass a file_id as String to send an audio file that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get an audio file from the Internet, or upload a new one using multipart/form-data. + * @param array $options Optional parameters + * @return Message + * @throws TelegramException + * @link https://core.telegram.org/bots/api#sendaudio + */ + public function sendAudio(string $chat_id, string $audio, array $options=[]): Message + { + return Message::fromArray($this->sendRequest('sendAudio', array_merge($options, [ + 'chat_id' => $chat_id, + 'audio' => (file_exists($audio) ? "@{$audio}" : $audio) + ]))); + } + + /** + * 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. + * + * @param string $chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername) + * @param string $document File to send. Pass a file_id as String to send a file that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get a file from the Internet, or upload a new one using multipart/form-data. + * @param array $options Optional parameters + * @return Message + * @throws TelegramException + * @link https://core.telegram.org/bots/api#senddocument + */ + public function sendDocument(string $chat_id, string $document, array $options=[]): Message + { + return Message::fromArray($this->sendRequest('sendDocument', array_merge($options, [ + 'chat_id' => $chat_id, + 'document' => (file_exists($document) ? "@{$document}" : $document) + ]))); + } + + /** + * Use this method to send video files, Telegram clients support MPEG4 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. + * + * @param string $chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername) + * @param string $video Video to send. Pass a file_id as String to send a video that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get a video from the Internet, or upload a new video using multipart/form-data. + * @param array $options Optional parameters + * @return Message + * @throws TelegramException + * @link https://core.telegram.org/bots/api#sendvideo + */ + public function sendVideo(string $chat_id, string $video, array $options=[]): Message + { + return Message::fromArray($this->sendRequest('sendVideo', array_merge($options, [ + 'chat_id' => $chat_id, + 'video' => (file_exists($video) ? "@{$video}" : $video) + ]))); + } + + /** + * 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. + * + * @param string $chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername) + * @param string $animation Animation to send. Pass a file_id as String to send an animation that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get an animation from the Internet, or upload a new animation using multipart/form-data. + * @param array $options Optional parameters + * @return Message + * @throws TelegramException + * @link https://core.telegram.org/bots/api#sendanimation + */ + public function sendAnimation(string $chat_id, string $animation, array $options=[]): Message + { + return Message::fromArray($this->sendRequest('sendAnimation', array_merge($options, [ + 'chat_id' => $chat_id, + 'animation' => (file_exists($animation) ? "@{$animation}" : $animation) + ]))); + } + + /** + * 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. + * + * @param string $chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername) + * @param string $voice Audio file to send. Pass a file_id as String to send a file that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get a file from the Internet, or upload a new one using multipart/form-data. + * @param array $options Optional parameters + * @return Message + * @throws TelegramException + * @link https://core.telegram.org/bots/api#sendvoice + */ + public function sendVoice(string $chat_id, string $voice, array $options=[]): Message + { + return Message::fromArray($this->sendRequest('sendVoice', array_merge($options, [ + 'chat_id' => $chat_id, + 'voice' => (file_exists($voice) ? "@{$voice}" : $voice) + ]))); + } } \ No newline at end of file diff --git a/src/TgBotLib/Objects/Telegram/Message.php b/src/TgBotLib/Objects/Telegram/Message.php index 1558dbe..0ee51b5 100644 --- a/src/TgBotLib/Objects/Telegram/Message.php +++ b/src/TgBotLib/Objects/Telegram/Message.php @@ -1221,9 +1221,9 @@ * Constructs the object from an array representation * * @param array $data - * @return ObjectTypeInterface + * @return Message */ - public static function fromArray(array $data): ObjectTypeInterface + public static function fromArray(array $data): Message { $object = new self(); diff --git a/src/TgBotLib/Objects/Telegram/WebhookInfo.php b/src/TgBotLib/Objects/Telegram/WebhookInfo.php index aea00c2..9e43a06 100644 --- a/src/TgBotLib/Objects/Telegram/WebhookInfo.php +++ b/src/TgBotLib/Objects/Telegram/WebhookInfo.php @@ -169,9 +169,9 @@ * Constructs the object from an array * * @param array $data - * @return ObjectTypeInterface + * @return WebhookInfo */ - public static function fromArray(array $data): ObjectTypeInterface + public static function fromArray(array $data): WebhookInfo { $object = new self(); diff --git a/tests/example.png b/tests/example.png new file mode 100644 index 0000000..fd29b62 Binary files /dev/null and b/tests/example.png differ diff --git a/tests/get_updates.php b/tests/get_updates.php index 552dde4..3357c8d 100644 --- a/tests/get_updates.php +++ b/tests/get_updates.php @@ -12,6 +12,8 @@ if($update->getMessage() !== null && $update->getMessage()->getText() !== null) { print(sprintf("%s: %s", $update->getMessage()->getFrom()->getFirstName(), $update->getMessage()->getText()) . PHP_EOL); + $bot->sendMessage($update->getMessage()->getChat()->getId(), sprintf("Hello %s!", $update->getMessage()->getFrom()->getFirstName())); + $bot->sendPhoto($update->getMessage()->getChat()->getId(), 'https://git.n64.cc/uploads/-/system/appearance/header_logo/1/9d9bd13afce1a798d22ecfd9897730ed.gif'); } } } \ No newline at end of file