Commit fa88ab2b authored by MBoretto's avatar MBoretto

new table schema works!

parent 820130b0
......@@ -12,6 +12,8 @@
namespace Longman\TelegramBot;
use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\Entities\Message;
use Longman\TelegramBot\Entities\InlineQuery;
use Longman\TelegramBot\Entities\User;
use Longman\TelegramBot\Entities\Chat;
use Longman\TelegramBot\Exception\TelegramException;
......@@ -75,17 +77,27 @@ class DB
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);
//Define table
if (!defined('TB_MESSAGES')) {
define('TB_MESSAGES', self::$table_prefix.'messages');
if (!defined('TB_TELEGRAM_UPDATE')) {
define('TB_TELEGRAM_UPDATE', self::$table_prefix.'telegram_update');
}
if (!defined('TB_USERS')) {
define('TB_USERS', self::$table_prefix.'users');
if (!defined('TB_MESSAGE')) {
define('TB_MESSAGE', self::$table_prefix.'message');
}
if (!defined('TB_CHATS')) {
define('TB_CHATS', self::$table_prefix.'chats');
if (!defined('TB_INLINE_QUERY')) {
define('TB_INLINE_QUERY', self::$table_prefix.'inline_query');
}
if (!defined('TB_USERS_CHATS')) {
define('TB_USERS_CHATS', self::$table_prefix.'users_chats');
if (!defined('TB_CHOSEN_INLINE_QUERY')) {
define('TB_CHOSEN_INLINE_QUERY', self::$table_prefix.'chosen_inline_query');
}
if (!defined('TB_USER')) {
define('TB_USER', self::$table_prefix.'user');
}
if (!defined('TB_CHAT')) {
define('TB_CHAT', self::$table_prefix.'chat');
}
if (!defined('TB_USER_CHAT')) {
define('TB_USER_CHAT', self::$table_prefix.'user_chat');
}
} catch (\PDOException $e) {
......@@ -111,6 +123,40 @@ class DB
}
}
/**
* fetch update from DB
*
* @param fetch Message from the DB
*
* @return bool/ array with data
*/
public static function selectTelegramUpdate($limit = null)
{
if (!self::isDbConnected()) {
return false;
}
try {
$query = 'SELECT `id` FROM `'.TB_TELEGRAM_UPDATE.'` ';
$query .= 'ORDER BY `id` DESC';
if (!is_null($limit)) {
$query .=' LIMIT :limit ';
}
$sth_select_telegram_update = self::$pdo->prepare($query);
$sth_select_telegram_update->bindParam(':limit', $limit, \PDO::PARAM_INT);
$sth_select_telegram_update->execute();
$results = $sth_select_telegram_update->fetchAll(\PDO::FETCH_ASSOC);
} catch (PDOException $e) {
throw new TelegramException($e->getMessage());
}
return $results;
}
/**
* fetch message from DB
*
......@@ -127,9 +173,9 @@ class DB
}
try {
$query = 'SELECT * FROM `'.TB_MESSAGES.'` ';
$query .= 'WHERE '.TB_MESSAGES.'.`update_id` != 0 ';
$query .= 'ORDER BY '.TB_MESSAGES.'.`message_id` DESC';
$query = 'SELECT * FROM `'.TB_MESSAGE.'` ';
$query .= 'WHERE '.TB_MESSAGE.'.`update_id` != 0 ';
$query .= 'ORDER BY '.TB_MESSAGE.'.`message_id` DESC';
if (!is_null($limit)) {
$query .=' LIMIT :limit ';
......@@ -155,11 +201,51 @@ class DB
protected static function getTimestamp($time = null)
{
if (is_null($time)) {
$time = time();
return null;
}
return date('Y-m-d H:i:s', $time);
}
/**
* Insert telegram_update table
*
* @return bool
*/
public static function insertTelegramUpdate($id, $message_id, $inline_query_id, $chosen_inline_query_id)
{
if (is_null($message_id) && is_null($inline_query_id) && is_null($chosen_inline_query_id)) {
throw new TelegramException("Error both query_id and message_id are null");
}
if (!self::isDbConnected()) {
return false;
}
try {
//Users table
$sth_insert_telegram_update = self::$pdo->prepare('INSERT IGNORE INTO `'.TB_TELEGRAM_UPDATE.'`
(
`id`, `message_id`, `inline_query_id`, `chosen_inline_query_id`
)
VALUES (
:id, :message_id, :inline_query_id, :chosen_inline_query_id
)
');
$sth_insert_telegram_update->bindParam(':id', $id, \PDO::PARAM_INT);
$sth_insert_telegram_update->bindParam(':message_id', $message_id, \PDO::PARAM_INT);
$sth_insert_telegram_update->bindParam(':inline_query_id', $inline_query_id, \PDO::PARAM_INT);
$sth_insert_telegram_update->bindParam(':chosen_inline_query_id', $chosen_inline_query_id, \PDO::PARAM_INT);
$status = $sth_insert_telegram_update->execute();
} catch (PDOException $e) {
throw new TelegramException($e->getMessage());
}
}
/**
* Insert users eventually connection to chats
*
......@@ -179,7 +265,7 @@ class DB
try {
//Users table
$sth1 = self::$pdo->prepare('INSERT INTO `'.TB_USERS.'`
$sth1 = self::$pdo->prepare('INSERT INTO `'.TB_USER.'`
(
`id`, `username`, `first_name`, `last_name`, `created_at`, `updated_at`
)
......@@ -206,7 +292,7 @@ class DB
$chat_id = $chat->getId();
try {
//user_chat table
$sth3 = self::$pdo->prepare('INSERT IGNORE INTO `'.TB_USERS_CHATS.'`
$sth3 = self::$pdo->prepare('INSERT IGNORE INTO `'.TB_USER_CHAT.'`
(
`user_id`, `chat_id`
)
......@@ -224,19 +310,125 @@ class DB
}
}
/**
* Insert request in db
*
* @return bool
*/
public static function insertRequest(Update $update)
public static function insertRequest(Update &$update)
{
$update_id = $update->getUpdateId();
if ($update->getUpdateType() == 'message') {
$message = $update->getMessage();
$message_id = $message->getMessageId();
self::insertMessageRequest($message);
return self::insertTelegramUpdate($update_id, $message_id, 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, $inline_query_id, 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`, `query`, `created_at`
)
VALUES (
:result_id, :user_id, :query , :created_at
)';
$message = $update->getMessage();
$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);
}
$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(':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, $chosen_inline_query_local_id);
}
}
/**
* Insert Message request in db
*
* @return bool
*/
public static function insertInlineQueryRequest(InlineQuery &$inline_query)
{
if (!self::isDbConnected()) {
return false;
}
try {
//Inline query Table
$mysql_query = 'INSERT IGNORE INTO `'.TB_INLINE_QUERY.'`
(
`id`, `user_id`, `query`, `offset`, `created_at`
)
VALUES (
:inline_query_id , :user_id, :query , :param_offset , :created_at
)';
$sth_insert_inline_query = self::$pdo->prepare($mysql_query);
$date = self::getTimestamp(time());
$inline_query_id = (int) $inline_query->getId();
$from = $inline_query->getFrom();
$user_id = null;
if (is_object($from)) {
$user_id = $from->getId();
self::insertUser($from, $date);
}
$query = $inline_query->getQuery();
$offset = $inline_query->getOffset();
$sth_insert_inline_query->bindParam(':inline_query_id', $inline_query_id, \PDO::PARAM_INT);
$sth_insert_inline_query->bindParam(':user_id', $user_id, \PDO::PARAM_INT);
$sth_insert_inline_query->bindParam(':query', $query, \PDO::PARAM_STR);
$sth_insert_inline_query->bindParam(':param_offset', $offset, \PDO::PARAM_STR);
$sth_insert_inline_query->bindParam(':created_at', $date, \PDO::PARAM_STR);
$status = $sth_insert_inline_query->execute();
} catch (PDOException $e) {
throw new TelegramException($e->getMessage());
}
}
/**
* Insert Message request in db
*
* @return bool
*/
public static function insertMessageRequest(Message &$message)
{
if (!self::isDbConnected()) {
return false;
}
$from = $message->getFrom();
$chat = $message->getChat();
......@@ -246,6 +438,7 @@ class DB
$date = self::getTimestamp($message->getDate());
$forward_from = $message->getForwardFrom();
$forward_date = self::getTimestamp($message->getForwardDate());
$photo = $message->getPhoto();
$new_chat_participant = $message->getNewChatParticipant();
......@@ -253,13 +446,10 @@ class DB
$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.'`
$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');
......@@ -288,7 +478,7 @@ class DB
self::insertUser($forward_from, $forward_date);
$forward_from = $forward_from->getId();
} else {
$forward_from = '';
$forward_from = null;
}
//Insert the new chat user
......@@ -310,9 +500,9 @@ class DB
try {
//Messages Table
$sth = self::$pdo->prepare('INSERT IGNORE INTO `'.TB_MESSAGES.'`
$sth = self::$pdo->prepare('INSERT IGNORE INTO `'.TB_MESSAGE.'`
(
`update_id`, `message_id`, `user_id`, `date`, `chat_id`, `forward_from`,
`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`,
......@@ -320,7 +510,7 @@ class DB
`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,
VALUES (: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,
......@@ -329,7 +519,6 @@ class DB
:migrate_from_chat_id, :migrate_to_chat_id
)');
$update_id = $update->getUpdateId();
$message_id = $message->getMessageId();
$from_id = $from->getId();
......@@ -337,14 +526,9 @@ class DB
$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));
self::insertMessageRequest($reply_to_message);
}
$text = $message->getText();
......@@ -363,12 +547,11 @@ class DB
$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(':chat_id', $chat_id, \PDO::PARAM_INT);
$sth->bindParam(':forward_from', $forward_from, \PDO::PARAM_INT);
$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);
......@@ -393,8 +576,8 @@ class DB
$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_participant', $new_chat_paticipant, \PDO::PARAM_INT);
$sth->bindParam(':left_chat_participant', $left_chat_paticipant, \PDO::PARAM_INT);
$sth->bindParam(':new_chat_title', $new_chat_title, \PDO::PARAM_STR);
//Array of Photosize
......@@ -449,11 +632,11 @@ class DB
}
try {
$query = 'SELECT * ,
'.TB_CHATS.'.`id` AS `chat_id`,
'.TB_CHATS.'.`updated_at` AS `chat_updated_at`,
'.TB_USERS.'.`id` AS `user_id`
FROM `'.TB_CHATS.'` LEFT JOIN `'.TB_USERS.'`
ON '.TB_CHATS.'.`id`='.TB_USERS.'.`id`';
'.TB_CHAT.'.`id` AS `chat_id`,
'.TB_CHAT.'.`updated_at` AS `chat_updated_at`,
'.TB_USER.'.`id` AS `user_id`
FROM `'.TB_CHAT.'` LEFT JOIN `'.TB_USER.'`
ON '.TB_CHAT.'.`id`='.TB_USER.'.`id`';
//Building parts of query
$chat_or_user = '';
......@@ -462,23 +645,23 @@ class DB
if (!$select_groups || !$select_users || !$select_super_groups) {
if ($select_groups) {
$where[] = TB_CHATS.'.`type` = "group"';
$where[] = TB_CHAT.'.`type` = "group"';
}
if ($select_super_groups) {
$where[] = TB_CHATS.'.`type` = "supergroup"';
$where[] = TB_CHAT.'.`type` = "supergroup"';
}
if ($select_users) {
$where[] = TB_CHATS.'.`type` = "private"';
$where[] = TB_CHAT.'.`type` = "private"';
}
}
if (! is_null($date_from)) {
$where[] = TB_CHATS.'.`updated_at` >= :date_from';
$where[] = TB_CHAT.'.`updated_at` >= :date_from';
$tokens[':date_from'] = $date_from;
}
if (! is_null($date_to)) {
$where[] = TB_CHATS.'.`updated_at` <= :date_to';
$where[] = TB_CHAT.'.`updated_at` <= :date_to';
$tokens[':date_to'] = $date_to;
}
......@@ -492,7 +675,7 @@ class DB
}
}
$query .= ' ORDER BY '.TB_CHATS.'.`updated_at` ASC';
$query .= ' ORDER BY '.TB_CHAT.'.`updated_at` ASC';
$sth = self::$pdo->prepare($query);
$sth->execute($tokens);
......
......@@ -37,12 +37,12 @@ class ChosenInlineResult extends Entity
$this->offset = isset($data['offset']) ? $data['offset'] : null;
}
public function getId()
public function getResultId()
{
return $this->id;
return $this->result_id;
}
public function geFrom()
public function getFrom()
{
return $this->from;
}
......
......@@ -21,7 +21,7 @@ class Update extends Entity
protected $chosen_inline_result;
private $update_type;
public function __construct(array $data, $bot_name, $let_update_id_empty = 0)
public function __construct(array $data, $bot_name)
{
$this->bot_name = $bot_name;
......@@ -35,11 +35,10 @@ class Update extends Entity
$this->update_type = 'message';
}
if (empty($update_id) && !$let_update_id_empty) {
if (empty($update_id)) {
throw new TelegramException('update_id is empty!');
}
$this->inline_query = isset($data['inline_query']) ? $data['inline_query'] : null;
if (!empty($this->inline_query)) {
$this->inline_query = new InlineQuery($this->inline_query);
......
......@@ -29,7 +29,7 @@ class Telegram
*
* @var string
*/
protected $version = '0.25.0';
protected $version = '0.26.0';
/**
* Telegram API key
......@@ -343,11 +343,11 @@ class Telegram
public function handleGetUpdates($limit = null, $timeout = null)
{
//DB Query
$last_message = DB::selectMessages(1);
$last_update = DB::selectTelegramUpdate(1);
if (isset($last_message[0]['update_id'])) {
if (isset($last_update[0]['id'])) {
//As explained in the telegram bot api documentation
$offset = $last_message[0]['update_id']+1;
$offset = $last_update[0]['id']+1;
} else {
$offset = null;
}
......@@ -418,8 +418,6 @@ class Telegram
}
}
DB::insertRequest($update);
// check type
$message = $update->getMessage();
$type = $message->getType();
......@@ -475,6 +473,7 @@ class Telegram
$command = 'Choseninlineresult';
}
DB::insertRequest($update);
$result = $this->executeCommand($command, $update);
return $result;
}
......
CREATE TABLE `messages` (
`message_id` bigint UNSIGNED COMMENT 'Unique message identifier',
`user_id` bigint COMMENT 'User identifier',
`chat_id` bigint NOT NULL DEFAULT '0' COMMENT 'Chat identifier.',
`date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT 'Date the message was sent in timestamp format',
`forward_from` bigint NOT NULL DEFAULT '0' COMMENT 'User id. For forwarded messages, sender of the original message',
`forward_date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT 'For forwarded messages, date the original message was sent in Unix time',
`reply_to_message` bigint UNSIGNED DEFAULT NULL COMMENT 'Message is a reply to another message.',
`text` TEXT COMMENT 'For text messages, the actual UTF-8 text of the message max message length 4096 char utf8',
`audio` TEXT DEFAULT '' COMMENT 'Audio object. Message is an audio file, information about the file',
`document` TEXT DEFAULT '' COMMENT 'Document object. Message is a general file, information about the file',
`photo` TEXT DEFAULT '' COMMENT 'Array of PhotoSize objects. Message is a photo, available sizes of the photo',
`sticker` TEXT DEFAULT '' COMMENT 'Sticker object. Message is a sticker, information about the sticker',
`video` TEXT DEFAULT '' COMMENT 'Video object. Message is a video, information about the video',
`voice` TEXT DEFAULT '' COMMENT 'Voice Object. Message is a Voice, information about the Voice',
`caption` TEXT COMMENT 'For message with caption, the actual UTF-8 text of the caption',
`contact` TEXT DEFAULT '' COMMENT 'Contact object. Message is a shared contact, information about the contact',
`location` TEXT DEFAULT '' COMMENT 'Location object. Message is a shared location, information about the location',
`new_chat_participant` bigint NOT NULL DEFAULT '0' COMMENT 'User id. A new member was added to the group, information about them (this member may be bot itself)',
`left_chat_participant` bigint NOT NULL DEFAULT '0' COMMENT 'User id. A member was removed from the group, information about them (this member may be bot itself)',
`new_chat_title` CHAR(255) DEFAULT '' COMMENT 'A group title was changed to this value',
`new_chat_photo` TEXT DEFAULT '' COMMENT 'Array of PhotoSize objects. A group photo was change to this value',
`delete_chat_photo` tinyint(1) DEFAULT 0 COMMENT 'Informs that the group photo was deleted',
`group_chat_created` tinyint(1) DEFAULT 0 COMMENT 'Informs that the group has been created',
`supergroup_chat_created` tinyint(1) DEFAULT 0 COMMENT 'Informs that the supergroup has been created',
`channel_chat_created` tinyint(1) DEFAULT 0 COMMENT 'Informs that the channel chat has been created',
`migrate_from_chat_id` bigint NOT NULL DEFAULT '0' COMMENT 'Migrate from chat identifier.',
`migrate_to_chat_id` bigint NOT NULL DEFAULT '0' COMMENT 'Migrate to chat identifier.',
`update_id` bigint UNSIGNED COMMENT 'The update\'s unique identifier.',
PRIMARY KEY (`message_id`),
KEY `user_id` (`chat_id`),
KEY `user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
CREATE TABLE `users` (
`id` bigint NOT NULL DEFAULT '0' COMMENT 'Unique user identifier',
CREATE TABLE IF NOT EXISTS `user` (
`id` bigint NULL DEFAULT NULL COMMENT 'Unique user identifier',
`first_name` CHAR(255) NOT NULL DEFAULT '' COMMENT 'User first name',
`last_name` CHAR(255) DEFAULT '' COMMENT 'User last name',
`username` CHAR(255) DEFAULT '' COMMENT 'User username',
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT 'Entry date creation',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT 'Entry date update',
`last_name` CHAR(255) DEFAULT NULL COMMENT 'User last name',
`username` CHAR(255) DEFAULT NULL COMMENT 'User username',
`created_at` timestamp NULL DEFAULT NULL COMMENT 'Entry date creation',
`updated_at` timestamp NULL DEFAULT NULL COMMENT 'Entry date update',
PRIMARY KEY (`id`),
KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
CREATE TABLE `chats` (
`id` bigint NOT NULL DEFAULT '0' COMMENT 'Unique user or chat identifier',
CREATE TABLE IF NOT EXISTS `chat` (
`id` bigint NULL DEFAULT NULL COMMENT 'Unique user or chat identifier',
`type` ENUM('private', 'group', 'supergroup', 'channel') NOT NULL COMMENT 'chat type private, group, supergroup or channel',
`title` CHAR(255) DEFAULT '' COMMENT 'chat title null if case of single chat with the bot',
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT 'Entry date creation',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT 'Entry date update',
`old_id` bigint NOT NULL DEFAULT '0' COMMENT 'Unique chat identifieri this is filled when a chat is converted to a superchat',
`created_at` timestamp NULL DEFAULT NULL COMMENT 'Entry date creation',
`updated_at` timestamp NULL DEFAULT NULL COMMENT 'Entry date update',
`old_id` bigint NULL DEFAULT NULL COMMENT 'Unique chat identifieri this is filled when a chat is converted to a superchat',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
CREATE TABLE `users_chats` (
`user_id` bigint NOT NULL DEFAULT '0' COMMENT 'Unique user identifier',
`chat_id` bigint NOT NULL DEFAULT '0' COMMENT 'Unique user or chat identifier',
CREATE TABLE IF NOT EXISTS `user_chat` (
`user_id` bigint NULL DEFAULT NULL COMMENT 'Unique user identifier',
`chat_id` bigint NULL DEFAULT NULL COMMENT 'Unique user or chat identifier',
PRIMARY KEY (`user_id`, `chat_id`),
FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (`chat_id`) REFERENCES `chats` (`id`)
FOREIGN KEY (`chat_id`) REFERENCES `chat` (`id`)
ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
CREATE TABLE IF NOT EXISTS `inline_query` (
`id` bigint UNSIGNED NULL COMMENT 'Unique identifier for this query.',
`user_id` bigint NULL COMMENT 'Sender',
`query` CHAR(255) NOT NULL DEFAULT '' COMMENT 'Text of the query',
`offset` CHAR(255) NOT NULL DEFAULT '' COMMENT 'Offset of the result',
`created_at` timestamp NULL DEFAULT NULL COMMENT 'Entry date creation',
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`),
FOREIGN KEY (`user_id`)
REFERENCES `user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
CREATE TABLE IF NOT EXISTS `chosen_inline_query` (
`id` bigint UNSIGNED NULL AUTO_INCREMENT COMMENT 'Unique identifier for chosen query.',
`result_id` CHAR(255) NOT NULL DEFAULT '' COMMENT 'Id of the chosen result',
`user_id` bigint NULL COMMENT 'Sender',
`query` CHAR(255) NOT NULL DEFAULT '' COMMENT 'Text of the query',
`created_at` timestamp NULL DEFAULT NULL COMMENT 'Entry date creation',
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`),
FOREIGN KEY (`user_id`)
REFERENCES `user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
CREATE TABLE IF NOT EXISTS `message` (
`id` bigint UNSIGNED NULL COMMENT 'Unique message identifier',
`user_id` bigint NULL COMMENT 'User identifier',
`chat_id` bigint NULL DEFAULT NULL COMMENT 'Chat 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_date` timestamp NULL DEFAULT NULL COMMENT 'For forwarded messages, date the original message was sent in Unix time',
`reply_to_message` bigint UNSIGNED DEFAULT NULL COMMENT 'Message is a reply to another message.',
`text` TEXT DEFAULT NULL COMMENT 'For text messages, the actual UTF-8 text of the message max message length 4096 char utf8',
`audio` TEXT DEFAULT NULL COMMENT 'Audio object. Message is an audio file, information about the file',
`document` TEXT DEFAULT NULL COMMENT 'Document object. Message is a general file, information about the file',
`photo` TEXT DEFAULT NULL COMMENT 'Array of PhotoSize objects. Message is a photo, available sizes of the photo',
`sticker` TEXT DEFAULT NULL COMMENT 'Sticker object. Message is a sticker, information about the sticker',
`video` TEXT DEFAULT NULL COMMENT 'Video object. Message is a video, information about the video',
`voice` TEXT DEFAULT NULL COMMENT 'Voice Object. Message is a Voice, information about the Voice',
`caption` TEXT DEFAULT NULL COMMENT 'For message with caption, the actual UTF-8 text of the caption',
`contact` TEXT DEFAULT NULL COMMENT 'Contact object. Message is a shared contact, information about the contact',
`location` TEXT DEFAULT NULL COMMENT 'Location object. Message is a shared location, information about the location',
`new_chat_participant` bigint NULL DEFAULT NULL COMMENT 'User id. A new member was added to the group, information about them (this member may be bot itself)',
`left_chat_participant` bigint NULL DEFAULT NULL COMMENT 'User id. A member was removed from the group, information about them (this member may be bot itself)',
`new_chat_title` CHAR(255) DEFAULT NULL COMMENT 'A group title was changed to this value',
`new_chat_photo` TEXT DEFAULT NULL COMMENT 'Array of PhotoSize objects. A group photo was change to this value',
`delete_chat_photo` tinyint(1) DEFAULT 0 COMMENT 'Informs that the group photo was deleted',
`group_chat_created` tinyint(1) DEFAULT 0 COMMENT 'Informs that the group has been created',
`supergroup_chat_created` tinyint(1) DEFAULT 0 COMMENT 'Informs that the supergroup has been created',
`channel_chat_created` tinyint(1) DEFAULT 0 COMMENT 'Informs that the channel chat has been created',
`migrate_from_chat_id` bigint NULL DEFAULT NULL COMMENT 'Migrate from chat identifier.',
`migrate_to_chat_id` bigint NULL DEFAULT NULL COMMENT 'Migrate to chat identifier.',
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`),
KEY `chat_id` (`chat_id`),
KEY `forward_from` (`forward_from`),
KEY `reply_to_message` (`reply_to_message`),
KEY `new_chat_participant` (`new_chat_participant`),
KEY `left_chat_participant` (`left_chat_participant`),
KEY `migrate_from_chat_id` (`migrate_from_chat_id`),
KEY `migrate_to_chat_id` (`migrate_to_chat_id`),
FOREIGN KEY (`user_id`)
REFERENCES `user` (`id`),
FOREIGN KEY (`chat_id`)
REFERENCES `chat` (`id`),
FOREIGN KEY (`forward_from`)
REFERENCES `user` (`id`),
FOREIGN KEY (`reply_to_message`)
REFERENCES `message` (`id`),
FOREIGN KEY (`forward_from`)
REFERENCES `user` (`id`),
FOREIGN KEY (`new_chat_participant`)
REFERENCES `user` (`id`),
FOREIGN KEY (`left_chat_participant`)
REFERENCES `user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
CREATE TABLE IF NOT EXISTS `telegram_update` (
`id` bigint UNSIGNED NULL COMMENT 'The update\'s unique identifier.',
`message_id` bigint UNSIGNED DEFAULT NULL COMMENT 'Unique message identifier',
`inline_query_id` bigint UNSIGNED DEFAULT NULL COMMENT 'The query unique identifier.',
`chosen_inline_query_id` bigint UNSIGNED DEFAULT NULL COMMENT 'The chosen query unique identifier.',
PRIMARY KEY (`id`),
KEY `message_id` (`message_id`),
KEY `inline_query_id` (`inline_query_id`),
KEY `chosen_inline_query_id` (`chosen_inline_query_id`),
FOREIGN KEY (`message_id`)
REFERENCES `message` (`id`),
FOREIGN KEY (`inline_query_id`)
REFERENCES `inline_query` (`id`),
FOREIGN KEY (`chosen_inline_query_id`)
REFERENCES `chosen_inline_query` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
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