diff --git a/examples/Commands/ForcereplyCommand.php b/examples/Commands/ForcereplyCommand.php index d5b3151b4..23a96e12a 100644 --- a/examples/Commands/ForcereplyCommand.php +++ b/examples/Commands/ForcereplyCommand.php @@ -11,8 +11,8 @@ namespace Longman\TelegramBot\Commands\UserCommands; use Longman\TelegramBot\Commands\UserCommand; +use Longman\TelegramBot\Entities\Keyboard; use Longman\TelegramBot\Request; -use Longman\TelegramBot\Entities\ForceReply; /** * User "/forcereply" command @@ -25,7 +25,7 @@ class ForceReplyCommand extends UserCommand protected $name = 'forcereply'; protected $description = 'Force reply with reply markup'; protected $usage = '/forcereply'; - protected $version = '0.0.6'; + protected $version = '0.1.0'; /**#@-*/ /** @@ -33,13 +33,12 @@ class ForceReplyCommand extends UserCommand */ public function execute() { - $message = $this->getMessage(); - $chat_id = $message->getChat()->getId(); + $chat_id = $this->getMessage()->getChat()->getId(); $data = [ 'chat_id' => $chat_id, 'text' => 'Write something:', - 'reply_markup' => new ForceReply(['selective' => false]), + 'reply_markup' => Keyboard::forceReply(), ]; return Request::sendMessage($data); diff --git a/examples/Commands/HidekeyboardCommand.php b/examples/Commands/HidekeyboardCommand.php index 269ffd492..a5abf1493 100644 --- a/examples/Commands/HidekeyboardCommand.php +++ b/examples/Commands/HidekeyboardCommand.php @@ -11,8 +11,7 @@ namespace Longman\TelegramBot\Commands\UserCommands; use Longman\TelegramBot\Commands\UserCommand; -use Longman\TelegramBot\Entities\ReplyKeyboardHide; -use Longman\TelegramBot\Entities\ReplyKeyboardMarkup; +use Longman\TelegramBot\Entities\Keyboard; use Longman\TelegramBot\Request; /** @@ -26,7 +25,7 @@ class HidekeyboardCommand extends UserCommand protected $name = 'hidekeyboard'; protected $description = 'Hide the custom keyboard'; protected $usage = '/hidekeyboard'; - protected $version = '0.0.6'; + protected $version = '0.1.0'; /**#@-*/ /** @@ -34,13 +33,12 @@ class HidekeyboardCommand extends UserCommand */ public function execute() { - $message = $this->getMessage(); - $chat_id = $message->getChat()->getId(); + $chat_id = $this->getMessage()->getChat()->getId(); $data = [ 'chat_id' => $chat_id, 'text' => 'Keyboard Hidden', - 'reply_markup' => new ReplyKeyboardHide(['selective' => false]), + 'reply_markup' => Keyboard::hide(), ]; return Request::sendMessage($data); diff --git a/examples/Commands/InlinekeyboardCommand.php b/examples/Commands/InlinekeyboardCommand.php index 575c63f8c..1f60a2537 100644 --- a/examples/Commands/InlinekeyboardCommand.php +++ b/examples/Commands/InlinekeyboardCommand.php @@ -11,9 +11,8 @@ namespace Longman\TelegramBot\Commands\UserCommands; use Longman\TelegramBot\Commands\UserCommand; +use Longman\TelegramBot\Entities\InlineKeyboard; use Longman\TelegramBot\Request; -use Longman\TelegramBot\Entities\InlineKeyboardMarkup; -use Longman\TelegramBot\Entities\InlineKeyboardButton; /** * User "/inlinekeyboard" command @@ -23,10 +22,10 @@ class InlinekeyboardCommand extends UserCommand /**#@+ * {@inheritdoc} */ - protected $name = 'Inlinekeyboard'; + protected $name = 'inlinekeyboard'; protected $description = 'Show inline keyboard'; protected $usage = '/inlinekeyboard'; - protected $version = '0.0.2'; + protected $version = '0.1.0'; /**#@-*/ /** @@ -34,17 +33,18 @@ class InlinekeyboardCommand extends UserCommand */ public function execute() { - $message = $this->getMessage(); + $chat_id = $this->getMessage()->getChat()->getId(); - $inline_keyboard = [ - new InlineKeyboardButton(['text' => 'inline', 'switch_inline_query' => 'true']), - new InlineKeyboardButton(['text' => 'callback', 'callback_data' => 'identifier']), - new InlineKeyboardButton(['text' => 'open url', 'url' => 'https://github.com/akalongman/php-telegram-bot']), - ]; - $data = [ - 'chat_id' => $message->getChat()->getId(), + $inline_keyboard = new InlineKeyboard([ + ['text' => 'inline', 'switch_inline_query' => 'true'], + ['text' => 'callback', 'callback_data' => 'identifier'], + ['text' => 'open url', 'url' => 'https://github.com/akalongman/php-telegram-bot'], + ]); + + $data = [ + 'chat_id' => $chat_id, 'text' => 'inline keyboard', - 'reply_markup' => new InlineKeyboardMarkup(['inline_keyboard' => [$inline_keyboard]]), + 'reply_markup' => $inline_keyboard, ]; return Request::sendMessage($data); diff --git a/examples/Commands/KeyboardCommand.php b/examples/Commands/KeyboardCommand.php index 6800977b5..0c12b738b 100644 --- a/examples/Commands/KeyboardCommand.php +++ b/examples/Commands/KeyboardCommand.php @@ -11,8 +11,8 @@ namespace Longman\TelegramBot\Commands\UserCommands; use Longman\TelegramBot\Commands\UserCommand; +use Longman\TelegramBot\Entities\Keyboard; use Longman\TelegramBot\Request; -use Longman\TelegramBot\Entities\ReplyKeyboardMarkup; /** * User "/keyboard" command @@ -25,7 +25,7 @@ class KeyboardCommand extends UserCommand protected $name = 'keyboard'; protected $description = 'Show a custom keyboard with reply markup'; protected $usage = '/keyboard'; - protected $version = '0.1.0'; + protected $version = '0.2.0'; /**#@-*/ /** @@ -33,71 +33,54 @@ class KeyboardCommand extends UserCommand */ public function execute() { - $message = $this->getMessage(); - $chat_id = $message->getChat()->getId(); - - $data = [ - 'chat_id' => $chat_id, - 'text' => 'Press a Button:', - ]; - //Keyboard examples + /** @var Keyboard[] $keyboards */ $keyboards = []; //Example 0 - $keyboard = []; - $keyboard[] = ['7', '8', '9']; - $keyboard[] = ['4', '5', '6']; - $keyboard[] = ['1', '2', '3']; - $keyboard[] = [' ', '0', ' ']; - $keyboards[] = $keyboard; + $keyboards[] = new Keyboard( + ['7', '8', '9'], + ['4', '5', '6'], + ['1', '2', '3'], + [' ', '0', ' '] + ); //Example 1 - $keyboard = []; - $keyboard[] = ['7', '8', '9', '+']; - $keyboard[] = ['4', '5', '6', '-']; - $keyboard[] = ['1', '2', '3', '*']; - $keyboard[] = [' ', '0', ' ', '/']; - $keyboards[] = $keyboard; + $keyboards[] = new Keyboard( + ['7', '8', '9', '+'], + ['4', '5', '6', '-'], + ['1', '2', '3', '*'], + [' ', '0', ' ', '/'] + ); //Example 2 - $keyboard = []; - $keyboard[] = ['A']; - $keyboard[] = ['B']; - $keyboard[] = ['C']; - $keyboards[] = $keyboard; + $keyboards[] = new Keyboard('A', 'B', 'C'); //Example 3 - $keyboard = []; - $keyboard[] = ['A']; - $keyboard[] = ['B']; - $keyboard[] = ['C', 'D']; - $keyboards[] = $keyboard; + $keyboards[] = new Keyboard( + ['text' => 'A'], + 'B', + ['C', 'D'] + ); //Example 4 (bots version 2.0) - $keyboard = []; - $keyboard[] = [ - [ - 'text' => 'Send my contact', - 'request_contact' => true, - ], - [ - 'text' => 'Send my location', - 'request_location' => true, - ], - ]; - $keyboards[] = $keyboard; + $keyboards[] = new Keyboard([ + ['text' => 'Send my contact', 'request_contact' => true], + ['text' => 'Send my location', 'request_location' => true], + ]); //Return a random keyboard. - $keyboard = $keyboards[mt_rand(0, count($keyboards) - 1)]; - $data['reply_markup'] = new ReplyKeyboardMarkup( - [ - 'keyboard' => $keyboard, - 'resize_keyboard' => true, - 'one_time_keyboard' => false, - 'selective' => false, - ] - ); + $keyboard = $keyboards[mt_rand(0, count($keyboards) - 1)] + ->setResizeKeyboard(true) + ->setOneTimeKeyboard(true) + ->setSelective(false); + + $chat_id = $this->getMessage()->getChat()->getId(); + $data = [ + 'chat_id' => $chat_id, + 'text' => 'Press a Button:', + 'reply_markup' => $keyboard, + ]; return Request::sendMessage($data); } diff --git a/examples/Commands/MarkdownCommand.php b/examples/Commands/MarkdownCommand.php index 07f863691..5aefdd51c 100644 --- a/examples/Commands/MarkdownCommand.php +++ b/examples/Commands/MarkdownCommand.php @@ -23,7 +23,7 @@ class MarkdownCommand extends UserCommand * {@inheritdoc} */ protected $name = 'markdown'; - protected $description = 'Print Markdown tesxt'; + protected $description = 'Print Markdown text'; protected $usage = '/markdown'; protected $version = '1.0.1'; /**#@-*/ diff --git a/src/Commands/AdminCommands/SendtochannelCommand.php b/src/Commands/AdminCommands/SendtochannelCommand.php index 1ac187ea8..d7ae5b0e7 100644 --- a/src/Commands/AdminCommands/SendtochannelCommand.php +++ b/src/Commands/AdminCommands/SendtochannelCommand.php @@ -10,12 +10,11 @@ namespace Longman\TelegramBot\Commands\AdminCommands; +use Longman\TelegramBot\Entities\Keyboard; use Longman\TelegramBot\Request; use Longman\TelegramBot\Conversation; use Longman\TelegramBot\Commands\AdminCommand; use Longman\TelegramBot\Entities\Message; -use Longman\TelegramBot\Entities\ReplyKeyboardHide; -use Longman\TelegramBot\Entities\ReplyKeyboardMarkup; use Longman\TelegramBot\Exception\TelegramException; class SendtochannelCommand extends AdminCommand @@ -67,7 +66,7 @@ public function execute() $type = $message->getType(); // 'Cast' the command type into message to protect the machine state // if the commmad is recalled when the conversation is already started - $type = ($type === 'command') ? 'Message' : $type; + in_array($type, ['command', 'text'], true) && $type = 'message'; $text = trim($message->getText(true)); $text_yes_or_no = ($text === 'Yes' || $text === 'No'); @@ -78,7 +77,9 @@ public function execute() // Conversation $this->conversation = new Conversation($user_id, $chat_id, $this->getName()); - $notes = &$this->conversation->notes; + + $notes = &$this->conversation->notes; + !is_array($notes) && $notes = []; $channels = (array)$this->getConfig('your_channel'); if (isset($notes['state'])) { @@ -91,12 +92,12 @@ public function execute() switch ($state) { case -1: // getConfig has not been configured asking for channel to administer - if ($type !== 'Message' || $text === '') { + if ($type !== 'message' || $text === '') { $notes['state'] = -1; $this->conversation->update(); $data['text'] = 'Insert the channel name: (@yourchannel)'; - $data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]); + $data['reply_markup'] = Keyboard::hide(['selective' => true]); $result = Request::sendMessage($data); break; @@ -110,7 +111,7 @@ public function execute() default: case 0: // getConfig has been configured choose channel - if ($type !== 'Message' || !in_array($text, $channels, true)) { + if ($type !== 'message' || !in_array($text, $channels, true)) { $notes['state'] = 0; $this->conversation->update(); @@ -118,7 +119,7 @@ public function execute() foreach ($channels as $channel) { $keyboard[] = [$channel]; } - $data['reply_markup'] = new ReplyKeyboardMarkup( + $data['reply_markup'] = new Keyboard( [ 'keyboard' => $keyboard, 'resize_keyboard' => true, @@ -137,17 +138,17 @@ public function execute() // no break case 1: insert: - if (($type === 'Message' && $text === '') || $notes['last_message_id'] === $message->getMessageId()) { + if (($type === 'message' && $text === '') || $notes['last_message_id'] === $message->getMessageId()) { $notes['state'] = 1; $this->conversation->update(); - $data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]); + $data['reply_markup'] = Keyboard::hide(['selective' => true]); $data['text'] = 'Insert the content you want to share: text, photo, audio...'; $result = Request::sendMessage($data); break; } $notes['last_message_id'] = $message->getMessageId(); - $notes['message'] = $message->reflect(); + $notes['message'] = $message->getRawData(); $notes['message_type'] = $type; // no break case 2: @@ -156,8 +157,8 @@ public function execute() $this->conversation->update(); // Execute this just with object that allow caption - if ($notes['message_type'] === 'Video' || $notes['message_type'] === 'Photo') { - $data['reply_markup'] = new ReplyKeyboardMarkup( + if (in_array($notes['message_type'], ['video', 'photo'], true)) { + $data['reply_markup'] = new Keyboard( [ 'keyboard' => [['Yes', 'No']], 'resize_keyboard' => true, @@ -178,12 +179,12 @@ public function execute() $notes['last_message_id'] = $message->getMessageId(); // no break case 3: - if ($notes['set_caption'] && ($notes['last_message_id'] === $message->getMessageId() || $type !== 'Message')) { + if ($notes['set_caption'] && ($notes['last_message_id'] === $message->getMessageId() || $type !== 'message')) { $notes['state'] = 3; $this->conversation->update(); $data['text'] = 'Insert caption:'; - $data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]); + $data['reply_markup'] = Keyboard::hide(['selective' => true]); $result = Request::sendMessage($data); break; } @@ -204,7 +205,7 @@ public function execute() } $this->sendBack(new Message($notes['message'], $this->telegram->getBotName()), $data); - $data['reply_markup'] = new ReplyKeyboardMarkup( + $data['reply_markup'] = new Keyboard( [ 'keyboard' => [['Yes', 'No']], 'resize_keyboard' => true, @@ -226,7 +227,7 @@ public function execute() $notes['last_message_id'] = $message->getMessageId(); // no break case 5: - $data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]); + $data['reply_markup'] = Keyboard::hide(['selective' => true]); if ($notes['post_message']) { $data['text'] = $this->publish( @@ -265,35 +266,37 @@ public function execute() protected function sendBack(Message $message, array $data) { $type = $message->getType(); - $type = ($type === 'command') ? 'Message' : $type; - if ($type === 'Message') { + in_array($type, ['command', 'text'], true) && $type = 'message'; + + if ($type === 'message') { $data['text'] = $message->getText(true); - } elseif ($type === 'Audio') { + } elseif ($type === 'audio') { $data['audio'] = $message->getAudio()->getFileId(); $data['duration'] = $message->getAudio()->getDuration(); $data['performer'] = $message->getAudio()->getPerformer(); $data['title'] = $message->getAudio()->getTitle(); - } elseif ($type === 'Document') { + } elseif ($type === 'document') { $data['document'] = $message->getDocument()->getFileId(); - } elseif ($type === 'Photo') { + } elseif ($type === 'photo') { $data['photo'] = $message->getPhoto()[0]->getFileId(); - } elseif ($type === 'Sticker') { + } elseif ($type === 'sticker') { $data['sticker'] = $message->getSticker()->getFileId(); - } elseif ($type === 'Video') { + } elseif ($type === 'video') { $data['video'] = $message->getVideo()->getFileId(); - } elseif ($type === 'Voice') { + } elseif ($type === 'voice') { $data['voice'] = $message->getVoice()->getFileId(); - } elseif ($type === 'Location') { + } elseif ($type === 'location') { $data['latitude'] = $message->getLocation()->getLatitude(); $data['longitude'] = $message->getLocation()->getLongitude(); } + $callback_path = 'Longman\TelegramBot\Request'; - $callback_function = 'send' . $type; + $callback_function = 'send' . ucfirst($type); if (!method_exists($callback_path, $callback_function)) { throw new TelegramException('Methods: ' . $callback_function . ' not found in class Request.'); } - return call_user_func_array($callback_path . '::' . $callback_function, [$data]); + return $callback_path::$callback_function($data); } /** @@ -347,7 +350,7 @@ public function executeNoDb() $channels = (array)$this->getConfig('your_channel'); $first_channel = $channels[0]; $data['text'] = $this->publish( - new Message($message->reflect(), $this->telegram->getBotName()), + new Message($message->getRawData(), $this->telegram->getBotName()), $first_channel ); } diff --git a/src/Commands/SystemCommands/InlinequeryCommand.php b/src/Commands/SystemCommands/InlinequeryCommand.php index cbd9c6110..5dc1f0107 100644 --- a/src/Commands/SystemCommands/InlinequeryCommand.php +++ b/src/Commands/SystemCommands/InlinequeryCommand.php @@ -11,8 +11,8 @@ namespace Longman\TelegramBot\Commands\SystemCommands; use Longman\TelegramBot\Commands\SystemCommand; -use Longman\TelegramBot\Entities\InlineQueryResultArticle; -use Longman\TelegramBot\Entities\InputTextMessageContent; +use Longman\TelegramBot\Entities\InlineQuery\InlineQueryResultArticle; +use Longman\TelegramBot\Entities\InputMessageContent\InputTextMessageContent; use Longman\TelegramBot\Request; /** diff --git a/src/Commands/UserCommands/CancelCommand.php b/src/Commands/UserCommands/CancelCommand.php index b6eb93d87..5c7d6313d 100644 --- a/src/Commands/UserCommands/CancelCommand.php +++ b/src/Commands/UserCommands/CancelCommand.php @@ -12,7 +12,7 @@ use Longman\TelegramBot\Commands\UserCommand; use Longman\TelegramBot\Conversation; -use Longman\TelegramBot\Entities\ReplyKeyboardHide; +use Longman\TelegramBot\Entities\Keyboard; use Longman\TelegramBot\Request; /** @@ -85,7 +85,7 @@ private function hideKeyboard($text) { return Request::sendMessage( [ - 'reply_markup' => new ReplyKeyboardHide(['selective' => true]), + 'reply_markup' => Keyboard::hide(['selective' => true]), 'chat_id' => $this->getMessage()->getChat()->getId(), 'text' => $text, ] diff --git a/src/Commands/UserCommands/SurveyCommand.php b/src/Commands/UserCommands/SurveyCommand.php index 7907d8fd7..05293cd7e 100644 --- a/src/Commands/UserCommands/SurveyCommand.php +++ b/src/Commands/UserCommands/SurveyCommand.php @@ -10,13 +10,12 @@ namespace Longman\TelegramBot\Commands\UserCommands; +use Longman\TelegramBot\Commands\UserCommand; +use Longman\TelegramBot\Conversation; +use Longman\TelegramBot\Entities\Keyboard; +use Longman\TelegramBot\Entities\KeyboardButton; use Longman\TelegramBot\Entities\PhotoSize; use Longman\TelegramBot\Request; -use Longman\TelegramBot\Conversation; -use Longman\TelegramBot\Commands\UserCommand; -use Longman\TelegramBot\Entities\ForceReply; -use Longman\TelegramBot\Entities\ReplyKeyboardHide; -use Longman\TelegramBot\Entities\ReplyKeyboardMarkup; /** * User "/survery" command @@ -68,7 +67,6 @@ public function execute() $chat = $message->getChat(); $user = $message->getFrom(); $text = trim($message->getText(true)); - $chat_id = $chat->getId(); $user_id = $user->getId(); @@ -80,12 +78,14 @@ public function execute() if ($chat->isGroupChat() || $chat->isSuperGroup()) { //reply to message id is applied by default //Force reply is applied by default so it can work with privacy on - $data['reply_markup'] = new ForceReply(['selective' => true]); + $data['reply_markup'] = Keyboard::forceReply(['selective' => true]); } //Conversation start $this->conversation = new Conversation($user_id, $chat_id, $this->getName()); - $notes = &$this->conversation->notes; + + $notes = &$this->conversation->notes; + !is_array($notes) && $notes = []; //cache data from the tracking session if any $state = 0; @@ -105,7 +105,7 @@ public function execute() $this->conversation->update(); $data['text'] = 'Type your name:'; - $data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]); + $data['reply_markup'] = Keyboard::hide(['selective' => true]); $result = Request::sendMessage($data); break; @@ -149,18 +149,14 @@ public function execute() // no break case 3: - if ($text === '' || !($text === 'M' || $text === 'F')) { + if ($text === '' || !in_array($text, ['M', 'F'], true)) { $notes['state'] = 3; $this->conversation->update(); - $data['reply_markup'] = new ReplyKeyboardMarkup( - [ - 'keyboard' => [['M', 'F']], - 'resize_keyboard' => true, - 'one_time_keyboard' => true, - 'selective' => true, - ] - ); + $data['reply_markup'] = (new Keyboard(['M', 'F'])) + ->setResizeKeyboard(true) + ->setOneTimeKeyboard(true) + ->setSelective(true); $data['text'] = 'Select your gender:'; if ($text !== '') { @@ -170,6 +166,7 @@ public function execute() $result = Request::sendMessage($data); break; } + $notes['gender'] = $text; // no break @@ -178,21 +175,12 @@ public function execute() $notes['state'] = 4; $this->conversation->update(); - $data['reply_markup'] = new ReplyKeyboardMarkup( - [ - 'keyboard' => [ - [ - [ - 'text' => 'Share Location', - 'request_location' => true, - ], - ], - ], - 'resize_keyboard' => true, - 'one_time_keyboard' => true, - 'selective' => true, - ] - ); + $data['reply_markup'] = (new Keyboard( + (new KeyboardButton('Share Location'))->setRequestLocation(true) + )) + ->setOneTimeKeyboard(true) + ->setResizeKeyboard(true) + ->setSelective(true); $data['text'] = 'Share your location:'; @@ -225,21 +213,12 @@ public function execute() $notes['state'] = 6; $this->conversation->update(); - $data['reply_markup'] = new ReplyKeyboardMarkup( - [ - 'keyboard' => [ - [ - [ - 'text' => 'Share Contact', - 'request_contact' => true, - ], - ], - ], - 'resize_keyboard' => true, - 'one_time_keyboard' => true, - 'selective' => true, - ] - ); + $data['reply_markup'] = (new Keyboard( + (new KeyboardButton('Share Contact'))->setRequestContact(true) + )) + ->setOneTimeKeyboard(true) + ->setResizeKeyboard(true) + ->setSelective(true); $data['text'] = 'Share your contact information:'; @@ -259,7 +238,7 @@ public function execute() } $data['photo'] = $notes['photo_id']; - $data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]); + $data['reply_markup'] = Keyboard::hide(['selective' => true]); $data['caption'] = $out_text; $this->conversation->stop(); diff --git a/src/Entities/Audio.php b/src/Entities/Audio.php index 75aea53a5..ac1f650ca 100644 --- a/src/Entities/Audio.php +++ b/src/Entities/Audio.php @@ -10,122 +10,19 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - +/** + * Class Audio + * + * @link https://core.telegram.org/bots/api#audio + * + * @method string getFileId() Unique identifier for this file + * @method int getDuration() Duration of the audio in seconds as defined by sender + * @method string getPerformer() Optional. Performer of the audio as defined by sender or by audio tags + * @method string getTitle() Optional. Title of the audio as defined by sender or by audio tags + * @method string getMimeType() Optional. MIME type of the file as defined by sender + * @method int getFileSize() Optional. File size + */ class Audio extends Entity { - /** - * @var mixed|null - */ - protected $file_id; - - /** - * @var mixed|null - */ - protected $duration; - - /** - * @var mixed|null - */ - protected $performer; - - /** - * @var mixed|null - */ - protected $title; - - /** - * @var mixed|null - */ - protected $mime_type; - - /** - * @var mixed|null - */ - protected $file_size; - - /** - * Audio constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - $this->file_id = isset($data['file_id']) ? $data['file_id'] : null; - if (empty($this->file_id)) { - throw new TelegramException('file_id is empty!'); - } - - $this->duration = isset($data['duration']) ? $data['duration'] : null; - if ($this->duration === '' || $this->duration === null) { - throw new TelegramException('duration is empty!'); - } - - $this->performer = isset($data['performer']) ? $data['performer'] : null; - - $this->title = isset($data['title']) ? $data['title'] : null; - $this->mime_type = isset($data['mime_type']) ? $data['mime_type'] : null; - $this->file_size = isset($data['file_size']) ? $data['file_size'] : null; - } - - /** - * Get file id - * - * @return mixed|null - */ - public function getFileId() - { - return $this->file_id; - } - - /** - * Get duration - * - * @return mixed|null - */ - public function getDuration() - { - return $this->duration; - } - - /** - * Get performer - * - * @return mixed|null - */ - public function getPerformer() - { - return $this->performer; - } - - /** - * Get title - * - * @return mixed|null - */ - public function getTitle() - { - return $this->title; - } - - /** - * Get mime type - * - * @return mixed|null - */ - public function getMimeType() - { - return $this->mime_type; - } - /** - * Get file size - * - * @return mixed|null - */ - public function getFileSize() - { - return $this->file_size; - } } diff --git a/src/Entities/CallbackQuery.php b/src/Entities/CallbackQuery.php index 1222215d2..e90815025 100644 --- a/src/Entities/CallbackQuery.php +++ b/src/Entities/CallbackQuery.php @@ -10,114 +10,27 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - +/** + * Class CallbackQuery. + * + * @link https://core.telegram.org/bots/api#callbackquery + * + * @method string getId() Unique identifier for this query + * @method User getFrom() Sender + * @method Message getMessage() Optional. Message with the callback button that originated the query. Note that message content and message date will not be available if the message is too old + * @method string getInlineMessageId() Optional. Identifier of the message sent via the bot in inline mode, that originated the query + * @method string getData() Data associated with the callback button. Be aware that a bad client can send arbitrary data in this field + */ class CallbackQuery extends Entity { /** - * @var mixed|null - */ - protected $id; - - /** - * @var \Longman\TelegramBot\Entities\User - */ - protected $from; - - /** - * @var \Longman\TelegramBot\Entities\Message - */ - protected $message; - - /** - * @var mixed|null - */ - protected $inline_message_id; - - /** - * @var mixed|null - */ - protected $data; - - /** - * CallbackQuery constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - $this->id = isset($data['id']) ? $data['id'] : null; - if (empty($this->id)) { - throw new TelegramException('id is empty!'); - } - - $this->from = isset($data['from']) ? $data['from'] : null; - if (empty($this->from)) { - throw new TelegramException('from is empty!'); - } - $this->from = new User($this->from); - - $this->message = isset($data['message']) ? $data['message'] : null; - if (!empty($this->message)) { - $this->message = new Message($this->message, $this->getBotName()); - } - - $this->inline_message_id = isset($data['inline_message_id']) ? $data['inline_message_id'] : null; - - $this->data = isset($data['data']) ? $data['data'] : null; - if (empty($this->data)) { - throw new TelegramException('data is empty!'); - } - } - - /** - * Get id - * - * @return mixed|null - */ - public function getId() - { - return $this->id; - } - - /** - * Get from - * - * @return \Longman\TelegramBot\Entities\User - */ - public function getFrom() - { - return $this->from; - } - - /** - * Get message - * - * @return \Longman\TelegramBot\Entities\Message - */ - public function getMessage() - { - return $this->message; - } - - /** - * Get inline message id - * - * @return mixed|null - */ - public function getInlineMessageId() - { - return $this->inline_message_id; - } - - /** - * Get data - * - * @return mixed|null + * {@inheritdoc} */ - public function getData() + public function subEntities() { - return $this->data; + return [ + 'from' => User::class, + 'message' => Message::class, + ]; } } diff --git a/src/Entities/Chat.php b/src/Entities/Chat.php index b69bb7a37..58ac876d3 100644 --- a/src/Entities/Chat.php +++ b/src/Entities/Chat.php @@ -10,198 +10,91 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - +/** + * Class Chat + * + * @link https://core.telegram.org/bots/api#chat + * + * @property int $id Unique identifier for this chat. This number may be greater than 32 bits and some programming languages may have difficulty/silent defects in interpreting it. But it smaller than 52 bits, so a signed 64 bit integer or double-precision float type are safe for storing this identifier. + * @property string $type Type of chat, can be either "private", "group", "supergroup" or "channel" + * @property string $title Optional. Title, for channels and group chats + * @property string $username Optional. Username, for private chats, supergroups and channels if available + * @property string $first_name Optional. First name of the other party in a private chat + * @property string $last_name Optional. Last name of the other party in a private chat + * @method int getId() Unique identifier for this chat. This number may be greater than 32 bits and some programming languages may have difficulty/silent defects in interpreting it. But it smaller than 52 bits, so a signed 64 bit integer or double-precision float type are safe for storing this identifier. + * @method string getType() Type of chat, can be either "private ", "group", "supergroup" or "channel" + * @method string getTitle() Optional. Title, for channels and group chats + * @method string getUsername() Optional. Username, for private chats, supergroups and channels if available + * @method string getFirstName() Optional. First name of the other party in a private chat + * @method string getLastName() Optional. Last name of the other party in a private chat + */ class Chat extends Entity { - /** - * @var mixed|null - */ - protected $id; - /** - * @var null - */ - protected $type; - - /** - * @var mixed|null - */ - protected $title; - - /** - * @var mixed|null - */ - protected $username; - - /** - * @var mixed|null - */ - protected $first_name; + public function __construct($data) + { + parent::__construct($data); - /** - * @var mixed|null - */ - protected $last_name; + $id = $this->getId(); + $type = $this->getType(); + if (!$type) { + $id > 0 && $this->type = 'private'; + $id < 0 && $this->type = 'group'; + } + } /** - * Chat constructor. + * Try to mention the user of this chat, else return the title * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException + * @param bool $escape_markdown + * + * @return string|null */ - public function __construct(array $data) + public function tryMention($escape_markdown = false) { - $this->id = isset($data['id']) ? $data['id'] : null; - if (empty($this->id)) { - throw new TelegramException('id is empty!'); - } - - if (isset($data['type'])) { - $this->type = $data['type']; - } else { - if ($this->id > 0) { - $this->type = 'private'; - } elseif ($this->id < 0) { - $this->type = 'group'; - } else { - $this->type = null; - } + if ($this->isPrivateChat()) { + return parent::tryMention($escape_markdown); } - $this->title = isset($data['title']) ? $data['title'] : null; - $this->first_name = isset($data['first_name']) ? $data['first_name'] : null; - $this->last_name = isset($data['last_name']) ? $data['last_name'] : null; - $this->username = isset($data['username']) ? $data['username'] : null; + return $this->getTitle(); } /** - * Check if is group chat + * Check if this is a group chat * * @return bool */ public function isGroupChat() { - if ($this->type == 'group' || $this->id < 0) { - return true; - } - return false; + return $this->getType() === 'group' || $this->getId() < 0; } /** - * Check if is private chat + * Check if this is a private chat * * @return bool */ public function isPrivateChat() { - if ($this->type == 'private') { - return true; - } - return false; + return $this->getType() === 'private'; } /** - * Check if is super group + * Check if this is a super group * * @return bool */ public function isSuperGroup() { - if ($this->type == 'supergroup') { - return true; - } - return false; + return $this->getType() === 'supergroup'; } /** - * Check if is channel + * Check if this is a channel * * @return bool */ public function isChannel() { - if ($this->type == 'channel') { - return true; - } - return false; - } - - /** - * Get id - * - * @return mixed|null - */ - public function getId() - { - return $this->id; - } - - /** - * Get type - * - * @return null - */ - public function getType() - { - return $this->type; - } - - /** - * Get title - * - * @return mixed|null - */ - public function getTitle() - { - return $this->title; - } - - /** - * Get first name - * - * @return mixed|null - */ - public function getFirstName() - { - return $this->first_name; - } - - /** - * Get last name - * - * @return mixed|null - */ - public function getLastName() - { - return $this->last_name; - } - - /** - * Get username - * - * @return mixed|null - */ - public function getUsername() - { - return $this->username; - } - - /** - * Try mention - * - * @return mixed|null|string - */ - public function tryMention() - { - if ($this->isPrivateChat()) { - if (is_null($this->username)) { - if (!is_null($this->last_name)) { - return $this->first_name . ' ' . $this->last_name; - } - return $this->first_name; - } - return '@' . $this->username; - } - return $this->getTitle(); + return $this->getType() === 'channel'; } } diff --git a/src/Entities/ChatMember.php b/src/Entities/ChatMember.php index 6211afdb2..26493334e 100644 --- a/src/Entities/ChatMember.php +++ b/src/Entities/ChatMember.php @@ -10,57 +10,23 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - +/** + * Class ChatMember + * + * @link https://core.telegram.org/bots/api#chatmember + * + * @method User getUser() Information about the user. + * @method string getStatus() The member's status in the chat. Can be "creator", "administrator", "member", "left" or "kicked" + */ class ChatMember extends Entity { /** - * @var \Longman\TelegramBot\Entities\User - */ - protected $user; - - /** - * @var mixed|null - */ - protected $status; - - /** - * ChatMember constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - $this->user = isset($data['user']) ? $data['user'] : null; - if (empty($this->user)) { - throw new TelegramException('user is empty!'); - } - $this->user = new User($data['user']); - - $this->status = isset($data['status']) ? $data['status'] : null; - if ($this->status === '') { - throw new TelegramException('status is empty!'); - } - } - - /** - * Get user - * - * @return \Longman\TelegramBot\Entities\User - */ - public function getUser() - { - return $this->user; - } - - /** - * Get status - * - * @return mixed|null + * {@inheritdoc} */ - public function getStatus() + public function subEntities() { - return $this->status; + return [ + 'user' => User::class, + ]; } } diff --git a/src/Entities/ChosenInlineResult.php b/src/Entities/ChosenInlineResult.php index 89756fe4b..6a1a199fd 100644 --- a/src/Entities/ChosenInlineResult.php +++ b/src/Entities/ChosenInlineResult.php @@ -10,110 +10,27 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - +/** + * Class ChosenInlineResult + * + * @link https://core.telegram.org/bots/api#choseninlineresult + * + * @method string getResultId() The unique identifier for the result that was chosen + * @method User getFrom() The user that chose the result + * @method Location getLocation() Optional. Sender location, only for bots that require user location + * @method string getInlineMessageId() Optional. Identifier of the sent inline message. Available only if there is an inline keyboard attached to the message. Will be also received in callback queries and can be used to edit the message. + * @method string getQuery() The query that was used to obtain the result + */ class ChosenInlineResult extends Entity { /** - * @var mixed|null - */ - protected $result_id; - - /** - * @var \Longman\TelegramBot\Entities\User - */ - protected $from; - - /** - * @var \Longman\TelegramBot\Entities\Location - */ - protected $location; - - /** - * @var mixed|null - */ - protected $inline_message_id; - - /** - * @var mixed|null - */ - protected $query; - - /** - * ChosenInlineResult constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - $this->result_id = isset($data['result_id']) ? $data['result_id'] : null; - if (empty($this->result_id)) { - throw new TelegramException('result_id is empty!'); - } - - $this->from = isset($data['from']) ? $data['from'] : null; - if (empty($this->from)) { - throw new TelegramException('from is empty!'); - } - $this->from = new User($this->from); - - $this->location = isset($data['location']) ? $data['location'] : null; - if (!empty($this->location)) { - $this->location = new Location($this->location); - } - - $this->inline_message_id = isset($data['inline_message_id']) ? $data['inline_message_id'] : null; - $this->query = isset($data['query']) ? $data['query'] : null; - } - - /** - * Ger result id - * - * @return mixed|null - */ - public function getResultId() - { - return $this->result_id; - } - - /** - * Get from - * - * @return \Longman\TelegramBot\Entities\User - */ - public function getFrom() - { - return $this->from; - } - - /** - * Get location - * - * @return \Longman\TelegramBot\Entities\Location - */ - public function getLocation() - { - return $this->location; - } - - /** - * Get inline message id - * - * @return mixed|null - */ - public function getInlineMessageId() - { - return $this->inline_message_id; - } - - /** - * Get query - * - * @return mixed|null + * {@inheritdoc} */ - public function getQuery() + protected function subEntities() { - return $this->query; + return [ + 'from' => User::class, + 'location' => Location::class, + ]; } } diff --git a/src/Entities/Contact.php b/src/Entities/Contact.php index ab74d598f..62d064421 100644 --- a/src/Entities/Contact.php +++ b/src/Entities/Contact.php @@ -10,89 +10,17 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - +/** + * Class Contact + * + * @link https://core.telegram.org/bots/api#contact + * + * @method string getPhoneNumber() Contact's phone number + * @method string getFirstName() Contact's first name + * @method string getLastName() Optional. Contact's last name + * @method int getUserId() Optional. Contact's user identifier in Telegram + */ class Contact extends Entity { - /** - * @var mixed|null - */ - protected $phone_number; - - /** - * @var mixed|null - */ - protected $first_name; - - /** - * @var mixed|null - */ - protected $last_name; - - /** - * @var mixed|null - */ - protected $user_id; - - /** - * Contact constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - $this->phone_number = isset($data['phone_number']) ? $data['phone_number'] : null; - if (empty($this->phone_number)) { - throw new TelegramException('phone_number is empty!'); - } - - $this->first_name = isset($data['first_name']) ? $data['first_name'] : null; - if (empty($this->first_name)) { - throw new TelegramException('first_name is empty!'); - } - - $this->last_name = isset($data['last_name']) ? $data['last_name'] : null; - $this->user_id = isset($data['user_id']) ? $data['user_id'] : null; - } - - /** - * Get phone number - * - * @return mixed|null - */ - public function getPhoneNumber() - { - return $this->phone_number; - } - - /** - * Get first name - * - * @return mixed|null - */ - public function getFirstName() - { - return $this->first_name; - } - - /** - * Get last name - * - * @return mixed|null - */ - public function getLastName() - { - return $this->last_name; - } - /** - * Get user id - * - * @return mixed|null - */ - public function getUserId() - { - return $this->user_id; - } } diff --git a/src/Entities/Document.php b/src/Entities/Document.php index c3c8bac4a..e532ee4b0 100644 --- a/src/Entities/Document.php +++ b/src/Entities/Document.php @@ -10,105 +10,26 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - +/** + * Class Document + * + * @link https://core.telegram.org/bots/api#document + * + * @method string getFileId() Unique file identifier + * @method PhotoSize getThumb() Optional. Document thumbnail as defined by sender + * @method string getFileName() Optional. Original filename as defined by sender + * @method string getMimeType() Optional. MIME type of the file as defined by sender + * @method int getFileSize() Optional. File size + */ class Document extends Entity { /** - * @var mixed|null - */ - protected $file_id; - - /** - * @var \Longman\TelegramBot\Entities\PhotoSize - */ - protected $thumb; - - /** - * @var mixed|null - */ - protected $file_name; - - /** - * @var mixed|null - */ - protected $mime_type; - - /** - * @var mixed|null - */ - protected $file_size; - - /** - * Document constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - $this->file_id = isset($data['file_id']) ? $data['file_id'] : null; - if (empty($this->file_id)) { - throw new TelegramException('file_id is empty!'); - } - - $this->thumb = isset($data['thumb']) ? $data['thumb'] : null; - if (!empty($this->thumb)) { - $this->thumb = new PhotoSize($this->thumb); - } - - $this->file_name = isset($data['file_name']) ? $data['file_name'] : null; - $this->mime_type = isset($data['mime_type']) ? $data['mime_type'] : null; - $this->file_size = isset($data['file_size']) ? $data['file_size'] : null; - } - - /** - * Get file id - * - * @return mixed|null - */ - public function getFileId() - { - return $this->file_id; - } - - /** - * Get thumb - * - * @return \Longman\TelegramBot\Entities\PhotoSize - */ - public function getThumb() - { - return $this->thumb; - } - - /** - * Get file name - * - * @return mixed|null - */ - public function getFileName() - { - return $this->file_name; - } - - /** - * Get mime type - * - * @return mixed|null - */ - public function getMimeType() - { - return $this->mime_type; - } - - /** - * Get file size - * - * @return mixed|null + * {@inheritdoc} */ - public function getFileSize() + protected function subEntities() { - return $this->file_size; + return [ + 'thumb' => PhotoSize::class, + ]; } } diff --git a/src/Entities/InputMessageContent.php b/src/Entities/EditedMessage.php similarity index 83% rename from src/Entities/InputMessageContent.php rename to src/Entities/EditedMessage.php index e4140a362..ba8c5af13 100644 --- a/src/Entities/InputMessageContent.php +++ b/src/Entities/EditedMessage.php @@ -10,7 +10,10 @@ namespace Longman\TelegramBot\Entities; -class InputMessageContent extends Entity +/** + * {@inheritdoc} + */ +class EditedMessage extends Message { } diff --git a/src/Entities/Entity.php b/src/Entities/Entity.php index 3f6c95191..0ba0bd528 100644 --- a/src/Entities/Entity.php +++ b/src/Entities/Entity.php @@ -10,120 +10,234 @@ namespace Longman\TelegramBot\Entities; +use Exception; +use Longman\TelegramBot\Entities\InlineQuery\InlineEntity; use ReflectionObject; -class Entity +/** + * Class Entity + * + * This is the base class for all entities. + * + * @link https://core.telegram.org/bots/api#available-types + * + * @method array getRawData() Get the raw data passed to this entity + */ +abstract class Entity { /** - * @var string + * Entity constructor. + * + * @todo Get rid of the $bot_name, it shouldn't be here! + * + * @param array $data + * @param string $bot_name + * + * @throws \Longman\TelegramBot\Exception\TelegramException */ - protected $bot_name; + public function __construct($data, $bot_name = '') + { + //Make sure we're not raw_data inception-ing + if (array_key_exists('raw_data', $data)) { + if ($data['raw_data'] === null) { + unset($data['raw_data']); + } + } else { + $data['raw_data'] = $data; + } + + $data['bot_name'] = $bot_name; + $this->assignMemberVariables($data); + $this->validate(); + } /** - * Get bot name + * Perform to json * * @return string */ - public function getBotName() + public function toJson() { - return $this->bot_name; + return json_encode($this->getRawData()); } /** - * Perform to json + * Perform to string * * @return string */ - public function toJson() + public function __toString() { - $fields = $this->reflect($this); - $json = json_encode($fields); + return $this->toJson(); + } - return $json; + /** + * Helper to set member variables + * + * @param array $data + */ + protected function assignMemberVariables(array $data) + { + foreach ($data as $key => $value) { + $this->$key = $value; + } } /** - * Reflect + * Get the list of the properties that are themselves Entities * - * @param null $object * @return array */ - public function reflect($object = null) + protected function subEntities() { - if ($object == null) { - $object = $this; + return []; + } + + /** + * Perform any special entity validation + * + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + protected function validate() + { + } + + /** + * Get a property from the current Entity + * + * @param mixed $property + * @param mixed $default + * + * @return mixed + */ + public function getProperty($property, $default = null) + { + if (isset($this->$property)) { + return $this->$property; } - $reflection = new ReflectionObject($object); - $properties = $reflection->getProperties(); + return $default; + } + + /** + * Return the variable for the called getter or magically set properties dynamically. + * + * @param $method + * @param $args + * + * @return mixed|null + */ + public function __call($method, $args) + { + //Convert method to snake_case (which is the name of the property) + $property_name = ltrim(strtolower(preg_replace('/[A-Z]/', '_$0', substr($method, 3))), '_'); + + $action = substr($method, 0, 3); + if ($action === 'get') { + $property = $this->getProperty($property_name); + + if ($property !== null) { + //Get all sub-Entities of the current Entity + $sub_entities = $this->subEntities(); - $fields = []; + if (isset($sub_entities[$property_name])) { + return new $sub_entities[$property_name]($property); + } - foreach ($properties as $property) { - $name = $property->getName(); - if ($name == 'bot_name') { - continue; + return $property; } + } elseif ($action === 'set') { + // Limit setters to specific classes. + if ($this instanceof InlineEntity || $this instanceof Keyboard || $this instanceof KeyboardButton) { + $this->$property_name = $args[0]; - if (!$property->isPrivate()) { - $array_of_obj = false; - $array_of_array_obj = false; - if (is_array($object->$name)) { - $array_of_obj = true; - $array_of_array_obj = true; - foreach ($object->$name as $elm) { - if (!is_object($elm)) { - //echo $name . " not array of object \n"; - $array_of_obj = false; - //break; - } - if (is_array($elm)) { - foreach ($elm as $more_net) { - if (!is_object($more_net)) { - $array_of_array_obj = false; - } - } - } - } - } + return $this; + } + } - if (is_object($object->$name)) { - $fields[$name] = $this->reflect($object->$name); - } elseif ($array_of_obj) { - foreach ($object->$name as $elm) { - $fields[$name][] = $this->reflect($elm); - } - } elseif ($array_of_array_obj) { - foreach ($object->$name as $elm) { - $temp = null; - if (!is_array($elm) && !is_object($elm)) { - continue; - } - foreach ($elm as $obj) { - $temp[] = $this->reflect($obj); - } - $fields[$name][] = $temp; - } - } else { - $property->setAccessible(true); - $value = $property->getValue($object); - if (is_null($value)) { - continue; + return null; + } + + /** + * Return an array of nice objects from an array of object arrays + * + * This method is used to generate pretty object arrays + * mainly for PhotoSize and Entities object arrays. + * + * @param string $class + * @param string $property + * + * @return array + */ + protected function makePrettyObjectArray($class, $property) + { + $new_objects = []; + + try { + if ($objects = $this->getProperty($property)) { + foreach ($objects as $object) { + if (!empty($object)) { + $new_objects[] = new $class($object); } - $fields[$name] = $value; } } + } catch (Exception $e) { + $new_objects = []; } - return $fields; + + return $new_objects; } /** - * Perform to string + * Escape markdown special characters + * + * @param string $string * * @return string */ - public function __toString() + public function escapeMarkdown($string) { - return $this->toJson(); + return str_replace( + ['[', '`', '*', '_',], + ['\[', '\`', '\*', '\_',], + $string + ); + } + + /** + * Try to mention the user + * + * Mention the user with the username otherwise print first and last name + * if the $escape_markdown argument is true special characters are escaped from the output + * + * @param bool $escape_markdown + * + * @return string|null + */ + public function tryMention($escape_markdown = false) + { + //TryMention only makes sense for the User and Chat entity. + if (!($this instanceof User || $this instanceof Chat)) { + return null; + } + + //Try with the username first... + $name = $this->getProperty('username'); + $is_username = $name !== null; + + if ($name === null) { + //...otherwise try with the names. + $name = $this->getProperty('first_name'); + $last_name = $this->getProperty('last_name'); + if ($last_name !== null) { + $name .= ' ' . $last_name; + } + } + + if ($escape_markdown) { + $name = $this->escapeMarkdown($name); + } + + return ($is_username ? '@' : '') . $name; } } diff --git a/src/Entities/File.php b/src/Entities/File.php index 24edb9fc7..0d4836806 100644 --- a/src/Entities/File.php +++ b/src/Entities/File.php @@ -10,70 +10,16 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - +/** + * Class File + * + * @link https://core.telegram.org/bots/api#file + * + * @method string getFileId() Unique identifier for this file + * @method int getFileSize() Optional. File size, if known + * @method string getFilePath() Optional. File path. Use https://api.telegram.org/file/bot/ to get the file. + */ class File extends Entity { - /** - * @var mixed|null - */ - protected $file_id; - - /** - * @var mixed|null - */ - protected $file_size; - - /** - * @var mixed|null - */ - protected $file_path; - - /** - * File constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - $this->file_id = isset($data['file_id']) ? $data['file_id'] : null; - if (empty($this->file_id)) { - throw new TelegramException('file_id is empty!'); - } - - $this->file_size = isset($data['file_size']) ? $data['file_size'] : null; - - $this->file_path = isset($data['file_path']) ? $data['file_path'] : null; - } - - /** - * Get file id - * - * @return mixed|null - */ - public function getFileId() - { - return $this->file_id; - } - - /** - * Get file size - * - * @return mixed|null - */ - public function getFileSize() - { - return $this->file_size; - } - /** - * Get file path - * - * @return mixed|null - */ - public function getFilePath() - { - return $this->file_path; - } } diff --git a/src/Entities/ForceReply.php b/src/Entities/ForceReply.php deleted file mode 100644 index 8ca0b2b23..000000000 --- a/src/Entities/ForceReply.php +++ /dev/null @@ -1,37 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - * - * Written by Marco Boretto - */ - -namespace Longman\TelegramBot\Entities; - -class ForceReply extends Entity -{ - /** - * @var bool - */ - protected $force_reply; - - /** - * @var bool|mixed - */ - protected $selective; - - /** - * ForceReply constructor. - * - * @param array|null $data - */ - public function __construct(array $data = null) - { - $this->force_reply = true; - $this->selective = isset($data['selective']) ? $data['selective'] : false; - } -} diff --git a/src/Entities/InlineKeyboard.php b/src/Entities/InlineKeyboard.php new file mode 100644 index 000000000..1981abb69 --- /dev/null +++ b/src/Entities/InlineKeyboard.php @@ -0,0 +1,20 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities; + +/** + * Class InlineKeyboard + * + * @link https://core.telegram.org/bots/api#inlinekeyboardmarkup + */ +class InlineKeyboard extends Keyboard +{ +} diff --git a/src/Entities/InlineKeyboardButton.php b/src/Entities/InlineKeyboardButton.php index 18a077745..b31a12f31 100644 --- a/src/Entities/InlineKeyboardButton.php +++ b/src/Entities/InlineKeyboardButton.php @@ -12,50 +12,72 @@ use Longman\TelegramBot\Exception\TelegramException; -class InlineKeyboardButton extends Entity +/** + * Class InlineKeyboardButton + * + * @link https://core.telegram.org/bots/api#inlinekeyboardbutton + * + * @method string getText() Label text on the button + * @method string getUrl() Optional. HTTP url to be opened when button is pressed + * @method string getCallbackData() Optional. Data to be sent in a callback query to the bot when button is pressed, 1-64 bytes + * @method string getSwitchInlineQuery() 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. + * + * @method $this setText(string $text) Label text on the button + * @method $this setUrl(string $url) Optional. HTTP url to be opened when button is pressed + * @method $this setCallbackData(string $callback_data) Optional. Data to be sent in a callback query to the bot when button is pressed, 1-64 bytes + * @method $this setSwitchInlineQuery(string $switch_inline_query) 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. + */ +class InlineKeyboardButton extends KeyboardButton { /** - * @var mixed|null - */ - protected $text; - - /** - * @var string - */ - protected $url; - - /** - * @var mixed - */ - protected $callback_data; - - /** - * @var mixed + * Check if the passed data array could be an InlineKeyboardButton. + * + * @param array $data + * + * @return bool */ - protected $switch_inline_query; + public static function couldBe($data) + { + return is_array($data) && + array_key_exists('text', $data) && ( + array_key_exists('url', $data) || + array_key_exists('callback_data', $data) || + array_key_exists('switch_inline_query', $data) + ); + } /** - * InlineKeyboardButton constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException + * {@inheritdoc} */ - public function __construct(array $data = []) + protected function validate() { - $this->text = isset($data['text']) ? $data['text'] : null; - if (empty($this->text)) { - throw new TelegramException('text is empty!'); + if ($this->getProperty('text', '') === '') { + throw new TelegramException('You must add some text to the button!'); } $num_params = 0; + foreach (['url', 'callback_data', 'switch_inline_query'] as $param) { - if (!empty($data[$param])) { - $this->$param = $data[$param]; + if (!empty($this->getProperty($param))) { $num_params++; } } + if ($num_params !== 1) { throw new TelegramException('You must use only one of these fields: url, callback_data, switch_inline_query!'); } } + + /** + * {@inheritdoc} + */ + public function __call($method, $args) + { + // Only 1 of these can be set, so clear the others when setting a new one. + if (in_array($method, ['setUrl', 'setCallbackData', 'setSwitchInlineQuery'], true)) { + unset($this->url, $this->callback_data, $this->switch_inline_query); + } + + return parent::__call($method, $args); + } } diff --git a/src/Entities/InlineKeyboardMarkup.php b/src/Entities/InlineKeyboardMarkup.php deleted file mode 100644 index 8f405b23c..000000000 --- a/src/Entities/InlineKeyboardMarkup.php +++ /dev/null @@ -1,45 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InlineKeyboardMarkup extends Entity -{ - /** - * @var array - */ - protected $inline_keyboard; - - /** - * InlineKeyboardMarkup constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct($data = array()) - { - if (isset($data['inline_keyboard'])) { - if (is_array($data['inline_keyboard'])) { - foreach ($data['inline_keyboard'] as $item) { - if (!is_array($item)) { - throw new TelegramException('Inline Keyboard subfield is not an array!'); - } - } - $this->inline_keyboard = $data['inline_keyboard']; - } else { - throw new TelegramException('Inline Keyboard field is not an array!'); - } - } else { - throw new TelegramException('Inline Keyboard field is empty!'); - } - } -} diff --git a/src/Entities/InlineQuery.php b/src/Entities/InlineQuery.php index 52e1a2899..6794140f1 100644 --- a/src/Entities/InlineQuery.php +++ b/src/Entities/InlineQuery.php @@ -10,110 +10,27 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - +/** + * Class InlineQuery + * + * @link https://core.telegram.org/bots/api#inlinequery + * + * @method string getId() Unique identifier for this query + * @method User getFrom() Sender + * @method Location getLocation() Optional. Sender location, only for bots that request user location + * @method string getQuery() Text of the query (up to 512 characters) + * @method string getOffset() Offset of the results to be returned, can be controlled by the bot + */ class InlineQuery extends Entity { /** - * @var mixed|null - */ - protected $id; - - /** - * @var \Longman\TelegramBot\Entities\User - */ - protected $from; - - /** - * @var \Longman\TelegramBot\Entities\Location - */ - protected $location; - - /** - * @var mixed|null - */ - protected $query; - - /** - * @var mixed|null - */ - protected $offset; - - /** - * InlineQuery constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - $this->id = isset($data['id']) ? $data['id'] : null; - if (empty($this->id)) { - throw new TelegramException('id is empty!'); - } - - $this->from = isset($data['from']) ? $data['from'] : null; - if (empty($this->from)) { - throw new TelegramException('from is empty!'); - } - $this->from = new User($this->from); - - $this->location = isset($data['location']) ? $data['location'] : null; - if (!empty($this->location)) { - $this->location = new Location($this->location); - } - - $this->query = isset($data['query']) ? $data['query'] : null; - $this->offset = isset($data['offset']) ? $data['offset'] : null; - } - - /** - * Get id - * - * @return mixed|null - */ - public function getId() - { - return $this->id; - } - - /** - * Get from - * - * @return \Longman\TelegramBot\Entities\User - */ - public function getFrom() - { - return $this->from; - } - - /** - * Get location - * - * @return \Longman\TelegramBot\Entities\Location - */ - public function getLocation() - { - return $this->location; - } - - /** - * Get query - * - * @return mixed|null - */ - public function getQuery() - { - return $this->query; - } - - /** - * Get offset - * - * @return mixed|null + * {@inheritdoc} */ - public function getOffset() + protected function subEntities() { - return $this->offset; + return [ + 'from' => User::class, + 'location' => Location::class, + ]; } } diff --git a/src/Entities/InlineQuery/InlineEntity.php b/src/Entities/InlineQuery/InlineEntity.php new file mode 100644 index 000000000..57f32db8d --- /dev/null +++ b/src/Entities/InlineQuery/InlineEntity.php @@ -0,0 +1,23 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InlineQuery; + +use Longman\TelegramBot\Entities\Entity; + +/** + * Class InlineEntity + * + * This is the base class for all inline entities. + */ +abstract class InlineEntity extends Entity +{ + +} diff --git a/src/Entities/InlineQuery/InlineQueryResultArticle.php b/src/Entities/InlineQuery/InlineQueryResultArticle.php new file mode 100644 index 000000000..f4b74a156 --- /dev/null +++ b/src/Entities/InlineQuery/InlineQueryResultArticle.php @@ -0,0 +1,73 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InlineQuery; + +use Longman\TelegramBot\Entities\InlineKeyboard; +use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; + +/** + * Class InlineQueryResultArticle + * + * @link https://core.telegram.org/bots/api#inlinequeryresultarticle + * + * + * $data = [ + * 'id' => '', + * 'title' => '', + * 'input_message_content' => , + * 'reply_markup' => , + * 'url' => '', + * 'hide_url' => true, + * 'description' => '', + * 'thumb_url' => '', + * 'thumb_width' => 30, + * 'thumb_height' => 30, + * ]; + * + * + * @method string getType() Type of the result, must be article + * @method string getId() Unique identifier for this result, 1-64 Bytes + * @method string getTitle() Title of the result + * @method InputMessageContent getInputMessageContent() Content of the message to be sent + * @method InlineKeyboard getReplyMarkup() Optional. Inline keyboard attached to the message + * @method string getUrl() Optional. URL of the result + * @method bool getHideUrl() Optional. Pass True, if you don't want the URL to be shown in the message + * @method string getDescription() Optional. Short description of the result + * @method string getThumbUrl() Optional. Url of the thumbnail for the result + * @method int getThumbWidth() Optional. Thumbnail width + * @method int getThumbHeight() Optional. Thumbnail height + * + * @method $this setId(string $id) Unique identifier for this result, 1-64 Bytes + * @method $this setTitle(string $title) Title of the result + * @method $this setInputMessageContent(InputMessageContent $input_message_content) Content of the message to be sent + * @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. Inline keyboard attached to the message + * @method $this setUrl(string $url) Optional. URL of the result + * @method $this setHideUrl(bool $hide_url) Optional. Pass True, if you don't want the URL to be shown in the message + * @method $this setDescription(string $description) Optional. Short description of the result + * @method $this setThumbUrl(string $thumb_url) Optional. Url of the thumbnail for the result + * @method $this setThumbWidth(int $thumb_width) Optional. Thumbnail width + * @method $this setThumbHeight(int $thumb_height) Optional. Thumbnail height + */ +class InlineQueryResultArticle extends InlineEntity +{ + /** + * InlineQueryResultArticle constructor + * + * @param array $data + * + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public function __construct(array $data = []) + { + $data['type'] = 'article'; + parent::__construct($data); + } +} diff --git a/src/Entities/InlineQuery/InlineQueryResultAudio.php b/src/Entities/InlineQuery/InlineQueryResultAudio.php new file mode 100644 index 000000000..891bd679f --- /dev/null +++ b/src/Entities/InlineQuery/InlineQueryResultAudio.php @@ -0,0 +1,64 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InlineQuery; + +use Longman\TelegramBot\Entities\InlineKeyboard; +use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; + +/** + * Class InlineQueryResultAudio + * + * @link https://core.telegram.org/bots/api#inlinequeryresultaudio + * + * + * $data = [ + * 'id' => '', + * 'audio_url' => '', + * 'title' => '', + * 'performer' => '', + * 'audio_duration' => 123, + * 'reply_markup' => , + * 'input_message_content' => , + * ]; + * + * + * @method string getType() Type of the result, must be audio + * @method string getId() Unique identifier for this result, 1-64 bytes + * @method string getAudioUrl() A valid URL for the audio file + * @method string getTitle() Title + * @method string getPerformer() Optional. Performer + * @method int getAudioDuration() Optional. Audio duration in seconds + * @method InlineKeyboard getReplyMarkup() Optional. Inline keyboard attached to the message + * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the audio + * + * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes + * @method $this setAudioUrl(string $audio_url) A valid URL for the audio file + * @method $this setTitle(string $title) Title + * @method $this setPerformer(string $performer) Optional. Performer + * @method $this setAudioDuration(int $audio_duration) Optional. Audio duration in seconds + * @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. Inline keyboard attached to the message + * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the audio + */ +class InlineQueryResultAudio extends InlineEntity +{ + /** + * InlineQueryResultAudio constructor + * + * @param array $data + * + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public function __construct(array $data = []) + { + $data['type'] = 'audio'; + parent::__construct($data); + } +} diff --git a/src/Entities/InlineQuery/InlineQueryResultCachedAudio.php b/src/Entities/InlineQuery/InlineQueryResultCachedAudio.php new file mode 100644 index 000000000..ed64020b5 --- /dev/null +++ b/src/Entities/InlineQuery/InlineQueryResultCachedAudio.php @@ -0,0 +1,55 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InlineQuery; + +use Longman\TelegramBot\Entities\InlineKeyboard; +use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; + +/** + * Class InlineQueryResultCachedAudio + * + * @link https://core.telegram.org/bots/api#inlinequeryresultcachedaudio + * + * + * $data = [ + * 'id' => '', + * 'audio_file_id' => '', + * 'reply_markup' => , + * 'input_message_content' => , + * ]; + * + * + * @method string getType() Type of the result, must be audio + * @method string getId() Unique identifier for this result, 1-64 bytes + * @method string getAudioFileId() A valid file identifier for the audio file + * @method InlineKeyboard getReplyMarkup() Optional. An Inline keyboard attached to the message + * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the audio + * + * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes + * @method $this setAudioFileId(string $audio_file_id) A valid file identifier for the audio file + * @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. An Inline keyboard attached to the message + * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the audio + */ +class InlineQueryResultCachedAudio extends InlineEntity +{ + /** + * InlineQueryResultCachedAudio constructor + * + * @param array $data + * + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public function __construct(array $data = []) + { + $data['type'] = 'audio'; + parent::__construct($data); + } +} diff --git a/src/Entities/InlineQuery/InlineQueryResultCachedDocument.php b/src/Entities/InlineQuery/InlineQueryResultCachedDocument.php new file mode 100644 index 000000000..89fbf9c42 --- /dev/null +++ b/src/Entities/InlineQuery/InlineQueryResultCachedDocument.php @@ -0,0 +1,64 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InlineQuery; + +use Longman\TelegramBot\Entities\InlineKeyboard; +use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; + +/** + * Class InlineQueryResultCachedDocument + * + * @link https://core.telegram.org/bots/api#inlinequeryresultcacheddocument + * + * + * $data = [ + * 'id' => '', + * 'title' => '', + * 'document_file_id' => '', + * 'description' => '', + * 'caption' => '', + * 'reply_markup' => , + * 'input_message_content' => , + * ]; + * + * + * @method string getType() Type of the result, must be document + * @method string getId() Unique identifier for this result, 1-64 bytes + * @method string getTitle() Title for the result + * @method string getDocumentFileId() A valid file identifier for the file + * @method string getDescription() Optional. Short description of the result + * @method string getCaption() Optional. Caption of the document to be sent, 0-200 characters + * @method InlineKeyboard getReplyMarkup() Optional. An Inline keyboard attached to the message + * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the file + * + * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes + * @method $this setTitle(string $title) Title for the result + * @method $this setDocumentFileId(string $document_file_id) A valid file identifier for the file + * @method $this setDescription(string $description) Optional. Short description of the result + * @method $this setCaption(string $caption) Optional. Caption of the document to be sent, 0-200 characters + * @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. An Inline keyboard attached to the message + * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the file + */ +class InlineQueryResultCachedDocument extends InlineEntity +{ + /** + * InlineQueryResultCachedDocument constructor + * + * @param array $data + * + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public function __construct(array $data = []) + { + $data['type'] = 'document'; + parent::__construct($data); + } +} diff --git a/src/Entities/InlineQuery/InlineQueryResultCachedGif.php b/src/Entities/InlineQuery/InlineQueryResultCachedGif.php new file mode 100644 index 000000000..9c4192faf --- /dev/null +++ b/src/Entities/InlineQuery/InlineQueryResultCachedGif.php @@ -0,0 +1,61 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InlineQuery; + +use Longman\TelegramBot\Entities\InlineKeyboard; +use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; + +/** + * Class InlineQueryResultCachedGif + * + * @link https://core.telegram.org/bots/api#inlinequeryresultcachedgif + * + * + * $data = [ + * 'id' => '', + * 'gif_file_id' => '', + * 'title' => '', + * 'caption' => '', + * 'reply_markup' => , + * 'input_message_content' => , + * ]; + * + * + * @method string getType() Type of the result, must be gif + * @method string getId() Unique identifier for this result, 1-64 bytes + * @method string getGifFileId() A valid file identifier for the GIF file + * @method string getTitle() Optional. Title for the result + * @method string getCaption() Optional. Caption of the GIF file to be sent, 0-200 characters + * @method InlineKeyboard getReplyMarkup() Optional. An Inline keyboard attached to the message + * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the GIF animation + * + * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes + * @method $this setGifFileId(string $gif_file_id) A valid file identifier for the GIF file + * @method $this setTitle(string $title) Optional. Title for the result + * @method $this setCaption(string $caption) Optional. Caption of the GIF file to be sent, 0-200 characters + * @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. An Inline keyboard attached to the message + * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the GIF animation + */ +class InlineQueryResultCachedGif extends InlineEntity +{ + /** + * InlineQueryResultCachedGif constructor + * + * @param array $data + * + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public function __construct(array $data = []) + { + $data['type'] = 'gif'; + parent::__construct($data); + } +} diff --git a/src/Entities/InlineQuery/InlineQueryResultCachedMpeg4Gif.php b/src/Entities/InlineQuery/InlineQueryResultCachedMpeg4Gif.php new file mode 100644 index 000000000..8d52efdc0 --- /dev/null +++ b/src/Entities/InlineQuery/InlineQueryResultCachedMpeg4Gif.php @@ -0,0 +1,61 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InlineQuery; + +use Longman\TelegramBot\Entities\InlineKeyboard; +use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; + +/** + * Class InlineQueryResultCachedMpeg4Gif + * + * @link https://core.telegram.org/bots/api#inlinequeryresultcachedmpeg4gif + * + * + * $data = [ + * 'id' => '', + * 'mpeg4_file_id' => '', + * 'title' => '', + * 'caption' => '', + * 'reply_markup' => , + * 'input_message_content' => , + * ]; + * + * + * @method string getType() Type of the result, must be mpeg4_gif + * @method string getId() Unique identifier for this result, 1-64 bytes + * @method string getMpeg4FileId() A valid file identifier for the MP4 file + * @method string getTitle() Optional. Title for the result + * @method string getCaption() Optional. Caption of the MPEG-4 file to be sent, 0-200 characters + * @method InlineKeyboard getReplyMarkup() Optional. An Inline keyboard attached to the message + * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the video animation + * + * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes + * @method $this setMpeg4FileId(string $mpeg4_file_id) A valid file identifier for the MP4 file + * @method $this setTitle(string $title) Optional. Title for the result + * @method $this setCaption(string $caption) Optional. Caption of the MPEG-4 file to be sent, 0-200 characters + * @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. An Inline keyboard attached to the message + * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the video animation + */ +class InlineQueryResultCachedMpeg4Gif extends InlineEntity +{ + /** + * InlineQueryResultCachedMpeg4Gif constructor + * + * @param array $data + * + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public function __construct(array $data = []) + { + $data['type'] = 'mpeg4_gif'; + parent::__construct($data); + } +} diff --git a/src/Entities/InlineQuery/InlineQueryResultCachedPhoto.php b/src/Entities/InlineQuery/InlineQueryResultCachedPhoto.php new file mode 100644 index 000000000..31f614901 --- /dev/null +++ b/src/Entities/InlineQuery/InlineQueryResultCachedPhoto.php @@ -0,0 +1,64 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InlineQuery; + +use Longman\TelegramBot\Entities\InlineKeyboard; +use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; + +/** + * Class InlineQueryResultCachedPhoto + * + * @link https://core.telegram.org/bots/api#inlinequeryresultcachedphoto + * + * + * $data = [ + * 'id' => '', + * 'photo_file_id' => '', + * 'title' => '', + * 'description' => '', + * 'caption' => '', + * 'reply_markup' => , + * 'input_message_content' => , + * ]; + * + * + * @method string getType() Type of the result, must be photo + * @method string getId() Unique identifier for this result, 1-64 bytes + * @method string getPhotoFileId() A valid file identifier of the photo + * @method string getTitle() Optional. Title for the result + * @method string getDescription() Optional. Short description of the result + * @method string getCaption() Optional. Caption of the photo to be sent, 0-200 characters + * @method InlineKeyboard getReplyMarkup() Optional. Inline keyboard attached to the message + * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the photo + * + * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes + * @method $this setPhotoFileId(string $photo_file_id) A valid file identifier of the photo + * @method $this setTitle(string $title) Optional. Title for the result + * @method $this setDescription(string $description) Optional. Short description of the result + * @method $this setCaption(string $caption) Optional. Caption of the photo to be sent, 0-200 characters + * @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. Inline keyboard attached to the message + * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the photo + */ +class InlineQueryResultCachedPhoto extends InlineEntity +{ + /** + * InlineQueryResultCachedPhoto constructor + * + * @param array $data + * + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public function __construct(array $data = []) + { + $data['type'] = 'photo'; + parent::__construct($data); + } +} diff --git a/src/Entities/InlineQuery/InlineQueryResultCachedSticker.php b/src/Entities/InlineQuery/InlineQueryResultCachedSticker.php new file mode 100644 index 000000000..1f7c1d4fe --- /dev/null +++ b/src/Entities/InlineQuery/InlineQueryResultCachedSticker.php @@ -0,0 +1,55 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InlineQuery; + +use Longman\TelegramBot\Entities\InlineKeyboard; +use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; + +/** + * Class InlineQueryResultCachedSticker + * + * @link https://core.telegram.org/bots/api#inlinequeryresultcachedsticker + * + * + * $data = [ + * 'id' => '', + * 'sticker_file_id' => '', + * 'reply_markup' => , + * 'input_message_content' => , + * ]; + * + * + * @method string getType() Type of the result, must be sticker + * @method string getId() Unique identifier for this result, 1-64 bytes + * @method string getStickerFileId() A valid file identifier of the sticker + * @method InlineKeyboard getReplyMarkup() Optional. An Inline keyboard attached to the message + * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the sticker + * + * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes + * @method $this setStickerFileId(string $sticker_file_id) A valid file identifier of the sticker + * @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. An Inline keyboard attached to the message + * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the sticker + */ +class InlineQueryResultCachedSticker extends InlineEntity +{ + /** + * InlineQueryResultCachedSticker constructor + * + * @param array $data + * + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public function __construct(array $data = []) + { + $data['type'] = 'sticker'; + parent::__construct($data); + } +} diff --git a/src/Entities/InlineQuery/InlineQueryResultCachedVideo.php b/src/Entities/InlineQuery/InlineQueryResultCachedVideo.php new file mode 100644 index 000000000..3173fe483 --- /dev/null +++ b/src/Entities/InlineQuery/InlineQueryResultCachedVideo.php @@ -0,0 +1,64 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InlineQuery; + +use Longman\TelegramBot\Entities\InlineKeyboard; +use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; + +/** + * Class InlineQueryResultCachedVideo + * + * @link https://core.telegram.org/bots/api#inlinequeryresultcachedvideo + * + * + * $data = [ + * 'id' => '', + * 'video_file_id' => '', + * 'title' => '', + * 'description' => '', + * 'caption' => '', + * 'reply_markup' => , + * 'input_message_content' => , + * ]; + * + * + * @method string getType() Type of the result, must be video + * @method string getId() Unique identifier for this result, 1-64 bytes + * @method string getVideoFileId() A valid file identifier for the video file + * @method string getTitle() Title for the result + * @method string getDescription() Optional. Short description of the result + * @method string getCaption() Optional. Caption of the video to be sent, 0-200 characters + * @method InlineKeyboard getReplyMarkup() Optional. An Inline keyboard attached to the message + * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the video + * + * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes + * @method $this setVideoFileId(string $video_file_id) A valid file identifier for the video file + * @method $this setTitle(string $title) Title for the result + * @method $this setDescription(string $description) Optional. Short description of the result + * @method $this setCaption(string $caption) Optional. Caption of the video to be sent, 0-200 characters + * @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. An Inline keyboard attached to the message + * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the video + */ +class InlineQueryResultCachedVideo extends InlineEntity +{ + /** + * InlineQueryResultCachedVideo constructor + * + * @param array $data + * + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public function __construct(array $data = []) + { + $data['type'] = 'video'; + parent::__construct($data); + } +} diff --git a/src/Entities/InlineQuery/InlineQueryResultCachedVoice.php b/src/Entities/InlineQuery/InlineQueryResultCachedVoice.php new file mode 100644 index 000000000..8e03507df --- /dev/null +++ b/src/Entities/InlineQuery/InlineQueryResultCachedVoice.php @@ -0,0 +1,58 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InlineQuery; + +use Longman\TelegramBot\Entities\InlineKeyboard; +use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; + +/** + * Class InlineQueryResultCachedVoice + * + * @link https://core.telegram.org/bots/api#inlinequeryresultcachedvoice + * + * + * $data = [ + * 'id' => '', + * 'voice_file_id' => '', + * 'title' => '', + * 'reply_markup' => , + * 'input_message_content' => , + * ]; + * + * + * @method string getType() Type of the result, must be voice + * @method string getId() Unique identifier for this result, 1-64 bytes + * @method string getVoiceFileId() A valid file identifier for the voice message + * @method string getTitle() Voice message title + * @method InlineKeyboard getReplyMarkup() Optional. An Inline keyboard attached to the message + * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the voice message + * + * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes + * @method $this setVoiceFileId(string $voice_file_id) A valid file identifier for the voice message + * @method $this setTitle(string $title) Voice message title + * @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. An Inline keyboard attached to the message + * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the voice message + */ +class InlineQueryResultCachedVoice extends InlineEntity +{ + /** + * InlineQueryResultCachedVoice constructor + * + * @param array $data + * + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public function __construct(array $data = []) + { + $data['type'] = 'voice'; + parent::__construct($data); + } +} diff --git a/src/Entities/InlineQuery/InlineQueryResultContact.php b/src/Entities/InlineQuery/InlineQueryResultContact.php new file mode 100644 index 000000000..18d7e5a1c --- /dev/null +++ b/src/Entities/InlineQuery/InlineQueryResultContact.php @@ -0,0 +1,70 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InlineQuery; + +use Longman\TelegramBot\Entities\InlineKeyboard; +use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; + +/** + * Class InlineQueryResultContact + * + * @link https://core.telegram.org/bots/api#inlinequeryresultcontact + * + * + * $data = [ + * 'id' => '', + * 'phone_number' => '', + * 'first_name' => '', + * 'last_name' => '', + * 'reply_markup' => , + * 'input_message_content' => , + * 'thumb_url' => '', + * 'thumb_width' => 30, + * 'thumb_height' => 30, + * ]; + * + * + * @method string getType() Type of the result, must be contact + * @method string getId() Unique identifier for this result, 1-64 Bytes + * @method string getPhoneNumber() Contact's phone number + * @method string getFirstName() Contact's first name + * @method string getLastName() Optional. Contact's last name + * @method InlineKeyboard getReplyMarkup() Optional. Inline keyboard attached to the message + * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the contact + * @method string getThumbUrl() Optional. Url of the thumbnail for the result + * @method int getThumbWidth() Optional. Thumbnail width + * @method int getThumbHeight() Optional. Thumbnail height + * + * @method $this setId(string $id) Unique identifier for this result, 1-64 Bytes + * @method $this setPhoneNumber(string $phone_number) Contact's phone number + * @method $this setFirstName(string $first_name) Contact's first name + * @method $this setLastName(string $last_name) Optional. Contact's last name + * @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. Inline keyboard attached to the message + * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the contact + * @method $this setThumbUrl(string $thumb_url) Optional. Url of the thumbnail for the result + * @method $this setThumbWidth(int $thumb_width) Optional. Thumbnail width + * @method $this setThumbHeight(int $thumb_height) Optional. Thumbnail height + */ +class InlineQueryResultContact extends InlineEntity +{ + /** + * InlineQueryResultContact constructor + * + * @param array $data + * + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public function __construct(array $data = []) + { + $data['type'] = 'contact'; + parent::__construct($data); + } +} diff --git a/src/Entities/InlineQuery/InlineQueryResultDocument.php b/src/Entities/InlineQuery/InlineQueryResultDocument.php new file mode 100644 index 000000000..7fa084ca8 --- /dev/null +++ b/src/Entities/InlineQuery/InlineQueryResultDocument.php @@ -0,0 +1,76 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InlineQuery; + +use Longman\TelegramBot\Entities\InlineKeyboard; +use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; + +/** + * Class InlineQueryResultDocument + * + * @link https://core.telegram.org/bots/api#inlinequeryresultdocument + * + * + * $data = [ + * 'id' => '', + * 'title' => '', + * 'caption' => '', + * 'document_url' => '', + * 'mime_type' => '', + * 'description' => '', + * 'reply_markup' => , + * 'input_message_content' => , + * 'thumb_url' => '', + * 'thumb_width' => 30, + * 'thumb_height' => 30, + * ]; + * + * + * @method string getType() Type of the result, must be document + * @method string getId() Unique identifier for this result, 1-64 bytes + * @method string getTitle() Title for the result + * @method string getCaption() Optional. Caption of the document to be sent, 0-200 characters + * @method string getDocumentUrl() A valid URL for the file + * @method string getMimeType() Mime type of the content of the file, either “application/pdf” or “application/zip” + * @method string getDescription() Optional. Short description of the result + * @method InlineKeyboard getReplyMarkup() Optional. Inline keyboard attached to the message + * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the file + * @method string getThumbUrl() Optional. URL of the thumbnail (jpeg only) for the file + * @method int getThumbWidth() Optional. Thumbnail width + * @method int getThumbHeight() Optional. Thumbnail height + * + * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes + * @method $this setTitle(string $title) Title for the result + * @method $this setCaption(string $caption) Optional. Caption of the document to be sent, 0-200 characters + * @method $this setDocumentUrl(string $document_url) A valid URL for the file + * @method $this setMimeType(string $mime_type) Mime type of the content of the file, either “application/pdf” or “application/zip” + * @method $this setDescription(string $description) Optional. Short description of the result + * @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. Inline keyboard attached to the message + * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the file + * @method $this setThumbUrl(string $thumb_url) Optional. URL of the thumbnail (jpeg only) for the file + * @method $this setThumbWidth(int $thumb_width) Optional. Thumbnail width + * @method $this setThumbHeight(int $thumb_height) Optional. Thumbnail height + */ +class InlineQueryResultDocument extends InlineEntity +{ + /** + * InlineQueryResultDocument constructor + * + * @param array $data + * + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public function __construct(array $data = []) + { + $data['type'] = 'document'; + parent::__construct($data); + } +} diff --git a/src/Entities/InlineQuery/InlineQueryResultGif.php b/src/Entities/InlineQuery/InlineQueryResultGif.php new file mode 100644 index 000000000..e7cc2ed4f --- /dev/null +++ b/src/Entities/InlineQuery/InlineQueryResultGif.php @@ -0,0 +1,70 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InlineQuery; + +use Longman\TelegramBot\Entities\InlineKeyboard; +use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; + +/** + * Class InlineQueryResultGif + * + * @link https://core.telegram.org/bots/api#inlinequeryresultgif + * + * + * $data = [ + * 'id' => '', + * 'gif_url' => '', + * 'gif_width' => 30, + * 'gif_height' => 30, + * 'thumb_url' => '', + * 'title' => '', + * 'caption' => '', + * 'reply_markup' => , + * 'input_message_content' => , + * ]; + * + * + * @method string getType() Type of the result, must be gif + * @method string getId() Unique identifier for this result, 1-64 bytes + * @method string getGifUrl() A valid URL for the GIF file. File size must not exceed 1MB + * @method int getGifWidth() Optional. Width of the GIF + * @method int getGifHeight() Optional. Height of the GIF + * @method string getThumbUrl() URL of the static thumbnail for the result (jpeg or gif) + * @method string getTitle() Optional. Title for the result + * @method string getCaption() Optional. Caption of the GIF file to be sent, 0-200 characters + * @method InlineKeyboard getReplyMarkup() Optional. Inline keyboard attached to the message + * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the GIF animation + * + * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes + * @method $this setGifUrl(string $gif_url) A valid URL for the GIF file. File size must not exceed 1MB + * @method $this setGifWidth(int $gif_width) Optional. Width of the GIF + * @method $this setGifHeight(int $gif_height) Optional. Height of the GIF + * @method $this setThumbUrl(string $thumb_url) URL of the static thumbnail for the result (jpeg or gif) + * @method $this setTitle(string $title) Optional. Title for the result + * @method $this setCaption(string $caption) Optional. Caption of the GIF file to be sent, 0-200 characters + * @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. Inline keyboard attached to the message + * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the GIF animation + */ +class InlineQueryResultGif extends InlineEntity +{ + /** + * InlineQueryResultGif constructor + * + * @param array $data + * + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public function __construct(array $data = []) + { + $data['type'] = 'gif'; + parent::__construct($data); + } +} diff --git a/src/Entities/InlineQuery/InlineQueryResultLocation.php b/src/Entities/InlineQuery/InlineQueryResultLocation.php new file mode 100644 index 000000000..89a036ab7 --- /dev/null +++ b/src/Entities/InlineQuery/InlineQueryResultLocation.php @@ -0,0 +1,70 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InlineQuery; + +use Longman\TelegramBot\Entities\InlineKeyboard; +use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; + +/** + * Class InlineQueryResultLocation + * + * @link https://core.telegram.org/bots/api#inlinequeryresultlocation + * + * + * $data = [ + * 'id' => '', + * 'latitude' => 36.0338, + * 'longitude' => 71.8601, + * 'title' => '', + * 'reply_markup' => , + * 'input_message_content' => , + * 'thumb_url' => '', + * 'thumb_width' => 30, + * 'thumb_height' => 30, + * ]; + * + * + * @method string getType() Type of the result, must be location + * @method string getId() Unique identifier for this result, 1-64 Bytes + * @method float getLatitude() Location latitude in degrees + * @method float getLongitude() Location longitude in degrees + * @method string getTitle() Location title + * @method InlineKeyboard getReplyMarkup() Optional. Inline keyboard attached to the message + * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the location + * @method string getThumbUrl() Optional. Url of the thumbnail for the result + * @method int getThumbWidth() Optional. Thumbnail width + * @method int getThumbHeight() Optional. Thumbnail height + * + * @method $this setId(string $id) Unique identifier for this result, 1-64 Bytes + * @method $this setLatitude(float $latitude) Location latitude in degrees + * @method $this setLongitude(float $longitude) Location longitude in degrees + * @method $this setTitle(string $title) Location title + * @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. Inline keyboard attached to the message + * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the location + * @method $this setThumbUrl(string $thumb_url) Optional. Url of the thumbnail for the result + * @method $this setThumbWidth(int $thumb_width) Optional. Thumbnail width + * @method $this setThumbHeight(int $thumb_height) Optional. Thumbnail height + */ +class InlineQueryResultLocation extends InlineEntity +{ + /** + * InlineQueryResultLocation constructor + * + * @param array $data + * + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public function __construct(array $data = []) + { + $data['type'] = 'location'; + parent::__construct($data); + } +} diff --git a/src/Entities/InlineQuery/InlineQueryResultMpeg4Gif.php b/src/Entities/InlineQuery/InlineQueryResultMpeg4Gif.php new file mode 100644 index 000000000..9eae124e0 --- /dev/null +++ b/src/Entities/InlineQuery/InlineQueryResultMpeg4Gif.php @@ -0,0 +1,70 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InlineQuery; + +use Longman\TelegramBot\Entities\InlineKeyboard; +use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; + +/** + * Class InlineQueryResultMpeg4Gif + * + * @link https://core.telegram.org/bots/api#inlinequeryresultmpeg4gif + * + * + * $data = [ + * 'id' => '', + * 'mpeg4_url' => '', + * 'mpeg4_width' => 30, + * 'mpeg4_height' => 30, + * 'thumb_url' => '', + * 'title' => '', + * 'caption' => '', + * 'reply_markup' => , + * 'input_message_content' => , + * ]; + * + * + * @method string getType() Type of the result, must be mpeg4_gif + * @method string getId() Unique identifier for this result, 1-64 bytes + * @method string getMpeg4Url() A valid URL for the MP4 file. File size must not exceed 1MB + * @method int getMpeg4Width() Optional. Video width + * @method int getMpeg4Height() Optional. Video height + * @method string getThumbUrl() URL of the static thumbnail (jpeg or gif) for the result + * @method string getTitle() Optional. Title for the result + * @method string getCaption() Optional. Caption of the MPEG-4 file to be sent, 0-200 characters + * @method InlineKeyboard getReplyMarkup() Optional. Inline keyboard attached to the message + * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the video animation + * + * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes + * @method $this setMpeg4Url(string $mpeg4_url) A valid URL for the MP4 file. File size must not exceed 1MB + * @method $this setMpeg4Width(int $mpeg4_width) Optional. Video width + * @method $this setMpeg4Height(int $mpeg4_height) Optional. Video height + * @method $this setThumbUrl(string $thumb_url) URL of the static thumbnail (jpeg or gif) for the result + * @method $this setTitle(string $title) Optional. Title for the result + * @method $this setCaption(string $caption) Optional. Caption of the MPEG-4 file to be sent, 0-200 characters + * @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. Inline keyboard attached to the message + * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the video animation + */ +class InlineQueryResultMpeg4Gif extends InlineEntity +{ + /** + * InlineQueryResultMpeg4Gif constructor + * + * @param array $data + * + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public function __construct(array $data = []) + { + $data['type'] = 'mpeg4_gif'; + parent::__construct($data); + } +} diff --git a/src/Entities/InlineQuery/InlineQueryResultPhoto.php b/src/Entities/InlineQuery/InlineQueryResultPhoto.php new file mode 100644 index 000000000..5fd8ea724 --- /dev/null +++ b/src/Entities/InlineQuery/InlineQueryResultPhoto.php @@ -0,0 +1,73 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InlineQuery; + +use Longman\TelegramBot\Entities\InlineKeyboard; +use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; + +/** + * Class InlineQueryResultPhoto + * + * @link https://core.telegram.org/bots/api#inlinequeryresultphoto + * + * + * $data = [ + * 'id' => '', + * 'photo_url' => '', + * 'thumb_url' => '', + * 'photo_width' => 30, + * 'photo_height' => 30, + * 'title' => '', + * 'description' => '', + * 'caption' => '', + * 'reply_markup' => , + * 'input_message_content' => , + * ]; + * + * + * @method string getType() Type of the result, must be photo + * @method string getId() Unique identifier for this result, 1-64 bytes + * @method string getPhotoUrl() A valid URL of the photo. Photo must be in jpeg format. Photo size must not exceed 5MB + * @method string getThumbUrl() URL of the thumbnail for the photo + * @method int getPhotoWidth() Optional. Width of the photo + * @method int getPhotoHeight() Optional. Height of the photo + * @method string getTitle() Optional. Title for the result + * @method string getDescription() Optional. Short description of the result + * @method string getCaption() Optional. Caption of the photo to be sent, 0-200 characters + * @method InlineKeyboard getReplyMarkup() Optional. Inline keyboard attached to the message + * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the photo + * + * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes + * @method $this setPhotoUrl(string $photo_url) A valid URL of the photo. Photo must be in jpeg format. Photo size must not exceed 5MB + * @method $this setThumbUrl(string $thumb_url) URL of the thumbnail for the photo + * @method $this setPhotoWidth(int $photo_width) Optional. Width of the photo + * @method $this setPhotoHeight(int $photo_height) Optional. Height of the photo + * @method $this setTitle(string $title) Optional. Title for the result + * @method $this setDescription(string $description) Optional. Short description of the result + * @method $this setCaption(string $caption) Optional. Caption of the photo to be sent, 0-200 characters + * @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. Inline keyboard attached to the message + * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the photo + */ +class InlineQueryResultPhoto extends InlineEntity +{ + /** + * InlineQueryResultPhoto constructor + * + * @param array $data + * + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public function __construct(array $data = []) + { + $data['type'] = 'photo'; + parent::__construct($data); + } +} diff --git a/src/Entities/InlineQuery/InlineQueryResultVenue.php b/src/Entities/InlineQuery/InlineQueryResultVenue.php new file mode 100644 index 000000000..c9df43826 --- /dev/null +++ b/src/Entities/InlineQuery/InlineQueryResultVenue.php @@ -0,0 +1,76 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InlineQuery; + +use Longman\TelegramBot\Entities\InlineKeyboard; +use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; + +/** + * Class InlineQueryResultVenue + * + * @link https://core.telegram.org/bots/api#inlinequeryresultvenue + * + * + * $data = [ + * 'id' => '', + * 'latitude' => 36.0338, + * 'longitude' => 71.8601, + * 'title' => '', + * 'address' => '', + * 'foursquare_id' => '', + * 'reply_markup' => , + * 'input_message_content' => , + * 'thumb_url' => '', + * 'thumb_width' => 30, + * 'thumb_height' => 30, + * ]; + * + * + * @method string getType() Type of the result, must be venue + * @method string getId() Unique identifier for this result, 1-64 Bytes + * @method float getLatitude() Latitude of the venue location in degrees + * @method float getLongitude() Longitude of the venue location in degrees + * @method string getTitle() Title of the venue + * @method string getAddress() Address of the venue + * @method string getFoursquareId() Optional. Foursquare identifier of the venue if known + * @method InlineKeyboard getReplyMarkup() Optional. Inline keyboard attached to the message + * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the venue + * @method string getThumbUrl() Optional. Url of the thumbnail for the result + * @method int getThumbWidth() Optional. Thumbnail width + * @method int getThumbHeight() Optional. Thumbnail height + * + * @method $this setId(string $id) Unique identifier for this result, 1-64 Bytes + * @method $this setLatitude(float $latitude) Latitude of the venue location in degrees + * @method $this setLongitude(float $longitude) Longitude of the venue location in degrees + * @method $this setTitle(string $title) Title of the venue + * @method $this setAddress(string $address) Address of the venue + * @method $this setFoursquareId(string $foursquare_id) Optional. Foursquare identifier of the venue if known + * @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. Inline keyboard attached to the message + * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the venue + * @method $this setThumbUrl(string $thumb_url) Optional. Url of the thumbnail for the result + * @method $this setThumbWidth(int $thumb_width) Optional. Thumbnail width + * @method $this setThumbHeight(int $thumb_height) Optional. Thumbnail height + */ +class InlineQueryResultVenue extends InlineEntity +{ + /** + * InlineQueryResultVenue constructor + * + * @param array $data + * + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public function __construct(array $data = []) + { + $data['type'] = 'venue'; + parent::__construct($data); + } +} diff --git a/src/Entities/InlineQuery/InlineQueryResultVideo.php b/src/Entities/InlineQuery/InlineQueryResultVideo.php new file mode 100644 index 000000000..7f6fb5d16 --- /dev/null +++ b/src/Entities/InlineQuery/InlineQueryResultVideo.php @@ -0,0 +1,79 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InlineQuery; + +use Longman\TelegramBot\Entities\InlineKeyboard; +use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; + +/** + * Class InlineQueryResultVideo + * + * @link https://core.telegram.org/bots/api#inlinequeryresultvideo + * + * + * $data = [ + * 'id' => '', + * 'video_url' => '', + * 'mime_type' => '', + * 'thumb_url' => '', + * 'title' => '', + * 'caption' => '', + * 'video_width' => 30, + * 'video_height' => 30, + * 'video_duration' => 123, + * 'description' => '', + * 'reply_markup' => , + * 'input_message_content' => , + * ]; + * + * + * @method string getType() Type of the result, must be video + * @method string getId() Unique identifier for this result, 1-64 bytes + * @method string getVideoUrl() A valid URL for the embedded video player or video file + * @method string getMimeType() Mime type of the content of video url, “text/html” or “video/mp4” + * @method string getThumbUrl() URL of the thumbnail (jpeg only) for the video + * @method string getTitle() Title for the result + * @method string getCaption() Optional. Caption of the video to be sent, 0-200 characters + * @method int getVideoWidth() Optional. Video width + * @method int getVideoHeight() Optional. Video height + * @method int getVideoDuration() Optional. Video duration in seconds + * @method string getDescription() Optional. Short description of the result + * @method InlineKeyboard getReplyMarkup() Optional. Inline keyboard attached to the message + * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the video + * + * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes + * @method $this setVideoUrl(string $video_url) A valid URL for the embedded video player or video file + * @method $this setMimeType(string $mime_type) Mime type of the content of video url, “text/html” or “video/mp4” + * @method $this setThumbUrl(string $thumb_url) URL of the thumbnail (jpeg only) for the video + * @method $this setTitle(string $title) Title for the result + * @method $this setCaption(string $caption) Optional. Caption of the video to be sent, 0-200 characters + * @method $this setVideoWidth(int $video_width) Optional. Video width + * @method $this setVideoHeight(int $video_height) Optional. Video height + * @method $this setVideoDuration(int $video_duration) Optional. Video duration in seconds + * @method $this setDescription(string $description) Optional. Short description of the result + * @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. Inline keyboard attached to the message + * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the video + */ +class InlineQueryResultVideo extends InlineEntity +{ + /** + * InlineQueryResultVideo constructor + * + * @param array $data + * + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public function __construct(array $data = []) + { + $data['type'] = 'video'; + parent::__construct($data); + } +} diff --git a/src/Entities/InlineQuery/InlineQueryResultVoice.php b/src/Entities/InlineQuery/InlineQueryResultVoice.php new file mode 100644 index 000000000..5996c1765 --- /dev/null +++ b/src/Entities/InlineQuery/InlineQueryResultVoice.php @@ -0,0 +1,61 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InlineQuery; + +use Longman\TelegramBot\Entities\InlineKeyboard; +use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; + +/** + * Class InlineQueryResultVoice + * + * @link https://core.telegram.org/bots/api#inlinequeryresultvoice + * + * + * $data = [ + * 'id' => '', + * 'voice_url' => '', + * 'title' => '', + * 'voice_duration' => 123, + * 'reply_markup' => , + * 'input_message_content' => , + * ]; + * + * + * @method string getType() Type of the result, must be voice + * @method string getId() Unique identifier for this result, 1-64 bytes + * @method string getVoiceUrl() A valid URL for the voice recording + * @method string getTitle() Recording title + * @method int getVoiceDuration() Optional. Recording duration in seconds + * @method InlineKeyboard getReplyMarkup() Optional. Inline keyboard attached to the message + * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the voice recording + * + * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes + * @method $this setVoiceUrl(string $voice_url) A valid URL for the voice recording + * @method $this setTitle(string $title) Recording title + * @method $this setVoiceDuration(int $voice_duration) Optional. Recording duration in seconds + * @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. Inline keyboard attached to the message + * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the voice recording + */ +class InlineQueryResultVoice extends InlineEntity +{ + /** + * InlineQueryResultVoice constructor + * + * @param array $data + * + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public function __construct(array $data = []) + { + $data['type'] = 'voice'; + parent::__construct($data); + } +} diff --git a/src/Entities/InlineQueryResult.php b/src/Entities/InlineQueryResult.php deleted file mode 100644 index 1f19a13fe..000000000 --- a/src/Entities/InlineQueryResult.php +++ /dev/null @@ -1,94 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InlineQueryResult extends Entity -{ - /** - * @var string - */ - protected $type; - - /** - * @var mixed|null - */ - protected $id; - - /** - * @var mixed|null - */ - protected $input_message_content; - - /** - * @var mixed|null - */ - protected $reply_markup; - - /** - * InlineQueryResult constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - $this->type = null; - $this->id = isset($data['id']) ? $data['id'] : null; - if (empty($this->id)) { - throw new TelegramException('id is empty!'); - } - - $this->input_message_content = isset($data['input_message_content']) ? $data['input_message_content'] : null; - $this->reply_markup = isset($data['reply_markup']) ? $data['reply_markup'] : null; - } - - /** - * Get type - * - * @return string - */ - public function getType() - { - return $this->type; - } - - /** - * Get id - * - * @return mixed|null - */ - public function getId() - { - return $this->id; - } - - /** - * Get input message content - * - * @return mixed|null - */ - public function getInputMessageContent() - { - return $this->input_message_content; - } - - /** - * Get reply markup - * - * @return mixed|null - */ - public function getReplyMarkup() - { - return $this->reply_markup; - } -} diff --git a/src/Entities/InlineQueryResultArticle.php b/src/Entities/InlineQueryResultArticle.php deleted file mode 100644 index 1ce74ad32..000000000 --- a/src/Entities/InlineQueryResultArticle.php +++ /dev/null @@ -1,166 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InlineQueryResultArticle extends InlineQueryResult -{ - /** - * @var mixed|null - */ - protected $title; - - /** - * @var mixed|null - */ - protected $input_message_content; - - /** - * @var mixed|null - */ - protected $url; - - /** - * @var mixed|null - */ - protected $hide_url; - - /** - * @var mixed|null - */ - protected $description; - - /** - * @var mixed|null - */ - protected $thumb_url; - - /** - * @var mixed|null - */ - protected $thumb_width; - - /** - * @var mixed|null - */ - protected $thumb_height; - - /** - * InlineQueryResultArticle constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - parent::__construct($data); - - $this->type = 'article'; - - $this->title = isset($data['title']) ? $data['title'] : null; - if (empty($this->title)) { - throw new TelegramException('title is empty!'); - } - - $this->input_message_content = isset($data['input_message_content']) ? $data['input_message_content'] : null; - if (empty($this->input_message_content)) { - throw new TelegramException('input_message_content is empty!'); - } - - $this->url = isset($data['url']) ? $data['url'] : null; - $this->hide_url = isset($data['hide_url']) ? $data['hide_url'] : null; - $this->description = isset($data['description']) ? $data['description'] : null; - $this->thumb_url = isset($data['thumb_url']) ? $data['thumb_url'] : null; - $this->thumb_width = isset($data['thumb_width']) ? $data['thumb_width'] : null; - $this->thumb_height = isset($data['thumb_height']) ? $data['thumb_height'] : null; - } - - /** - * Get title - * - * @return mixed|null - */ - public function getTitle() - { - return $this->title; - } - - /** - * Get input message content - * - * @return mixed|null - */ - public function getInputMessageContent() - { - return $this->input_message_content; - } - - /** - * Get url - * - * @return mixed|null - */ - public function getUrl() - { - return $this->url; - } - - /** - * Get hide url - * - * @return mixed|null - */ - public function getHideUrl() - { - return $this->hide_url; - } - - /** - * Get description - * - * @return mixed|null - */ - public function getDescription() - { - return $this->description; - } - - /** - * Get thumb url - * - * @return mixed|null - */ - public function getThumbUrl() - { - return $this->thumb_url; - } - - /** - * Get thumb width - * - * @return mixed|null - */ - public function getThumbWidth() - { - return $this->thumb_width; - } - - /** - * Get thumb height - * - * @return mixed|null - */ - public function getThumbHeight() - { - return $this->thumb_height; - } -} diff --git a/src/Entities/InlineQueryResultAudio.php b/src/Entities/InlineQueryResultAudio.php deleted file mode 100644 index c00aa411c..000000000 --- a/src/Entities/InlineQueryResultAudio.php +++ /dev/null @@ -1,102 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InlineQueryResultAudio extends InlineQueryResult -{ - /** - * @var mixed|null - */ - protected $audio_url; - - /** - * @var mixed|null - */ - protected $title; - - /** - * @var mixed|null - */ - protected $performer; - - /** - * @var mixed|null - */ - protected $audio_duration; - - /** - * InlineQueryResultAudio constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - parent::__construct($data); - - $this->type = 'audio'; - - $this->audio_url = isset($data['audio_url']) ? $data['audio_url'] : null; - if (empty($this->audio_url)) { - throw new TelegramException('audio_url is empty!'); - } - - $this->title = isset($data['title']) ? $data['title'] : null; - if (empty($this->title)) { - throw new TelegramException('title is empty!'); - } - - $this->performer = isset($data['performer']) ? $data['performer'] : null; - $this->audio_duration = isset($data['audio_duration']) ? $data['audio_duration'] : null; - } - - /** - * Get audio url - * - * @return mixed|null - */ - public function getAudioUrl() - { - return $this->audio_url; - } - - /** - * Get title - * - * @return mixed|null - */ - public function getTitle() - { - return $this->title; - } - - /** - * Get performer - * - * @return mixed|null - */ - public function getPerformer() - { - return $this->performer; - } - - /** - * Get audio duration - * - * @return mixed|null - */ - public function getAudioDuration() - { - return $this->audio_duration; - } -} diff --git a/src/Entities/InlineQueryResultCachedAudio.php b/src/Entities/InlineQueryResultCachedAudio.php deleted file mode 100644 index f7e884eca..000000000 --- a/src/Entities/InlineQueryResultCachedAudio.php +++ /dev/null @@ -1,49 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InlineQueryResultCachedAudio extends InlineQueryResult -{ - /** - * @var mixed|null - */ - protected $audio_file_id; - - /** - * InlineQueryResultCachedAudio constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - parent::__construct($data); - - $this->type = 'audio'; - - $this->audio_file_id = isset($data['audio_file_id']) ? $data['audio_file_id'] : null; - if (empty($this->audio_file_id)) { - throw new TelegramException('audio_file_id is empty!'); - } - } - - /** - * Get audio file id - * - * @return mixed|null - */ - public function getAudioFileId() - { - return $this->audio_file_id; - } -} diff --git a/src/Entities/InlineQueryResultCachedDocument.php b/src/Entities/InlineQueryResultCachedDocument.php deleted file mode 100644 index d45f89ecf..000000000 --- a/src/Entities/InlineQueryResultCachedDocument.php +++ /dev/null @@ -1,102 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InlineQueryResultCachedDocument extends InlineQueryResult -{ - /** - * @var mixed|null - */ - protected $document_file_id; - - /** - * @var mixed|null - */ - protected $title; - - /** - * @var mixed|null - */ - protected $description; - - /** - * @var mixed|null - */ - protected $caption; - - /** - * InlineQueryResultCachedDocument constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - parent::__construct($data); - - $this->type = 'document'; - - $this->document_file_id = isset($data['document_file_id']) ? $data['document_file_id'] : null; - if (empty($this->document_file_id)) { - throw new TelegramException('document_file_id is empty!'); - } - - $this->title = isset($data['title']) ? $data['title'] : null; - if (empty($this->title)) { - throw new TelegramException('title is empty!'); - } - - $this->description = isset($data['description']) ? $data['description'] : null; - $this->caption = isset($data['caption']) ? $data['caption'] : null; - } - - /** - * Get document file id - * - * @return mixed|null - */ - public function getDocumentFileId() - { - return $this->document_file_id; - } - - /** - * Get title - * - * @return mixed|null - */ - public function getTitle() - { - return $this->title; - } - - /** - * Get description - * - * @return mixed|null - */ - public function getDescription() - { - return $this->description; - } - - /** - * Get caption - * - * @return mixed|null - */ - public function getCaption() - { - return $this->caption; - } -} diff --git a/src/Entities/InlineQueryResultCachedGif.php b/src/Entities/InlineQueryResultCachedGif.php deleted file mode 100644 index c314e2c05..000000000 --- a/src/Entities/InlineQueryResultCachedGif.php +++ /dev/null @@ -1,87 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InlineQueryResultCachedGif extends InlineQueryResult -{ - /** - * @var mixed|null - */ - protected $gif_file_id; - - /** - * @var mixed|null - */ - protected $title; - - /** - * @var - */ - protected $description; - - /** - * @var mixed|null - */ - protected $caption; - - /** - * InlineQueryResultCachedGif constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - parent::__construct($data); - - $this->type = 'gif'; - - $this->gif_file_id = isset($data['gif_file_id']) ? $data['gif_file_id'] : null; - if (empty($this->gif_file_id)) { - throw new TelegramException('gif_file_id is empty!'); - } - - $this->title = isset($data['title']) ? $data['title'] : null; - $this->caption = isset($data['caption']) ? $data['caption'] : null; - } - - /** - * Get gif file id - * - * @return mixed|null - */ - public function getGifFileId() - { - return $this->gif_file_id; - } - - /** - * Get title - * - * @return mixed|null - */ - public function getTitle() - { - return $this->title; - } - - /** - * Get caption - * - * @return mixed|null - */ - public function getCaption() - { - return $this->caption; - } -} diff --git a/src/Entities/InlineQueryResultCachedMpeg4Gif.php b/src/Entities/InlineQueryResultCachedMpeg4Gif.php deleted file mode 100644 index 02aee0fea..000000000 --- a/src/Entities/InlineQueryResultCachedMpeg4Gif.php +++ /dev/null @@ -1,82 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InlineQueryResultCachedMpeg4Gif extends InlineQueryResult -{ - /** - * @var mixed|null - */ - protected $mpeg4_file_id; - - /** - * @var mixed|null - */ - protected $title; - - /** - * @var mixed|null - */ - protected $caption; - - /** - * InlineQueryResultCachedMpeg4Gif constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - parent::__construct($data); - - $this->type = 'mpeg4_gif'; - - $this->mpeg4_file_id = isset($data['mpeg4_file_id']) ? $data['mpeg4_file_id'] : null; - if (empty($this->mpeg4_file_id)) { - throw new TelegramException('mpeg4_file_id is empty!'); - } - - $this->title = isset($data['title']) ? $data['title'] : null; - $this->caption = isset($data['caption']) ? $data['caption'] : null; - } - - /** - * Get mp4 file id - * - * @return mixed|null - */ - public function getMpeg4FileId() - { - return $this->mpeg4_file_id; - } - - /** - * Get title - * - * @return mixed|null - */ - public function getTitle() - { - return $this->title; - } - - /** - * Get caption - * - * @return mixed|null - */ - public function getCaption() - { - return $this->caption; - } -} diff --git a/src/Entities/InlineQueryResultCachedPhoto.php b/src/Entities/InlineQueryResultCachedPhoto.php deleted file mode 100644 index 32f4d7603..000000000 --- a/src/Entities/InlineQueryResultCachedPhoto.php +++ /dev/null @@ -1,98 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InlineQueryResultCachedPhoto extends InlineQueryResult -{ - /** - * @var mixed|null - */ - protected $photo_file_id; - - /** - * @var mixed|null - */ - protected $title; - - /** - * @var mixed|null - */ - protected $description; - - /** - * @var mixed|null - */ - protected $caption; - - /** - * InlineQueryResultCachedPhoto constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - parent::__construct($data); - - $this->type = 'photo'; - - $this->photo_file_id = isset($data['photo_file_id']) ? $data['photo_file_id'] : null; - if (empty($this->photo_file_id)) { - throw new TelegramException('photo_file_id is empty!'); - } - - $this->title = isset($data['title']) ? $data['title'] : null; - $this->description = isset($data['description']) ? $data['description'] : null; - $this->caption = isset($data['caption']) ? $data['caption'] : null; - } - - /** - * Get photo file id - * - * @return mixed|null - */ - public function getPhotoFileId() - { - return $this->photo_file_id; - } - - /** - * Get title - * - * @return mixed|null - */ - public function getTitle() - { - return $this->title; - } - - /** - * Get description - * - * @return mixed|null - */ - public function getDescription() - { - return $this->description; - } - - /** - * Get caption - * - * @return mixed|null - */ - public function getCaption() - { - return $this->caption; - } -} diff --git a/src/Entities/InlineQueryResultCachedSticker.php b/src/Entities/InlineQueryResultCachedSticker.php deleted file mode 100644 index d1f1a7db4..000000000 --- a/src/Entities/InlineQueryResultCachedSticker.php +++ /dev/null @@ -1,49 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InlineQueryResultCachedSticker extends InlineQueryResult -{ - /** - * @var mixed|null - */ - protected $sticker_file_id; - - /** - * InlineQueryResultCachedSticker constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - parent::__construct($data); - - $this->type = 'sticker'; - - $this->photo_file_id = isset($data['sticker_file_id']) ? $data['sticker_file_id'] : null; - if (empty($this->sticker_file_id)) { - throw new TelegramException('sticker_file_id is empty!'); - } - } - - /** - * Get sticker file id - * - * @return mixed|null - */ - public function getStickerFileId() - { - return $this->sticker_file_id; - } -} diff --git a/src/Entities/InlineQueryResultCachedVideo.php b/src/Entities/InlineQueryResultCachedVideo.php deleted file mode 100644 index 9ade9d5f6..000000000 --- a/src/Entities/InlineQueryResultCachedVideo.php +++ /dev/null @@ -1,102 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InlineQueryResultCachedVideo extends InlineQueryResult -{ - /** - * @var mixed|null - */ - protected $video_file_id; - - /** - * @var mixed|null - */ - protected $title; - - /** - * @var mixed|null - */ - protected $description; - - /** - * @var mixed|null - */ - protected $caption; - - /** - * InlineQueryResultCachedVideo constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - parent::__construct($data); - - $this->type = 'photo'; - - $this->video_file_id = isset($data['video_file_id']) ? $data['video_file_id'] : null; - if (empty($this->video_file_id)) { - throw new TelegramException('video_file_id is empty!'); - } - - $this->title = isset($data['title']) ? $data['title'] : null; - if (empty($this->title)) { - throw new TelegramException('title is empty!'); - } - - $this->description = isset($data['description']) ? $data['description'] : null; - $this->caption = isset($data['caption']) ? $data['caption'] : null; - } - - /** - * Get video file id - * - * @return mixed|null - */ - public function getVideoFileId() - { - return $this->video_file_id; - } - - /** - * Get title - * - * @return mixed|null - */ - public function getTitle() - { - return $this->title; - } - - /** - * Get description - * - * @return mixed|null - */ - public function getDescription() - { - return $this->description; - } - - /** - * Get caption - * - * @return mixed|null - */ - public function getCaption() - { - return $this->caption; - } -} diff --git a/src/Entities/InlineQueryResultCachedVoice.php b/src/Entities/InlineQueryResultCachedVoice.php deleted file mode 100644 index ef9c9822b..000000000 --- a/src/Entities/InlineQueryResultCachedVoice.php +++ /dev/null @@ -1,102 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InlineQueryResultCachedVoice extends InlineQueryResult -{ - /** - * @var mixed|null - */ - protected $voice_file_id; - - /** - * @var mixed|null - */ - protected $title; - - /** - * @var mixed|null - */ - protected $description; - - /** - * @var mixed|null - */ - protected $caption; - - /** - * InlineQueryResultCachedVoice constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - parent::__construct($data); - - $this->type = 'voice'; - - $this->voice_file_id = isset($data['voice_file_id']) ? $data['voice_file_id'] : null; - if (empty($this->voice_file_id)) { - throw new TelegramException('voice_file_id is empty!'); - } - - $this->title = isset($data['title']) ? $data['title'] : null; - if (empty($this->title)) { - throw new TelegramException('title is empty!'); - } - - $this->description = isset($data['description']) ? $data['description'] : null; - $this->caption = isset($data['caption']) ? $data['caption'] : null; - } - - /** - * Get voice file id - * - * @return mixed|null - */ - public function getVoiceFileId() - { - return $this->voice_file_id; - } - - /** - * Get title - * - * @return mixed|null - */ - public function getTitle() - { - return $this->title; - } - - /** - * Get description - * - * @return mixed|null - */ - public function getDescription() - { - return $this->description; - } - - /** - * Get caption - * - * @return mixed|null - */ - public function getCaption() - { - return $this->caption; - } -} diff --git a/src/Entities/InlineQueryResultContact.php b/src/Entities/InlineQueryResultContact.php deleted file mode 100644 index db5f4ed94..000000000 --- a/src/Entities/InlineQueryResultContact.php +++ /dev/null @@ -1,135 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InlineQueryResultContact extends InlineQueryResult -{ - /** - * @var mixed|null - */ - protected $phone_number; - - /** - * @var mixed|null - */ - protected $first_name; - - /** - * @var mixed|null - */ - protected $last_name; - - /** - * @var mixed|null - */ - protected $thumb_url; - - /** - * @var mixed|null - */ - protected $thumb_width; - - /** - * @var mixed|null - */ - protected $thumb_height; - - /** - * InlineQueryResultContact constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - parent::__construct($data); - - $this->type = 'contact'; - - $this->phone_number = isset($data['phone_number']) ? $data['phone_number'] : null; - if (empty($this->phone_number)) { - throw new TelegramException('phone_number is empty!'); - } - - $this->first_name = isset($data['first_name']) ? $data['first_name'] : null; - if (empty($this->first_name)) { - throw new TelegramException('first_name is empty!'); - } - - $this->last_name = isset($data['last_name']) ? $data['last_name'] : null; - - $this->thumb_url = isset($data['thumb_url']) ? $data['thumb_url'] : null; - $this->thumb_width = isset($data['thumb_width']) ? $data['thumb_width'] : null; - $this->thumb_height = isset($data['thumb_height']) ? $data['thumb_height'] : null; - } - - /** - * Get phone number - * - * @return mixed|null - */ - public function getPhoneNumber() - { - return $this->phone_number; - } - - /** - * Get first name - * - * @return mixed|null - */ - public function getFirstName() - { - return $this->first_name; - } - - /** - * Get last name - * - * @return mixed|null - */ - public function getLastName() - { - return $this->last_name; - } - - /** - * Get thumb url - * - * @return mixed|null - */ - public function getThumbUrl() - { - return $this->thumb_url; - } - - /** - * Get thumb width - * - * @return mixed|null - */ - public function getThumbWidth() - { - return $this->thumb_width; - } - - /** - * Get thumb height - * - * @return mixed|null - */ - public function getThumbHeight() - { - return $this->thumb_height; - } -} diff --git a/src/Entities/InlineQueryResultDocument.php b/src/Entities/InlineQueryResultDocument.php deleted file mode 100644 index c4e1ca5b8..000000000 --- a/src/Entities/InlineQueryResultDocument.php +++ /dev/null @@ -1,171 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InlineQueryResultDocument extends InlineQueryResult -{ - /** - * @var mixed|null - */ - protected $title; - - /** - * @var mixed|null - */ - protected $caption; - - /** - * @var mixed|null - */ - protected $document_url; - - /** - * @var mixed|null - */ - protected $mime_type; - - /** - * @var mixed|null - */ - protected $description; - - /** - * @var mixed|null - */ - protected $thumb_url; - - /** - * @var mixed|null - */ - protected $thumb_width; - - /** - * @var mixed|null - */ - protected $thumb_height; - - /** - * InlineQueryResultDocument constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - parent::__construct($data); - - $this->type = 'document'; - - $this->title = isset($data['title']) ? $data['title'] : null; - if (empty($this->title)) { - throw new TelegramException('title is empty!'); - } - - $this->caption = isset($data['caption']) ? $data['caption'] : null; - - $this->document_url = isset($data['document_url']) ? $data['document_url'] : null; - if (empty($this->document_url)) { - throw new TelegramException('document_url is empty!'); - } - - $this->mime_type = isset($data['mime_type']) ? $data['mime_type'] : null; - if (empty($this->mime_type)) { - throw new TelegramException('mime_type is empty!'); - } - - $this->description = isset($data['description']) ? $data['description'] : null; - $this->thumb_url = isset($data['thumb_url']) ? $data['thumb_url'] : null; - $this->thumb_width = isset($data['thumb_width']) ? $data['thumb_width'] : null; - $this->thumb_height = isset($data['thumb_height']) ? $data['thumb_height'] : null; - } - - /** - * Get title - * - * @return mixed|null - */ - public function getTitle() - { - return $this->title; - } - - /** - * Get caption - * - * @return mixed|null - */ - public function getCaption() - { - return $this->caption; - } - - /** - * Get document url - * - * @return mixed|null - */ - public function getDocumentUrl() - { - return $this->document_url; - } - - /** - * Get mime type - * - * @return mixed|null - */ - public function getMimeType() - { - return $this->mime_type; - } - - /** - * Get description - * - * @return mixed|null - */ - public function getDescription() - { - return $this->description; - } - - /** - * Get thumb url - * - * @return mixed|null - */ - public function getThumbUrl() - { - return $this->thumb_url; - } - - /** - * Get thumb width - * - * @return mixed|null - */ - public function getThumbWidth() - { - return $this->thumb_width; - } - - /** - * Get thumb height - * - * @return mixed|null - */ - public function getThumbHeight() - { - return $this->thumb_height; - } -} diff --git a/src/Entities/InlineQueryResultGif.php b/src/Entities/InlineQueryResultGif.php deleted file mode 100644 index 2f3df845e..000000000 --- a/src/Entities/InlineQueryResultGif.php +++ /dev/null @@ -1,135 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InlineQueryResultGif extends InlineQueryResult -{ - /** - * @var mixed|null - */ - protected $gif_url; - - /** - * @var mixed|null - */ - protected $gif_width; - - /** - * @var mixed|null - */ - protected $gif_height; - - /** - * @var mixed|null - */ - protected $thumb_url; - - /** - * @var mixed|null - */ - protected $title; - - /** - * @var mixed|null - */ - protected $caption; - - /** - * InlineQueryResultGif constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - parent::__construct($data); - - $this->type = 'gif'; - - $this->gif_url = isset($data['gif_url']) ? $data['gif_url'] : null; - if (empty($this->gif_url)) { - throw new TelegramException('gif_url is empty!'); - } - - $this->gif_width = isset($data['gif_width']) ? $data['gif_width'] : null; - $this->gif_height = isset($data['gif_height']) ? $data['gif_height'] : null; - - $this->thumb_url = isset($data['thumb_url']) ? $data['thumb_url'] : null; - if (empty($this->thumb_url)) { - throw new TelegramException('thumb_url is empty!'); - } - - $this->title = isset($data['title']) ? $data['title'] : null; - $this->caption = isset($data['caption']) ? $data['caption'] : null; - } - - /** - * Get gif url - * - * @return mixed|null - */ - public function getGifUrl() - { - return $this->gif_url; - } - - /** - * Get gif width - * - * @return mixed|null - */ - public function getGifWidth() - { - return $this->gif_width; - } - - /** - * Get gif height - * - * @return mixed|null - */ - public function getGifHeight() - { - return $this->gif_height; - } - - /** - * Get thumb url - * - * @return mixed|null - */ - public function getThumbUrl() - { - return $this->thumb_url; - } - - /** - * Get title - * - * @return mixed|null - */ - public function getTitle() - { - return $this->title; - } - - /** - * Get caption - * - * @return mixed|null - */ - public function getCaption() - { - return $this->caption; - } -} diff --git a/src/Entities/InlineQueryResultLocation.php b/src/Entities/InlineQueryResultLocation.php deleted file mode 100644 index 22ece50f9..000000000 --- a/src/Entities/InlineQueryResultLocation.php +++ /dev/null @@ -1,138 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InlineQueryResultLocation extends InlineQueryResult -{ - /** - * @var mixed|null - */ - protected $latitude; - - /** - * @var mixed|null - */ - protected $longitude; - - /** - * @var mixed|null - */ - protected $title; - - /** - * @var mixed|null - */ - protected $thumb_url; - - /** - * @var mixed|null - */ - protected $thumb_width; - - /** - * @var mixed|null - */ - protected $thumb_height; - - /** - * InlineQueryResultLocation constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - parent::__construct($data); - - $this->type = 'location'; - - $this->latitude = isset($data['latitude']) ? $data['latitude'] : null; - if (empty($this->latitude)) { - throw new TelegramException('latitude is empty!'); - } - - $this->longitude = isset($data['longitude']) ? $data['longitude'] : null; - if (empty($this->longitude)) { - throw new TelegramException('longitude is empty!'); - } - - $this->title = isset($data['title']) ? $data['title'] : null; - if (empty($this->title)) { - throw new TelegramException('title is empty!'); - } - - $this->thumb_url = isset($data['thumb_url']) ? $data['thumb_url'] : null; - $this->thumb_width = isset($data['thumb_width']) ? $data['thumb_width'] : null; - $this->thumb_height = isset($data['thumb_height']) ? $data['thumb_height'] : null; - } - - /** - * Get latitude - * - * @return mixed|null - */ - public function getLatitude() - { - return $this->latitude; - } - - /** - * Get longitude - * - * @return mixed|null - */ - public function getLongitude() - { - return $this->longitude; - } - - /** - * Get title - * - * @return mixed|null - */ - public function getTitle() - { - return $this->title; - } - - /** - * Get thumb url - * - * @return mixed|null - */ - public function getThumbUrl() - { - return $this->thumb_url; - } - - /** - * Get thumb width - * - * @return mixed|null - */ - public function getThumbWidth() - { - return $this->thumb_width; - } - - /** - * Get thumb height - * - * @return mixed|null - */ - public function getThumbHeight() - { - return $this->thumb_height; - } -} diff --git a/src/Entities/InlineQueryResultMpeg4Gif.php b/src/Entities/InlineQueryResultMpeg4Gif.php deleted file mode 100644 index 7884317eb..000000000 --- a/src/Entities/InlineQueryResultMpeg4Gif.php +++ /dev/null @@ -1,135 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InlineQueryResultMpeg4Gif extends InlineQueryResult -{ - /** - * @var mixed|null - */ - protected $mpeg4_url; - - /** - * @var mixed|null - */ - protected $mpeg4_width; - - /** - * @var mixed|null - */ - protected $mpeg4_height; - - /** - * @var mixed|null - */ - protected $thumb_url; - - /** - * @var mixed|null - */ - protected $title; - - /** - * @var mixed|null - */ - protected $caption; - - /** - * InlineQueryResultMpeg4Gif constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - parent::__construct($data); - - $this->type = 'mpeg4_gif'; - - $this->mpeg4_url = isset($data['mpeg4_url']) ? $data['mpeg4_url'] : null; - if (empty($this->mpeg4_url)) { - throw new TelegramException('mpeg4_url is empty!'); - } - - $this->mpeg4_width = isset($data['mpeg4_width']) ? $data['mpeg4_width'] : null; - $this->mpeg4_height = isset($data['mpeg4_height']) ? $data['mpeg4_height'] : null; - - $this->thumb_url = isset($data['thumb_url']) ? $data['thumb_url'] : null; - if (empty($this->thumb_url)) { - throw new TelegramException('thumb_url is empty!'); - } - - $this->title = isset($data['title']) ? $data['title'] : null; - $this->caption = isset($data['caption']) ? $data['caption'] : null; - } - - /** - * Get mp4 url - * - * @return mixed|null - */ - public function getMpeg4Url() - { - return $this->mpeg4_url; - } - - /** - * Get mp4 width - * - * @return mixed|null - */ - public function getMpeg4Width() - { - return $this->mpeg4_width; - } - - /** - * Get mp4 height - * - * @return mixed|null - */ - public function getMpeg4Height() - { - return $this->mpeg4_height; - } - - /** - * Get thumb url - * - * @return mixed|null - */ - public function getThumbUrl() - { - return $this->thumb_url; - } - - /** - * Get title - * - * @return mixed|null - */ - public function getTitle() - { - return $this->title; - } - - /** - * Get caption - * - * @return mixed|null - */ - public function getCaption() - { - return $this->caption; - } -} diff --git a/src/Entities/InlineQueryResultPhoto.php b/src/Entities/InlineQueryResultPhoto.php deleted file mode 100644 index a6bdd2b1d..000000000 --- a/src/Entities/InlineQueryResultPhoto.php +++ /dev/null @@ -1,151 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InlineQueryResultPhoto extends InlineQueryResult -{ - /** - * @var mixed|null - */ - protected $photo_url; - - /** - * @var mixed|null - */ - protected $photo_width; - - /** - * @var mixed|null - */ - protected $photo_height; - - /** - * @var mixed|null - */ - protected $thumb_url; - - /** - * @var mixed|null - */ - protected $title; - - /** - * @var mixed|null - */ - protected $description; - - /** - * @var mixed|null - */ - protected $caption; - - /** - * InlineQueryResultPhoto constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - parent::__construct($data); - - $this->type = 'photo'; - - $this->photo_url = isset($data['photo_url']) ? $data['photo_url'] : null; - if (empty($this->photo_url)) { - throw new TelegramException('photo_url is empty!'); - } - - $this->photo_width = isset($data['photo_width']) ? $data['photo_width'] : null; - $this->photo_height = isset($data['photo_height']) ? $data['photo_height'] : null; - - $this->thumb_url = isset($data['thumb_url']) ? $data['thumb_url'] : null; - if (empty($this->thumb_url)) { - throw new TelegramException('thumb_url is empty!'); - } - - $this->title = isset($data['title']) ? $data['title'] : null; - $this->description = isset($data['description']) ? $data['description'] : null; - $this->caption = isset($data['caption']) ? $data['caption'] : null; - } - - /** - * Get photo url - * - * @return mixed|null - */ - public function getPhotoUrl() - { - return $this->photo_url; - } - - /** - * Get photo width - * - * @return mixed|null - */ - public function getPhotoWidth() - { - return $this->photo_width; - } - - /** - * Get photo height - * - * @return mixed|null - */ - public function getPhotoHeight() - { - return $this->photo_height; - } - - /** - * Get thumb url - * - * @return mixed|null - */ - public function getThumbUrl() - { - return $this->thumb_url; - } - - /** - * Get title - * - * @return mixed|null - */ - public function getTitle() - { - return $this->title; - } - - /** - * Get description - * - * @return mixed|null - */ - public function getDescription() - { - return $this->description; - } - - /** - * Get caption - * - * @return mixed|null - */ - public function getCaption() - { - return $this->caption; - } -} diff --git a/src/Entities/InlineQueryResultVenue.php b/src/Entities/InlineQueryResultVenue.php deleted file mode 100644 index c88f5ef73..000000000 --- a/src/Entities/InlineQueryResultVenue.php +++ /dev/null @@ -1,175 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InlineQueryResultVenue extends InlineQueryResult -{ - /** - * @var mixed|null - */ - protected $latitude; - - /** - * @var mixed|null - */ - protected $longitude; - - /** - * @var mixed|null - */ - protected $title; - - /** - * @var mixed|null - */ - protected $address; - - /** - * @var mixed|null - */ - protected $foursquare_id; - - /** - * @var mixed|null - */ - protected $thumb_url; - - /** - * @var mixed|null - */ - protected $thumb_width; - - /** - * @var mixed|null - */ - protected $thumb_height; - - /** - * InlineQueryResultVenue constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - parent::__construct($data); - - $this->type = 'venue'; - - $this->latitude = isset($data['latitude']) ? $data['latitude'] : null; - if (empty($this->latitude)) { - throw new TelegramException('latitude is empty!'); - } - - $this->longitude = isset($data['longitude']) ? $data['longitude'] : null; - if (empty($this->longitude)) { - throw new TelegramException('longitude is empty!'); - } - - $this->title = isset($data['title']) ? $data['title'] : null; - if (empty($this->title)) { - throw new TelegramException('title is empty!'); - } - - $this->address = isset($data['address']) ? $data['address'] : null; - if (empty($this->address)) { - throw new TelegramException('address is empty!'); - } - - $this->foursquare_id = isset($data['foursquare_id']) ? $data['foursquare_id'] : null; - - $this->thumb_url = isset($data['thumb_url']) ? $data['thumb_url'] : null; - $this->thumb_width = isset($data['thumb_width']) ? $data['thumb_width'] : null; - $this->thumb_height = isset($data['thumb_height']) ? $data['thumb_height'] : null; - } - - /** - * Get latitude - * - * @return mixed|null - */ - public function getLatitude() - { - return $this->latitude; - } - - /** - * Get longitude - * - * @return mixed|null - */ - public function getLongitude() - { - return $this->longitude; - } - - /** - * Get title - * - * @return mixed|null - */ - public function getTitle() - { - return $this->title; - } - - /** - * Get address - * - * @return mixed|null - */ - public function getAddress() - { - return $this->address; - } - - /** - * Get forsquare id - * - * @return mixed|null - */ - public function getFoursquareId() - { - return $this->foursquare_id; - } - - /** - * Get thumb url - * - * @return mixed|null - */ - public function getThumbUrl() - { - return $this->thumb_url; - } - - /** - * Get thumb width - * - * @return mixed|null - */ - public function getThumbWidth() - { - return $this->thumb_width; - } - - /** - * Get thumb height - * - * @return mixed|null - */ - public function getThumbHeight() - { - return $this->thumb_height; - } -} diff --git a/src/Entities/InlineQueryResultVideo.php b/src/Entities/InlineQueryResultVideo.php deleted file mode 100644 index 1d6be2e06..000000000 --- a/src/Entities/InlineQueryResultVideo.php +++ /dev/null @@ -1,184 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InlineQueryResultVideo extends InlineQueryResult -{ - /** - * @var mixed|null - */ - protected $video_url; - - /** - * @var mixed|null - */ - protected $mime_type; - - /** - * @var mixed|null - */ - protected $thumb_url; - - /** - * @var mixed|null - */ - protected $title; - - /** - * @var mixed|null - */ - protected $caption; - - /** - * @var mixed|null - */ - protected $video_width; - - /** - * @var mixed|null - */ - protected $video_height; - - /** - * @var mixed|null - */ - protected $video_duration; - - /** - * @var mixed|null - */ - protected $description; - - /** - * InlineQueryResultVideo constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - parent::__construct($data); - - $this->type = 'video'; - - $this->video_url = isset($data['video_url']) ? $data['video_url'] : null; - if (empty($this->video_url)) { - throw new TelegramException('video_url is empty!'); - } - $this->mime_type = isset($data['mime_type']) ? $data['mime_type'] : null; - if (empty($this->mime_type)) { - throw new TelegramException('mime_type is empty!'); - } - $this->thumb_url = isset($data['thumb_url']) ? $data['thumb_url'] : null; - if (empty($this->thumb_url)) { - throw new TelegramException('thumb_url is empty!'); - } - - $this->title = isset($data['title']) ? $data['title'] : null; - $this->caption = isset($data['caption']) ? $data['caption'] : null; - $this->video_width = isset($data['video_width']) ? $data['video_width'] : null; - $this->video_height = isset($data['video_height']) ? $data['video_height'] : null; - $this->video_duration = isset($data['video_duration']) ? $data['video_duration'] : null; - $this->description = isset($data['description']) ? $data['description'] : null; - } - - /** - * Get video url - * - * @return mixed|null - */ - public function getVideoUrl() - { - return $this->video_url; - } - - /** - * Get mime type - * - * @return mixed|null - */ - public function getMimeType() - { - return $this->mime_type; - } - - /** - * Get thumb url - * - * @return mixed|null - */ - public function getThumbUrl() - { - return $this->thumb_url; - } - - /** - * Get title - * - * @return mixed|null - */ - public function getTitle() - { - return $this->title; - } - - /** - * Get caption - * - * @return mixed|null - */ - public function getCaption() - { - return $this->caption; - } - - /** - * Get video width - * - * @return mixed|null - */ - public function getVideoWidth() - { - return $this->video_width; - } - - /** - * Get video height - * - * @return mixed|null - */ - public function getVideoHeight() - { - return $this->video_height; - } - - /** - * Get video duration - * - * @return mixed|null - */ - public function getVideoDuration() - { - return $this->video_duration; - } - - /** - * Get description - * - * @return mixed|null - */ - public function getDescription() - { - return $this->description; - } -} diff --git a/src/Entities/InlineQueryResultVoice.php b/src/Entities/InlineQueryResultVoice.php deleted file mode 100644 index 4dae9f2fa..000000000 --- a/src/Entities/InlineQueryResultVoice.php +++ /dev/null @@ -1,86 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InlineQueryResultVoice extends InlineQueryResult -{ - /** - * @var mixed|null - */ - protected $voice_url; - - /** - * @var mixed|null - */ - protected $title; - - /** - * @var mixed|null - */ - protected $voice_duration; - - /** - * InlineQueryResultVoice constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - parent::__construct($data); - - $this->type = 'voice'; - - $this->voice_url = isset($data['voice_url']) ? $data['voice_url'] : null; - if (empty($this->voice_url)) { - throw new TelegramException('voice_url is empty!'); - } - - $this->title = isset($data['title']) ? $data['title'] : null; - if (empty($this->title)) { - throw new TelegramException('title is empty!'); - } - - $this->voice_duration = isset($data['voice_duration']) ? $data['voice_duration'] : null; - } - - /** - * Get voice url - * - * @return mixed|null - */ - public function getVoiceUrl() - { - return $this->voice_url; - } - - /** - * Get title - * - * @return mixed|null - */ - public function getTitle() - { - return $this->title; - } - - /** - * Get voice duration - * - * @return mixed|null - */ - public function getVoiceDuration() - { - return $this->voice_duration; - } -} diff --git a/src/Entities/InputContactMessageContent.php b/src/Entities/InputContactMessageContent.php deleted file mode 100644 index 3a5da47b4..000000000 --- a/src/Entities/InputContactMessageContent.php +++ /dev/null @@ -1,52 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InputContactMessageContent extends InputMessageContent -{ - /** - * @var mixed|null - */ - protected $phone_number; - - /** - * @var mixed|null - */ - protected $first_name; - - /** - * @var mixed|null - */ - protected $last_name; - - /** - * InputContactMessageContent constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - $this->phone_number = isset($data['phone_number']) ? $data['phone_number'] : null; - if (empty($this->phone_number)) { - throw new TelegramException('phone_number is empty!'); - } - - $this->first_name = isset($data['first_name']) ? $data['first_name'] : null; - if (empty($this->first_name)) { - throw new TelegramException('first_name is empty!'); - } - - $this->last_name = isset($data['last_name']) ? $data['last_name'] : null; - } -} diff --git a/src/Entities/InputLocationMessageContent.php b/src/Entities/InputLocationMessageContent.php deleted file mode 100644 index 567da0464..000000000 --- a/src/Entities/InputLocationMessageContent.php +++ /dev/null @@ -1,45 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InputLocationMessageContent extends InputMessageContent -{ - /** - * @var mixed|null - */ - protected $latitude; - - /** - * @var mixed|null - */ - protected $longitude; - - /** - * InputLocationMessageContent constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - $this->latitude = isset($data['latitude']) ? $data['latitude'] : null; - if (empty($this->latitude)) { - throw new TelegramException('latitude is empty!'); - } - - $this->longitude = isset($data['longitude']) ? $data['longitude'] : null; - if (empty($this->longitude)) { - throw new TelegramException('longitude is empty!'); - } - } -} diff --git a/src/Entities/InputMessageContent/InputContactMessageContent.php b/src/Entities/InputMessageContent/InputContactMessageContent.php new file mode 100644 index 000000000..d6bf7ec59 --- /dev/null +++ b/src/Entities/InputMessageContent/InputContactMessageContent.php @@ -0,0 +1,39 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InputMessageContent; + +use Longman\TelegramBot\Entities\InlineQuery\InlineEntity; + +/** + * Class InputContactMessageContent + * + * @link https://core.telegram.org/bots/api#inputcontactmessagecontent + * + * + * $data = [ + * 'phone_number' => '', + * 'first_name' => '', + * 'last_name' => '', + * ]; + * + * + * @method string getPhoneNumber() Contact's phone number + * @method string getFirstName() Contact's first name + * @method string getLastName() Optional. Contact's last name + * + * @method $this setPhoneNumber(string $phone_number) Contact's phone number + * @method $this setFirstName(string $first_name) Contact's first name + * @method $this setLastName(string $last_name) Optional. Contact's last name + */ +class InputContactMessageContent extends InlineEntity implements InputMessageContent +{ + +} diff --git a/src/Entities/InputMessageContent/InputLocationMessageContent.php b/src/Entities/InputMessageContent/InputLocationMessageContent.php new file mode 100644 index 000000000..8e0fa9011 --- /dev/null +++ b/src/Entities/InputMessageContent/InputLocationMessageContent.php @@ -0,0 +1,35 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InputMessageContent; + +use Longman\TelegramBot\Entities\InlineQuery\InlineEntity; + +/** + * Class InputLocationMessageContent + * + * @link https://core.telegram.org/bots/api#inputlocationmessagecontent + * + * + * $data = [ + * 'latitude' => 36.0338, + * 'longitude' => 71.8601, + * ]; + * + * @method float getLatitude() Latitude of the location in degrees + * @method float getLongitude() Longitude of the location in degrees + * + * @method $this setLatitude(float $latitude) Latitude of the location in degrees + * @method $this setLongitude(float $longitude) Longitude of the location in degrees + */ +class InputLocationMessageContent extends InlineEntity implements InputMessageContent +{ + +} diff --git a/src/Entities/InputMessageContent/InputMessageContent.php b/src/Entities/InputMessageContent/InputMessageContent.php new file mode 100644 index 000000000..3b43be9e9 --- /dev/null +++ b/src/Entities/InputMessageContent/InputMessageContent.php @@ -0,0 +1,8 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InputMessageContent; + +use Longman\TelegramBot\Entities\InlineQuery\InlineEntity; + +/** + * Class InputTextMessageContent + * + * @link https://core.telegram.org/bots/api#inputtextmessagecontent + * + * + * $data = [ + * 'message_text' => '', + * 'parse_mode' => '', + * 'disable_web_page_preview' => true, + * ]; + * + * + * @method string getMessageText() Text of the message to be sent, 1-4096 characters. + * @method string getParseMode() Optional. Send Markdown or HTML, if you want Telegram apps to show bold, italic, fixed-width text or inline URLs in your bot's message. + * @method bool getDisableWebPagePreview() Optional. Disables link previews for links in the sent message + * + * @method $this setMessageText(string $message_text) Text of the message to be sent, 1-4096 characters. + * @method $this setParseMode(string $parse_mode) Optional. Send Markdown or HTML, if you want Telegram apps to show bold, italic, fixed-width text or inline URLs in your bot's message. + * @method $this setDisableWebPagePreview(bool $disable_web_page_preview) Optional. Disables link previews for links in the sent message + */ +class InputTextMessageContent extends InlineEntity implements InputMessageContent +{ + +} diff --git a/src/Entities/InputMessageContent/InputVenueMessageContent.php b/src/Entities/InputMessageContent/InputVenueMessageContent.php new file mode 100644 index 000000000..4d00cab15 --- /dev/null +++ b/src/Entities/InputMessageContent/InputVenueMessageContent.php @@ -0,0 +1,45 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InputMessageContent; + +use Longman\TelegramBot\Entities\InlineQuery\InlineEntity; + +/** + * Class InputVenueMessageContent + * + * @link https://core.telegram.org/bots/api#inputvenuemessagecontent + * + * + * $data = [ + * 'latitude' => 36.0338, + * 'longitude' => 71.8601, + * 'title' => '', + * 'address' => '', + * 'foursquare_id' => '', + * ]; + * + * + * @method float getLatitude() Latitude of the location in degrees + * @method float getLongitude() Longitude of the location in degrees + * @method string getTitle() Name of the venue + * @method string getAddress() Address of the venue + * @method string getFoursquareIdTitle() Optional. Foursquare identifier of the venue, if known + * + * @method $this setLatitude(float $latitude) Latitude of the location in degrees + * @method $this setLongitude(float $longitude) Longitude of the location in degrees + * @method $this setTitle(string $title) Name of the venue + * @method $this setAddress(string $address) Address of the venue + * @method $this setFoursquareIdTitle(string $foursquare_id_title) Optional. Foursquare identifier of the venue, if known + */ +class InputVenueMessageContent extends InlineEntity implements InputMessageContent +{ + +} diff --git a/src/Entities/InputTextMessageContent.php b/src/Entities/InputTextMessageContent.php deleted file mode 100644 index 608adba84..000000000 --- a/src/Entities/InputTextMessageContent.php +++ /dev/null @@ -1,48 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InputTextMessageContent extends InputMessageContent -{ - /** - * @var mixed|null - */ - protected $message_text; - - /** - * @var mixed|null - */ - protected $parse_mode; - - /** - * @var mixed|null - */ - protected $disable_web_page_preview; - - /** - * InputTextMessageContent constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - $this->message_text = isset($data['message_text']) ? $data['message_text'] : null; - if (empty($this->message_text)) { - throw new TelegramException('message_text is empty!'); - } - - $this->parse_mode = isset($data['parse_mode']) ? $data['parse_mode'] : null; - $this->disable_web_page_preview = isset($data['disable_web_page_preview']) ? $data['disable_web_page_preview'] : null; - } -} diff --git a/src/Entities/InputVenueMessageContent.php b/src/Entities/InputVenueMessageContent.php deleted file mode 100644 index 9516bde5b..000000000 --- a/src/Entities/InputVenueMessageContent.php +++ /dev/null @@ -1,72 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InputVenueMessageContent extends InputMessageContent -{ - /** - * @var mixed|null - */ - protected $latitude; - - /** - * @var mixed|null - */ - protected $longitude; - - /** - * @var mixed|null - */ - protected $title; - - /** - * @var mixed|null - */ - protected $address; - - /** - * @var mixed|null - */ - protected $foursquare_id; - - /** - * InputVenueMessageContent constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - $this->latitude = isset($data['latitude']) ? $data['latitude'] : null; - if (empty($this->latitude)) { - throw new TelegramException('latitude is empty!'); - } - - $this->longitude = isset($data['longitude']) ? $data['longitude'] : null; - if (empty($this->longitude)) { - throw new TelegramException('longitude is empty!'); - } - - $this->title = isset($data['title']) ? $data['title'] : null; - if (empty($this->title)) { - throw new TelegramException('title is empty!'); - } - - $this->address = isset($data['address']) ? $data['address'] : null; - if (empty($this->address)) { - throw new TelegramException('address is empty!'); - } - - $this->foursquare_id = isset($data['foursquare_id']) ? $data['foursquare_id'] : null; - } -} diff --git a/src/Entities/Keyboard.php b/src/Entities/Keyboard.php new file mode 100644 index 000000000..63c3419f7 --- /dev/null +++ b/src/Entities/Keyboard.php @@ -0,0 +1,226 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * Written by Marco Boretto + */ + +namespace Longman\TelegramBot\Entities; + +use Longman\TelegramBot\Exception\TelegramException; + +/** + * Class Keyboard + * + * @link https://core.telegram.org/bots/api#replykeyboardmarkup + * + * @method bool getResizeKeyboard() Optional. 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. + * @method bool getOneTimeKeyboard() Optional. Requests clients to hide the keyboard as soon as it's been used. The keyboard will still be available, but clients will automatically display the usual letter-keyboard in the chat – the user can press a special button in the input field to see the custom keyboard again. Defaults to false. + * @method bool getSelective() Optional. 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. + * + * @method $this setResizeKeyboard(bool $resize_keyboard) Optional. 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. + * @method $this setOneTimeKeyboard(bool $one_time_keyboard) Optional. Requests clients to hide the keyboard as soon as it's been used. The keyboard will still be available, but clients will automatically display the usual letter-keyboard in the chat – the user can press a special button in the input field to see the custom keyboard again. Defaults to false. + * @method $this setSelective(bool $selective) Optional. 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. + */ +class Keyboard extends Entity +{ + /** + * {@inheritdoc} + */ + public function __construct($data = []) + { + $data = call_user_func_array([$this, 'createFromParams'], func_get_args()); + parent::__construct($data); + + // Remove any empty buttons. + $this->{$this->getKeyboardType()} = array_filter($this->{$this->getKeyboardType()}); + } + + /** + * If this keyboard is an inline keyboard. + * + * @return bool + */ + public function isInlineKeyboard() + { + return $this instanceof InlineKeyboard; + } + + /** + * Get the proper keyboard button class for this keyboard. + * + * @return KeyboardButton|InlineKeyboardButton + */ + public function getKeyboardButtonClass() + { + return $this->isInlineKeyboard() ? InlineKeyboardButton::class : KeyboardButton::class; + } + + /** + * Get the type of keyboard, either "inline_keyboard" or "keyboard". + * + * @return string + */ + public function getKeyboardType() + { + return $this->isInlineKeyboard() ? 'inline_keyboard' : 'keyboard'; + } + + /** + * If no explicit keyboard is passed, try to create one from the parameters. + * + * @return array + */ + protected function createFromParams() + { + $keyboard_type = $this->getKeyboardType(); + + $args = func_get_args(); + + // Force button parameters into individual rows. + foreach ($args as &$arg) { + !is_array($arg) && $arg = [$arg]; + } + unset($arg); + + $data = reset($args); + + if ($from_data = array_key_exists($keyboard_type, (array)$data)) { + $args = $data[$keyboard_type]; + + // Make sure we're working with a proper row. + if (!is_array($args)) { + $args = []; + } + } + + $new_keyboard = []; + foreach ($args as $row) { + $new_keyboard[] = $this->parseRow($row); + } + + if (!empty($new_keyboard)) { + if (!$from_data) { + $data = []; + } + $data[$keyboard_type] = $new_keyboard; + } + + return $data; + } + + /** + * Create a new row in keyboard and add buttons. + * + * @return $this + */ + public function addRow() + { + if (($new_row = $this->parseRow(func_get_args())) !== null) { + $this->{$this->getKeyboardType()}[] = $new_row; + } + + return $this; + } + + /** + * Parse a given row to the correct array format. + * + * @param array $row + * + * @return array + */ + protected function parseRow($row) + { + if (!is_array($row)) { + return null; + } + + $new_row = []; + foreach ($row as $button) { + if (($new_button = $this->parseButton($button)) !== null) { + $new_row[] = $new_button; + } + } + + return $new_row; + } + + /** + * Parse a given button to the correct KeyboardButton object type. + * + * @param array|string|\Longman\TelegramBot\Entities\KeyboardButton $button + * + * @return \Longman\TelegramBot\Entities\KeyboardButton|null + */ + protected function parseButton($button) + { + $button_class = $this->getKeyboardButtonClass(); + + if ($button instanceof $button_class) { + return $button; + } + + if (!$this->isInlineKeyboard() || $button_class::couldBe($button)) { + return new $button_class($button); + } + + return null; + } + + /** + * {@inheritdoc} + */ + protected function validate() + { + $keyboard_type = $this->getKeyboardType(); + $keyboard = $this->getProperty($keyboard_type); + + if ($keyboard !== null) { + if (!is_array($keyboard)) { + throw new TelegramException($keyboard_type . ' field is not an array!'); + } + + foreach ($keyboard as $item) { + if (!is_array($item)) { + throw new TelegramException($keyboard_type . ' subfield is not an array!'); + } + } + } + } + + /** + * Hide the current custom keyboard and display the default letter-keyboard. + * + * @link https://core.telegram.org/bots/api#replykeyboardhide + * + * @param array $data + * + * @return \Longman\TelegramBot\Entities\Keyboard + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public static function hide(array $data = []) + { + return new static(array_merge(['keyboard' => [], 'hide_keyboard' => true, 'selective' => false], $data)); + } + + /** + * Display a reply interface to the user (act as if the user has selected the bot's message and tapped 'Reply'). + * + * @link https://core.telegram.org/bots/api#forcereply + * + * @param array $data + * + * @return \Longman\TelegramBot\Entities\Keyboard + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public static function forceReply(array $data = []) + { + return new static(array_merge(['keyboard' => [], 'force_reply' => true, 'selective' => false], $data)); + } +} diff --git a/src/Entities/KeyboardButton.php b/src/Entities/KeyboardButton.php index 82463a7ae..a7fb7ad5e 100644 --- a/src/Entities/KeyboardButton.php +++ b/src/Entities/KeyboardButton.php @@ -12,37 +12,68 @@ use Longman\TelegramBot\Exception\TelegramException; +/** + * Class KeyboardButton + * + * @link https://core.telegram.org/bots/api#keyboardbutton + * + * @method string getText() Text of the button. If none of the optional fields are used, it will be sent to the bot as a message when the button is pressed + * @method bool getRequestContact() Optional. If True, the user's phone number will be sent as a contact when the button is pressed. Available in private chats only + * @method bool getRequestLocation() Optional. If True, the user's current location will be sent when the button is pressed. Available in private chats only + * + * @method $this setText(string $text) Text of the button. If none of the optional fields are used, it will be sent to the bot as a message when the button is pressed + * @method $this setRequestContact(bool $request_contact) Optional. If True, the user's phone number will be sent as a contact when the button is pressed. Available in private chats only + * @method $this setRequestLocation(bool $request_location) Optional. If True, the user's current location will be sent when the button is pressed. Available in private chats only + */ class KeyboardButton extends Entity { /** - * @var mixed|null + * {@inheritdoc} */ - protected $text; + public function __construct($data) + { + if (is_string($data)) { + $data = ['text' => $data]; + } + parent::__construct($data); + } /** - * @var mixed|null + * Check if the passed data array could be a KeyboardButton. + * + * @param array $data + * + * @return bool */ - protected $request_contact; + public static function couldBe($data) + { + return is_array($data) && array_key_exists('text', $data); + } /** - * @var mixed|null + * {@inheritdoc} */ - protected $request_location; + protected function validate() + { + if ($this->getProperty('text', '') === '') { + throw new TelegramException('You must add some text to the button!'); + } + + if ($this->getRequestContact() && $this->getRequestLocation()) { + throw new TelegramException('You must use only one of these fields: request_contact, request_location!'); + } + } /** - * KeyboardButton constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException + * {@inheritdoc} */ - public function __construct(array $data = []) + public function __call($method, $args) { - $this->text = isset($data['text']) ? $data['text'] : null; - if (empty($this->text)) { - throw new TelegramException('text is empty!'); + // Only 1 of these can be set, so clear the others when setting a new one. + if (in_array($method, ['setRequestContact', 'setRequestLocation'], true)) { + unset($this->request_contact, $this->request_location); } - $this->request_contact = isset($data['request_contact']) ? $data['request_contact'] : null; - $this->request_location = isset($data['request_location']) ? $data['request_location'] : null; + return parent::__call($method, $args); } } diff --git a/src/Entities/Location.php b/src/Entities/Location.php index e9e20e037..c9340bce7 100644 --- a/src/Entities/Location.php +++ b/src/Entities/Location.php @@ -10,56 +10,15 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - +/** + * Class Location + * + * @link https://core.telegram.org/bots/api#location + * + * @method float getLongitude() Longitude as defined by sender + * @method float getLatitude() Latitude as defined by sender + */ class Location extends Entity { - /** - * @var mixed|null - */ - protected $longitude; - - /** - * @var mixed|null - */ - protected $latitude; - - /** - * Location constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - $this->longitude = isset($data['longitude']) ? $data['longitude'] : null; - if (empty($this->longitude)) { - throw new TelegramException('longitude is empty!'); - } - - $this->latitude = isset($data['latitude']) ? $data['latitude'] : null; - if (empty($this->latitude)) { - throw new TelegramException('latitude is empty!'); - } - } - - /** - * Get longitude - * - * @return mixed|null - */ - public function getLongitude() - { - return $this->longitude; - } - /** - * Get latitude - * - * @return mixed|null - */ - public function getLatitude() - { - return $this->latitude; - } } diff --git a/src/Entities/Message.php b/src/Entities/Message.php index 7cac7d22c..b437e4b95 100644 --- a/src/Entities/Message.php +++ b/src/Entities/Message.php @@ -10,414 +10,155 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - +/** + * Class Message + * + * @link https://core.telegram.org/bots/api#message + * + * @method int getMessageId() Unique message identifier + * @method User getFrom() Optional. Sender, can be empty for messages sent to channels + * @method int getDate() Date the message was sent in Unix time + * @method Chat getChat() Conversation the message belongs to + * @method User getForwardFrom() Optional. For forwarded messages, sender of the original message + * @method Chat getForwardFromChat() Optional. For messages forwarded from a channel, information about the original channel + * @method int getForwardDate() Optional. For forwarded messages, date the original message was sent in Unix time + * @method Message getReplyToMessage() Optional. For replies, the original message. Note that the Message object in this field will not contain further reply_to_message fields even if it itself is a reply. + * @method int getEditDate() Optional. Date the message was last edited in Unix time + * @method Audio getAudio() Optional. Message is an audio file, information about the file + * @method Document getDocument() Optional. Message is a general file, information about the file + * @method Sticker getSticker() Optional. Message is a sticker, information about the sticker + * @method Video getVideo() Optional. Message is a video, information about the video + * @method Voice getVoice() Optional. Message is a voice message, information about the file + * @method string getCaption() Optional. Caption for the document, photo or video, 0-200 characters + * @method Contact getContact() Optional. Message is a shared contact, information about the contact + * @method Location getLocation() Optional. Message is a shared location, information about the location + * @method Venue getVenue() Optional. Message is a venue, information about the venue + * @method User getNewChatMember() Optional. A new member was added to the group, information about them (this member may be the bot itself) + * @method User getLeftChatMember() Optional. A member was removed from the group, information about them (this member may be the bot itself) + * @method string getNewChatTitle() Optional. A chat title was changed to this value + * @method bool getDeleteChatPhoto() Optional. Service message: the chat photo was deleted + * @method bool getGroupChatCreated() Optional. Service message: the group has been created + * @method bool getSupergroupChatCreated() Optional. Service message: the supergroup has been created. This field can't be received in a message coming through updates, because bot can’t be a member of a supergroup when it is created. It can only be found in reply_to_message if someone replies to a very first message in a directly created supergroup. + * @method bool getChannelChatCreated() Optional. Service message: the channel has been created. This field can't be received in a message coming through updates, because bot can’t be a member of a channel when it is created. It can only be found in reply_to_message if someone replies to a very first message in a channel. + * @method int getMigrateToChatId() Optional. The group has been migrated to a supergroup with the specified identifier. This number may be greater than 32 bits and some programming languages may have difficulty/silent defects in interpreting it. But it smaller than 52 bits, so a signed 64 bit integer or double-precision float type are safe for storing this identifier. + * @method int getMigrateFromChatId() Optional. The supergroup has been migrated from a group with the specified identifier. This number may be greater than 32 bits and some programming languages may have difficulty/silent defects in interpreting it. But it smaller than 52 bits, so a signed 64 bit integer or double-precision float type are safe for storing this identifier. + * @method Message getPinnedMessage() Optional. Specified message was pinned. Note that the Message object in this field will not contain further reply_to_message fields even if it is itself a reply. + */ class Message extends Entity { /** - * @var mixed|null - */ - protected $message_id; - - /** - * @var \Longman\TelegramBot\Entities\User|null - */ - protected $from; - - /** - * @var mixed|null - */ - protected $date; - - /** - * @var \Longman\TelegramBot\Entities\Chat|null - */ - protected $chat; - - /** - * @var \Longman\TelegramBot\Entities\User|null - */ - protected $forward_from; - - /** - * @var \Longman\TelegramBot\Entities\Chat|null - */ - protected $forward_from_chat; - - /** - * @var mixed|null - */ - protected $forward_date; - - /** - * @var mixed|null - */ - protected $edit_date; - - /** - * @var \Longman\TelegramBot\Entities\ReplyToMessage - */ - protected $reply_to_message; - - /** - * @var string|null - */ - protected $text; - - /** - * @var \Longman\TelegramBot\Entities\Audio|null - */ - protected $audio; - - /** - * @var \Longman\TelegramBot\Entities\Document|null - */ - protected $document; - - /** - * @var array|null - */ - protected $photo; - - /** - * @var \Longman\TelegramBot\Entities\Sticker|null - */ - protected $sticker; - - /** - * @var \Longman\TelegramBot\Entities\Video|null - */ - protected $video; - - /** - * @var \Longman\TelegramBot\Entities\Voice|null - */ - protected $voice; - - /** - * @var mixed|null - */ - protected $caption; - - /** - * @var \Longman\TelegramBot\Entities\Contact|null - */ - protected $contact; - - /** - * @var \Longman\TelegramBot\Entities\Location|null - */ - protected $location; - - /** - * @var mixed|null - */ - protected $venue; - - /** - * @var \Longman\TelegramBot\Entities\User|null - */ - protected $new_chat_member; - - /** - * @var \Longman\TelegramBot\Entities\User|null - */ - protected $left_chat_member; - - /** - * @var mixed|null - */ - protected $new_chat_title; - - /** - * @var mixed|null - */ - protected $new_chat_photo; - - /** - * @var mixed|null + * {@inheritdoc} */ - protected $delete_chat_photo; - - /** - * @var mixed|null - */ - protected $group_chat_created; - - /** - * @var mixed|null - */ - protected $supergroup_chat_created; - - /** - * @var mixed|null - */ - protected $channel_chat_created; - - /** - * @var mixed|null - */ - protected $migrate_to_chat_id; - - /** - * @var mixed|null - */ - protected $migrate_from_chat_id; - - /** - * @var mixed|null - */ - protected $pinned_message; - - /** - * @var mixed|null - */ - protected $entities; - - /** - * @var mixed|null - */ - private $command; - - /** - * @var mixed|null - */ - private $type; - - /** - * Message constructor. - * - * @param array $data - * @param $bot_name - */ - public function __construct(array $data, $bot_name) + protected function subEntities() { - - $this->reply_to_message = isset($data['reply_to_message']) ? $data['reply_to_message'] : null; - if (!empty($this->reply_to_message)) { - $this->reply_to_message = new ReplyToMessage($this->reply_to_message, $bot_name); - } - - $this->init($data, $bot_name); + return [ + 'from' => User::class, + 'chat' => Chat::class, + 'forward_from' => User::class, + 'forward_from_chat' => User::class, + 'reply_to_message' => self::class, + 'entities' => MessageEntity::class, + 'audio' => Audio::class, + 'document' => Document::class, + 'photo' => PhotoSize::class, + 'sticker' => Sticker::class, + 'video' => Video::class, + 'voice' => Voice::class, + 'contact' => Contact::class, + 'location' => Location::class, + 'venue' => Venue::class, + 'new_chat_member' => User::class, + 'left_chat_member' => User::class, + 'new_chat_photo' => PhotoSize::class, + 'pinned_message' => Message::class, + ]; } /** - * Common init to Message and ReplyToMessage + * Message constructor * * @param array $data * @param string $bot_name + * * @throws \Longman\TelegramBot\Exception\TelegramException */ - protected function init(array &$data, $bot_name) + public function __construct(array $data, $bot_name = '') { - $this->bot_name = $bot_name; - - $this->type = 'Message'; - - $this->message_id = isset($data['message_id']) ? $data['message_id'] : null; - if (empty($this->message_id)) { - throw new TelegramException('message_id is empty!'); - } - - $this->from = isset($data['from']) ? $data['from'] : null; - if (!empty($this->from)) { - $this->from = new User($this->from); - } - - $this->chat = isset($data['chat']) ? $data['chat'] : null; - if (empty($this->chat)) { - throw new TelegramException('chat is empty!'); - } - $this->chat = new Chat($this->chat); - - $this->date = isset($data['date']) ? $data['date'] : null; - if (empty($this->date)) { - throw new TelegramException('date is empty!'); - } - - $this->forward_from = isset($data['forward_from']) ? $data['forward_from'] : null; - if (!empty($this->forward_from)) { - $this->forward_from = new User($this->forward_from); - } - - $this->forward_from_chat = isset($data['forward_from_chat']) ? $data['forward_from_chat'] : null; - if (!empty($this->forward_from_chat)) { - $this->forward_from_chat = new Chat($this->forward_from_chat); - } - - $this->forward_date = isset($data['forward_date']) ? $data['forward_date'] : null; - - $this->edit_date = isset($data['edit_date']) ? $data['edit_date'] : null; - - $this->text = isset($data['text']) ? $data['text'] : null; - $command = $this->getCommand(); - if (!empty($command)) { - $this->type = 'command'; - } - - $this->audio = isset($data['audio']) ? $data['audio'] : null; - if (!empty($this->audio)) { - $this->audio = new Audio($this->audio); - $this->type = 'Audio'; - } - - $this->document = isset($data['document']) ? $data['document'] : null; - if (!empty($this->document)) { - $this->document = new Document($this->document); - $this->type = 'Document'; - } - - $this->photo = isset($data['photo']) ? $data['photo'] : null; //array of photosize - if (!empty($this->photo)) { - foreach ($this->photo as $photo) { - if (!empty($photo)) { - $photos[] = new PhotoSize($photo); - } - } - $this->photo = $photos; - $this->type = 'Photo'; - } - - $this->sticker = isset($data['sticker']) ? $data['sticker'] : null; - if (!empty($this->sticker)) { - $this->sticker = new Sticker($this->sticker); - $this->type = 'Sticker'; - } - - $this->video = isset($data['video']) ? $data['video'] : null; - if (!empty($this->video)) { - $this->video = new Video($this->video); - $this->type = 'Video'; - } - - $this->voice = isset($data['voice']) ? $data['voice'] : null; - if (!empty($this->voice)) { - $this->voice = new Voice($this->voice); - $this->type = 'Voice'; - } - - $this->caption = isset($data['caption']) ? $data['caption'] : null;//string - - $this->contact = isset($data['contact']) ? $data['contact'] : null; - if (!empty($this->contact)) { - $this->contact = new Contact($this->contact); - } - - $this->location = isset($data['location']) ? $data['location'] : null; - if (!empty($this->location)) { - $this->location = new Location($this->location); - $this->type = 'Location'; - } - - $this->venue = isset($data['venue']) ? $data['venue'] : null; - if (!empty($this->venue)) { - $this->venue = new Venue($this->venue); - $this->type = 'Venue'; - } - - //retrocompatibility + //Retro-compatibility if (isset($data['new_chat_participant'])) { $data['new_chat_member'] = $data['new_chat_participant']; + unset($data['new_chat_participant']); } - if (isset($data['left_chat_participant'])) { $data['left_chat_member'] = $data['left_chat_participant']; + unset($data['left_chat_participant']); } - $this->new_chat_member = isset($data['new_chat_member']) ? $data['new_chat_member'] : null; - if (!empty($this->new_chat_member)) { - $this->new_chat_member = new User($this->new_chat_member); - $this->type = 'new_chat_member'; - } - - $this->left_chat_member = isset($data['left_chat_member']) ? $data['left_chat_member'] : null; - if (!empty($this->left_chat_member)) { - $this->left_chat_member = new User($this->left_chat_member); - $this->type = 'left_chat_member'; - } - - $this->new_chat_title = isset($data['new_chat_title']) ? $data['new_chat_title'] : null; - if (!is_null($this->new_chat_title)) { - $this->type = 'new_chat_title'; - } - - $this->new_chat_photo = isset($data['new_chat_photo']) ? $data['new_chat_photo'] : null; //array of photosize - if (!empty($this->new_chat_photo)) { - foreach ($this->new_chat_photo as $photo) { - if (!empty($photo)) { - $photos[] = new PhotoSize($photo); - } - } - $this->new_chat_photo = $photos; - $this->type = 'new_chat_photo'; - } - - $this->delete_chat_photo = isset($data['delete_chat_photo']) ? $data['delete_chat_photo'] : null; - if ($this->delete_chat_photo) { - $this->type = 'delete_chat_photo'; - } - - $this->group_chat_created = isset($data['group_chat_created']) ? $data['group_chat_created'] : null; - if ($this->group_chat_created) { - $this->type = 'group_chat_created'; - } + parent::__construct($data, $bot_name); + } - $this->supergroup_chat_created = isset($data['supergroup_chat_created']) ? $data['supergroup_chat_created'] : null; - if ($this->supergroup_chat_created) { - $this->type = 'supergroup_chat_created'; - } + /** + * Optional. Message is a photo, available sizes of the photo + * + * This method overrides the default getPhoto method + * and returns a nice array of PhotoSize objects. + * + * @return null|PhotoSize[] + */ + public function getPhoto() + { + $pretty_array = $this->makePrettyObjectArray(PhotoSize::class, 'photo'); - $this->channel_chat_created = isset($data['channel_chat_created']) ? $data['channel_chat_created'] : null; - if ($this->channel_chat_created) { - $this->type = 'channel_chat_created'; - } + return empty($pretty_array) ? null : $pretty_array; + } - $this->migrate_to_chat_id = isset($data['migrate_to_chat_id']) ? $data['migrate_to_chat_id'] : null; - if ($this->migrate_to_chat_id) { - $this->type = 'migrate_to_chat_id'; - } + /** + * Optional. A chat photo was changed to this value + * + * This method overrides the default getNewChatPhoto method + * and returns a nice array of PhotoSize objects. + * + * @return null|PhotoSize[] + */ + public function getNewChatPhoto() + { + $pretty_array = $this->makePrettyObjectArray(PhotoSize::class, 'new_chat_photo'); - $this->migrate_from_chat_id = isset($data['migrate_from_chat_id']) ? $data['migrate_from_chat_id'] : null; - if ($this->migrate_from_chat_id) { - $this->type = 'migrate_from_chat_id'; - } + return empty($pretty_array) ? null : $pretty_array; + } - $this->pinned_message = isset($data['pinned_message']) ? $data['pinned_message'] : null; - if ($this->pinned_message) { - $this->pinned_message = new Message($this->pinned_message, $this->getBotName()); - } + /** + * Optional. For text messages, special entities like usernames, URLs, bot commands, etc. that appear in the text + * + * This method overrides the default getEntities method + * and returns a nice array of MessageEntity objects. + * + * @return null|MessageEntity[] + */ + public function getEntities() + { + $pretty_array = $this->makePrettyObjectArray(MessageEntity::class, 'entities'); - $this->entities = isset($data['entities']) ? $data['entities'] : null; - if (!empty($this->entities)) { - foreach ($this->entities as $entity) { - if (!empty($entity)) { - $entities[] = new MessageEntity($entity); - } - } - $this->entities = $entities; - } + return empty($pretty_array) ? null : $pretty_array; } /** * return the entire command like /echo or /echo@bot1 if specified * - * @return string|void + * @return string|null */ public function getFullCommand() { - if (substr($this->text, 0, 1) === '/') { - $no_EOL = strtok($this->text, PHP_EOL); - $no_space = strtok($this->text, ' '); + $text = $this->getProperty('text'); + if (strpos($text, '/') === 0) { + $no_EOL = strtok($text, PHP_EOL); + $no_space = strtok($text, ' '); //try to understand which separator \n or space divide /command from text - if (strlen($no_space) < strlen($no_EOL)) { - return $no_space; - } else { - return $no_EOL; - } - } else { - return; + return strlen($no_space) < strlen($no_EOL) ? $no_space : $no_EOL; } + + return null; } /** @@ -427,25 +168,26 @@ public function getFullCommand() */ public function getCommand() { - if (!empty($this->command)) { - return $this->command; + $command = $this->getProperty('command'); + if (!empty($command)) { + return $command; } $cmd = $this->getFullCommand(); - if (substr($cmd, 0, 1) === '/') { + if (strpos($cmd, '/') === 0) { $cmd = substr($cmd, 1); //check if command is follow by botname $split_cmd = explode('@', $cmd); if (isset($split_cmd[1])) { //command is followed by name check if is addressed to me - if (strtolower($split_cmd[1]) == strtolower($this->bot_name)) { - return $this->command = $split_cmd[0]; + if (strtolower($split_cmd[1]) === strtolower($this->bot_name)) { + return $split_cmd[0]; } } else { //command is not followed by name - return $this->command = $cmd; + return $cmd; } } @@ -453,104 +195,15 @@ public function getCommand() } /** - * Get message id - * - * @return mixed - */ - public function getMessageId() - { - return $this->message_id; - } - - /** - * Get User object related to the message - * - * @return \Longman\TelegramBot\Entities\User - */ - public function getFrom() - { - return $this->from; - } - - /** - * Get date - * - * @return mixed - */ - public function getDate() - { - return $this->date; - } - - /** - * Get User object related to the message - * - * @return \Longman\TelegramBot\Entities\Chat - */ - public function getChat() - { - return $this->chat; - } - - /** - * Get User object related to the forwarded message - * - * @return \Longman\TelegramBot\Entities\User - */ - public function getForwardFrom() - { - return $this->forward_from; - } - - /** - * Get User object related to the message - * - * @return \Longman\TelegramBot\Entities\Chat - */ - public function getForwardFromChat() - { - return $this->forward_from_chat; - } - - /** - * Get forward date - * - * @return mixed - */ - public function getForwardDate() - { - return $this->forward_date; - } - - /** - * Get edit date - * - * @return mixed - */ - public function getEditDate() - { - return $this->edit_date; - } - - /** - * Get reply to message - * - * @return \Longman\TelegramBot\Entities\ReplyToMessage - */ - public function getReplyToMessage() - { - return $this->reply_to_message; - } - - /** - * Get text + * For text messages, the actual UTF-8 text of the message, 0-4096 characters. * * @param bool $without_cmd + * * @return string */ public function getText($without_cmd = false) { - $text = $this->text; + $text = $this->getProperty('text'); if ($without_cmd && $command = $this->getFullCommand()) { if (strlen($command) + 1 < strlen($text)) { @@ -563,226 +216,6 @@ public function getText($without_cmd = false) return $text; } - /** - * Get audio - * - * @return \Longman\TelegramBot\Entities\Audio - */ - public function getAudio() - { - return $this->audio; - } - - /** - * Get document - * - * @return \Longman\TelegramBot\Entities\Document - */ - public function getDocument() - { - return $this->document; - } - - /** - * Get photo - * - * @return array - */ - public function getPhoto() - { - return $this->photo; - } - - /** - * Get sticker - * - * @return \Longman\TelegramBot\Entities\Sticker - */ - public function getSticker() - { - return $this->sticker; - } - - /** - * Get video - * - * @return \Longman\TelegramBot\Entities\Video - */ - public function getVideo() - { - return $this->video; - } - - /** - * Get voice - * - * @return \Longman\TelegramBot\Entities\Voice - */ - public function getVoice() - { - return $this->voice; - } - - /** - * Get caption - * - * @return mixed - */ - public function getCaption() - { - return $this->caption; - } - - /** - * Get content - * - * @return \Longman\TelegramBot\Entities\Contact - */ - public function getContact() - { - return $this->contact; - } - - /** - * Get location - * - * @return \Longman\TelegramBot\Entities\Location - */ - public function getLocation() - { - return $this->location; - } - - /** - * Get venue - * - * @return mixed - */ - public function getVenue() - { - return $this->venue; - } - - /** - * Get new chat participant - * - * @return mixed - */ - public function getNewChatParticipant() - { - return $this->new_chat_member; - } - - /** - * Get left chat participant - * - * @return mixed - */ - public function getLeftChatParticipant() - { - return $this->left_chat_member; - } - - /** - * Get User object related to the new member - * - * @return \Longman\TelegramBot\Entities\User - */ - public function getNewChatMember() - { - return $this->new_chat_member; - } - - /** - * Get User object related to the left member - * - * @return \Longman\TelegramBot\Entities\User - */ - public function getLeftChatMember() - { - return $this->left_chat_member; - } - - /** - * Get new chat title - * - * @return mixed - */ - public function getNewChatTitle() - { - return $this->new_chat_title; - } - - /** - * Get new chat photo - * - * @return mixed - */ - public function getNewChatPhoto() - { - return $this->new_chat_photo; - } - - /** - * Get delete chat photo - * - * @return mixed - */ - public function getDeleteChatPhoto() - { - return $this->delete_chat_photo; - } - - /** - * Get group chat created - * - * @return mixed - */ - public function getGroupChatCreated() - { - return $this->group_chat_created; - } - - /** - * Get supergroup chat created - * - * @return mixed - */ - public function getSupergroupChatCreated() - { - return $this->supergroup_chat_created; - } - - /** - * Get channel chat created - * - * @return mixed - */ - public function getChannelChatCreated() - { - return $this->channel_chat_created; - } - - /** - * Get migrate to chat id - * - * @return mixed - */ - public function getMigrateToChatId() - { - return $this->migrate_to_chat_id; - } - - /** - * Get migrate from chat id - * - * @return mixed - */ - public function getMigrateFromChatId() - { - return $this->migrate_from_chat_id; - } - /** * Bot added in chat * @@ -790,42 +223,52 @@ public function getMigrateFromChatId() */ public function botAddedInChat() { - if (!empty($this->new_chat_member)) { - if ($this->new_chat_member->getUsername() == $this->getBotName()) { - return true; - } - } + $member = $this->getNewChatMember(); - return false; + return $member !== null && $member->getUsername() === $this->getBotName(); } /** - * Get type + * Detect type based on properties. * - * @return mixed + * @return string|null */ public function getType() { - return $this->type; - } + $types = [ + 'text', + 'audio', + 'document', + 'photo', + 'sticker', + 'video', + 'voice', + 'contact', + 'location', + 'venue', + 'new_chat_member', + 'left_chat_member', + 'new_chat_title', + 'new_chat_photo', + 'delete_chat_photo', + 'group_chat_created', + 'supergroup_chat_created', + 'channel_chat_created', + 'migrate_to_chat_id', + 'migrate_from_chat_id', + 'pinned_message', + ]; + + foreach ($types as $type) { + if ($this->getProperty($type)) { + if ($type === 'text' && $this->getCommand()) { + return 'command'; + } - /** - * Get pinned message - * - * @return mixed - */ - public function getPinnedMessage() - { - return $this->pinned_message; - } + return $type; + } + } - /** - * Get entities - * - * @return mixed - */ - public function getEntities() - { - return $this->entities; + return 'message'; } } diff --git a/src/Entities/MessageEntity.php b/src/Entities/MessageEntity.php index 3e6ce3259..7ab9c5976 100644 --- a/src/Entities/MessageEntity.php +++ b/src/Entities/MessageEntity.php @@ -10,111 +10,26 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - +/** + * Class MessageEntity + * + * @link https://core.telegram.org/bots/api#messageentity + * + * @method string getType() Type of the entity. Can be mention (@username), hashtag, bot_command, url, email, bold (bold text), italic (italic text), code (monowidth string), pre (monowidth block), text_link (for clickable text URLs), text_mention (for users without usernames) + * @method int getOffset() Offset in UTF-16 code units to the start of the entity + * @method int getLength() Length of the entity in UTF-16 code units + * @method string getUrl() Optional. For "text_link" only, url that will be opened after user taps on the text + * @method User getUser() Optional. For "text_mention" only, the mentioned user + */ class MessageEntity extends Entity { /** - * @var mixed|null - */ - protected $type; - - /** - * @var mixed|null - */ - protected $offset; - - /** - * @var mixed|null - */ - protected $length; - - /** - * @var mixed|null - */ - protected $url; - - /** - * @var \Longman\TelegramBot\Entities\User|null - */ - protected $user; - - /** - * MessageEntity constructor. - * - * @TODO check for type value from this list: https://core.telegram.org/bots/api#messageentity - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - $this->type = isset($data['type']) ? $data['type'] : null; - if (empty($this->type)) { - throw new TelegramException('type is empty!'); - } - - $this->offset = isset($data['offset']) ? $data['offset'] : null; - if ($this->offset === '') { - throw new TelegramException('offset is empty!'); - } - - $this->length = isset($data['length']) ? $data['length'] : null; - if ($this->length === '') { - throw new TelegramException('length is empty!'); - } - - $this->url = isset($data['url']) ? $data['url'] : null; - $this->user = isset($data['user']) ? new User($data['user']) : null; - } - - /** - * Get type - * - * @return mixed|null - */ - public function getType() - { - return $this->type; - } - - /** - * Get offset - * - * @return mixed|null - */ - public function getOffset() - { - return $this->offset; - } - - /** - * Get length - * - * @return mixed|null - */ - public function getLength() - { - return $this->length; - } - - /** - * Get url - * - * @return mixed|null - */ - public function getUrl() - { - return $this->url; - } - - /** - * Get user - * - * @return \Longman\TelegramBot\Entities\User|null + * {@inheritdoc} */ - public function getUser() + protected function subEntities() { - return $this->user; + return [ + 'user' => User::class, + ]; } } diff --git a/src/Entities/PhotoSize.php b/src/Entities/PhotoSize.php index 6331220e0..5b577afca 100644 --- a/src/Entities/PhotoSize.php +++ b/src/Entities/PhotoSize.php @@ -10,94 +10,17 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - +/** + * Class PhotoSize + * + * @link https://core.telegram.org/bots/api#photosize + * + * @method string getFileId() Unique identifier for this file + * @method int getWidth() Photo width + * @method int getHeight() Photo height + * @method int getFileSize() Optional. File size + */ class PhotoSize extends Entity { - /** - * @var mixed|null - */ - protected $file_id; - - /** - * @var mixed|null - */ - protected $width; - - /** - * @var mixed|null - */ - protected $height; - - /** - * @var mixed|null - */ - protected $file_size; - - /** - * PhotoSize constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - - $this->file_id = isset($data['file_id']) ? $data['file_id'] : null; - if (empty($this->file_id)) { - throw new TelegramException('file_id is empty!'); - } - - $this->width = isset($data['width']) ? $data['width'] : null; - if (empty($this->width)) { - throw new TelegramException('width is empty!'); - } - - $this->height = isset($data['height']) ? $data['height'] : null; - if (empty($this->height)) { - throw new TelegramException('height is empty!'); - } - - $this->file_size = isset($data['file_size']) ? $data['file_size'] : null; - } - - /** - * Get file id - * - * @return mixed|null - */ - public function getFileId() - { - return $this->file_id; - } - - /** - * Get width - * - * @return mixed|null - */ - public function getWidth() - { - return $this->width; - } - - /** - * Get height - * - * @return mixed|null - */ - public function getHeight() - { - return $this->height; - } - /** - * Get file size - * - * @return mixed|null - */ - public function getFileSize() - { - return $this->file_size; - } } diff --git a/src/Entities/ReplyKeyboardHide.php b/src/Entities/ReplyKeyboardHide.php deleted file mode 100644 index f3c3e88c4..000000000 --- a/src/Entities/ReplyKeyboardHide.php +++ /dev/null @@ -1,37 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - * - * Written by Marco Boretto - */ - -namespace Longman\TelegramBot\Entities; - -class ReplyKeyboardHide extends Entity -{ - /** - * @var bool - */ - protected $hide_keyboard; - - /** - * @var bool - */ - protected $selective; - - /** - * ReplyKeyboardHide constructor. - * - * @param array|null $data - */ - public function __construct(array $data = null) - { - $this->hide_keyboard = true; - $this->selective = isset($data['selective']) ? $data['selective'] : false; - } -} diff --git a/src/Entities/ReplyKeyboardMarkup.php b/src/Entities/ReplyKeyboardMarkup.php deleted file mode 100644 index 5271ce66a..000000000 --- a/src/Entities/ReplyKeyboardMarkup.php +++ /dev/null @@ -1,68 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - * - * Written by Marco Boretto - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class ReplyKeyboardMarkup extends Entity -{ - /** - * @var array - */ - protected $keyboard; - - /** - * @var bool - */ - protected $resize_keyboard; - - /** - * @var bool - */ - protected $one_time_keyboard; - - /** - * @var bool - */ - protected $selective; - - /** - * ReplyKeyboardMarkup constructor. - * - * @param array $data - * - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data = []) - { - if (!isset($data['keyboard'])) { - throw new TelegramException('Keyboard field is empty!'); - } - - if (!is_array($data['keyboard'])) { - throw new TelegramException('Keyboard field is not an array!'); - } - - foreach ($data['keyboard'] as $item) { - if (!is_array($item)) { - throw new TelegramException('Keyboard subfield is not an array!'); - } - } - $this->keyboard = $data['keyboard']; - - //Set the object members from the passed data params - foreach (['resize_keyboard', 'one_time_keyboard', 'selective'] as $param) { - $this->$param = isset($data[$param]) ? (bool)$data[$param] : false; - } - } -} diff --git a/src/Entities/ReplyToMessage.php b/src/Entities/ReplyToMessage.php index 92c6d947c..8e55e36e7 100644 --- a/src/Entities/ReplyToMessage.php +++ b/src/Entities/ReplyToMessage.php @@ -10,22 +10,27 @@ namespace Longman\TelegramBot\Entities; +/** + * Class ReplyToMessage + * + * @todo Is this even required?! + */ class ReplyToMessage extends Message { - /** * ReplyToMessage constructor. * * @param array $data * @param string $bot_name + * + * @throws \Longman\TelegramBot\Exception\TelegramException */ - public function __construct(array $data, $bot_name) + public function __construct(array $data, $bot_name = '') { - //As explained in the documentation //Reply to message can't contain other reply to message entities - $reply_to_message = null; + unset($data['reply_to_message']); - $this->init($data, $bot_name); + parent::__construct($data, $bot_name); } } diff --git a/src/Entities/ServerResponse.php b/src/Entities/ServerResponse.php index 982fe8e4c..49e7161af 100644 --- a/src/Entities/ServerResponse.php +++ b/src/Entities/ServerResponse.php @@ -8,116 +8,60 @@ namespace Longman\TelegramBot\Entities; +/** + * Class ServerResponse + * + * @link https://core.telegram.org/bots/api#making-requests + * + * @method bool getOk() If the request was successful + * @method mixed getResult() The result of the query + * @method int getErrorCode() Error code of the unsuccessful request + * @method string getDescription() Human-readable description of the result / unsuccessful request + * + * @todo method ResponseParameters getParameters() Field which can help to automatically handle the error + */ class ServerResponse extends Entity { - /** - * @var bool - */ - protected $ok; - - /** - * @var null - */ - protected $result; - - /** - * @var null - */ - protected $error_code; - - /** - * @var null - */ - protected $description; - /** * ServerResponse constructor. * - * @param array $data - * @param $bot_name + * @param array $data + * @param string $bot_name * * @throws \Longman\TelegramBot\Exception\TelegramException */ public function __construct(array $data, $bot_name) { - if (isset($data['ok'], $data['result'])) { - if (is_array($data['result'])) { - if ($data['ok'] && !$this->isAssoc($data['result']) && !isset($data['result'][0]['user'])) { - //Get Update - foreach ($data['result'] as $update) { - $this->result[] = new Update($update, $bot_name); - } - } elseif ($data['ok'] && !$this->isAssoc($data['result']) && isset($data['result'][0]['user'])) { - //Response from getChatAdministrators - $this->result = []; - foreach ($data['result'] as $user) { - $this->result[] = new ChatMember($user); - } - } elseif ($data['ok'] && $this->isAssoc($data['result'])) { - if (isset($data['result']['total_count'])) { - //Response from getUserProfilePhotos - $this->result = new UserProfilePhotos($data['result']); - } elseif (isset($data['result']['file_id'])) { - //Response from getFile - $this->result = new File($data['result']); - } elseif (isset($data['result']['username'])) { - //Response from getMe - $this->result = new User($data['result']); - } elseif (isset($data['result']['id'])) { - //Response from getChat - $this->result = new Chat($data['result']); - } elseif (isset($data['result']['user'])) { - //Response from getChatMember - $this->result = new ChatMember($data['result']); - } elseif (isset($data['result']['has_custom_certificate'])) { - //Response from getWebhookInfo - $this->result = new WebhookInfo($data['result']); - } else { - //Response from sendMessage - $this->result = new Message($data['result'], $bot_name); - } - } - - $this->ok = $data['ok']; - $this->error_code = null; - $this->description = null; + // Make sure we don't double-save the raw_data + unset($data['raw_data']); + $data['raw_data'] = $data; + + $is_ok = isset($data['ok']) ? (bool)$data['ok'] : false; + $result = isset($data['result']) ? $data['result'] : null; + + if ($is_ok && is_array($result)) { + if ($this->isAssoc($result)) { + $data['result'] = $this->createResultObject($result, $bot_name); } else { - if ($data['ok'] && $data['result'] === true) { - //Response from setWebhook set - $this->ok = $data['ok']; - $this->result = true; - $this->error_code = null; - $this->description = isset($data['description']) ? $data['description'] : ''; - } elseif (is_numeric($data['result'])) { - //Response from getChatMembersCount - $this->result = $data['result']; - } else { - $this->ok = false; - $this->result = null; - $this->error_code = $data['error_code']; - $this->description = $data['description']; - } + $data['result'] = $this->createResultObjects($result, $bot_name); } - } else { - //webHook not set - $this->ok = false; - $this->result = isset($data['result']) ? $data['result'] : null; - $this->error_code = isset($data['error_code']) ? $data['error_code'] : null; - $this->description = isset($data['description']) ? $data['description'] : null; - - //throw new TelegramException('ok(variable) is not set!'); } + + parent::__construct($data, $bot_name); } /** * Check if array is associative * + * @link https://stackoverflow.com/a/4254008 + * * @param array $array + * * @return bool */ protected function isAssoc(array $array) { - return (bool) count(array_filter(array_keys($array), 'is_string')); + return count(array_filter(array_keys($array), 'is_string')) > 0; } /** @@ -127,46 +71,83 @@ protected function isAssoc(array $array) */ public function isOk() { - return $this->ok; + return (bool)$this->getOk(); } /** - * Get result + * Print error * * @return string */ - public function getResult() + public function printError() { - return $this->result; + return 'Error N: ' . $this->getErrorCode() . ' Description: ' . $this->getDescription(); } /** - * Get error code + * Create and return the object of the received result * - * @return string - */ - public function getErrorCode() - { - return $this->error_code; - } - - /** - * Get description + * @param array $result + * @param string $bot_name * - * @return null + * @return \Longman\TelegramBot\Entities\Chat|\Longman\TelegramBot\Entities\ChatMember|\Longman\TelegramBot\Entities\File|\Longman\TelegramBot\Entities\Message|\Longman\TelegramBot\Entities\User|\Longman\TelegramBot\Entities\UserProfilePhotos|\Longman\TelegramBot\Entities\WebhookInfo + * @throws \Longman\TelegramBot\Exception\TelegramException */ - public function getDescription() + private function createResultObject($result, $bot_name) { - return $this->description; + // We don't need to save the raw_data of the response object! + $result['raw_data'] = null; + + $result_object_types = [ + 'total_count' => 'UserProfilePhotos', //Response from getUserProfilePhotos + 'file_id' => 'File', //Response from getFile + 'username' => 'User', //Response from getMe + 'id' => 'Chat', //Response from getChat + 'user' => 'ChatMember', //Response from getChatMember + 'url' => 'WebhookInfo', //Response from getWebhookInfo + ]; + foreach ($result_object_types as $type => $object_class) { + if (isset($result[$type])) { + $object_class = __NAMESPACE__ . '\\' . $object_class; + + return new $object_class($result); + } + } + + //Response from sendMessage + return new Message($result, $bot_name); } /** - * Print error + * Create and return the objects array of the received result * - * @return string + * @param array $result + * @param string $bot_name + * + * @return null|\Longman\TelegramBot\Entities\ChatMember[]|\Longman\TelegramBot\Entities\Update[] + * @throws \Longman\TelegramBot\Exception\TelegramException */ - public function printError() + private function createResultObjects($result, $bot_name) { - return 'Error N: ' . $this->getErrorCode() . ' Description: ' . $this->getDescription(); + $results = []; + if (isset($result[0]['user'])) { + //Response from getChatAdministrators + foreach ($result as $user) { + // We don't need to save the raw_data of the response object! + $user['raw_data'] = null; + + $results[] = new ChatMember($user); + } + } else { + //Get Update + foreach ($result as $update) { + // We don't need to save the raw_data of the response object! + $update['raw_data'] = null; + + $results[] = new Update($update, $bot_name); + } + } + + return $results; } } diff --git a/src/Entities/Sticker.php b/src/Entities/Sticker.php index fa770114c..2eebeb3a6 100644 --- a/src/Entities/Sticker.php +++ b/src/Entities/Sticker.php @@ -10,132 +10,27 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - +/** + * Class Sticker + * + * @link https://core.telegram.org/bots/api#sticker + * + * @method string getFileId() Unique identifier for this file + * @method int getWidth() Sticker width + * @method int getHeight() Sticker height + * @method PhotoSize getThumb() Optional. Sticker thumbnail in .webp or .jpg format + * @method string getEmoji() Optional. Emoji associated with the sticker + * @method int getFileSize() Optional. File size + */ class Sticker extends Entity { /** - * @var mixed|null - */ - protected $file_id; - - /** - * @var mixed|null - */ - protected $width; - - /** - * @var mixed|null - */ - protected $height; - - /** - * @var \Longman\TelegramBot\Entities\PhotoSize - */ - protected $thumb; - - /** - * @var mixed|null - */ - protected $emoji; - - /** - * @var mixed|null - */ - protected $file_size; - - /** - * Sticker constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - - $this->file_id = isset($data['file_id']) ? $data['file_id'] : null; - if (empty($this->file_id)) { - throw new TelegramException('file_id is empty!'); - } - - $this->width = isset($data['width']) ? $data['width'] : null; - if (empty($this->width)) { - throw new TelegramException('width is empty!'); - } - - $this->height = isset($data['height']) ? $data['height'] : null; - if (empty($this->height)) { - throw new TelegramException('height is empty!'); - } - - $this->thumb = isset($data['thumb']) ? $data['thumb'] : null; - if (empty($this->thumb)) { - throw new TelegramException('thumb is empty!'); - } - $this->thumb = new PhotoSize($this->thumb); - - $this->emoji = isset($data['emoji']) ? $data['emoji'] : null; - - $this->file_size = isset($data['file_size']) ? $data['file_size'] : null; - } - - /** - * Get file id - * - * @return mixed|null - */ - public function getFileId() - { - return $this->file_id; - } - - /** - * Get width - * - * @return mixed|null - */ - public function getWidth() - { - return $this->width; - } - - /** - * Get height - * - * @return mixed|null - */ - public function getHeight() - { - return $this->height; - } - - /** - * Get thumb - * - * @return \Longman\TelegramBot\Entities\PhotoSize - */ - public function getThumb() - { - return $this->thumb; - } - - /** - * Get emoji - * - * @return mixed|null - */ - public function getEmoji() - { - return $this->emoji; - } - - /** - * Get file size - * - * @return mixed|null + * {@inheritdoc} */ - public function getFileSize() + protected function subEntities() { - return $this->file_size; + return [ + 'thumb' => PhotoSize::class, + ]; } } diff --git a/src/Entities/Update.php b/src/Entities/Update.php index 5a4fe2e12..845b653f2 100644 --- a/src/Entities/Update.php +++ b/src/Entities/Update.php @@ -10,183 +10,71 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - +/** + * Class Update + * + * @link https://core.telegram.org/bots/api#update + * + * @method int getUpdateId() The update's unique identifier. Update identifiers start from a certain positive number and increase sequentially. This ID becomes especially handy if you’re using Webhooks, since it allows you to ignore repeated updates or to restore the correct update sequence, should they get out of order. + * @method Message getMessage() Optional. New incoming message of any kind — text, photo, sticker, etc. + * @method Message getEditedMessage() Optional. New version of a message that is known to the bot and was edited + * @method InlineQuery getInlineQuery() Optional. New incoming inline query + * @method ChosenInlineResult getChosenInlineResult() Optional. The result of an inline query that was chosen by a user and sent to their chat partner. + * @method CallbackQuery getCallbackQuery() Optional. New incoming callback query + */ class Update extends Entity { /** - * @var mixed|null - */ - protected $update_id; - - /** - * @var \Longman\TelegramBot\Entities\Message - */ - protected $message; - - /** - * @var \Longman\TelegramBot\Entities\Message + * {@inheritdoc} */ - protected $edited_message; - - /** - * @var \Longman\TelegramBot\Entities\InlineQuery - */ - protected $inline_query; - - /** - * @var \Longman\TelegramBot\Entities\ChosenInlineResult - */ - protected $chosen_inline_result; - - /** - * @var \Longman\TelegramBot\Entities\CallbackQuery - */ - protected $callback_query; - - /** - * @var string - */ - private $update_type; - - /** - * Update constructor. - * - * @param array $data - * @param $bot_name - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data, $bot_name) + protected function subEntities() { - - $this->bot_name = $bot_name; - - $update_id = isset($data['update_id']) ? $data['update_id'] : null; - $this->update_id = $update_id; - - $this->message = isset($data['message']) ? $data['message'] : null; - if (!empty($this->message)) { - $this->message = new Message($this->message, $bot_name); - $this->update_type = 'message'; - } - - $this->edited_message = isset($data['edited_message']) ? $data['edited_message'] : null; - if (!empty($this->edited_message)) { - $this->edited_message = new Message($this->edited_message, $bot_name); - $this->update_type = 'edited_message'; - } - - if (empty($update_id)) { - throw new TelegramException('update_id is empty!'); - } - - $this->inline_query = isset($data['inline_query']) ? $data['inline_query'] : null; - if (!empty($this->inline_query)) { - $this->inline_query = new InlineQuery($this->inline_query); - $this->update_type = 'inline_query'; - } - - $this->chosen_inline_result = isset($data['chosen_inline_result']) ? $data['chosen_inline_result'] : null; - if (!empty($this->chosen_inline_result)) { - $this->chosen_inline_result = new ChosenInlineResult($this->chosen_inline_result); - $this->update_type = 'chosen_inline_result'; - } - - $this->callback_query = isset($data['callback_query']) ? $data['callback_query'] : null; - if (!empty($this->callback_query)) { - $this->callback_query = new CallbackQuery($this->callback_query); - $this->update_type = 'callback_query'; - } + return [ + 'message' => Message::class, + 'edited_message' => EditedMessage::class, + 'inline_query' => InlineQuery::class, + 'chosen_inline_result' => ChosenInlineResult::class, + 'callback_query' => CallbackQuery::class, + ]; } /** - * Get update id + * Get the update type based on the set properties * - * @return mixed|null - */ - public function getUpdateId() - { - return $this->update_id; - } - - /** - * Get message - * - * @return \Longman\TelegramBot\Entities\Message - */ - public function getMessage() - { - return $this->message; - } - - /** - * Get edited message - * - * @return \Longman\TelegramBot\Entities\Message - */ - public function getEditedMessage() - { - return $this->edited_message; - } - - /** - * Get inline query - * - * @return \Longman\TelegramBot\Entities\InlineQuery - */ - public function getInlineQuery() - { - return $this->inline_query; - } - - /** - * Get callback query - * - * @return \Longman\TelegramBot\Entities\CallbackQuery - */ - public function getCallbackQuery() - { - return $this->callback_query; - } - - /** - * Get chosen inline result - * - * @return \Longman\TelegramBot\Entities\ChosenInlineResult - */ - public function getChosenInlineResult() - { - return $this->chosen_inline_result; - } - - /** - * Get update type - * - * @return string + * @return string|null */ public function getUpdateType() { - return $this->update_type; + $types = [ + 'message', + 'edited_message', + 'inline_query', + 'chosen_inline_result', + 'callback_query', + ]; + foreach ($types as $type) { + if ($this->getProperty($type)) { + return $type; + } + } + + return null; } /** * Get update content * * @return \Longman\TelegramBot\Entities\CallbackQuery - * |\Longman\TelegramBot\Entities\ChosenInlineResult - * |\Longman\TelegramBot\Entities\InlineQuery - * |\Longman\TelegramBot\Entities\Message + * |\Longman\TelegramBot\Entities\ChosenInlineResult + * |\Longman\TelegramBot\Entities\InlineQuery + * |\Longman\TelegramBot\Entities\Message */ public function getUpdateContent() { - if ($this->update_type == 'message') { - return $this->getMessage(); - } elseif ($this->update_type == 'inline_query') { - return $this->getInlineQuery(); - } elseif ($this->update_type == 'chosen_inline_result') { - return $this->getChosenInlineResult(); - } elseif ($this->update_type == 'callback_query') { - return $this->getCallbackQuery(); + if ($update_type = $this->getUpdateType()) { + return $this->getProperty($update_type); } + + return null; } } diff --git a/src/Entities/User.php b/src/Entities/User.php index b0ef6c7ca..ee1780322 100644 --- a/src/Entities/User.php +++ b/src/Entities/User.php @@ -10,103 +10,22 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - +/** + * Class User + * + * @link https://core.telegram.org/bots/api#user + * + * @property int $id Unique identifier for this user or bot + * @property string $first_name User's or bot’s first name + * @property string $last_name Optional. User's or bot’s last name + * @property string $username Optional. User's or bot’s username + * + * @method int getId() Unique identifier for this user or bot + * @method string getFirstName() User's or bot’s first name + * @method string getLastName() Optional. User's or bot’s last name + * @method string getUsername() Optional. User's or bot’s username + */ class User extends Entity { - /** - * @var mixed|null - */ - protected $id; - - /** - * @var mixed|null - */ - protected $first_name; - - /** - * @var mixed|null - */ - protected $last_name; - - /** - * @var mixed|null - */ - protected $username; - - /** - * User constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - - $this->id = isset($data['id']) ? $data['id'] : null; - if (empty($this->id)) { - throw new TelegramException('id is empty!'); - } - - $this->first_name = isset($data['first_name']) ? $data['first_name'] : null; - - $this->last_name = isset($data['last_name']) ? $data['last_name'] : null; - $this->username = isset($data['username']) ? $data['username'] : null; - } - - /** - * Get id - * - * @return mixed|null - */ - public function getId() - { - return $this->id; - } - - /** - * Get first name - * - * @return mixed|null - */ - public function getFirstName() - { - return $this->first_name; - } - - /** - * Get last name - * - * @return mixed|null - */ - public function getLastName() - { - return $this->last_name; - } - - /** - * Get username - * - * @return mixed|null - */ - public function getUsername() - { - return $this->username; - } - /** - * Try nebtion - * - * @return mixed|null|string - */ - public function tryMention() - { - if (is_null($this->username)) { - if (!is_null($this->last_name)) { - return $this->first_name . ' ' . $this->last_name; - } - return $this->first_name; - } - return '@' . $this->username; - } } diff --git a/src/Entities/UserProfilePhotos.php b/src/Entities/UserProfilePhotos.php index 592ee5727..29c3d4d4a 100644 --- a/src/Entities/UserProfilePhotos.php +++ b/src/Entities/UserProfilePhotos.php @@ -10,69 +10,46 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - +/** + * Class UserProfilePhotos + * + * @link https://core.telegram.org/bots/api#userprofilephotos + * + * @method int getTotalCount() Total number of profile pictures the target user has + */ class UserProfilePhotos extends Entity { /** - * @var mixed|null - */ - protected $total_count; - - /** - * @var array + * {@inheritdoc} */ - protected $photos; + protected function subEntities() + { + return [ + 'photos' => PhotoSize::class, + ]; + } /** - * UserProfilePhotos constructor. + * Requested profile pictures (in up to 4 sizes each) + * + * This method overrides the default getPhotos method and returns a nice array * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException + * @return PhotoSize[] */ - public function __construct(array $data) + public function getPhotos() { + $all_photos = []; - $this->total_count = isset($data['total_count']) ? $data['total_count'] : null; - if ($this->total_count === null || !is_numeric($this->total_count)) { - throw new TelegramException('total_count is empty!'); - } - $this->photos = isset($data['photos']) ? $data['photos'] : null; - if ($this->photos === null || !is_array($data['photos'])) { - throw new TelegramException('photos is empty!'); - } - - $photos = []; - foreach ($this->photos as $key => $photo) { - if (is_array($photo)) { - foreach ($photo as $photo_size) { - $photos[$key][] = new PhotoSize($photo_size); + if ($these_photos = $this->getProperty('photos')) { + foreach ($these_photos as $photos) { + $new_photos = []; + foreach ($photos as $photo) { + $new_photos[] = new PhotoSize($photo); } - } else { - throw new TelegramException('photo is not an array!'); + $all_photos[] = $new_photos; } } - $this->photos = $photos; - } - - /** - * Get total count - * - * @return mixed|null - */ - public function getTotalCount() - { - return $this->total_count; - } - - /** - * Get photos - * - * @return array - */ - public function getPhotos() - { - return $this->photos; + return $all_photos; } } diff --git a/src/Entities/Venue.php b/src/Entities/Venue.php index 3dc6cc708..46069c0b5 100644 --- a/src/Entities/Venue.php +++ b/src/Entities/Venue.php @@ -10,135 +10,25 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - +/** + * Class Venue + * + * @link https://core.telegram.org/bots/api#venue + * + * @method Location getLocation() Venue location + * @method string getTitle() Name of the venue + * @method string getAddress() Address of the venue + * @method string getFoursquareId() Optional. Foursquare identifier of the venue + */ class Venue extends Entity { /** - * @var \Longman\TelegramBot\Entities\Location - */ - protected $location; - - /** - * @var mixed|null - */ - protected $latitude; - - /** - * @var mixed|null - */ - protected $longitude; - - /** - * @var mixed|null - */ - protected $title; - - /** - * @var mixed|null - */ - protected $address; - - /** - * @var mixed|null - */ - protected $foursquare_id; - - /** - * Venue constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - $this->location = isset($data['location']) ? $data['location'] : null; - $this->latitude = isset($data['latitude']) ? $data['latitude'] : null; - $this->longitude = isset($data['longitude']) ? $data['longitude'] : null; - - // Venue can either contain location object or just latitude and longitude fields, check accordingly - if (!empty($this->location)) { - $this->location = new Location($this->location); - } else { - if (empty($this->latitude)) { - throw new TelegramException('latitude is empty!'); - } - - if (empty($this->longitude)) { - throw new TelegramException('longitude is empty!'); - } - } - - $this->title = isset($data['title']) ? $data['title'] : null; - if (empty($this->title)) { - throw new TelegramException('title is empty!'); - } - - $this->address = isset($data['address']) ? $data['address'] : null; - if (empty($this->address)) { - throw new TelegramException('address is empty!'); - } - - $this->foursquare_id = isset($data['foursquare_id']) ? $data['foursquare_id'] : null; - } - - /** - * Get location - * - * @return \Longman\TelegramBot\Entities\Location - */ - public function getLocation() - { - return $this->location; - } - - /** - * Get longitude - * - * @return mixed|null - */ - public function getLongitude() - { - return $this->longitude; - } - - /** - * Get latitude - * - * @return mixed|null - */ - public function getLatitude() - { - return $this->latitude; - } - - /** - * Get title - * - * @return mixed|null - */ - public function getTitle() - { - return $this->title; - } - - /** - * Get address - * - * @return mixed|null - */ - public function getAddress() - { - return $this->address; - } - - /** - * Get forsquare id - * - * @return mixed|null + * {@inheritdoc} */ - public function getFoursquareId() + protected function subEntities() { - return $this->foursquare_id; + return [ + 'location' => Location::class, + ]; } } diff --git a/src/Entities/Video.php b/src/Entities/Video.php index a159b5753..4cdc6b810 100644 --- a/src/Entities/Video.php +++ b/src/Entities/Video.php @@ -10,149 +10,28 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - +/** + * Class Video + * + * @link https://core.telegram.org/bots/api#video + * + * @method string getFileId() Unique identifier for this file + * @method int getWidth() Video width as defined by sender + * @method int getHeight() Video height as defined by sender + * @method int getDuration() Duration of the video in seconds as defined by sender + * @method PhotoSize getThumb() Optional. Video thumbnail + * @method string getMimeType() Optional. Mime type of a file as defined by sender + * @method int getFileSize() Optional. File size + */ class Video extends Entity { /** - * @var mixed|null - */ - protected $file_id; - - /** - * @var mixed|null - */ - protected $width; - - /** - * @var mixed|null - */ - protected $height; - - /** - * @var mixed|null - */ - protected $duration; - - /** - * @var \Longman\TelegramBot\Entities\PhotoSize - */ - protected $thumb; - - /** - * @var mixed|null - */ - protected $mime_type; - - /** - * @var mixed|null - */ - protected $file_size; - - /** - * Video constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - - $this->file_id = isset($data['file_id']) ? $data['file_id'] : null; - if (empty($this->file_id)) { - throw new TelegramException('file_id is empty!'); - } - - $this->width = isset($data['width']) ? $data['width'] : null; - if (empty($this->width)) { - throw new TelegramException('width is empty!'); - } - - $this->height = isset($data['height']) ? $data['height'] : null; - if (empty($this->height)) { - throw new TelegramException('height is empty!'); - } - $this->duration = isset($data['duration']) ? $data['duration'] : null; - if ($this->duration === '' || $this->duration === null) { - throw new TelegramException('duration is empty!'); - } - $this->thumb = isset($data['thumb']) ? $data['thumb'] : null; - if (empty($this->thumb)) { - throw new TelegramException('thumb is empty!'); - } - $this->thumb = new PhotoSize($this->thumb); - - $this->mime_type = isset($data['mime_type']) ? $data['mime_type'] : null; - $this->file_size = isset($data['file_size']) ? $data['file_size'] : null; - } - - /** - * Get file id - * - * @return mixed|null - */ - public function getFileId() - { - return $this->file_id; - } - - /** - * Get width - * - * @return mixed|null - */ - public function getWidth() - { - return $this->width; - } - - /** - * Get height - * - * @return mixed|null - */ - public function getHeight() - { - return $this->height; - } - - /** - * Get duration - * - * @return mixed|null - */ - public function getDuration() - { - return $this->duration; - } - - /** - * Get thumb - * - * @return \Longman\TelegramBot\Entities\PhotoSize - */ - public function getThumb() - { - return $this->thumb; - } - - /** - * Get mime type - * - * @return mixed|null - */ - public function getMimeType() - { - return $this->mime_type; - } - - /** - * Get file size - * - * @return mixed|null + * {@inheritdoc} */ - public function getFileSize() + protected function subEntities() { - return $this->file_size; + return [ + 'thumb' => PhotoSize::class, + ]; } } diff --git a/src/Entities/Voice.php b/src/Entities/Voice.php index b38cefb93..0b422f8c1 100644 --- a/src/Entities/Voice.php +++ b/src/Entities/Voice.php @@ -10,89 +10,17 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - +/** + * Class Voice + * + * @link https://core.telegram.org/bots/api#voice + * + * @method string getFileId() Unique identifier for this file + * @method int getDuration() Duration of the audio in seconds as defined by sender + * @method string getMimeType() Optional. MIME type of the file as defined by sender + * @method int getFileSize() Optional. File size + */ class Voice extends Entity { - /** - * @var mixed|null - */ - protected $file_id; - - /** - * @var mixed|null - */ - protected $duration; - - /** - * @var mixed|null - */ - protected $mime_type; - - /** - * @var mixed|null - */ - protected $file_size; - - /** - * Voice constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - $this->file_id = isset($data['file_id']) ? $data['file_id'] : null; - if (empty($this->file_id)) { - throw new TelegramException('file_id is empty!'); - } - - $this->duration = isset($data['duration']) ? $data['duration'] : null; - if ($this->duration === '' || $this->duration === null) { - throw new TelegramException('duration is empty!'); - } - - $this->mime_type = isset($data['mime_type']) ? $data['mime_type'] : null; - $this->file_size = isset($data['file_size']) ? $data['file_size'] : null; - } - - /** - * Get file id - * - * @return mixed|null - */ - public function getFileId() - { - return $this->file_id; - } - - /** - * Get duration - * - * @return mixed|null - */ - public function getDuration() - { - return $this->duration; - } - - /** - * Get mime type - * - * @return mixed|null - */ - public function getMimeType() - { - return $this->mime_type; - } - /** - * Get file size - * - * @return mixed|null - */ - public function getFileSize() - { - return $this->file_size; - } } diff --git a/src/Entities/WebhookInfo.php b/src/Entities/WebhookInfo.php index a17a0a033..331ccdd92 100644 --- a/src/Entities/WebhookInfo.php +++ b/src/Entities/WebhookInfo.php @@ -10,74 +10,19 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - +/** + * Class WebhookInfo + * + * @link https://core.telegram.org/bots/api#webhookinfo + * + * @method string getUrl() Webhook URL, may be empty if webhook is not set up + * @method bool getHasCustomCertificate() True, if a custom certificate was provided for webhook certificate checks + * @method int getPendingUpdateCount() Number of updates awaiting delivery + * @method int getLastErrorDate() Optional. Unix time for the most recent error that happened when trying to deliver an update via webhook + * @method string getLastErrorMessage() Optional. Error message in human-readable format for the most recent error that happened when trying to deliver an update via webhook + * + */ class WebhookInfo extends Entity { - protected $url; // String Webhook URL, may be empty if webhook is not set up - protected $has_custom_certificate; // Boolean True, if a custom certificate was provided for webhook certificate checks - protected $pending_update_count; // Integer Number of updates awaiting delivery - protected $last_error_date; // Integer Optional. Unix time for the most recent error that happened when trying to deliver an update via webhook - protected $last_error_message; // String Optional. Error message in human-readable format for the most recent error that happened when trying to deliver an update via webhook - - public function __construct(array $data) - { - $this->url = isset($data['url']) ? $data['url'] : null; - $this->has_custom_certificate = isset($data['has_custom_certificate']) ? $data['has_custom_certificate'] : null; - $this->pending_update_count = isset($data['pending_update_count']) ? $data['pending_update_count'] : null; - $this->last_error_date = isset($data['last_error_date']) ? $data['last_error_date'] : null; - $this->last_error_message = isset($data['last_error_message']) ? $data['last_error_message'] : null; - } - - /** - * Webhook URL, may be empty if webhook is not set up. - * - * @return string - */ - public function getUrl() - { - return $this->url; - } - - /** - * True, if a custom certificate was provided for webhook certificate checks. - * - * @return bool - */ - public function getHasCustomCertificate() - { - return $this->has_custom_certificate; - } - - /** - * Number of updates awaiting delivery. - * - * @return int - */ - public function getPendingUpdateCount() - { - return $this->pending_update_count; - } - - /** - * Optional. - * Unix time for the most recent error that happened when trying to deliver an update via webhook. - * - * @return int - */ - public function getLastErrorDate() - { - return $this->last_error_date; - } - /** - * Optional. - * Error message in human-readable format for the most recent error that happened when trying to deliver an update via webhook. - * - * @return string - */ - public function getLastErrorMessage() - { - return $this->last_error_message; - } } diff --git a/src/Request.php b/src/Request.php index e4e110371..79ebf42ee 100644 --- a/src/Request.php +++ b/src/Request.php @@ -223,14 +223,14 @@ public static function execute($action, array $data = []) if ($action === 'getUpdates') { TelegramLog::update($result); } - - return $result; } catch (RequestException $e) { - throw new TelegramException($e->getMessage()); + $result = (string)$e->getResponse()->getBody(); } finally { //Logging verbose debug output TelegramLog::endDebugLogTempStream("Verbose HTTP Request output:\n%s\n"); } + + return $result; } /** @@ -263,7 +263,7 @@ public static function downloadFile(File $file) return filesize($file_path) > 0; } catch (RequestException $e) { - throw new TelegramException($e->getMessage()); + return (string)$e->getResponse()->getBody(); } finally { //Logging verbose debug output TelegramLog::endDebugLogTempStream("Verbose HTTP File Download Request output:\n%s\n"); @@ -367,9 +367,11 @@ private static function assignEncodedFile(&$data, $field, $file) } /** - * Get me + * Returns basic information about the bot in form of a User object + * + * @link https://core.telegram.org/bots/api#getme * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function getMe() @@ -380,11 +382,13 @@ public static function getMe() } /** - * Send message + * Use this method to send text messages. On success, the sent Message is returned + * + * @link https://core.telegram.org/bots/api#sendmessage * * @param array $data * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function sendMessage(array $data) @@ -404,11 +408,13 @@ public static function sendMessage(array $data) } /** - * Forward message + * Use this method to forward messages of any kind. On success, the sent Message is returned + * + * @link https://core.telegram.org/bots/api#forwardmessage * * @param array $data * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function forwardMessage(array $data) @@ -417,12 +423,14 @@ public static function forwardMessage(array $data) } /** - * Send photo + * Use this method to send photos. On success, the sent Message is returned + * + * @link https://core.telegram.org/bots/api#sendphoto * * @param array $data * @param string $file * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function sendPhoto(array $data, $file = null) @@ -433,12 +441,18 @@ public static function sendPhoto(array $data, $file = null) } /** - * Send audio + * Use this method to send audio files + * + * 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. + * For sending voice messages, use the sendVoice method instead. + * + * @link https://core.telegram.org/bots/api#sendaudio * * @param array $data * @param string $file * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function sendAudio(array $data, $file = null) @@ -449,12 +463,16 @@ public static function sendAudio(array $data, $file = null) } /** - * Send document + * 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. + * + * @link https://core.telegram.org/bots/api#senddocument * * @param array $data * @param string $file * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function sendDocument(array $data, $file = null) @@ -465,12 +483,14 @@ public static function sendDocument(array $data, $file = null) } /** - * Send sticker + * Use this method to send .webp stickers. On success, the sent Message is returned. + * + * @link https://core.telegram.org/bots/api#sendsticker * * @param array $data * @param string $file * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function sendSticker(array $data, $file = null) @@ -481,12 +501,17 @@ public static function sendSticker(array $data, $file = null) } /** - * Send video + * Use this method to send video files. On success, the sent Message is returned. + * + * Telegram clients support mp4 videos (other formats may be sent as Document). + * Bots can currently send video files of up to 50 MB in size, this limit may be changed in the future. + * + * @link https://core.telegram.org/bots/api#sendvideo * * @param array $data * @param string $file * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function sendVideo(array $data, $file = null) @@ -497,12 +522,18 @@ public static function sendVideo(array $data, $file = null) } /** - * Send voice + * Use this method to send audio files. On success, the sent Message is returned. + * + * Telegram clients will 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). + * Bots can currently send voice messages of up to 50 MB in size, this limit may be changed in the future. + * + * @link https://core.telegram.org/bots/api#sendvoice * * @param array $data * @param string $file * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function sendVoice(array $data, $file = null) @@ -513,11 +544,13 @@ public static function sendVoice(array $data, $file = null) } /** - * Send location + * Use this method to send point on the map. On success, the sent Message is returned. + * + * @link https://core.telegram.org/bots/api#sendlocation * * @param array $data * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function sendLocation(array $data) @@ -526,11 +559,13 @@ public static function sendLocation(array $data) } /** - * Send venue + * Use this method to send information about a venue. On success, the sent Message is returned. + * + * @link https://core.telegram.org/bots/api#sendvenue * * @param array $data * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function sendVenue(array $data) @@ -539,11 +574,13 @@ public static function sendVenue(array $data) } /** - * Send contact + * Use this method to send phone contacts. On success, the sent Message is returned. + * + * @link https://core.telegram.org/bots/api#sendcontact * * @param array $data * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function sendContact(array $data) @@ -552,11 +589,16 @@ public static function sendContact(array $data) } /** - * Send chat action + * 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) + * + * @link https://core.telegram.org/bots/api#sendchataction * * @param array $data * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function sendChatAction(array $data) @@ -565,59 +607,32 @@ public static function sendChatAction(array $data) } /** - * Get user profile photos + * Use this method to get a list of profile pictures for a user. Returns a UserProfilePhotos object. * * @param array $data * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function getUserProfilePhotos(array $data) { - if (!isset($data['user_id'])) { - throw new TelegramException('User id is empty!'); - } - return self::send('getUserProfilePhotos', $data); } /** - * Get updates - * - * @param array $data + * Use this method to get basic info about a file and prepare it for downloading. On success, a File object is returned. * - * @return mixed - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public static function getUpdates(array $data) - { - return self::send('getUpdates', $data); - } - - /** - * Set webhook - * - * @param string $url - * @param string $file + * For the moment, bots can download files of up to 20MB in size. + * 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. * - * @return mixed - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public static function setWebhook($url = '', $file = null) - { - $data = ['url' => $url]; - - self::assignEncodedFile($data, 'certificate', $file); - - return self::send('setWebhook', $data); - } - - /** - * Get file + * @link https://core.telegram.org/bots/api#getfile * * @param array $data * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function getFile(array $data) @@ -626,11 +641,16 @@ public static function getFile(array $data) } /** - * Kick Chat Member + * Use this method to kick a user from a group or a supergroup. Returns True on success. + * + * In the case of supergroups, 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 group for this to work. + * + * @link https://core.telegram.org/bots/api#kickchatmember * * @param array $data * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function kickChatMember(array $data) @@ -639,11 +659,13 @@ public static function kickChatMember(array $data) } /** - * Leave Chat + * Use this method for your bot to leave a group, supergroup or channel. Returns True on success. + * + * @link https://core.telegram.org/bots/api#leavechat * * @param array $data * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function leaveChat(array $data) @@ -652,11 +674,16 @@ public static function leaveChat(array $data) } /** - * Unban Chat Member + * Use this method to unban a previously kicked user in a supergroup. Returns True on success. + * + * The user will not return to the group automatically, but will be able to join via link, etc. + * The bot must be an administrator in the group for this to work. + * + * @link https://core.telegram.org/bots/api#unbanchatmember * * @param array $data * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function unbanChatMember(array $data) @@ -665,13 +692,15 @@ public static function unbanChatMember(array $data) } /** - * Get Chat + * 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. * * @todo add get response in ServerResponse.php? * + * @link https://core.telegram.org/bots/api#getchat + * * @param array $data * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function getChat(array $data) @@ -680,13 +709,18 @@ public static function getChat(array $data) } /** - * Get Chat Administrators + * 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. * * @todo add get response in ServerResponse.php? * + * @link https://core.telegram.org/bots/api#getchatadministrators + * * @param array $data * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function getChatAdministrators(array $data) @@ -695,13 +729,15 @@ public static function getChatAdministrators(array $data) } /** - * Get Chat Members Count + * Use this method to get the number of members in a chat. Returns Int on success. * * @todo add get response in ServerResponse.php? * + * @link https://core.telegram.org/bots/api#getchatmemberscount + * * @param array $data * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function getChatMembersCount(array $data) @@ -710,13 +746,15 @@ public static function getChatMembersCount(array $data) } /** - * Get Chat Member + * Use this method to get information about a member of a chat. Returns a ChatMember object on success. * * @todo add get response in ServerResponse.php? * + * @link https://core.telegram.org/bots/api#getchatmember + * * @param array $data * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function getChatMember(array $data) @@ -725,11 +763,15 @@ public static function getChatMember(array $data) } /** - * Answer callback query + * Use this method to send answers to callback queries sent from inline keyboards. On success, True is returned. + * + * The answer will be displayed to the user as a notification at the top of the chat screen or as an alert. + * + * @link https://core.telegram.org/bots/api#answercallbackquery * * @param array $data * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function answerCallbackQuery(array $data) @@ -738,24 +780,50 @@ public static function answerCallbackQuery(array $data) } /** - * Answer inline query + * Get updates + * + * @link https://core.telegram.org/bots/api#getupdates * * @param array $data * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ - public static function answerInlineQuery(array $data) + public static function getUpdates(array $data) { - return self::send('answerInlineQuery', $data); + return self::send('getUpdates', $data); } /** - * Edit message text + * Set webhook + * + * @link https://core.telegram.org/bots/api#setwebhook + * + * @param string $url + * @param string $file + * + * @return \Longman\TelegramBot\Entities\ServerResponse + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public static function setWebhook($url = '', $file = null) + { + $data = ['url' => $url]; + + self::assignEncodedFile($data, 'certificate', $file); + + return self::send('setWebhook', $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. + * + * @link https://core.telegram.org/bots/api#editmessagetext * * @param array $data * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function editMessageText(array $data) @@ -764,11 +832,15 @@ public static function editMessageText(array $data) } /** - * Edit message caption + * 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. + * + * @link https://core.telegram.org/bots/api#editmessagecaption * * @param array $data * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function editMessageCaption(array $data) @@ -777,11 +849,15 @@ public static function editMessageCaption(array $data) } /** - * Edit message reply markup + * 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. + * + * @link https://core.telegram.org/bots/api#editmessagereplymarkup * * @param array $data * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function editMessageReplyMarkup(array $data) @@ -789,6 +865,23 @@ public static function editMessageReplyMarkup(array $data) return self::send('editMessageReplyMarkup', $data); } + /** + * Use this method to send answers to an inline query. On success, True is returned. + * + * No more than 50 results per query are allowed. + * + * @link https://core.telegram.org/bots/api#answerinlinequery + * + * @param array $data + * + * @return \Longman\TelegramBot\Entities\ServerResponse + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public static function answerInlineQuery(array $data) + { + return self::send('answerInlineQuery', $data); + } + /** * Return an empty Server Response * @@ -847,10 +940,14 @@ public static function sendToActiveChats( /** * Use this method to get current webhook status. * + * @link https://core.telegram.org/bots/api#getwebhookinfo + * * @return Entities\ServerResponse + * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function getWebhookInfo() { - return self::send('getWebhookInfo', []); + // Must send some arbitrary data for this to work for now... + return self::send('getWebhookInfo', ['info']); } } diff --git a/tests/unit/Entities/AudioTest.php b/tests/unit/Entities/AudioTest.php index f80bad1ce..9df28c36d 100644 --- a/tests/unit/Entities/AudioTest.php +++ b/tests/unit/Entities/AudioTest.php @@ -11,8 +11,6 @@ namespace Longman\TelegramBot\Tests\Unit; use Longman\TelegramBot\Entities\Audio; -use Longman\TelegramBot\Exception\TelegramException; -use Longman\TelegramBot\Tests\Unit\TestHelpers; /** * @package TelegramTest @@ -24,103 +22,32 @@ class AudioTest extends TestCase { /** - * @var array - */ + * @var array + */ private $record; + /** - * Set Up - */ + * Set Up + */ public function setUp() { $this->record = TestHelpers::getFakeRecordedAudio(); } - /** - * Testing base stage with data object creating - */ - - public function testBaseStageAudio() - { - $audio = new Audio($this->record); - $this->assertInstanceOf('Longman\TelegramBot\Entities\Audio', $audio); - } - - /** - * Test base stage without duration property - * - * @expectedException Longman\TelegramBot\Exception\TelegramException - */ - public function testBaseStageWithoutDuration() - { - $this->record['duration'] = null; - new Audio($this->record); - } - - /** - * Test base stage without file_id property - * - * @expectedException Longman\TelegramBot\Exception\TelegramException - */ - public function testBaseStageWithoutFileId() - { - $this->record['file_id'] = null; - new Audio($this->record); - } - /** - * Test get file id - */ - public function testGetFileId() + public function testInstance() { $audio = new Audio($this->record); - $file_id = $audio->getFileId(); - $this->assertEquals($this->record['file_id'], $file_id); + self::assertInstanceOf('Longman\TelegramBot\Entities\Audio', $audio); } - /** - * Test get duration track - */ - public function testGetDuration() - { - $audio = new Audio($this->record); - $duration = $audio->getDuration(); - $this->assertEquals($this->record['duration'], $duration); - } - - /** - * Test get performer track - */ - public function testGetPerformer() - { - $audio = new Audio($this->record); - $performer = $audio->getPerformer(); - $this->assertEquals($this->record['performer'], $performer); - } - - /** - * Test get title track - */ - public function testGetTitle() - { - $audio = new Audio($this->record); - $title = $audio->getTitle(); - $this->assertEquals($this->record['title'], $title); - } - /** - * Test get mime type file - */ - public function testGetMimeType() - { - $audio = new Audio($this->record); - $mime_type = $audio->getMimeType(); - $this->assertEquals($this->record['mime_type'], $mime_type); - } - /** - * Test get file size - */ - public function testGetFileSize() + public function testGetProperties() { $audio = new Audio($this->record); - $file_size = $audio->getFileSize(); - $this->assertEquals($this->record['file_size'], $file_size); + self::assertEquals($this->record['file_id'], $audio->getFileId()); + self::assertEquals($this->record['duration'], $audio->getDuration()); + self::assertEquals($this->record['performer'], $audio->getPerformer()); + self::assertEquals($this->record['title'], $audio->getTitle()); + self::assertEquals($this->record['mime_type'], $audio->getMimeType()); + self::assertEquals($this->record['file_size'], $audio->getFileSize()); } } diff --git a/tests/unit/Entities/ChatTest.php b/tests/unit/Entities/ChatTest.php index da4931e11..46520f5c6 100644 --- a/tests/unit/Entities/ChatTest.php +++ b/tests/unit/Entities/ChatTest.php @@ -19,20 +19,52 @@ */ class ChatTest extends TestCase { - /** - * @var \Longman\TelegramBot\Entities\Chat - */ - private $chat; - public function testChatType() { - $this->chat = TestHelpers::getFakeChatObject(); - $this->assertEquals('private', $this->chat->getType()); + $chat = TestHelpers::getFakeChatObject(); + self::assertEquals('private', $chat->getType()); + + $chat = TestHelpers::getFakeChatObject(['id' => -123, 'type' => null]); + self::assertEquals('group', $chat->getType()); + + $chat = TestHelpers::getFakeChatObject(['id' => -123, 'type' => 'supergroup']); + self::assertEquals('supergroup', $chat->getType()); + + $chat = TestHelpers::getFakeChatObject(['id' => -123, 'type' => 'channel']); + self::assertEquals('channel', $chat->getType()); + } + + public function testIsChatType() + { + $chat = TestHelpers::getFakeChatObject(); + self::assertTrue($chat->isPrivateChat()); + + $chat = TestHelpers::getFakeChatObject(['id' => -123, 'type' => null]); + self::assertTrue($chat->isGroupChat()); + + $chat = TestHelpers::getFakeChatObject(['id' => -123, 'type' => 'supergroup']); + self::assertTrue($chat->isSuperGroup()); + + $chat = TestHelpers::getFakeChatObject(['id' => -123, 'type' => 'channel']); + self::assertTrue($chat->isChannel()); + } + + public function testTryMention() + { + // Username. + $chat = TestHelpers::getFakeChatObject(['id' => 1, 'first_name' => 'John', 'last_name' => 'Taylor', 'username' => 'jtaylor']); + self::assertEquals('@jtaylor', $chat->tryMention()); + + // First name. + $chat = TestHelpers::getFakeChatObject(['id' => 1, 'first_name' => 'John', 'last_name' => null, 'username' => null]); + self::assertEquals('John', $chat->tryMention()); - $this->chat = TestHelpers::getFakeChatObject(['id' => -123, 'type' => null]); - $this->assertEquals('group', $this->chat->getType()); + // First and Last name. + $chat = TestHelpers::getFakeChatObject(['id' => 1, 'first_name' => 'John', 'last_name' => 'Taylor', 'username' => null]); + self::assertEquals('John Taylor', $chat->tryMention()); - $this->chat = TestHelpers::getFakeChatObject(['id' => -123, 'type' => 'channel']); - $this->assertEquals('channel', $this->chat->getType()); + // Non-private chat should return title. + $chat = TestHelpers::getFakeChatObject(['id' => -123, 'type' => null, 'title' => 'My group chat']); + self::assertSame('My group chat', $chat->tryMention()); } } diff --git a/tests/unit/Entities/InlineKeyboardButtonTest.php b/tests/unit/Entities/InlineKeyboardButtonTest.php index f682abac3..d7cd69185 100644 --- a/tests/unit/Entities/InlineKeyboardButtonTest.php +++ b/tests/unit/Entities/InlineKeyboardButtonTest.php @@ -23,7 +23,7 @@ class InlineKeyboardButtonTest extends TestCase { /** * @expectedException \Longman\TelegramBot\Exception\TelegramException - * @expectedExceptionMessage text is empty! + * @expectedExceptionMessage You must add some text to the button! */ public function testInlineKeyboardButtonNoTextFail() { @@ -45,7 +45,31 @@ public function testInlineKeyboardButtonNoParameterFail() */ public function testInlineKeyboardButtonTooManyParametersFail() { - new InlineKeyboardButton(['text' => 'message', 'url' => 'url_value', 'callback_data' => 'callback_data_value']); + $test_funcs = [ + function () { + new InlineKeyboardButton([ + 'text' => 'message', + 'url' => 'url_value', + 'callback_data' => 'callback_data_value', + ]); + }, + function () { + new InlineKeyboardButton([ + 'text' => 'message', + 'url' => 'url_value', + 'switch_inline_query' => 'switch_inline_query_value', + ]); + }, + function () { + new InlineKeyboardButton([ + 'text' => 'message', + 'callback_data' => 'callback_data_value', + 'switch_inline_query' => 'switch_inline_query_value', + ]); + }, + ]; + + $test_funcs[array_rand($test_funcs)](); } public function testInlineKeyboardButtonSuccess() @@ -54,4 +78,50 @@ public function testInlineKeyboardButtonSuccess() new InlineKeyboardButton(['text' => 'message', 'callback_data' => 'callback_data_value']); new InlineKeyboardButton(['text' => 'message', 'switch_inline_query' => 'switch_inline_query_value']); } + + public function testInlineKeyboardButtonCouldBe() + { + self::assertTrue(InlineKeyboardButton::couldBe( + ['text' => 'message', 'url' => 'url_value'] + )); + self::assertTrue(InlineKeyboardButton::couldBe( + ['text' => 'message', 'callback_data' => 'callback_data_value'] + )); + self::assertTrue(InlineKeyboardButton::couldBe( + ['text' => 'message', 'switch_inline_query' => 'switch_inline_query_value'] + )); + + self::assertFalse(InlineKeyboardButton::couldBe(['no_text' => 'message'])); + self::assertFalse(InlineKeyboardButton::couldBe(['text' => 'message'])); + self::assertFalse(InlineKeyboardButton::couldBe(['url' => 'url_value'])); + self::assertFalse(InlineKeyboardButton::couldBe( + ['callback_data' => 'callback_data_value'] + )); + self::assertFalse(InlineKeyboardButton::couldBe( + ['switch_inline_query' => 'switch_inline_query_value'] + )); + self::assertFalse(InlineKeyboardButton::couldBe([ + 'url' => 'url_value', + 'callback_data' => 'callback_data_value', + 'switch_inline_query' => 'switch_inline_query_value', + ])); + } + + public function testInlineKeyboardButtonParameterSetting() + { + $button = new InlineKeyboardButton(['text' => 'message', 'url' => 'url_value']); + self::assertSame('url_value', $button->getUrl()); + self::assertEmpty($button->getCallbackData()); + self::assertEmpty($button->getSwitchInlineQuery()); + + $button->setCallbackData('callback_data_value'); + self::assertEmpty($button->getUrl()); + self::assertSame('callback_data_value', $button->getCallbackData()); + self::assertEmpty($button->getSwitchInlineQuery()); + + $button->setSwitchInlineQuery('switch_inline_query_value'); + self::assertEmpty($button->getUrl()); + self::assertEmpty($button->getCallbackData()); + self::assertSame('switch_inline_query_value', $button->getSwitchInlineQuery()); + } } diff --git a/tests/unit/Entities/InlineKeyboardTest.php b/tests/unit/Entities/InlineKeyboardTest.php new file mode 100644 index 000000000..b3007affe --- /dev/null +++ b/tests/unit/Entities/InlineKeyboardTest.php @@ -0,0 +1,138 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Tests\Unit; + +use Longman\TelegramBot\Entities\InlineKeyboard; +use Longman\TelegramBot\Entities\InlineKeyboardButton; + +/** + * @package TelegramTest + * @author Avtandil Kikabidze + * @copyright Avtandil Kikabidze + * @license http://opensource.org/licenses/mit-license.php The MIT License (MIT) + * @link http://www.github.com/akalongman/php-telegram-bot + */ +class InlineKeyboardTest extends TestCase +{ + private function getRandomButton($text) + { + $random_params = ['url', 'callback_data', 'switch_inline_query']; + $param = $random_params[array_rand($random_params, 1)]; + $data = [ + 'text' => $text, + $param => 'random_param', + ]; + + return new InlineKeyboardButton($data); + } + + /** + * @expectedException \Longman\TelegramBot\Exception\TelegramException + * @expectedExceptionMessage inline_keyboard field is not an array! + */ + public function testInlineKeyboardDataMalformedField() + { + new InlineKeyboard(['inline_keyboard' => 'wrong']); + } + + /** + * @expectedException \Longman\TelegramBot\Exception\TelegramException + * @expectedExceptionMessage inline_keyboard subfield is not an array! + */ + public function testInlineKeyboardDataMalformedSubfield() + { + new InlineKeyboard(['inline_keyboard' => ['wrong']]); + } + + public function testInlineKeyboardSingleButtonSingleRow() + { + $inline_keyboard = (new InlineKeyboard( + $this->getRandomButton('Button Text 1') + ))->getProperty('inline_keyboard'); + self::assertSame('Button Text 1', $inline_keyboard[0][0]->getText()); + + $inline_keyboard = (new InlineKeyboard( + [$this->getRandomButton('Button Text 2')] + ))->getProperty('inline_keyboard'); + self::assertSame('Button Text 2', $inline_keyboard[0][0]->getText()); + } + + public function testInlineKeyboardSingleButtonMultipleRows() + { + $keyboard = (new InlineKeyboard( + $this->getRandomButton('Button Text 1'), + $this->getRandomButton('Button Text 2'), + $this->getRandomButton('Button Text 3') + ))->getProperty('inline_keyboard'); + self::assertSame('Button Text 1', $keyboard[0][0]->getText()); + self::assertSame('Button Text 2', $keyboard[1][0]->getText()); + self::assertSame('Button Text 3', $keyboard[2][0]->getText()); + + $keyboard = (new InlineKeyboard( + [$this->getRandomButton('Button Text 4')], + [$this->getRandomButton('Button Text 5')], + [$this->getRandomButton('Button Text 6')] + ))->getProperty('inline_keyboard'); + self::assertSame('Button Text 4', $keyboard[0][0]->getText()); + self::assertSame('Button Text 5', $keyboard[1][0]->getText()); + self::assertSame('Button Text 6', $keyboard[2][0]->getText()); + } + + public function testInlineKeyboardMultipleButtonsSingleRow() + { + $keyboard = (new InlineKeyboard([ + $this->getRandomButton('Button Text 1'), + $this->getRandomButton('Button Text 2'), + ]))->getProperty('inline_keyboard'); + self::assertSame('Button Text 1', $keyboard[0][0]->getText()); + self::assertSame('Button Text 2', $keyboard[0][1]->getText()); + } + + public function testInlineKeyboardMultipleButtonsMultipleRows() + { + $keyboard = (new InlineKeyboard( + [ + $this->getRandomButton('Button Text 1'), + $this->getRandomButton('Button Text 2'), + ], + [ + $this->getRandomButton('Button Text 3'), + $this->getRandomButton('Button Text 4'), + ] + ))->getProperty('inline_keyboard'); + + self::assertSame('Button Text 1', $keyboard[0][0]->getText()); + self::assertSame('Button Text 2', $keyboard[0][1]->getText()); + self::assertSame('Button Text 3', $keyboard[1][0]->getText()); + self::assertSame('Button Text 4', $keyboard[1][1]->getText()); + } + + public function testInlineKeyboardAddRows() + { + $keyboard_obj = new InlineKeyboard([]); + + $keyboard_obj->addRow($this->getRandomButton('Button Text 1')); + $keyboard = $keyboard_obj->getProperty('inline_keyboard'); + self::assertSame('Button Text 1', $keyboard[0][0]->getText()); + + $keyboard_obj->addRow( + $this->getRandomButton('Button Text 2'), + $this->getRandomButton('Button Text 3') + ); + $keyboard = $keyboard_obj->getProperty('inline_keyboard'); + self::assertSame('Button Text 2', $keyboard[1][0]->getText()); + self::assertSame('Button Text 3', $keyboard[1][1]->getText()); + + $keyboard_obj->addRow($this->getRandomButton('Button Text 4')); + $keyboard = $keyboard_obj->getProperty('inline_keyboard'); + self::assertSame('Button Text 4', $keyboard[2][0]->getText()); + } +} diff --git a/tests/unit/Entities/KeyboardButtonTest.php b/tests/unit/Entities/KeyboardButtonTest.php new file mode 100644 index 000000000..0e1260946 --- /dev/null +++ b/tests/unit/Entities/KeyboardButtonTest.php @@ -0,0 +1,69 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Tests\Unit; + +use Longman\TelegramBot\Entities\KeyboardButton; + +/** + * @package TelegramTest + * @author Avtandil Kikabidze + * @copyright Avtandil Kikabidze + * @license http://opensource.org/licenses/mit-license.php The MIT License (MIT) + * @link http://www.github.com/akalongman/php-telegram-bot + */ +class KeyboardButtonTest extends TestCase +{ + /** + * @expectedException \Longman\TelegramBot\Exception\TelegramException + * @expectedExceptionMessage You must add some text to the button! + */ + public function testKeyboardButtonNoTextFail() + { + new KeyboardButton([]); + } + + /** + * @expectedException \Longman\TelegramBot\Exception\TelegramException + * @expectedExceptionMessage You must use only one of these fields: request_contact, request_location! + */ + public function testKeyboardButtonTooManyParametersFail() + { + new KeyboardButton(['text' => 'message', 'request_contact' => true, 'request_location' => true]); + } + + public function testKeyboardButtonSuccess() + { + new KeyboardButton(['text' => 'message']); + new KeyboardButton(['text' => 'message', 'request_contact' => true]); + new KeyboardButton(['text' => 'message', 'request_location' => true]); + } + + public function testInlineKeyboardButtonCouldBe() + { + self::assertTrue(KeyboardButton::couldBe(['text' => 'message'])); + self::assertFalse(KeyboardButton::couldBe(['no_text' => 'message'])); + } + + public function testKeyboardButtonParameterSetting() + { + $button = new KeyboardButton('message'); + self::assertEmpty($button->getRequestContact()); + self::assertEmpty($button->getRequestLocation()); + + $button->setRequestContact(true); + self::assertTrue($button->getRequestContact()); + self::assertEmpty($button->getRequestLocation()); + + $button->setRequestLocation(true); + self::assertEmpty($button->getRequestContact()); + self::assertTrue($button->getRequestLocation()); + } +} diff --git a/tests/unit/Entities/KeyboardTest.php b/tests/unit/Entities/KeyboardTest.php new file mode 100644 index 000000000..ce93c2ed1 --- /dev/null +++ b/tests/unit/Entities/KeyboardTest.php @@ -0,0 +1,187 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Tests\Unit; + +use Longman\TelegramBot\Entities\Keyboard; +use Longman\TelegramBot\Entities\KeyboardButton; + +/** + * @package TelegramTest + * @author Avtandil Kikabidze + * @copyright Avtandil Kikabidze + * @license http://opensource.org/licenses/mit-license.php The MIT License (MIT) + * @link http://www.github.com/akalongman/php-telegram-bot + */ +class KeyboardTest extends TestCase +{ + /** + * @expectedException \Longman\TelegramBot\Exception\TelegramException + * @expectedExceptionMessage keyboard field is not an array! + */ + public function testKeyboardDataMalformedField() + { + new Keyboard(['keyboard' => 'wrong']); + } + + /** + * @expectedException \Longman\TelegramBot\Exception\TelegramException + * @expectedExceptionMessage keyboard subfield is not an array! + */ + public function testKeyboardDataMalformedSubfield() + { + new Keyboard(['keyboard' => ['wrong']]); + } + + public function testKeyboardSingleButtonSingleRow() + { + $keyboard = (new Keyboard('Button Text 1'))->getProperty('keyboard'); + self::assertSame('Button Text 1', $keyboard[0][0]->getText()); + + $keyboard = (new Keyboard(['Button Text 2']))->getProperty('keyboard'); + self::assertSame('Button Text 2', $keyboard[0][0]->getText()); + } + + public function testKeyboardSingleButtonMultipleRows() + { + $keyboard = (new Keyboard( + 'Button Text 1', + 'Button Text 2', + 'Button Text 3' + ))->getProperty('keyboard'); + self::assertSame('Button Text 1', $keyboard[0][0]->getText()); + self::assertSame('Button Text 2', $keyboard[1][0]->getText()); + self::assertSame('Button Text 3', $keyboard[2][0]->getText()); + + $keyboard = (new Keyboard( + ['Button Text 4'], + ['Button Text 5'], + ['Button Text 6'] + ))->getProperty('keyboard'); + self::assertSame('Button Text 4', $keyboard[0][0]->getText()); + self::assertSame('Button Text 5', $keyboard[1][0]->getText()); + self::assertSame('Button Text 6', $keyboard[2][0]->getText()); + } + + public function testKeyboardMultipleButtonsSingleRow() + { + $keyboard = (new Keyboard(['Button Text 1', 'Button Text 2']))->getProperty('keyboard'); + self::assertSame('Button Text 1', $keyboard[0][0]->getText()); + self::assertSame('Button Text 2', $keyboard[0][1]->getText()); + } + + public function testKeyboardMultipleButtonsMultipleRows() + { + $keyboard = (new Keyboard( + ['Button Text 1', 'Button Text 2'], + ['Button Text 3', 'Button Text 4'] + ))->getProperty('keyboard'); + + self::assertSame('Button Text 1', $keyboard[0][0]->getText()); + self::assertSame('Button Text 2', $keyboard[0][1]->getText()); + self::assertSame('Button Text 3', $keyboard[1][0]->getText()); + self::assertSame('Button Text 4', $keyboard[1][1]->getText()); + } + + public function testKeyboardWithButtonObjects() + { + $keyboard = (new Keyboard( + new KeyboardButton('Button Text 1') + ))->getProperty('keyboard'); + self::assertSame('Button Text 1', $keyboard[0][0]->getText()); + + $keyboard = (new Keyboard( + new KeyboardButton('Button Text 2'), + new KeyboardButton('Button Text 3') + ))->getProperty('keyboard'); + self::assertSame('Button Text 2', $keyboard[0][0]->getText()); + self::assertSame('Button Text 3', $keyboard[1][0]->getText()); + + $keyboard = (new Keyboard( + [new KeyboardButton('Button Text 4')], + [new KeyboardButton('Button Text 5'), new KeyboardButton('Button Text 6')] + ))->getProperty('keyboard'); + self::assertSame('Button Text 4', $keyboard[0][0]->getText()); + self::assertSame('Button Text 5', $keyboard[1][0]->getText()); + self::assertSame('Button Text 6', $keyboard[1][1]->getText()); + } + + public function testKeyboardWithDataArray() + { + $resize_keyboard = (bool)mt_rand(0, 1); + $one_time_keyboard = (bool)mt_rand(0, 1); + $selective = (bool)mt_rand(0, 1); + + $keyboard_obj = new Keyboard([ + 'resize_keyboard' => $resize_keyboard, + 'one_time_keyboard' => $one_time_keyboard, + 'selective' => $selective, + 'keyboard' => [['Button Text 1']], + ]); + + $keyboard = $keyboard_obj->getProperty('keyboard'); + self::assertSame('Button Text 1', $keyboard[0][0]->getText()); + + self::assertSame($resize_keyboard, $keyboard_obj->getResizeKeyboard()); + self::assertSame($one_time_keyboard, $keyboard_obj->getOneTimeKeyboard()); + self::assertSame($selective, $keyboard_obj->getSelective()); + } + + public function testPredefinedKeyboards() + { + $keyboard_hide = Keyboard::hide(); + self::assertTrue($keyboard_hide->getProperty('hide_keyboard')); + + $keyboard_force_reply = Keyboard::forceReply(); + self::assertTrue($keyboard_force_reply->getProperty('force_reply')); + } + + public function testKeyboardMethods() + { + $keyboard_obj = new Keyboard([]); + + self::assertEmpty($keyboard_obj->getOneTimeKeyboard()); + self::assertEmpty($keyboard_obj->getResizeKeyboard()); + self::assertEmpty($keyboard_obj->getSelective()); + + $keyboard_obj->setOneTimeKeyboard(true); + self::assertTrue($keyboard_obj->getOneTimeKeyboard()); + $keyboard_obj->setOneTimeKeyboard(false); + self::assertFalse($keyboard_obj->getOneTimeKeyboard()); + + $keyboard_obj->setResizeKeyboard(true); + self::assertTrue($keyboard_obj->getResizeKeyboard()); + $keyboard_obj->setResizeKeyboard(false); + self::assertFalse($keyboard_obj->getResizeKeyboard()); + + $keyboard_obj->setSelective(true); + self::assertTrue($keyboard_obj->getSelective()); + $keyboard_obj->setSelective(false); + self::assertFalse($keyboard_obj->getSelective()); + } + + public function testKeyboardAddRows() + { + $keyboard_obj = new Keyboard([]); + + $keyboard_obj->addRow('Button Text 1'); + $keyboard = $keyboard_obj->getProperty('keyboard'); + self::assertSame('Button Text 1', $keyboard[0][0]->getText()); + + $keyboard_obj->addRow('Button Text 2', 'Button Text 3'); + $keyboard = $keyboard_obj->getProperty('keyboard'); + self::assertSame('Button Text 2', $keyboard[1][0]->getText()); + self::assertSame('Button Text 3', $keyboard[1][1]->getText()); + + $keyboard_obj->addRow(['text' => 'Button Text 4']); + $keyboard = $keyboard_obj->getProperty('keyboard'); + self::assertSame('Button Text 4', $keyboard[2][0]->getText()); + } +} diff --git a/tests/unit/Entities/MessageTest.php b/tests/unit/Entities/MessageTest.php index 35a4ffd16..639ff0b13 100644 --- a/tests/unit/Entities/MessageTest.php +++ b/tests/unit/Entities/MessageTest.php @@ -19,66 +19,74 @@ */ class MessageTest extends TestCase { - /** - * @var \Longman\TelegramBot\Entities\Message - */ - private $message; - - public function testTextAndCommandRecognise() { + public function testTextAndCommandRecognise() + { // /command - $this->message = TestHelpers::getFakeMessageObject(['text' => '/help']); - $this->assertEquals('/help', $this->message->getFullCommand()); - $this->assertEquals('help', $this->message->getCommand()); - $this->assertEquals('/help', $this->message->getText()); - $this->assertEquals('', $this->message->getText(true)); + $message = TestHelpers::getFakeMessageObject(['text' => '/help']); + self::assertEquals('/help', $message->getFullCommand()); + self::assertEquals('help', $message->getCommand()); + self::assertEquals('/help', $message->getText()); + self::assertEquals('', $message->getText(true)); // text - $this->message = TestHelpers::getFakeMessageObject(['text' => 'some text']); - $this->assertEquals('', $this->message->getFullCommand()); - $this->assertEquals('', $this->message->getCommand()); - $this->assertEquals('some text', $this->message->getText()); - $this->assertEquals('some text', $this->message->getText(true)); + $message = TestHelpers::getFakeMessageObject(['text' => 'some text']); + self::assertEquals('', $message->getFullCommand()); + self::assertEquals('', $message->getCommand()); + self::assertEquals('some text', $message->getText()); + self::assertEquals('some text', $message->getText(true)); // /command@bot - $this->message = TestHelpers::getFakeMessageObject(['text' => '/help@testbot']); - $this->assertEquals('/help@testbot', $this->message->getFullCommand()); - $this->assertEquals('help', $this->message->getCommand()); - $this->assertEquals('/help@testbot', $this->message->getText()); - $this->assertEquals('', $this->message->getText(true)); + $message = TestHelpers::getFakeMessageObject(['text' => '/help@testbot']); + self::assertEquals('/help@testbot', $message->getFullCommand()); + self::assertEquals('help', $message->getCommand()); + self::assertEquals('/help@testbot', $message->getText()); + self::assertEquals('', $message->getText(true)); // /commmad text - $this->message = TestHelpers::getFakeMessageObject(['text' => '/help some text']); - $this->assertEquals('/help', $this->message->getFullCommand()); - $this->assertEquals('help', $this->message->getCommand()); - $this->assertEquals('/help some text', $this->message->getText()); - $this->assertEquals('some text', $this->message->getText(true)); + $message = TestHelpers::getFakeMessageObject(['text' => '/help some text']); + self::assertEquals('/help', $message->getFullCommand()); + self::assertEquals('help', $message->getCommand()); + self::assertEquals('/help some text', $message->getText()); + self::assertEquals('some text', $message->getText(true)); // /command@bot some text - $this->message = TestHelpers::getFakeMessageObject(['text' => '/help@testbot some text']); - $this->assertEquals('/help@testbot', $this->message->getFullCommand()); - $this->assertEquals('help', $this->message->getCommand()); - $this->assertEquals('/help@testbot some text', $this->message->getText()); - $this->assertEquals('some text', $this->message->getText(true)); + $message = TestHelpers::getFakeMessageObject(['text' => '/help@testbot some text']); + self::assertEquals('/help@testbot', $message->getFullCommand()); + self::assertEquals('help', $message->getCommand()); + self::assertEquals('/help@testbot some text', $message->getText()); + self::assertEquals('some text', $message->getText(true)); // /commmad\n text - $this->message = TestHelpers::getFakeMessageObject(['text' => "/help\n some text"]); - $this->assertEquals('/help', $this->message->getFullCommand()); - $this->assertEquals('help', $this->message->getCommand()); - $this->assertEquals("/help\n some text", $this->message->getText()); - $this->assertEquals(' some text', $this->message->getText(true)); + $message = TestHelpers::getFakeMessageObject(['text' => "/help\n some text"]); + self::assertEquals('/help', $message->getFullCommand()); + self::assertEquals('help', $message->getCommand()); + self::assertEquals("/help\n some text", $message->getText()); + self::assertEquals(' some text', $message->getText(true)); // /command@bot\nsome text - $this->message = TestHelpers::getFakeMessageObject(['text' => "/help@testbot\nsome text"]); - $this->assertEquals('/help@testbot', $this->message->getFullCommand()); - $this->assertEquals('help', $this->message->getCommand()); - $this->assertEquals("/help@testbot\nsome text", $this->message->getText()); - $this->assertEquals('some text', $this->message->getText(true)); + $message = TestHelpers::getFakeMessageObject(['text' => "/help@testbot\nsome text"]); + self::assertEquals('/help@testbot', $message->getFullCommand()); + self::assertEquals('help', $message->getCommand()); + self::assertEquals("/help@testbot\nsome text", $message->getText()); + self::assertEquals('some text', $message->getText(true)); // /command@bot \nsome text - $this->message = TestHelpers::getFakeMessageObject(['text' => "/help@testbot \nsome text"]); - $this->assertEquals('/help@testbot', $this->message->getFullCommand()); - $this->assertEquals('help', $this->message->getCommand()); - $this->assertEquals("/help@testbot \nsome text", $this->message->getText()); - $this->assertEquals("\nsome text", $this->message->getText(true)); - } + $message = TestHelpers::getFakeMessageObject(['text' => "/help@testbot \nsome text"]); + self::assertEquals('/help@testbot', $message->getFullCommand()); + self::assertEquals('help', $message->getCommand()); + self::assertEquals("/help@testbot \nsome text", $message->getText()); + self::assertEquals("\nsome text", $message->getText(true)); + } + + public function testGetType() + { + $message = TestHelpers::getFakeMessageObject(['text' => null]); + self::assertSame('message', $message->getType()); + + $message = TestHelpers::getFakeMessageObject(['text' => '/help']); + self::assertSame('command', $message->getType()); + + $message = TestHelpers::getFakeMessageObject(['text' => 'some text']); + self::assertSame('text', $message->getType()); + } } diff --git a/tests/unit/Entities/ReplyToMessageTest.php b/tests/unit/Entities/ReplyToMessageTest.php index 112e21843..c8287126c 100644 --- a/tests/unit/Entities/ReplyToMessageTest.php +++ b/tests/unit/Entities/ReplyToMessageTest.php @@ -10,7 +10,7 @@ namespace Longman\TelegramBot\Tests\Unit; -use \Longman\TelegramBot\Entities\Update; +use Longman\TelegramBot\Entities\Update; /** * @package TelegramTest @@ -21,29 +21,16 @@ */ class ReplyToMessageTest extends TestCase { - /** - * @var \Longman\TelegramBot\Entities\Message - */ - private $reply_to_message; - - /** - * @var \Longman\TelegramBot\Entities\Message - */ - private $message; - public function testChatType() { $json = ' {"update_id":137809335, "message":{"message_id":4479,"from":{"id":123,"first_name":"John","username":"MJohn"},"chat":{"id":-123,"title":"MyChat","type":"group"},"date":1449092987,"reply_to_message":{"message_id":11,"from":{"id":121,"first_name":"Myname","username":"mybot"},"chat":{"id":-123,"title":"MyChat","type":"group"},"date":1449092984,"text":"type some text"},"text":"some text"}} '; - $struct = json_decode($json, true); - $update = new Update($struct, 'mybot'); - - $this->message = $update->getMessage(); - $this->reply_to_message = $this->message->getReplyToMessage(); - $this->assertNull($this->reply_to_message->getReplyToMessage()); + $update = new Update(json_decode($json, true), 'mybot'); + $reply_to_message = $update->getMessage()->getReplyToMessage(); + self::assertNull($reply_to_message->getReplyToMessage()); } } diff --git a/tests/unit/Entities/ServerResponseTest.php b/tests/unit/Entities/ServerResponseTest.php index df4c2d10b..c14d7db0c 100644 --- a/tests/unit/Entities/ServerResponseTest.php +++ b/tests/unit/Entities/ServerResponseTest.php @@ -12,8 +12,9 @@ namespace Longman\TelegramBot\Tests\Unit; -use \Longman\TelegramBot\Entities\ServerResponse; -use \Longman\TelegramBot\Request; +use Longman\TelegramBot\Entities\Message; +use Longman\TelegramBot\Entities\ServerResponse; +use Longman\TelegramBot\Request; /** * @package TelegramTest @@ -24,11 +25,6 @@ */ class ServerResponseTest extends TestCase { - /** - * @var \Longman\TelegramBot\Entities\ServerResponse - */ - private $server; - public function sendMessageOk() { return '{ @@ -43,26 +39,27 @@ public function sendMessageOk() }'; } - public function testSendMessageOk() { - $result = $this->sendMessageOk(); - $this->server = new ServerResponse(json_decode($result, true), 'testbot'); - $server_result = $this->server->getResult(); + public function testSendMessageOk() + { + $result = $this->sendMessageOk(); + $server = new ServerResponse(json_decode($result, true), 'testbot'); + $server_result = $server->getResult(); - $this->assertTrue($this->server->isOk()); - $this->assertNull($this->server->getErrorCode()); - $this->assertNull($this->server->getDescription()); - $this->assertInstanceOf('\Longman\TelegramBot\Entities\Message', $server_result); + self::assertTrue($server->isOk()); + self::assertNull($server->getErrorCode()); + self::assertNull($server->getDescription()); + self::assertInstanceOf('\Longman\TelegramBot\Entities\Message', $server_result); //Message - $this->assertEquals('1234', $server_result->getMessageId()); - $this->assertEquals('123456789', $server_result->getFrom()->getId()); - $this->assertEquals('botname', $server_result->getFrom()->getFirstName()); - $this->assertEquals('namebot', $server_result->getFrom()->getUserName()); - $this->assertEquals('123456789', $server_result->getChat()->getId()); - $this->assertEquals('john', $server_result->getChat()->getFirstName()); - $this->assertEquals('Mjohn', $server_result->getChat()->getUserName()); - $this->assertEquals('1441378360', $server_result->getDate()); - $this->assertEquals('hello', $server_result->getText()); + self::assertEquals('1234', $server_result->getMessageId()); + self::assertEquals('123456789', $server_result->getFrom()->getId()); + self::assertEquals('botname', $server_result->getFrom()->getFirstName()); + self::assertEquals('namebot', $server_result->getFrom()->getUsername()); + self::assertEquals('123456789', $server_result->getChat()->getId()); + self::assertEquals('john', $server_result->getChat()->getFirstName()); + self::assertEquals('Mjohn', $server_result->getChat()->getUsername()); + self::assertEquals('1441378360', $server_result->getDate()); + self::assertEquals('hello', $server_result->getText()); //... they are not finished... } @@ -76,29 +73,31 @@ public function sendMessageFail() }'; } - public function testSendMessageFail() { + public function testSendMessageFail() + { $result = $this->sendMessageFail(); - $this->server = new ServerResponse(json_decode($result, true), 'testbot'); + $server = new ServerResponse(json_decode($result, true), 'testbot'); - $this->assertFalse($this->server->isOk()); - $this->assertNull($this->server->getResult()); - $this->assertEquals('400', $this->server->getErrorCode()); - $this->assertEquals('Error: Bad Request: wrong chat id', $this->server->getDescription()); + self::assertFalse($server->isOk()); + self::assertNull($server->getResult()); + self::assertEquals('400', $server->getErrorCode()); + self::assertEquals('Error: Bad Request: wrong chat id', $server->getDescription()); } - public function setWebhookOk() + public function setWebhookOk() { return '{"ok":true,"result":true,"description":"Webhook was set"}'; } - public function testSetWebhookOk() { + public function testSetWebhookOk() + { $result = $this->setWebhookOk(); - $this->server = new ServerResponse(json_decode($result, true), 'testbot'); + $server = new ServerResponse(json_decode($result, true), 'testbot'); - $this->assertTrue($this->server->isOk()); - $this->assertTrue($this->server->getResult()); - $this->assertNull($this->server->getErrorCode()); - $this->assertEquals('Webhook was set', $this->server->getDescription()); + self::assertTrue($server->isOk()); + self::assertTrue($server->getResult()); + self::assertNull($server->getErrorCode()); + self::assertEquals('Webhook was set', $server->getDescription()); } public function setWebhookFail() @@ -110,14 +109,15 @@ public function setWebhookFail() }'; } - public function testSetWebhookFail() { + public function testSetWebhookFail() + { $result = $this->setWebhookFail(); - $this->server = new ServerResponse(json_decode($result, true), 'testbot'); + $server = new ServerResponse(json_decode($result, true), 'testbot'); - $this->assertFalse($this->server->isOk()); - $this->assertNull($this->server->getResult()); - $this->assertEquals(400, $this->server->getErrorCode()); - $this->assertEquals("Error: Bad request: htttps://domain.host.org/dir/hook.php", $this->server->getDescription()); + self::assertFalse($server->isOk()); + self::assertNull($server->getResult()); + self::assertEquals(400, $server->getErrorCode()); + self::assertEquals('Error: Bad request: htttps://domain.host.org/dir/hook.php', $server->getDescription()); } public function getUpdatesArray() @@ -169,12 +169,13 @@ public function getUpdatesArray() }'; } - public function testGetUpdatesArray() { + public function testGetUpdatesArray() + { $result = $this->getUpdatesArray(); - $this->server = new ServerResponse(json_decode($result, true), 'testbot'); + $server = new ServerResponse(json_decode($result, true), 'testbot'); - $this->assertCount(4, $this->server->getResult()); - $this->assertInstanceOf('\Longman\TelegramBot\Entities\Update', $this->server->getResult()[0]); + self::assertCount(4, $server->getResult()); + self::assertInstanceOf('\Longman\TelegramBot\Entities\Update', $server->getResult()[0]); } public function getUpdatesEmpty() @@ -182,11 +183,12 @@ public function getUpdatesEmpty() return '{"ok":true,"result":[]}'; } - public function testGetUpdatesEmpty() { + public function testGetUpdatesEmpty() + { $result = $this->getUpdatesEmpty(); - $this->server = new ServerResponse(json_decode($result, true), 'testbot'); + $server = new ServerResponse(json_decode($result, true), 'testbot'); - $this->assertNull($this->server->getResult()); + self::assertEmpty($server->getResult()); } public function getUserProfilePhotos() @@ -218,18 +220,20 @@ public function getUserProfilePhotos() public function testGetUserProfilePhotos() { - $result = $this->getUserProfilePhotos(); - $this->server = new ServerResponse(json_decode($result, true), 'testbot'); - $server_result = $this->server->getResult(); + $result = $this->getUserProfilePhotos(); + $server = new ServerResponse(json_decode($result, true), 'testbot'); + $server_result = $server->getResult(); + $photos = $server_result->getPhotos(); //Photo count - $this->assertCount(3, $photos); + self::assertEquals(3, $server_result->getTotalCount()); + self::assertCount(3, $photos); //Photo size count - $this->assertCount(3, $photos[0]); + self::assertCount(3, $photos[0]); - $this->assertInstanceOf('\Longman\TelegramBot\Entities\UserProfilePhotos', $server_result); - $this->assertInstanceOf('\Longman\TelegramBot\Entities\PhotoSize', $photos[0][0]); + self::assertInstanceOf('\Longman\TelegramBot\Entities\UserProfilePhotos', $server_result); + self::assertInstanceOf('\Longman\TelegramBot\Entities\PhotoSize', $photos[0][0]); } public function getFile() @@ -247,45 +251,50 @@ public function getFile() public function testGetFile() { $result = $this->getFile(); - $this->server = new ServerResponse(json_decode($result, true), 'testbot'); + $server = new ServerResponse(json_decode($result, true), 'testbot'); - $this->assertInstanceOf('\Longman\TelegramBot\Entities\File', $this->server->getResult()); + self::assertInstanceOf('\Longman\TelegramBot\Entities\File', $server->getResult()); } - public function testSetGeneralTestFakeResponse() { + public function testSetGeneralTestFakeResponse() + { //setWebhook ok $fake_response = Request::generateGeneralFakeServerResponse(); - $this->server = new ServerResponse($fake_response, 'testbot'); + $server = new ServerResponse($fake_response, 'testbot'); - $this->assertTrue($this->server->isOk()); - $this->assertTrue($this->server->getResult()); - $this->assertNull($this->server->getErrorCode()); - $this->assertEquals('', $this->server->getDescription()); + self::assertTrue($server->isOk()); + self::assertTrue($server->getResult()); + self::assertNull($server->getErrorCode()); + self::assertEquals('', $server->getDescription()); //sendMessage ok $fake_response = Request::generateGeneralFakeServerResponse(['chat_id' => 123456789, 'text' => 'hello']); - $this->server = new ServerResponse($fake_response, 'testbot'); - $server_result = $this->server->getResult(); + $server = new ServerResponse($fake_response, 'testbot'); - $this->assertTrue($this->server->isOk()); - $this->assertNull($this->server->getErrorCode()); - $this->assertNull($this->server->getDescription()); - $this->assertInstanceOf('\Longman\TelegramBot\Entities\Message', $server_result); + /** @var Message $server_result */ + $server_result = $server->getResult(); + + self::assertTrue($server->isOk()); + self::assertNull($server->getErrorCode()); + self::assertNull($server->getDescription()); + self::assertInstanceOf('\Longman\TelegramBot\Entities\Message', $server_result); //Message - $this->assertEquals('1234', $server_result->getMessageId()); - $this->assertEquals('1441378360', $server_result->getDate()); - $this->assertEquals('hello', $server_result->getText()); + self::assertEquals('1234', $server_result->getMessageId()); + self::assertEquals('1441378360', $server_result->getDate()); + self::assertEquals('hello', $server_result->getText()); + //Message //User - $this->assertEquals('123456789', $server_result->getFrom()->getId()); - $this->assertEquals('botname', $server_result->getFrom()->getFirstName()); - $this->assertEquals('namebot', $server_result->getFrom()->getUserName()); + self::assertEquals('123456789', $server_result->getFrom()->getId()); + self::assertEquals('botname', $server_result->getFrom()->getFirstName()); + self::assertEquals('namebot', $server_result->getFrom()->getUsername()); + //Message //Chat - $this->assertEquals('123456789', $server_result->getChat()->getId()); - $this->assertEquals('', $server_result->getChat()->getFirstName()); - $this->assertEquals('', $server_result->getChat()->getUserName()); + self::assertEquals('123456789', $server_result->getChat()->getId()); + self::assertEquals('', $server_result->getChat()->getFirstName()); + self::assertEquals('', $server_result->getChat()->getUsername()); //... they are not finished... } diff --git a/tests/unit/Entities/UpdateTest.php b/tests/unit/Entities/UpdateTest.php index c2717b679..81c32039b 100644 --- a/tests/unit/Entities/UpdateTest.php +++ b/tests/unit/Entities/UpdateTest.php @@ -10,7 +10,7 @@ namespace Longman\TelegramBot\Tests\Unit; -use \Longman\TelegramBot\Entities\Update; +use Longman\TelegramBot\Entities\Update; /** * @package TelegramTest @@ -21,14 +21,9 @@ */ class UpdateTest extends TestCase { - /** - * @var \Longman\TelegramBot\Entities\Update - */ - private $update; - public function testUpdateCast() { - $json = ' + $json = ' {"update_id":137809336, "message":{"message_id":4479,"from":{"id":123,"first_name":"John","username":"MJohn"},"chat":{"id":-123,"title":"MyChat","type":"group"},"date":1449092987,"reply_to_message":{"message_id":11,"from":{"id":121,"first_name":"Myname","username":"mybot"},"chat":{"id":-123,"title":"MyChat","type":"group"},"date":1449092984,"text":"type some text"},"text":"some text"}} '; @@ -36,6 +31,6 @@ public function testUpdateCast() $update = new Update($struct, 'mybot'); $array_string_after = json_decode($update->toJson(), true); - $this->assertEquals($struct, $array_string_after); + self::assertEquals($struct, $array_string_after); } } diff --git a/tests/unit/Entities/UserTest.php b/tests/unit/Entities/UserTest.php index 3c650c4cc..a21660a0c 100644 --- a/tests/unit/Entities/UserTest.php +++ b/tests/unit/Entities/UserTest.php @@ -11,132 +11,76 @@ namespace Longman\TelegramBot\Tests\Unit; use Longman\TelegramBot\Entities\User; -use Longman\TelegramBot\Exception\TelegramException; /** - * @package UserTest - * @author Baev Nikolay + * @package TelegramTest + * @author Avtandil Kikabidze * @copyright Avtandil Kikabidze * @license http://opensource.org/licenses/mit-license.php The MIT License (MIT) * @link http://www.github.com/akalongman/php-telegram-bot */ class UserTest extends TestCase { - /** - * setUp - * - */ - public function setUp() + public function testInstance() { - //void + $user = new User(['id' => 1]); + self::assertInstanceOf('Longman\TelegramBot\Entities\User', $user); } - /** - * tearDown - * - */ - public function tearDown() + public function testGetId() { - //void + $user = new User(['id' => 123]); + self::assertEquals(123, $user->getId()); } - /** - * Test base stage - * - */ - public function testStageBase() + public function testTryMention() { - $user = new User(['id' => mt_rand(1, 99)]); - $this->assertInstanceOf('Longman\TelegramBot\Entities\User', $user); - } + // Username + $user = new User(['id' => 1, 'first_name' => 'John', 'last_name' => 'Taylor', 'username' => 'jtaylor']); + self::assertEquals('@jtaylor', $user->tryMention()); - /** - * Test sage without param user id - * - * @expectedException Longman\TelegramBot\Exception\TelegramException - * - */ - public function testStageWithoutId() - { - new User([]); - } + // First name. + $user = new User(['id' => 1, 'first_name' => 'John']); + self::assertEquals('John', $user->tryMention()); - /** - * Test stage get user id - * - */ - public function testGetId() - { - $user = new User(['id' => mt_rand(1, 99)]); - $result = $user->getId(); - $this->assertGreaterThan(0, $result); + // First and Last name. + $user = new User(['id' => 1, 'first_name' => 'John', 'last_name' => 'Taylor']); + self::assertEquals('John Taylor', $user->tryMention()); } - /** - * Test stage get first name - * - */ - public function testGetFirstName() + public function testEscapeMarkdown() { - $user = new User(['id' => mt_rand(1, 99), 'first_name' => 'name_phpunit']); - $result = $user->getFirstName(); - $this->assertEquals('name_phpunit', $result); - } + // Username. + $user = new User(['id' => 1, 'first_name' => 'John', 'last_name' => 'Taylor', 'username' => 'j_taylor']); + self::assertEquals('@j_taylor', $user->tryMention()); + self::assertEquals('@j\_taylor', $user->tryMention(true)); - /** - * Test stage get last name - * - */ - public function testGetLastName() - { - $user = new User(['id' => mt_rand(1, 99), 'last_name' => 'name_phpunit']); - $result = $user->getLastName(); - $this->assertEquals('name_phpunit', $result); - } + // First name. + $user = new User(['id' => 1, 'first_name' => 'John[']); + self::assertEquals('John[', $user->tryMention()); + self::assertEquals('John\[', $user->tryMention(true)); - /** - * Test stage get username - * - */ - public function testGetUsername() - { - $user = new User(['id' => mt_rand(1, 99), 'username' => 'name_phpunit']); - $result = $user->getUsername(); - $this->assertEquals('name_phpunit', $result); - } + // First and Last name. + $user = new User(['id' => 1, 'first_name' => 'John', 'last_name' => '`Taylor`']); + self::assertEquals('John `Taylor`', $user->tryMention()); + self::assertEquals('John \`Taylor\`', $user->tryMention(true)); - /** - * Test stage mention user - * - */ - public function testTryMention() - { - $user = new User(['id' => mt_rand(1, 99), 'username' => 'name_phpunit']); - $result = $user->tryMention(); - $this->assertRegExp('/^\@.*/', $result); + // Plain escapeMarkdown functionality. + self::assertEquals('a\`b\[c\*d\_e', $user->escapeMarkdown('a`b[c*d_e')); } - /** - * Test stage mention user without param username - * - */ - public function testTryMentionWithoutUsernameStageOne() + public function testGetProperties() { - $user = new User(['id' => mt_rand(1, 99), 'first_name' => 'name_phpunit']); - $result = $user->tryMention(); - $this->assertEquals('name_phpunit', $result); - } + // Username. + $user = new User(['id' => 1, 'username' => 'name_phpunit']); + self::assertEquals('name_phpunit', $user->getUsername()); - /** - * Test stage mention user without username but with last and first name - * - */ - public function testTryMentionWithoutUsernameStageTwo() - { - $user = new User(['id' => mt_rand(1, 99), 'first_name' => 'name_phpunit', - 'last_name' => 'name_phpunit']); - $result = $user->tryMention(); - $this->assertRegExp('/^.*\s{1}.*$/', $result); + // First name. + $user = new User(['id' => 1, 'first_name' => 'name_phpunit']); + self::assertEquals('name_phpunit', $user->getFirstName()); + + // Last name. + $user = new User(['id' => 1, 'last_name' => 'name_phpunit']); + self::assertEquals('name_phpunit', $user->getLastName()); } - } diff --git a/tests/unit/TestHelpers.php b/tests/unit/TestHelpers.php index f42fc1050..a9ef61ce7 100644 --- a/tests/unit/TestHelpers.php +++ b/tests/unit/TestHelpers.php @@ -151,6 +151,26 @@ public static function getFakeChatObject(array $data = []) return new Chat($data + self::$chat_template); } + /** + * Get fake recorded audio track + * + * @return array + */ + public static function getFakeRecordedAudio() + { + $mime_type = ['audio/ogg', 'audio/mpeg', 'audio/vnd.wave', 'audio/x-ms-wma', 'audio/basic']; + $data = [ + 'file_id' => mt_rand(1, 999), + 'duration' => (string)mt_rand(1, 99) . ':' . mt_rand(1, 60), + 'performer' => 'phpunit', + 'title' => 'track from phpunit', + 'mime_type' => $mime_type[array_rand($mime_type, 1)], + 'file_size' => mt_rand(1, 99999) + ]; + + return $data; + } + /** * Return a fake message object using the passed ids. * @@ -174,24 +194,6 @@ public static function getFakeMessageObject(array $message_data = [], array $use 'text' => 'dummy', ], 'testbot'); } - /** - * Get fake recorded audio track - * - * @return array - */ - public static function getFakeRecordedAudio() - { - $mime_type = ['audio/ogg', 'audio/mpeg', 'audio/vnd.wave', 'audio/x-ms-wma', 'audio/basic']; - $data = [ - 'file_id' => mt_rand(1, 999), - 'duration' => (string)mt_rand(1, 99) . ':' . mt_rand(1, 60), - 'performer' => 'phpunit', - 'title' => 'track from phpunit', - 'mime_type' => $mime_type[array_rand($mime_type, 1)], - 'file_size' => mt_rand(1, 99999) - ]; - return $data; - } /** * Start a fake conversation for the passed command and return the randomly generated ids.