Exemplo n.º 1
0
 /**
  * Insert Message request in db
  *
  * @param Entities\Message &$message
  *
  * @return bool If the insert was successful
  * @throws TelegramException
  */
 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_to_chat_id = $message->getMigrateToChatId();
     //Insert chat, update chat id in case it migrated
     self::insertChat($chat, $date, $migrate_to_chat_id);
     //Insert user and the relation with the chat
     self::insertUser($from, $date, $chat);
     //Insert the forwarded message user in users table
     if (is_object($forward_from)) {
         $forward_date = self::getTimestamp($message->getForwardDate());
         self::insertUser($forward_from, $forward_date);
         $forward_from = $forward_from->getId();
     }
     if (is_object($forward_from_chat)) {
         $forward_date = self::getTimestamp($message->getForwardDate());
         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 {
         $sth = self::$pdo->prepare('INSERT IGNORE INTO `' . TB_MESSAGE . '`
             (
             `id`, `user_id`, `chat_id`, `date`, `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, :chat_id, :date, :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 explained 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);
         return $sth->execute();
     } catch (\PDOException $e) {
         throw new TelegramException($e->getMessage());
     }
 }