Longman\TelegramBot\DB::insertMessageRequest PHP Method

insertMessageRequest() public static method

Insert Message request in db
public static insertMessageRequest ( Message $message ) : boolean
$message Longman\TelegramBot\Entities\Message
return boolean 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 = self::entitiesArrayToJson($message->getPhoto(), '');
        $entities = self::entitiesArrayToJson($message->getEntities(), null);
        $new_chat_member = $message->getNewChatMember();
        $new_chat_photo = self::entitiesArrayToJson($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
        if (is_object($from)) {
            self::insertUser($from, $date, $chat);
        }
        //Insert the forwarded message user in users table
        if ($forward_from instanceof User) {
            $forward_date = self::getTimestamp($message->getForwardDate());
            self::insertUser($forward_from, $forward_date);
            $forward_from = $forward_from->getId();
        }
        if ($forward_from_chat instanceof 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 instanceof User) {
            //Insert the new chat user
            self::insertUser($new_chat_member, $date, $chat);
            $new_chat_member = $new_chat_member->getId();
        } elseif ($left_chat_member instanceof User) {
            //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();
            if (is_object($from)) {
                $from_id = $from->getId();
            } else {
                $from_id = null;
            }
            $reply_to_message = $message->getReplyToMessage();
            $reply_to_message_id = null;
            if ($reply_to_message instanceof ReplyToMessage) {
                $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_to_chat_id = null;
            if ($reply_to_message_id) {
                $reply_to_chat_id = $chat_id;
            }
            $sth->bindParam(':reply_to_chat', $reply_to_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);
            $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);
            $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());
        }
    }

Usage Example

 /**
  * Start a fake conversation for the passed command and return the randomly generated ids.
  *
  * @return array
  */
 public static function startFakeConversation()
 {
     if (!DB::isDbConnected()) {
         return false;
     }
     //Just get some random values.
     $message_id = mt_rand();
     $user_id = mt_rand();
     $chat_id = mt_rand();
     //Make sure we have a valid user and chat available.
     $message = self::getFakeMessageObject(['message_id' => $message_id], ['id' => $user_id], ['id' => $chat_id]);
     DB::insertMessageRequest($message);
     DB::insertUser($message->getFrom(), null, $message->getChat());
     return compact('message_id', 'user_id', 'chat_id');
 }