Commit e060ae01 authored by MBoretto's avatar MBoretto

merge conflict

parents 2a96b072 e5b6d415
...@@ -66,7 +66,7 @@ and have interactions in a matter of minutes. ...@@ -66,7 +66,7 @@ and have interactions in a matter of minutes.
The Bot can: The Bot can:
- retrieve updates with webhook and getUpdate methods. - 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. - supports supergroups.
- handle commands in chat with other bots. - handle commands in chat with other bots.
- manage Channel from the bot admin interface. - manage Channel from the bot admin interface.
......
...@@ -364,6 +364,57 @@ class DB ...@@ -364,6 +364,57 @@ class DB
} }
} }
/**
* 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 * Insert request into database
* *
...@@ -562,64 +613,35 @@ class DB ...@@ -562,64 +613,35 @@ class DB
$date = self::getTimestamp($message->getDate()); $date = self::getTimestamp($message->getDate());
$forward_from = $message->getForwardFrom(); $forward_from = $message->getForwardFrom();
if ($forward_from) { $forward_from_chat = $message->getForwardFromChat();
$forward_date = self::getTimestamp($message->getForwardDate());
}
$photo = $message->getPhoto(); $photo = $message->getPhoto();
$entities = $message->getEntities(); $entities = $message->getEntities();
$new_chat_member = $message->getNewChatMember(); $new_chat_member = $message->getNewChatMember();
$new_chat_photo = $message->getNewChatPhoto(); $new_chat_photo = $message->getNewChatPhoto();
$left_chat_member = $message->getLeftChatMember(); $left_chat_member = $message->getLeftChatMember();
$migrate_from_chat_id = $message->getMigrateFromChatId(); $migrate_from_chat_id = $message->getMigrateFromChatId();
$migrate_to_chat_id = $message->getMigrateToChatId(); $migrate_to_chat_id = $message->getMigrateToChatId();
try { self::insertChat($chat, $date, $migrate_to_chat_id);
//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());
}
//Insert user and the relation with the chat //Insert user and the relation with the chat
self::insertUser($from, $date, $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 //Insert the forwarded message user in users table
$forward_from = null;
if (is_object($forward_from)) { if (is_object($forward_from)) {
self::insertUser($forward_from, $forward_date); self::insertUser($forward_from, $forward_date);
$forward_from = $forward_from->getId(); $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) { if ($new_chat_member) {
//Insert the new chat user //Insert the new chat user
self::insertUser($new_chat_member, $date, $chat); self::insertUser($new_chat_member, $date, $chat);
...@@ -634,7 +656,7 @@ class DB ...@@ -634,7 +656,7 @@ class DB
//message Table //message Table
$sth = self::$pdo->prepare('INSERT IGNORE INTO `' . TB_MESSAGE . '` $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`, `forward_date`, `reply_to_chat`, `reply_to_message`, `text`, `entities`, `audio`, `document`,
`photo`, `sticker`, `video`, `voice`, `caption`, `contact`, `photo`, `sticker`, `video`, `voice`, `caption`, `contact`,
`location`, `venue`, `new_chat_member`, `left_chat_member`, `location`, `venue`, `new_chat_member`, `left_chat_member`,
...@@ -643,7 +665,7 @@ class DB ...@@ -643,7 +665,7 @@ class DB
`migrate_from_chat_id`, `migrate_to_chat_id`, `pinned_message` `migrate_from_chat_id`, `migrate_to_chat_id`, `pinned_message`
) )
VALUES ( 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, :forward_date, :reply_to_chat, :reply_to_message, :text, :entities, :audio, :document,
:photo, :sticker, :video, :voice, :caption, :contact, :photo, :sticker, :video, :voice, :caption, :contact,
:location, :venue, :new_chat_member, :left_chat_member, :location, :venue, :new_chat_member, :left_chat_member,
...@@ -688,6 +710,7 @@ class DB ...@@ -688,6 +710,7 @@ class DB
$sth->bindParam(':user_id', $from_id, \PDO::PARAM_INT); $sth->bindParam(':user_id', $from_id, \PDO::PARAM_INT);
$sth->bindParam(':date', $date, \PDO::PARAM_STR); $sth->bindParam(':date', $date, \PDO::PARAM_STR);
$sth->bindParam(':forward_from', $forward_from, \PDO::PARAM_INT); $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); $sth->bindParam(':forward_date', $forward_date, \PDO::PARAM_STR);
$reply_chat_id = null; $reply_chat_id = null;
if ($reply_to_message_id) { if ($reply_to_message_id) {
......
...@@ -24,6 +24,8 @@ class Message extends Entity ...@@ -24,6 +24,8 @@ class Message extends Entity
protected $forward_from; protected $forward_from;
protected $forward_from_chat;
protected $forward_date; protected $forward_date;
protected $reply_to_message; protected $reply_to_message;
...@@ -120,6 +122,12 @@ class Message extends Entity ...@@ -120,6 +122,12 @@ class Message extends Entity
$this->forward_from = isset($data['forward_from']) ? $data['forward_from'] : null; $this->forward_from = isset($data['forward_from']) ? $data['forward_from'] : null;
if (!empty($this->forward_from)) { if (!empty($this->forward_from)) {
$this->forward_from = new User($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; $this->forward_date = isset($data['forward_date']) ? $data['forward_date'] : null;
...@@ -343,6 +351,11 @@ class Message extends Entity ...@@ -343,6 +351,11 @@ class Message extends Entity
return $this->forward_from; return $this->forward_from;
} }
public function getForwardFromChat()
{
return $this->forward_from_chat;
}
public function getForwardDate() public function getForwardDate()
{ {
return $this->forward_date; return $this->forward_date;
......
...@@ -18,9 +18,9 @@ class Sticker extends Entity ...@@ -18,9 +18,9 @@ class Sticker extends Entity
protected $width; protected $width;
protected $height; protected $height;
protected $thumb; protected $thumb;
protected $emoji;
protected $file_size; protected $file_size;
public function __construct(array $data) public function __construct(array $data)
{ {
...@@ -45,8 +45,9 @@ class Sticker extends Entity ...@@ -45,8 +45,9 @@ class Sticker extends Entity
} }
$this->thumb = new PhotoSize($this->thumb); $this->thumb = new PhotoSize($this->thumb);
$this->file_size = isset($data['file_size']) ? $data['file_size'] : null; $this->emoji = isset($data['emoji']) ? $data['emoji'] : null;
$this->file_size = isset($data['file_size']) ? $data['file_size'] : null;
} }
public function getFileId() public function getFileId()
...@@ -63,10 +64,17 @@ class Sticker extends Entity ...@@ -63,10 +64,17 @@ class Sticker extends Entity
{ {
return $this->height; return $this->height;
} }
public function getThumb() public function getThumb()
{ {
return $this->thumb; return $this->thumb;
} }
public function getEmoji()
{
return $this->emoji;
}
public function getFileSize() public function getFileSize()
{ {
return $this->file_size; return $this->file_size;
......
...@@ -30,7 +30,7 @@ class Telegram ...@@ -30,7 +30,7 @@ class Telegram
* *
* @var string * @var string
*/ */
protected $version = '0.31.0'; protected $version = '0.32.0';
/** /**
* Telegram API key * Telegram API key
......
...@@ -82,6 +82,7 @@ CREATE TABLE IF NOT EXISTS `message` ( ...@@ -82,6 +82,7 @@ CREATE TABLE IF NOT EXISTS `message` (
`user_id` bigint NULL COMMENT 'User identifier', `user_id` bigint NULL COMMENT 'User identifier',
`date` timestamp NULL DEFAULT NULL COMMENT 'Date the message was sent in timestamp format', `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` 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', `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_chat` bigint NULL DEFAULT NULL COMMENT 'Chat identifier.',
`reply_to_message` bigint UNSIGNED DEFAULT NULL COMMENT 'Message is a reply to another message.', `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` ( ...@@ -111,6 +112,7 @@ CREATE TABLE IF NOT EXISTS `message` (
PRIMARY KEY (`chat_id`, `id`), PRIMARY KEY (`chat_id`, `id`),
KEY `user_id` (`user_id`), KEY `user_id` (`user_id`),
KEY `forward_from` (`forward_from`), KEY `forward_from` (`forward_from`),
KEY `forward_from_chat` (`forward_from_chat`),
KEY `reply_to_chat` (`reply_to_chat`), KEY `reply_to_chat` (`reply_to_chat`),
KEY `reply_to_message` (`reply_to_message`), KEY `reply_to_message` (`reply_to_message`),
KEY `new_chat_member` (`new_chat_member`), KEY `new_chat_member` (`new_chat_member`),
...@@ -121,6 +123,7 @@ CREATE TABLE IF NOT EXISTS `message` ( ...@@ -121,6 +123,7 @@ CREATE TABLE IF NOT EXISTS `message` (
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`), FOREIGN KEY (`user_id`) REFERENCES `user` (`id`),
FOREIGN KEY (`chat_id`) REFERENCES `chat` (`id`), FOREIGN KEY (`chat_id`) REFERENCES `chat` (`id`),
FOREIGN KEY (`forward_from`) REFERENCES `user` (`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 (`reply_to_chat`, `reply_to_message`) REFERENCES `message` (`chat_id`,`id`),
FOREIGN KEY (`forward_from`) REFERENCES `user` (`id`), FOREIGN KEY (`forward_from`) REFERENCES `user` (`id`),
FOREIGN KEY (`new_chat_member`) REFERENCES `user` (`id`), FOREIGN KEY (`new_chat_member`) REFERENCES `user` (`id`),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment