diff --git a/README.md b/README.md index d04bd5d44..545859399 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ and have interactions in a matter of minutes. The Bot can: - retrieve updates with webhook and getUpdate methods. -- supports all types and methods according to Telegram API (9 April 2016). +- supports all types and methods according to Telegram API (6 May 2016). - supports supergroups. - handle commands in chat with other bots. - manage Channel from the bot admin interface. diff --git a/src/DB.php b/src/DB.php index 61cc53616..cc463580e 100644 --- a/src/DB.php +++ b/src/DB.php @@ -363,6 +363,57 @@ public static function insertUser(User $user, $date, Chat $chat = null) } } + /** + * Insert chat + * + * @todo Needs to return something if successful + * + * @param Entities\Chat $chat + * @param string $date + * @param int $migrate_to_chat_id + */ + public static function insertChat(Chat $chat, $date, $migrate_to_chat_id = null) + { + if (!self::isDbConnected()) { + return false; + } + + $chat_id = $chat->getId(); + $chat_title = $chat->getTitle(); + $type = $chat->getType(); + + try { + //chat table + $sth2 = self::$pdo->prepare('INSERT INTO `' . TB_CHAT . '` + ( + `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 + '); + + if ($migrate_to_chat_id) { + $type = 'supergroup'; + + $sth2->bindParam(':id', $migrate_to_chat_id, \PDO::PARAM_INT); + $sth2->bindParam(':oldid', $chat_id, \PDO::PARAM_INT); + } else { + $sth2->bindParam(':id', $chat_id, \PDO::PARAM_INT); + $sth2->bindParam(':oldid', $migrate_to_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); + + $status = $sth2->execute(); + } catch (PDOException $e) { + throw new TelegramException($e->getMessage()); + } + } + /** * Insert request into database * @@ -561,64 +612,35 @@ public static function insertMessageRequest(Message &$message) $date = self::getTimestamp($message->getDate()); $forward_from = $message->getForwardFrom(); - if ($forward_from) { - $forward_date = self::getTimestamp($message->getForwardDate()); - } - + $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(); - try { - //chat table - $sth2 = self::$pdo->prepare('INSERT INTO `' . TB_CHAT . '` - ( - `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(); - - if ($migrate_to_chat_id) { - $type = 'supergroup'; - - $sth2->bindParam(':id', $migrate_to_chat_id, \PDO::PARAM_INT); - $sth2->bindParam(':oldid', $chat_id, \PDO::PARAM_INT); - } else { - $sth2->bindParam(':id', $chat_id, \PDO::PARAM_INT); - $sth2->bindParam(':oldid', $migrate_to_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); - - $status = $sth2->execute(); - } catch (PDOException $e) { - throw new TelegramException($e->getMessage()); - } + 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 - $forward_from = null; 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); @@ -633,7 +655,7 @@ public static function insertMessageRequest(Message &$message) //message Table $sth = self::$pdo->prepare('INSERT IGNORE INTO `' . TB_MESSAGE . '` ( - `id`, `user_id`, `date`, `chat_id`, `forward_from`, + `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`, @@ -642,7 +664,7 @@ public static function insertMessageRequest(Message &$message) `migrate_from_chat_id`, `migrate_to_chat_id`, `pinned_message` ) VALUES ( - :message_id, :user_id, :date, :chat_id, :forward_from, + :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, @@ -687,6 +709,7 @@ public static function insertMessageRequest(Message &$message) $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) { diff --git a/src/Entities/Message.php b/src/Entities/Message.php index 83f5d36ca..e51cc6183 100644 --- a/src/Entities/Message.php +++ b/src/Entities/Message.php @@ -24,6 +24,8 @@ class Message extends Entity protected $forward_from; + protected $forward_from_chat; + protected $forward_date; protected $reply_to_message; @@ -120,6 +122,12 @@ protected function init(array & $data, & $bot_name) $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; @@ -343,6 +351,11 @@ public function getForwardFrom() return $this->forward_from; } + public function getForwardFromChat() + { + return $this->forward_from_chat; + } + public function getForwardDate() { return $this->forward_date; diff --git a/src/Telegram.php b/src/Telegram.php index 33a353642..1d6ec8499 100644 --- a/src/Telegram.php +++ b/src/Telegram.php @@ -30,7 +30,7 @@ class Telegram * * @var string */ - protected $version = '0.31.0'; + protected $version = '0.32.0'; /** * Telegram API key diff --git a/structure.sql b/structure.sql index f48d6bcfd..9263c64e4 100644 --- a/structure.sql +++ b/structure.sql @@ -82,6 +82,7 @@ CREATE TABLE IF NOT EXISTS `message` ( `user_id` bigint NULL COMMENT 'User identifier', `date` timestamp NULL DEFAULT NULL COMMENT 'Date the message was sent in timestamp format', `forward_from` bigint NULL DEFAULT NULL COMMENT 'User id. For forwarded messages, sender of the original message', + `forward_from_chat` bigint NULL DEFAULT NULL COMMENT 'Chat id. For forwarded messages from channel', `forward_date` timestamp NULL DEFAULT NULL COMMENT 'For forwarded messages, date the original message was sent in Unix time', `reply_to_chat` bigint NULL DEFAULT NULL COMMENT 'Chat identifier.', `reply_to_message` bigint UNSIGNED DEFAULT NULL COMMENT 'Message is a reply to another message.', @@ -111,6 +112,7 @@ CREATE TABLE IF NOT EXISTS `message` ( PRIMARY KEY (`chat_id`, `id`), KEY `user_id` (`user_id`), KEY `forward_from` (`forward_from`), + KEY `forward_from_chat` (`forward_from_chat`), KEY `reply_to_chat` (`reply_to_chat`), KEY `reply_to_message` (`reply_to_message`), KEY `new_chat_member` (`new_chat_member`), @@ -121,6 +123,7 @@ CREATE TABLE IF NOT EXISTS `message` ( FOREIGN KEY (`user_id`) REFERENCES `user` (`id`), FOREIGN KEY (`chat_id`) REFERENCES `chat` (`id`), FOREIGN KEY (`forward_from`) REFERENCES `user` (`id`), + FOREIGN KEY (`forward_from_chat`) REFERENCES `chat` (`id`), FOREIGN KEY (`reply_to_chat`, `reply_to_message`) REFERENCES `message` (`chat_id`,`id`), FOREIGN KEY (`forward_from`) REFERENCES `user` (`id`), FOREIGN KEY (`new_chat_member`) REFERENCES `user` (`id`),