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"}} '; $update = new Update(json_decode($json, true), 'mybot'); $reply_to_message = $update->getMessage()->getReplyToMessage(); self::assertNull($reply_to_message->getReplyToMessage()); }
/** * @test */ public function testUpdateCast() { $json = ' {"update_id":137809336, "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'); $array_string_after = json_decode($update->toJSON(), true); $this->assertEquals($struct, $array_string_after); }
/** * Process bot Update request * * @param \Longman\TelegramBot\Entities\Update $update * * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public function processUpdate(Update $update) { $this->update = $update; //If all else fails, it's a generic message. $command = 'genericmessage'; $update_type = $this->update->getUpdateType(); if (in_array($update_type, ['inline_query', 'chosen_inline_result', 'callback_query', 'edited_message'], true)) { $command = $this->getCommandFromType($update_type); } elseif ($update_type === 'message') { $message = $this->update->getMessage(); //Load admin commands if ($this->isAdmin()) { $this->addCommandsPath(BASE_COMMANDS_PATH . '/AdminCommands', false); } $this->addCommandsPath(BASE_COMMANDS_PATH . '/UserCommands', false); $type = $message->getType(); if ($type === 'command') { $command = $message->getCommand(); } elseif (in_array($type, ['channel_chat_created', 'delete_chat_photo', 'group_chat_created', 'left_chat_member', 'migrate_from_chat_id', 'migrate_to_chat_id', 'new_chat_member', 'new_chat_photo', 'new_chat_title', 'supergroup_chat_created'], true)) { $command = $this->getCommandFromType($type); } } //Make sure we have an up-to-date command list //This is necessary to "require" all the necessary command files! $this->getCommandsList(); DB::insertRequest($this->update); return $this->executeCommand($command); }
/** * Set update object * * @param \Longman\TelegramBot\Entities\Update $update * * @return \Longman\TelegramBot\Commands\Command */ public function setUpdate(Update $update = null) { if ($update !== null) { $this->update = $update; $this->message = $this->update->getMessage(); } return $this; }
/** * Check if the passed user is an admin * * If no user id is passed, the current update is checked for a valid message sender. * * @param int|null $user_id * * @return bool */ public function isAdmin($user_id = null) { if ($user_id === null && $this->update !== null) { if (($message = $this->update->getMessage()) && ($from = $message->getFrom())) { $user_id = $from->getId(); } } return $user_id === null ? false : in_array($user_id, $this->admins_list); }
/** * Check if the passed user is an admin * * If no user id is passed, the current update is checked for a valid message sender. * * @param int|null $user_id * * @return bool */ public function isAdmin($user_id = null) { if ($user_id === null && $this->update !== null) { if (($message = $this->update->getMessage()) && ($from = $message->getFrom())) { $user_id = $from->getId(); } elseif (($inline_query = $this->update->getInlineQuery()) && ($from = $inline_query->getFrom())) { $user_id = $from->getId(); } elseif (($chosen_inline_result = $this->update->getChosenInlineResult()) && ($from = $chosen_inline_result->getFrom())) { $user_id = $from->getId(); } elseif (($callback_query = $this->update->getCallbackQuery()) && ($from = $callback_query->getFrom())) { $user_id = $from->getId(); } elseif (($edited_message = $this->update->getEditedMessage()) && ($from = $edited_message->getFrom())) { $user_id = $from->getId(); } } return $user_id === null ? false : in_array($user_id, $this->admins_list); }
/** * Insert request into database * * @todo self::$pdo->lastInsertId() - unsafe usage if expected previous insert fails? * * @param Entities\Update &$update * * @return bool */ public static function insertRequest(Update &$update) { $update_id = $update->getUpdateId(); if ($update->getUpdateType() == 'message') { $message = $update->getMessage(); if (self::insertMessageRequest($message)) { $message_id = $message->getMessageId(); $chat_id = $message->getChat()->getId(); return self::insertTelegramUpdate($update_id, $chat_id, $message_id, null, null, null, null); } } elseif ($update->getUpdateType() == 'inline_query') { $inline_query = $update->getInlineQuery(); if (self::insertInlineQueryRequest($inline_query)) { $inline_query_id = $inline_query->getId(); return self::insertTelegramUpdate($update_id, null, null, $inline_query_id, null, null, null); } } elseif ($update->getUpdateType() == 'chosen_inline_result') { $chosen_inline_result = $update->getChosenInlineResult(); if (self::insertChosenInlineResultRequest($chosen_inline_result)) { $chosen_inline_result_local_id = self::$pdo->lastInsertId(); return self::insertTelegramUpdate($update_id, null, null, null, $chosen_inline_result_local_id, null, null); } } elseif ($update->getUpdateType() == 'callback_query') { $callback_query = $update->getCallbackQuery(); if (self::insertCallbackQueryRequest($callback_query)) { $callback_query_id = $callback_query->getId(); return self::insertTelegramUpdate($update_id, null, null, null, null, $callback_query_id, null); } } elseif ($update->getUpdateType() == 'edited_message') { $edited_message = $update->getEditedMessage(); if (self::insertEditedMessageRequest($edited_message)) { $chat_id = $edited_message->getChat()->getId(); $edited_message_local_id = self::$pdo->lastInsertId(); return self::insertTelegramUpdate($update_id, $chat_id, null, null, null, null, $edited_message_local_id); } } return false; }
/** * Insert request in db * * @return bool */ public static function insertRequest(Update $update) { if (!self::isDbConnected()) { return false; } $message = $update->getMessage(); $from = $message->getFrom(); $chat = $message->getChat(); $chat_id = $chat->getId(); $date = self::toTimestamp($message->getDate()); $forward_from = $message->getForwardFrom(); $forward_date = self::toTimestamp($message->getForwardDate()); $photo = $message->getPhoto(); $new_chat_participant = $message->getNewChatParticipant(); $new_chat_photo = $message->getNewChatPhoto(); $left_chat_participant = $message->getLeftChatParticipant(); //inser 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)) { self::insertUser($forward_from, $forward_date); $forward_from = $forward_from->getId(); } else { $forward_from = ''; } //Insert the new chat user if (is_object($new_chat_participant)) { self::insertUser($new_chat_participant, $date, $chat); $new_chat_participant = $new_chat_participant->getId(); } else { $new_chat_participant = ''; } //Insert the left chat user if (is_object($left_chat_participant)) { self::insertUser($left_chat_participant, $date, $chat); $left_chat_participant = $left_chat_participant->getId(); } else { $left_chat_participant = ''; } try { //chats table $sth2 = self::$pdo->prepare('INSERT INTO `' . TB_CHATS . '` ( `id`, `title`, `created_at` ,`updated_at` ) VALUES (:id, :title, :date, :date) ON DUPLICATE KEY UPDATE `title`=:title, `updated_at`=:date '); $sth2->bindParam(':id', $chat_id, \PDO::PARAM_INT); $sth2->bindParam(':title', $chat->getTitle(), \PDO::PARAM_STR, 255); $sth2->bindParam(':date', $date, \PDO::PARAM_STR); $status = $sth2->execute(); //Messages Table $sth = self::$pdo->prepare('INSERT IGNORE INTO `' . TB_MESSAGES . '` ( `update_id`, `message_id`, `user_id`, `date`, `chat_id`, `forward_from`, `forward_date`, `reply_to_message`, `text`, `audio`, `document`, `photo`, `sticker`, `video`, `voice`, `caption`, `contact`, `location`, `new_chat_participant`, `left_chat_participant`, `new_chat_title`,`new_chat_photo`, `delete_chat_photo`, `group_chat_created` ) VALUES (:update_id, :message_id, :user_id, :date, :chat_id, :forward_from, :forward_date, :reply_to_message, :text, :audio, :document, :photo, :sticker, :video, :voice, :caption, :contact, :location, :new_chat_participant, :left_chat_participant, :new_chat_title, :new_chat_photo, :delete_chat_photo, :group_chat_created )'); $sth->bindParam(':update_id', $update->getUpdateId(), \PDO::PARAM_INT); $sth->bindParam(':message_id', $message->getMessageId(), \PDO::PARAM_INT); $sth->bindParam(':user_id', $from->getId(), \PDO::PARAM_INT); $sth->bindParam(':date', $date, \PDO::PARAM_STR); $sth->bindParam(':chat_id', $chat_id, \PDO::PARAM_STR); $sth->bindParam(':forward_from', $forward_from, \PDO::PARAM_STR); $sth->bindParam(':forward_date', $forward_date, \PDO::PARAM_STR); $sth->bindParam(':reply_to_message', $message->getReplyToMessage(), \PDO::PARAM_STR); $sth->bindParam(':text', $message->getText(), \PDO::PARAM_STR); $sth->bindParam(':audio', $message->getAudio(), \PDO::PARAM_STR); $sth->bindParam(':document', $message->getDocument(), \PDO::PARAM_STR); //Try with to magic shoud work $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', $message->getSticker(), \PDO::PARAM_STR); $sth->bindParam(':video', $message->getVideo(), \PDO::PARAM_STR); $sth->bindParam(':voice', $message->getVoice(), \PDO::PARAM_STR); $sth->bindParam(':caption', $message->getCaption(), \PDO::PARAM_STR); $sth->bindParam(':contact', $message->getContact(), \PDO::PARAM_STR); $sth->bindParam(':location', $message->getLocation(), \PDO::PARAM_STR); $sth->bindParam(':new_chat_participant', $new_chat_paticipant, \PDO::PARAM_STR); $sth->bindParam(':left_chat_participant', $left_chat_paticipant, \PDO::PARAM_STR); $sth->bindParam(':new_chat_title', $message->getNewChatTitle(), \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', $message->getDeleteChatPhoto(), \PDO::PARAM_STR); $sth->bindParam(':group_chat_created', $message->getGroupChatCreated(), \PDO::PARAM_STR); $status = $sth->execute(); } catch (PDOException $e) { throw new TelegramException($e->getMessage()); } return true; }
/** * Insert request in db * * @return bool */ protected function insertRequest(Update $update) { if (empty($this->pdo)) { return false; } $message = $update->getMessage(); try { $sth1 = $this->pdo->prepare('INSERT INTO `users` ( `id`, `username`, `first_name`, `last_name` ) VALUES (:id, :username, :first_name, :last_name) ON DUPLICATE KEY UPDATE `username`=:username, `first_name`=:first_name, `last_name`=:last_name '); $from = $message->getFrom(); $user_id = $from->getId(); $username = $from->getUsername(); $first_name = $from->getFirstName(); $last_name = $from->getLastName(); $sth1->bindParam(':id', $user_id, \PDO::PARAM_INT); $sth1->bindParam(':username', $username, \PDO::PARAM_STR, 255); $sth1->bindParam(':first_name', $first_name, \PDO::PARAM_STR, 255); $sth1->bindParam(':last_name', $last_name, \PDO::PARAM_STR, 255); $status = $sth1->execute(); $sth = $this->pdo->prepare('INSERT IGNORE INTO `messages` ( `update_id`, `message_id`, `user_id`, `date`, `chat`, `forward_from`, `forward_date`, `reply_to_message`, `text` ) VALUES (:update_id, :message_id, :user_id, :date, :chat, :forward_from, :forward_date, :reply_to_message, :text)'); $update_id = $update->getUpdateId(); $message_id = $message->getMessageId(); $from = $message->getFrom()->toJSON(); $user_id = $message->getFrom()->getId(); $date = $message->getDate(); $chat = $message->getChat()->toJSON(); $forward_from = $message->getForwardFrom(); $forward_date = $message->getForwardDate(); $reply_to_message = $message->getReplyToMessage(); if (is_object($reply_to_message)) { $reply_to_message = $reply_to_message->toJSON(); } $text = $message->getText(); $sth->bindParam(':update_id', $update_id, \PDO::PARAM_INT); $sth->bindParam(':message_id', $message_id, \PDO::PARAM_INT); $sth->bindParam(':user_id', $user_id, \PDO::PARAM_INT); $sth->bindParam(':date', $date, \PDO::PARAM_INT); $sth->bindParam(':chat', $chat, \PDO::PARAM_STR); $sth->bindParam(':forward_from', $forward_from, \PDO::PARAM_STR); $sth->bindParam(':forward_date', $forward_date, \PDO::PARAM_INT); $sth->bindParam(':reply_to_message', $reply_to_message, \PDO::PARAM_STR); $sth->bindParam(':text', $text, \PDO::PARAM_STR); $status = $sth->execute(); } catch (PDOException $e) { throw new TelegramException($e->getMessage()); } return true; }
/** * Insert request in db * * @return bool */ protected function insertRequest(Update $update) { if (empty($this->pdo)) { return false; } try { $sth = $this->pdo->prepare('INSERT INTO `messages` ( `update_id`, `message_id`, `from`, `date`, `chat`, `forward_from`, `forward_date`, `reply_to_message`, `text` ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)'); $message = $update->getMessage(); $update_id = $update->getUpdateId(); $message_id = $message->getMessageId(); $from = $message->getFrom()->toJSON(); $date = $message->getDate(); $chat = $message->getChat()->toJSON(); $forward_from = $message->getForwardFrom(); $forward_date = $message->getForwardDate(); $reply_to_message = $message->getReplyToMessage(); if (is_object($reply_to_message)) { $reply_to_message = $reply_to_message->toJSON(); } $text = $message->getText(); $sth->bindParam(1, $update_id, \PDO::PARAM_INT); $sth->bindParam(2, $message_id, \PDO::PARAM_INT); $sth->bindParam(3, $from, \PDO::PARAM_STR, 255); $sth->bindParam(4, $date, \PDO::PARAM_INT); $sth->bindParam(5, $chat, \PDO::PARAM_STR); $sth->bindParam(6, $forward_from, \PDO::PARAM_STR); $sth->bindParam(7, $forward_date, \PDO::PARAM_INT); $sth->bindParam(8, $reply_to_message, \PDO::PARAM_STR); $sth->bindParam(9, $text, \PDO::PARAM_STR); $status = $sth->execute(); } catch (PDOException $e) { throw new TelegramException($e->getMessage()); } return true; }
/** * Insert request in db * * @return bool */ public static function insertRequest(Update $update) { if (!self::isDbConnected()) { return false; } $message = $update->getMessage(); try { //Users table $sth1 = self::$pdo->prepare('INSERT INTO `' . TB_USERS . '` ( `id`, `username`, `first_name`, `last_name`, `created_at`, `updated_at` ) VALUES ( :id, :username, :first_name, :last_name, :date, :date ) ON DUPLICATE KEY UPDATE `username`=:username, `first_name`=:first_name, `last_name`=:last_name, `updated_at`=:date '); $from = $message->getFrom(); $date = self::toTimestamp($message->getDate()); $user_id = $from->getId(); $username = $from->getUsername(); $first_name = $from->getFirstName(); $last_name = $from->getLastName(); $sth1->bindParam(':id', $user_id, \PDO::PARAM_INT); $sth1->bindParam(':username', $username, \PDO::PARAM_STR, 255); $sth1->bindParam(':first_name', $first_name, \PDO::PARAM_STR, 255); $sth1->bindParam(':last_name', $last_name, \PDO::PARAM_STR, 255); $sth1->bindParam(':date', $date, \PDO::PARAM_STR); $status = $sth1->execute(); //chats table $sth2 = self::$pdo->prepare('INSERT INTO `' . TB_CHATS . '` ( `id`, `title`, `created_at` ,`updated_at` ) VALUES (:id, :title, :date, :date) ON DUPLICATE KEY UPDATE `title`=:title, `updated_at`=:date '); $chat_id = $message->getChat()->getId(); $chat_title = $message->getChat()->getTitle(); $sth2->bindParam(':id', $chat_id, \PDO::PARAM_INT); $sth2->bindParam(':title', $chat_title, \PDO::PARAM_STR, 255); $sth2->bindParam(':date', $date, \PDO::PARAM_STR); $status = $sth2->execute(); //user_chat table $sth3 = self::$pdo->prepare('INSERT IGNORE INTO `' . TB_USERS_CHATS . '` ( `user_id`, `chat_id` ) VALUES (:user_id, :chat_id) '); $sth3->bindParam(':user_id', $user_id, \PDO::PARAM_INT); $sth3->bindParam(':chat_id', $chat_id, \PDO::PARAM_INT); $status = $sth3->execute(); //Messages Table $sth = self::$pdo->prepare('INSERT IGNORE INTO `' . TB_MESSAGES . '` ( `update_id`, `message_id`, `user_id`, `date`, `chat_id`, `forward_from`, `forward_date`, `reply_to_message`, `text` ) VALUES (:update_id, :message_id, :user_id, :date, :chat_id, :forward_from, :forward_date, :reply_to_message, :text)'); $update_id = $update->getUpdateId(); $message_id = $message->getMessageId(); $forward_from = $message->getForwardFrom(); $forward_date = self::toTimestamp($message->getForwardDate()); $reply_to_message = $message->getReplyToMessage(); if (is_object($reply_to_message)) { $reply_to_message = $reply_to_message->toJSON(); } $text = $message->getText(); $sth->bindParam(':update_id', $update_id, \PDO::PARAM_INT); $sth->bindParam(':message_id', $message_id, \PDO::PARAM_INT); $sth->bindParam(':user_id', $user_id, \PDO::PARAM_INT); $sth->bindParam(':date', $date, \PDO::PARAM_STR); $sth->bindParam(':chat_id', $chat_id, \PDO::PARAM_STR); $sth->bindParam(':forward_from', $forward_from, \PDO::PARAM_STR); $sth->bindParam(':forward_date', $forward_date, \PDO::PARAM_STR); $sth->bindParam(':reply_to_message', $reply_to_message, \PDO::PARAM_STR); $sth->bindParam(':text', $text, \PDO::PARAM_STR); $status = $sth->execute(); } catch (PDOException $e) { throw new TelegramException($e->getMessage()); } return true; }
/** * Insert request in db * * @return bool */ public static function insertRequest(Update $update) { if (!self::isDbConnected()) { return false; } $message = $update->getMessage(); $from = $message->getFrom(); $chat = $message->getChat(); $chat_id = $chat->getId(); $date = self::getTimestamp($message->getDate()); $forward_from = $message->getForwardFrom(); $forward_date = self::getTimestamp($message->getForwardDate()); $photo = $message->getPhoto(); $new_chat_participant = $message->getNewChatParticipant(); $new_chat_photo = $message->getNewChatPhoto(); $left_chat_participant = $message->getLeftChatParticipant(); $migrate_from_chat_id = $message->getMigrateFromChatId(); if (is_null($migrate_from_chat_id)) { $migrate_from_chat_id = 0; } try { //chats table $sth2 = self::$pdo->prepare('INSERT INTO `' . TB_CHATS . '` (`id`, `type`, `title`, `created_at` ,`updated_at`, `old_id`) VALUES (:id, :type, :title, :date, :date, :oldid) ON DUPLICATE KEY UPDATE `type`=:type, `title`=:title, `updated_at`=:date'); $chat_title = $chat->getTitle(); $type = $chat->getType(); $sth2->bindParam(':id', $chat_id, \PDO::PARAM_INT); $sth2->bindParam(':type', $type, \PDO::PARAM_INT); $sth2->bindParam(':title', $chat_title, \PDO::PARAM_STR, 255); $sth2->bindParam(':date', $date, \PDO::PARAM_STR); $sth2->bindParam(':oldid', $migrate_from_chat_id, \PDO::PARAM_INT); $status = $sth2->execute(); } catch (PDOException $e) { throw new TelegramException($e->getMessage()); } //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)) { self::insertUser($forward_from, $forward_date); $forward_from = $forward_from->getId(); } else { $forward_from = ''; } //Insert the new chat user if (is_object($new_chat_participant)) { self::insertUser($new_chat_participant, $date, $chat); $new_chat_participant = $new_chat_participant->getId(); } else { $new_chat_participant = ''; } //Insert the left chat user if (is_object($left_chat_participant)) { self::insertUser($left_chat_participant, $date, $chat); $left_chat_participant = $left_chat_participant->getId(); } else { $left_chat_participant = ''; } try { //Messages Table $sth = self::$pdo->prepare('INSERT IGNORE INTO `' . TB_MESSAGES . '` ( `update_id`, `message_id`, `user_id`, `date`, `chat_id`, `forward_from`, `forward_date`, `reply_to_message`, `text`, `audio`, `document`, `photo`, `sticker`, `video`, `voice`, `caption`, `contact`, `location`, `new_chat_participant`, `left_chat_participant`, `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` ) VALUES (:update_id, :message_id, :user_id, :date, :chat_id, :forward_from, :forward_date, :reply_to_message, :text, :audio, :document, :photo, :sticker, :video, :voice, :caption, :contact, :location, :new_chat_participant, :left_chat_participant, :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 )'); $update_id = $update->getUpdateId(); $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(); //Creating a fake update to insert this message if privacy is on //text messages between users are not delivered to the bot //this can be also a message sent by the bot to the user $fake_update['update_id'] = 0; $fake_update['message'] = $reply_to_message->reflect(); // 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::insertRequest(new Update($fake_update, self::$telegram->getBotName(), 1)); } $text = $message->getText(); $audio = $message->getAudio(); $document = $message->getDocument(); $sticker = $message->getSticker(); $video = $message->getVideo(); $voice = $message->getVoice(); $caption = $message->getCaption(); $contanc = $message->getContact(); $location = $message->getLocation(); $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(); $sth->bindParam(':update_id', $update_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(':chat_id', $chat_id, \PDO::PARAM_STR); $sth->bindParam(':forward_from', $forward_from, \PDO::PARAM_STR); $sth->bindParam(':forward_date', $forward_date, \PDO::PARAM_STR); $sth->bindParam(':reply_to_message', $reply_to_message_id, \PDO::PARAM_INT); $sth->bindParam(':text', $text, \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', $contanct, \PDO::PARAM_STR); $sth->bindParam(':location', $location, \PDO::PARAM_STR); $sth->bindParam(':new_chat_participant', $new_chat_paticipant, \PDO::PARAM_STR); $sth->bindParam(':left_chat_participant', $left_chat_paticipant, \PDO::PARAM_STR); $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', $migrate_from_chat_id, \PDO::PARAM_INT); $sth->bindParam(':channel_chat_created', $supergroup_chat_created, \PDO::PARAM_INT); $sth->bindParam(':migrate_from_chat_id', $channel_chat_created, \PDO::PARAM_INT); $sth->bindParam(':migrate_to_chat_id', $migrate_from_chat_id, \PDO::PARAM_INT); $status = $sth->execute(); } catch (PDOException $e) { throw new TelegramException($e->getMessage()); } return true; }
/** * Insert request into database * * @param Entities\Update &$update * * @return bool */ public static function insertRequest(Update &$update) { $update_id = $update->getUpdateId(); if ($update->getUpdateType() == 'message') { $message = $update->getMessage(); $message_id = $message->getMessageId(); $chat_id = $message->getChat()->getId(); self::insertMessageRequest($message); return self::insertTelegramUpdate($update_id, $chat_id, $message_id, null, null, null); } elseif ($update->getUpdateType() == 'inline_query') { $inline_query = $update->getInlineQuery(); $inline_query_id = $inline_query->getId(); self::insertInlineQueryRequest($inline_query); return self::insertTelegramUpdate($update_id, null, null, $inline_query_id, null, null); } elseif ($update->getUpdateType() == 'chosen_inline_result') { $chosen_inline_query = $update->getChosenInlineResult(); if (!self::isDbConnected()) { return false; } try { //Inline query Table $mysql_query = 'INSERT INTO `' . TB_CHOSEN_INLINE_QUERY . '` ( `result_id`, `user_id`, `location`, `inline_message_id`, `query`, `created_at` ) VALUES ( :result_id, :user_id, :location, :inline_message_id, :query, :created_at )'; $sth_insert_chosen_inline_query = self::$pdo->prepare($mysql_query); $date = self::getTimestamp(time()); $result_id = $chosen_inline_query->getResultId(); $from = $chosen_inline_query->getFrom(); $user_id = null; if (is_object($from)) { $user_id = $from->getId(); self::insertUser($from, $date); } $location = $chosen_inline_query->getLocation(); $inline_message_id = $chosen_inline_query->getInlineMessageId(); $query = $chosen_inline_query->getQuery(); $sth_insert_chosen_inline_query->bindParam(':result_id', $result_id, \PDO::PARAM_STR); $sth_insert_chosen_inline_query->bindParam(':user_id', $user_id, \PDO::PARAM_INT); $sth_insert_chosen_inline_query->bindParam(':location', $location, \PDO::PARAM_INT); $sth_insert_chosen_inline_query->bindParam(':inline_message_id', $inline_message_id, \PDO::PARAM_INT); $sth_insert_chosen_inline_query->bindParam(':query', $query, \PDO::PARAM_STR); $sth_insert_chosen_inline_query->bindParam(':created_at', $date, \PDO::PARAM_STR); $status = $sth_insert_chosen_inline_query->execute(); $chosen_inline_query_local_id = self::$pdo->lastInsertId(); } catch (PDOException $e) { throw new TelegramException($e->getMessage()); } return self::insertTelegramUpdate($update_id, null, null, null, $chosen_inline_query_local_id, null); } elseif ($update->getUpdateType() == 'callback_query') { $callback_query = $update->getCallbackQuery(); $callback_query_id = $callback_query->getId(); self::insertCallbackQueryRequest($callback_query); return self::insertTelegramUpdate($update_id, null, null, null, null, $callback_query_id); } }
/** * Handle bot request * * @return \Longman\TelegramBot\Telegram */ public function handle() { $this->input = Request::getInput(); if (empty($this->input)) { throw new \Exception('Input is empty!'); } $post = json_decode($this->input, true); if (empty($post)) { throw new \Exception('Invalid JSON!'); } $update = new Update($post); $command = $update->getMessage()->getCommand(); if (!empty($command)) { return $this->executeCommand($command, $update); } }