public function testChatType()
    {
        $json = '
{"update_id":137809335,
"message":{"message_id":4479,"from":{"id":123,"first_name":"John","username":"******"},"chat":{"id":-123,"title":"MyChat","type":"group"},"date":1449092987,"reply_to_message":{"message_id":11,"from":{"id":121,"first_name":"Myname","username":"******"},"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());
    }
Exemplo n.º 2
0
 /**
  * Insert Edited Message request in db
  *
  * @param Entities\Message &$edited_message
  *
  * @return bool If the insert was successful
  * @throws TelegramException
  */
 public static function insertEditedMessageRequest(Message &$edited_message)
 {
     if (!self::isDbConnected()) {
         return false;
     }
     $from = $edited_message->getFrom();
     $chat = $edited_message->getChat();
     $chat_id = $chat->getId();
     $edit_date = self::getTimestamp($edited_message->getEditDate());
     $entities = $edited_message->getEntities();
     //Insert chat
     self::insertChat($chat, $edit_date);
     //Insert user and the relation with the chat
     self::insertUser($from, $edit_date, $chat);
     try {
         $sth = self::$pdo->prepare('INSERT INTO `' . TB_EDITED_MESSAGE . '`
             (
             `chat_id`, `message_id`, `user_id`, `edit_date`, `text`, `entities`, `caption`
             )
             VALUES (
             :chat_id, :message_id, :user_id, :date, :text, :entities, :caption
             )');
         $message_id = $edited_message->getMessageId();
         $from_id = $from->getId();
         $text = $edited_message->getText();
         $caption = $edited_message->getCaption();
         $sth->bindParam(':chat_id', $chat_id, \PDO::PARAM_INT);
         $sth->bindParam(':message_id', $message_id, \PDO::PARAM_INT);
         $sth->bindParam(':user_id', $from_id, \PDO::PARAM_INT);
         $sth->bindParam(':date', $edit_date, \PDO::PARAM_STR);
         $var = [];
         if (is_array($entities)) {
             foreach ($entities as $elm) {
                 $var[] = json_decode($elm, true);
             }
             $entities = json_encode($var);
         } else {
             $entities = null;
         }
         $sth->bindParam(':text', $text, \PDO::PARAM_STR);
         $sth->bindParam(':entities', $entities, \PDO::PARAM_STR);
         $sth->bindParam(':caption', $caption, \PDO::PARAM_STR);
         return $sth->execute();
     } catch (\PDOException $e) {
         throw new TelegramException($e->getMessage());
     }
 }
 /**
  * SendBack
  *
  * Received a message, the bot can send a copy of it to another chat/channel.
  * You don't have to care about the type of the message, the function detect it and use the proper
  * REQUEST:: function to send it.
  * $data include all the var that you need to send the message to the proper chat
  *
  * @todo This method will be moved at an higher level maybe in AdminCommand or Command
  * @todo Looking for a more significative name
  *
  * @param Entities\Message $message
  * @param array            $data
  *
  * @return Entities\ServerResponse
  */
 protected function sendBack(Message $message, array $data)
 {
     $type = $message->getType();
     $type = $type == 'command' ? 'Message' : $type;
     if ($type == 'Message') {
         $data['text'] = $message->getText(true);
     } 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') {
         $data['document'] = $message->getDocument()->getFileId();
     } elseif ($type == 'Photo') {
         $data['photo'] = $message->getPhoto()[0]->getFileId();
     } elseif ($type == 'Sticker') {
         $data['sticker'] = $message->getSticker()->getFileId();
     } elseif ($type == 'Video') {
         $data['video'] = $message->getVideo()->getFileId();
     } elseif ($type == 'Voice') {
         $data['voice'] = $message->getVoice()->getFileId();
     } elseif ($type == 'Location') {
         $data['latitude'] = $message->getLocation()->getLatitude();
         $data['longitude'] = $message->getLocation()->getLongitude();
     }
     $callback_path = 'Longman\\TelegramBot\\Request';
     $callback_function = 'send' . $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]);
 }
Exemplo n.º 4
0
 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));
     // 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));
     // /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));
     // /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));
     // /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));
     // /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));
     // /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));
     // /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));
 }
Exemplo n.º 5
0
 /**
  * Insert Edited Message request in db
  *
  * @param \Longman\TelegramBot\Entities\Message $edited_message
  *
  * @return bool If the insert was successful
  * @throws \Longman\TelegramBot\Exception\TelegramException
  */
 public static function insertEditedMessageRequest(Message $edited_message)
 {
     if (!self::isDbConnected()) {
         return false;
     }
     $from = $edited_message->getFrom();
     $chat = $edited_message->getChat();
     $chat_id = $chat->getId();
     $edit_date = self::getTimestamp($edited_message->getEditDate());
     $entities = self::entitiesArrayToJson($edited_message->getEntities(), null);
     //Insert chat
     self::insertChat($chat, $edit_date);
     //Insert user and the relation with the chat
     if (is_object($from)) {
         self::insertUser($from, $edit_date, $chat);
     }
     try {
         $sth = self::$pdo->prepare('
             INSERT IGNORE INTO `' . TB_EDITED_MESSAGE . '`
             (`chat_id`, `message_id`, `user_id`, `edit_date`, `text`, `entities`, `caption`)
             VALUES
             (:chat_id, :message_id, :user_id, :date, :text, :entities, :caption)
         ');
         $message_id = $edited_message->getMessageId();
         if (is_object($from)) {
             $from_id = $from->getId();
         } else {
             $from_id = null;
         }
         $text = $edited_message->getText();
         $caption = $edited_message->getCaption();
         $sth->bindParam(':chat_id', $chat_id, PDO::PARAM_INT);
         $sth->bindParam(':message_id', $message_id, PDO::PARAM_INT);
         $sth->bindParam(':user_id', $from_id, PDO::PARAM_INT);
         $sth->bindParam(':date', $edit_date, PDO::PARAM_STR);
         $sth->bindParam(':text', $text, PDO::PARAM_STR);
         $sth->bindParam(':entities', $entities, PDO::PARAM_STR);
         $sth->bindParam(':caption', $caption, PDO::PARAM_STR);
         return $sth->execute();
     } catch (PDOException $e) {
         throw new TelegramException($e->getMessage());
     }
 }
Exemplo n.º 6
0
 /**
  * Insert Message request in db
  *
  * @param Entities\Message &$message
  *
  * @return bool If the insert was successful
  */
 public static function insertMessageRequest(Message &$message)
 {
     if (!self::isDbConnected()) {
         return false;
     }
     $from = $message->getFrom();
     $chat = $message->getChat();
     $chat_id = $chat->getId();
     $date = self::getTimestamp($message->getDate());
     $forward_from = $message->getForwardFrom();
     $forward_from_chat = $message->getForwardFromChat();
     $photo = $message->getPhoto();
     $entities = $message->getEntities();
     $new_chat_member = $message->getNewChatMember();
     $new_chat_photo = $message->getNewChatPhoto();
     $left_chat_member = $message->getLeftChatMember();
     $migrate_from_chat_id = $message->getMigrateFromChatId();
     $migrate_to_chat_id = $message->getMigrateToChatId();
     self::insertChat($chat, $date, $migrate_to_chat_id);
     //Insert user and the relation with the chat
     self::insertUser($from, $date, $chat);
     //Forwarded object
     if ($forward_from || $forward_from_chat) {
         $forward_date = self::getTimestamp($message->getForwardDate());
     }
     //Insert the forwarded message user in users table
     if (is_object($forward_from)) {
         self::insertUser($forward_from, $forward_date);
         $forward_from = $forward_from->getId();
     }
     if (is_object($forward_from_chat)) {
         self::insertChat($forward_from_chat, $forward_date);
         $forward_from_chat = $forward_from_chat->getId();
     }
     //New and left chat member
     if ($new_chat_member) {
         //Insert the new chat user
         self::insertUser($new_chat_member, $date, $chat);
         $new_chat_member = $new_chat_member->getId();
     } elseif ($left_chat_member) {
         //Insert the left chat user
         self::insertUser($left_chat_member, $date, $chat);
         $left_chat_member = $left_chat_member->getId();
     }
     try {
         //message Table
         $sth = self::$pdo->prepare('INSERT IGNORE INTO `' . TB_MESSAGE . '`
             (
             `id`, `user_id`, `date`, `chat_id`, `forward_from`, `forward_from_chat`,
             `forward_date`, `reply_to_chat`, `reply_to_message`, `text`, `entities`, `audio`, `document`,
             `photo`, `sticker`, `video`, `voice`, `caption`, `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_from_chat_id`, `migrate_to_chat_id`, `pinned_message`
             )
             VALUES (
             :message_id, :user_id, :date, :chat_id, :forward_from, :forward_from_chat,
             :forward_date, :reply_to_chat, :reply_to_message, :text, :entities, :audio, :document,
             :photo, :sticker, :video, :voice, :caption, :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_from_chat_id, :migrate_to_chat_id, :pinned_message
             )');
         $message_id = $message->getMessageId();
         $from_id = $from->getId();
         $reply_to_message = $message->getReplyToMessage();
         $reply_to_message_id = null;
         if (is_object($reply_to_message)) {
             $reply_to_message_id = $reply_to_message->getMessageId();
             // please notice that, as explaied in the documentation, reply_to_message don't contain other
             // reply_to_message field so recursion deep is 1
             self::insertMessageRequest($reply_to_message);
         }
         $text = $message->getText();
         $audio = $message->getAudio();
         $document = $message->getDocument();
         $sticker = $message->getSticker();
         $video = $message->getVideo();
         $voice = $message->getVoice();
         $caption = $message->getCaption();
         $contact = $message->getContact();
         $location = $message->getLocation();
         $venue = $message->getVenue();
         $new_chat_title = $message->getNewChatTitle();
         $delete_chat_photo = $message->getDeleteChatPhoto();
         $group_chat_created = $message->getGroupChatCreated();
         $supergroup_chat_created = $message->getSupergroupChatCreated();
         $channel_chat_created = $message->getChannelChatCreated();
         $migrate_from_chat_id = $message->getMigrateFromChatId();
         $migrate_to_chat_id = $message->getMigrateToChatId();
         $pinned_message = $message->getPinnedMessage();
         $sth->bindParam(':chat_id', $chat_id, \PDO::PARAM_INT);
         $sth->bindParam(':message_id', $message_id, \PDO::PARAM_INT);
         $sth->bindParam(':user_id', $from_id, \PDO::PARAM_INT);
         $sth->bindParam(':date', $date, \PDO::PARAM_STR);
         $sth->bindParam(':forward_from', $forward_from, \PDO::PARAM_INT);
         $sth->bindParam(':forward_from_chat', $forward_from_chat, \PDO::PARAM_INT);
         $sth->bindParam(':forward_date', $forward_date, \PDO::PARAM_STR);
         $reply_chat_id = null;
         if ($reply_to_message_id) {
             $reply_chat_id = $chat_id;
         }
         $var = [];
         if (is_array($entities)) {
             foreach ($entities as $elm) {
                 $var[] = json_decode($elm, true);
             }
             $entities = json_encode($var);
         } else {
             $entities = null;
         }
         $sth->bindParam(':reply_to_chat', $reply_chat_id, \PDO::PARAM_INT);
         $sth->bindParam(':reply_to_message', $reply_to_message_id, \PDO::PARAM_INT);
         $sth->bindParam(':text', $text, \PDO::PARAM_STR);
         $sth->bindParam(':entities', $entities, \PDO::PARAM_STR);
         $sth->bindParam(':audio', $audio, \PDO::PARAM_STR);
         $sth->bindParam(':document', $document, \PDO::PARAM_STR);
         $var = [];
         if (is_array($photo)) {
             foreach ($photo as $elm) {
                 $var[] = json_decode($elm, true);
             }
             $photo = json_encode($var);
         } else {
             $photo = '';
         }
         $sth->bindParam(':photo', $photo, \PDO::PARAM_STR);
         $sth->bindParam(':sticker', $sticker, \PDO::PARAM_STR);
         $sth->bindParam(':video', $video, \PDO::PARAM_STR);
         $sth->bindParam(':voice', $voice, \PDO::PARAM_STR);
         $sth->bindParam(':caption', $caption, \PDO::PARAM_STR);
         $sth->bindParam(':contact', $contact, \PDO::PARAM_STR);
         $sth->bindParam(':location', $location, \PDO::PARAM_STR);
         $sth->bindParam(':venue', $venue, \PDO::PARAM_STR);
         $sth->bindParam(':new_chat_member', $new_chat_member, \PDO::PARAM_INT);
         $sth->bindParam(':left_chat_member', $left_chat_member, \PDO::PARAM_INT);
         $sth->bindParam(':new_chat_title', $new_chat_title, \PDO::PARAM_STR);
         //Array of Photosize
         $var = [];
         if (is_array($new_chat_photo)) {
             foreach ($new_chat_photo as $elm) {
                 $var[] = json_decode($elm, true);
             }
             $new_chat_photo = json_encode($var);
         } else {
             $new_chat_photo = '';
         }
         $sth->bindParam(':new_chat_photo', $new_chat_photo, \PDO::PARAM_STR);
         $sth->bindParam(':delete_chat_photo', $delete_chat_photo, \PDO::PARAM_STR);
         $sth->bindParam(':group_chat_created', $group_chat_created, \PDO::PARAM_STR);
         $sth->bindParam(':supergroup_chat_created', $supergroup_chat_created, \PDO::PARAM_INT);
         $sth->bindParam(':channel_chat_created', $channel_chat_created, \PDO::PARAM_INT);
         $sth->bindParam(':migrate_from_chat_id', $migrate_from_chat_id, \PDO::PARAM_INT);
         $sth->bindParam(':migrate_to_chat_id', $migrate_to_chat_id, \PDO::PARAM_INT);
         $sth->bindParam(':pinned_message', $pinned_message, \PDO::PARAM_INT);
         $status = $sth->execute();
     } catch (PDOException $e) {
         throw new TelegramException($e->getMessage());
     }
     return true;
 }