Commit fe4c5455 authored by Jack'lul's avatar Jack'lul

Merge pull request #202 from jacklul/quickupdate

Quick updates
parents 1549a4fd 0fcd30a8
......@@ -34,6 +34,11 @@ class Botan
*/
protected static $token = '';
/**
* @var string The actual command that is going to be reported
*/
public static $command = '';
/**
* Initilize botan
*/
......@@ -47,17 +52,30 @@ class Botan
}
/**
* Track function
* Lock function to make sure only the first command is reported
* ( in case commands are calling other commands $telegram->executedCommand() )
*
* @todo Advanced integration: https://github.com/botanio/sdk#advanced-integration
* @param string $command
*/
public static function lock($command = '')
{
if (empty(self::$command)) {
self::$command = $command;
}
}
/**
* Track function
*
* @param string $input
* @param string $command
*
* @return bool|string
* @throws TelegramException
*/
public static function track($input, $command = '')
{
if (empty(self::$token)) {
if (empty(self::$token) || $command != self::$command) {
return false;
}
......@@ -65,22 +83,26 @@ class Botan
throw new TelegramException('Input is empty!');
}
self::$command = '';
$obj = json_decode($input, true);
if (isset($obj['message'])) {
$data = $obj['message'];
if ((isset($obj['message']['entities']) && $obj['message']['entities'][0]['type'] == 'bot_command' && $obj['message']['entities'][0]['offset'] == 0) || substr($obj['message']['text'], 0, 1) == '/') {
if (strtolower($command) == 'generic') {
$command = 'Generic';
} elseif (strtolower($command) == 'genericmessage') {
$command = 'Generic Message';
} else {
$command = '/' . strtolower($command);
$event_name = 'Message';
if (isset($obj['message']['entities']) && is_array($obj['message']['entities'])) {
foreach ($obj['message']['entities'] as $entity) {
if ($entity['type'] == 'bot_command' && $entity['offset'] == 0) {
if (strtolower($command) == 'generic') {
$command = 'Generic';
} else {
$command = '/' . strtolower($command);
}
$event_name = 'Command (' . $command . ')';
break;
}
}
$event_name = 'Command ('.$command.')';
} else {
$event_name = 'Message';
}
} elseif (isset($obj['inline_query'])) {
$data = $obj['inline_query'];
......@@ -129,7 +151,9 @@ class Botan
*
* @param $url
* @param $user_id
*
* @return string
* @throws TelegramException
*/
public static function shortenUrl($url, $user_id)
{
......
......@@ -40,9 +40,9 @@ class InlinequeryCommand extends SystemCommand
$data = ['inline_query_id' => $inline_query->getId()];
$articles = [
['id' => '001', 'title' => 'https://core.telegram.org/bots/api#answerinlinequery', 'message_text' => 'you enter: ' . $query , 'input_message_content' => new InputTextMessageContent([ 'message_text' => $query ]) ],
['id' => '002', 'title' => 'https://core.telegram.org/bots/api#answerinlinequery', 'message_text' => 'you enter: ' . $query , 'input_message_content' => new InputTextMessageContent([ 'message_text' => $query ]) ],
['id' => '003', 'title' => 'https://core.telegram.org/bots/api#answerinlinequery', 'message_text' => 'you enter: ' . $query , 'input_message_content' => new InputTextMessageContent([ 'message_text' => $query ]) ],
['id' => '001', 'title' => 'https://core.telegram.org/bots/api#answerinlinequery', 'message_text' => 'you enter: ' . $query , 'input_message_content' => new InputTextMessageContent([ 'message_text' => ' ' . $query ])],
['id' => '002', 'title' => 'https://core.telegram.org/bots/api#answerinlinequery', 'message_text' => 'you enter: ' . $query , 'input_message_content' => new InputTextMessageContent([ 'message_text' => ' ' . $query ])],
['id' => '003', 'title' => 'https://core.telegram.org/bots/api#answerinlinequery', 'message_text' => 'you enter: ' . $query , 'input_message_content' => new InputTextMessageContent([ 'message_text' => ' ' . $query ])],
];
$array_article = [];
......
......@@ -43,7 +43,7 @@ class DB
/**
* PDO object
*
* @var PDO
* @var \PDO
*/
static protected $pdo;
......@@ -69,7 +69,8 @@ class DB
* @param string $table_prefix Table prefix
* @param string $encoding Database character encoding
*
* @return PDO PDO database object
* @return \PDO PDO database object
* @throws TelegramException
*/
public static function initialize(array $credentials, Telegram $telegram, $table_prefix = null, $encoding = 'utf8mb4')
{
......@@ -101,11 +102,12 @@ class DB
*
* Let you use the class with an external already existing Pdo Mysql connection.
*
* @param PDO $external_pdo_connection PDO database object
* @param \PDO $external_pdo_connection PDO database object
* @param Telegram $telegram Telegram object to connect with this object
* @param string $table_prefix Table prefix
*
* @return PDO PDO database object
* @return \PDO PDO database object
* @throws TelegramException
*/
public static function externalInitialize($external_pdo_connection, Telegram $telegram, $table_prefix = null)
{
......@@ -177,6 +179,7 @@ class DB
* @param int $limit Limit the number of updates to fetch
*
* @return array|bool Fetched data or false if not connected
* @throws TelegramException
*/
public static function selectTelegramUpdate($limit = null)
{
......@@ -209,6 +212,7 @@ class DB
* @param int $limit Limit the number of messages to fetch
*
* @return array|bool Fetched data or false if not connected
* @throws TelegramException
*/
public static function selectMessages($limit = null)
{
......@@ -255,8 +259,6 @@ class DB
/**
* Insert entry to telegram_update table
*
* @todo Needs to return something if successful
*
* @param int $id
* @param int $chat_id
* @param int $message_id
......@@ -265,7 +267,8 @@ class DB
* @param int $callback_query_id
* @param int $edited_message_id
*
* @return bool|null
* @return bool If the insert was successful
* @throws TelegramException
*/
public static function insertTelegramUpdate($id, $chat_id, $message_id, $inline_query_id, $chosen_inline_result_id, $callback_query_id, $edited_message_id)
{
......@@ -278,7 +281,6 @@ class DB
}
try {
//telegram_update table
$sth_insert_telegram_update = self::$pdo->prepare('INSERT IGNORE INTO `' . TB_TELEGRAM_UPDATE . '`
(
`id`, `chat_id`, `message_id`, `inline_query_id`, `chosen_inline_result_id`, `callback_query_id`, `edited_message_id`
......@@ -296,7 +298,7 @@ class DB
$sth_insert_telegram_update->bindParam(':callback_query_id', $callback_query_id, \PDO::PARAM_INT);
$sth_insert_telegram_update->bindParam(':edited_message_id', $edited_message_id, \PDO::PARAM_INT);
$status = $sth_insert_telegram_update->execute();
return $sth_insert_telegram_update->execute();
} catch (PDOException $e) {
throw new TelegramException($e->getMessage());
}
......@@ -305,13 +307,12 @@ class DB
/**
* Insert users and save their connection to chats
*
* @todo Needs to return something if successful
*
* @param Entities\User $user
* @param string $date
* @param string $date
* @param Entities\Chat $chat
*
* @return bool|null
* @return bool If the insert was successful
* @throws TelegramException
*/
public static function insertUser(User $user, $date, Chat $chat = null)
{
......@@ -325,7 +326,6 @@ class DB
$last_name = $user->getLastName();
try {
//user table
$sth1 = self::$pdo->prepare('INSERT INTO `' . TB_USER . '`
(
`id`, `username`, `first_name`, `last_name`, `created_at`, `updated_at`
......@@ -347,11 +347,11 @@ class DB
} catch (PDOException $e) {
throw new TelegramException($e->getMessage());
}
//insert also the relationship to the chat
//insert also the relationship to the chat into user_chat table
if (!is_null($chat)) {
$chat_id = $chat->getId();
try {
//user_chat table
$sth3 = self::$pdo->prepare('INSERT IGNORE INTO `' . TB_USER_CHAT . '`
(
`user_id`, `chat_id`
......@@ -368,16 +368,19 @@ class DB
throw new TelegramException($e->getMessage());
}
}
return $status;
}
/**
* Insert chat
*
* @todo Needs to return something if successful
*
* @param Entities\Chat $chat
* @param string $date
* @param int $migrate_to_chat_id
* @param string $date
* @param int $migrate_to_chat_id
*
* @return bool If the insert was successful
* @throws TelegramException
*/
public static function insertChat(Chat $chat, $date, $migrate_to_chat_id = null)
{
......@@ -390,7 +393,6 @@ class DB
$type = $chat->getType();
try {
//chat table
$sth2 = self::$pdo->prepare('INSERT INTO `' . TB_CHAT . '`
(
`id`, `type`, `title`, `created_at` ,`updated_at`, `old_id`
......@@ -415,7 +417,7 @@ class DB
$sth2->bindParam(':title', $chat_title, \PDO::PARAM_STR, 255);
$sth2->bindParam(':date', $date, \PDO::PARAM_STR);
$status = $sth2->execute();
return $sth2->execute();
} catch (PDOException $e) {
throw new TelegramException($e->getMessage());
}
......@@ -435,50 +437,61 @@ class DB
$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, null);
} elseif ($update->getUpdateType() == 'edited_message') {
$edited_message = $update->getEditedMessage();
$chat_id = $edited_message->getChat()->getId();
self::insertEditedMessageRequest($edited_message);
$edited_message_local_id = self::$pdo->lastInsertId();
return self::insertTelegramUpdate($update_id, $chat_id, null, null, null, null, $edited_message_local_id);
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();
$inline_query_id = $inline_query->getId();
self::insertInlineQueryRequest($inline_query);
return self::insertTelegramUpdate($update_id, null, null, $inline_query_id, null, null, null);
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();
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);
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();
$callback_query_id = $callback_query->getId();
self::insertCallbackQueryRequest($callback_query);
return self::insertTelegramUpdate($update_id, null, null, null, null, $callback_query_id, null);
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 inline query request into database
*
* @todo No return value at the end. Just return true?
*
* @param Entities\InlineQuery &$inline_query
*
* @return bool
* @return bool If the insert was successful
* @throws TelegramException
*/
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`, `location`, `query`, `offset`, `created_at`
......@@ -490,7 +503,7 @@ class DB
$sth_insert_inline_query = self::$pdo->prepare($mysql_query);
$date = self::getTimestamp(time());
$inline_query_id = (int) $inline_query->getId();
$inline_query_id = $inline_query->getId();
$from = $inline_query->getFrom();
$user_id = null;
if (is_object($from)) {
......@@ -509,7 +522,7 @@ class DB
$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();
return $sth_insert_inline_query->execute();
} catch (PDOException $e) {
throw new TelegramException($e->getMessage());
}
......@@ -520,15 +533,16 @@ class DB
*
* @param Entities\ChosenInlineResult &$chosen_inline_result
*
* @return bool
* @return bool If the insert was successful
* @throws TelegramException
*/
public static function insertChosenInlineResultRequest(ChosenInlineResult &$chosen_inline_result)
{
if (!self::isDbConnected()) {
return false;
}
try {
//Chosen inline result Table
$mysql_query = 'INSERT INTO `' . TB_CHOSEN_INLINE_RESULT . '`
(
`result_id`, `user_id`, `location`, `inline_message_id`, `query`, `created_at`
......@@ -559,7 +573,7 @@ class DB
$sth_insert_chosen_inline_result->bindParam(':query', $query, \PDO::PARAM_STR);
$sth_insert_chosen_inline_result->bindParam(':created_at', $date, \PDO::PARAM_STR);
$status = $sth_insert_chosen_inline_result->execute();
return $sth_insert_chosen_inline_result->execute();
} catch (PDOException $e) {
throw new TelegramException($e->getMessage());
}
......@@ -568,19 +582,18 @@ class DB
/**
* Insert callback query request into database
*
* @todo No return value at the end. Just return true?
*
* @param Entities\CallbackQuery &$callback_query
*
* @return bool
* @return bool If the insert was successful
* @throws TelegramException
*/
public static function insertCallbackQueryRequest(CallbackQuery &$callback_query)
{
if (!self::isDbConnected()) {
return false;
}
try {
//Callback query Table
$mysql_query = 'INSERT IGNORE INTO `' . TB_CALLBACK_QUERY . '`
(
`id`, `user_id`, `message`, `inline_message_id`, `data`, `created_at`
......@@ -592,7 +605,8 @@ class DB
$sth_insert_callback_query = self::$pdo->prepare($mysql_query);
$date = self::getTimestamp(time());
$callback_query_id = (int) $callback_query->getId();
$callback_query_id = $callback_query->getId();
$from = $callback_query->getFrom();
$user_id = null;
if (is_object($from)) {
......@@ -611,7 +625,7 @@ class DB
$sth_insert_callback_query->bindParam(':data', $data, \PDO::PARAM_STR);
$sth_insert_callback_query->bindParam(':created_at', $date, \PDO::PARAM_STR);
$status = $sth_insert_callback_query->execute();
return $sth_insert_callback_query->execute();
} catch (PDOException $e) {
throw new TelegramException($e->getMessage());
}
......@@ -623,6 +637,7 @@ class DB
* @param Entities\Message &$message
*
* @return bool If the insert was successful
* @throws TelegramException
*/
public static function insertMessageRequest(Message &$message)
{
......@@ -643,9 +658,9 @@ class DB
$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();
//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
......@@ -655,11 +670,13 @@ class DB
if ($forward_from || $forward_from_chat) {
$forward_date = self::getTimestamp($message->getForwardDate());
}
//Insert the forwarded message user in users table
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();
......@@ -677,7 +694,6 @@ class DB
}
try {
//message Table
$sth = self::$pdo->prepare('INSERT IGNORE INTO `' . TB_MESSAGE . '`
(
`id`, `user_id`, `date`, `chat_id`, `forward_from`, `forward_from_chat`,
......@@ -705,7 +721,7 @@ class DB
$reply_to_message_id = null;
if (is_object($reply_to_message)) {
$reply_to_message_id = $reply_to_message->getMessageId();
// please notice that, as explaied in the documentation, reply_to_message don't contain other
// 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);
}
......@@ -783,7 +799,7 @@ class DB
$sth->bindParam(':left_chat_member', $left_chat_member, \PDO::PARAM_INT);
$sth->bindParam(':new_chat_title', $new_chat_title, \PDO::PARAM_STR);
//Array of Photosize
//Array of PhotoSize
$var = [];
if (is_array($new_chat_photo)) {
foreach ($new_chat_photo as $elm) {
......@@ -804,12 +820,10 @@ class DB
$sth->bindParam(':migrate_to_chat_id', $migrate_to_chat_id, \PDO::PARAM_INT);
$sth->bindParam(':pinned_message', $pinned_message, \PDO::PARAM_INT);
$status = $sth->execute();
return $sth->execute();
} catch (PDOException $e) {
throw new TelegramException($e->getMessage());
}
return true;
}
/**
......@@ -818,6 +832,7 @@ class DB
* @param Entities\Message &$edited_message
*
* @return bool If the insert was successful
* @throws TelegramException
*/
public static function insertEditedMessageRequest(Message &$edited_message)
{
......@@ -835,7 +850,6 @@ class DB
$entities = $edited_message->getEntities();
try {
//edited_message Table
$sth = self::$pdo->prepare('INSERT IGNORE INTO `' . TB_EDITED_MESSAGE . '`
(
`chat_id`, `message_id`, `user_id`, `edit_date`, `text`, `entities`, `caption`
......@@ -870,24 +884,25 @@ class DB
$sth->bindParam(':entities', $entities, \PDO::PARAM_STR);
$sth->bindParam(':caption', $caption, \PDO::PARAM_STR);
$status = $sth->execute();
return $sth->execute();
} catch (PDOException $e) {
throw new TelegramException($e->getMessage());
}
return true;
}
/**
* Select Group and single Chats
* Select Group and/or single Chats
*
* @param bool $select_groups
* @param bool $select_super_groups
* @param bool $select_users
* @param bool $select_groups
* @param bool $select_super_groups
* @param bool $select_users
* @param string $date_from
* @param string $date_to
* @param int $chat_id
* @param string $text
*
* @return array|bool Selected chats or false if invalid arguments
* @return array|bool (Selected chats or false if invalid arguments)
* @throws TelegramException
*/
public static function selectChats(
$select_groups = true,
......
......@@ -515,11 +515,16 @@ class Telegram
//Handle a generic command or non existing one
$this->last_command_response = $this->executeCommand('Generic');
} else {
//Botan.io integration, make sure only the command user executed is reported
if ($this->botan_enabled) {
Botan::lock($command);
}
//execute() method is executed after preExecute()
//This is to prevent executing a DB query without a valid connection
$this->last_command_response = $command_obj->preExecute();
//Botan.io integration
//Botan.io integration, send report after executing the command
if ($this->botan_enabled) {
Botan::track($this->update, $command);
}
......@@ -582,9 +587,11 @@ class Telegram
$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 (($chosen_inline_result = $this->update->getChosenInlineResult()) && ($from = $chosen_inline_result->getFrom())) {
} elseif (($edited_message = $this->update->getEditedMessage()) && ($from = $edited_message->getFrom())) {
$user_id = $from->getId();
}
}
......@@ -808,7 +815,6 @@ class Telegram
* Enable Botan.io integration
*
* @param $token
* @param $custom
* @return Telegram
*/
public function enableBotan($token)
......
CREATE TABLE IF NOT EXISTS `user` (
`id` bigint COMMENT 'Unique user identifier',
`first_name` CHAR(255) NOT NULL DEFAULT '' COMMENT 'User first name',
`last_name` CHAR(255) DEFAULT NULL COMMENT 'User last name',
`username` CHAR(255) DEFAULT NULL COMMENT 'User username',
`first_name` CHAR(255) NOT NULL DEFAULT '' COMMENT 'User\'s first name',
`last_name` CHAR(255) DEFAULT NULL COMMENT 'User\'s last name',
`username` CHAR(255) DEFAULT NULL COMMENT 'User\'s 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=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
CREATE TABLE IF NOT EXISTS `chat` (
`id` bigint 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',
`type` ENUM('private', 'group', 'supergroup', 'channel') NOT NULL COMMENT 'Chat type, either private, group, supergroup or channel',
`title` CHAR(255) DEFAULT '' COMMENT 'Chat (group) title, is null if chat type is private',
`created_at` timestamp NULL DEFAULT NULL COMMENT 'Entry date creation',
`updated_at` timestamp NULL DEFAULT NULL COMMENT 'Entry date update',
`old_id` bigint DEFAULT NULL COMMENT 'Unique chat identifieri this is filled when a chat is converted to a superchat',
PRIMARY KEY (`id`),
KEY `old_id` (`old_id`)
`old_id` bigint DEFAULT NULL COMMENT 'Unique chat identifier, this is filled when a group is converted to a supergroup',
PRIMARY KEY (`id`),
KEY `old_id` (`old_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
CREATE TABLE IF NOT EXISTS `user_chat` (
`user_id` bigint COMMENT 'Unique user identifier',
`chat_id` bigint COMMENT 'Unique user or chat identifier',
PRIMARY KEY (`user_id`, `chat_id`),
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (`chat_id`) REFERENCES `chat` (`id`)
ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
CREATE TABLE IF NOT EXISTS `inline_query` (
`id` bigint UNSIGNED COMMENT 'Unique identifier for this query.',
`user_id` bigint NULL COMMENT 'Sender',
`location` CHAR(255) NULL DEFAULT NULL COMMENT 'Location of the sender',
`query` CHAR(255) NOT NULL DEFAULT '' COMMENT 'Text of the query',
`offset` CHAR(255) NOT NULL DEFAULT '' COMMENT 'Offset of the result',
`id` bigint UNSIGNED COMMENT 'Unique identifier for this query',
`user_id` bigint NULL COMMENT 'Unique user identifier',
`location` CHAR(255) NULL DEFAULT NULL COMMENT 'Location of the user',
`query` TEXT NOT NULL DEFAULT '' COMMENT 'Text of the query',
`offset` CHAR(255) NULL DEFAULT NULL 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`)
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`),
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
CREATE TABLE IF NOT EXISTS `chosen_inline_result` (
`id` bigint UNSIGNED 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',
`location` CHAR(255) NULL DEFAULT NULL COMMENT 'Location object, senders\'s location.',
`inline_message_id` CHAR(255) NULL DEFAULT NULL COMMENT 'Identifier of the message sent via the bot in inline mode, that originated the query',
`query` CHAR(255) NOT NULL DEFAULT '' COMMENT 'Text of the query',
`id` bigint UNSIGNED AUTO_INCREMENT COMMENT 'Unique identifier for this entry',
`result_id` CHAR(255) NOT NULL DEFAULT '' COMMENT 'Identifier for this result',
`user_id` bigint NULL COMMENT 'Unique user identifier',
`location` CHAR(255) NULL DEFAULT NULL COMMENT 'Location object, user\'s location',
`inline_message_id` CHAR(255) NULL DEFAULT NULL COMMENT 'Identifier of the sent inline message',
`query` TEXT NOT NULL DEFAULT '' COMMENT 'The query that was used to obtain 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`)
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`),
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
CREATE TABLE IF NOT EXISTS `callback_query` (
`id` bigint UNSIGNED COMMENT 'Unique identifier for this query.',
`user_id` bigint NULL COMMENT 'Sender',
`message` text NULL DEFAULT NULL COMMENT 'Message',
`id` bigint UNSIGNED COMMENT 'Unique identifier for this query',
`user_id` bigint NULL COMMENT 'Unique user identifier',
`message` text NULL DEFAULT NULL COMMENT 'Message with the callback button that originated the query',
`inline_message_id` CHAR(255) NULL DEFAULT NULL COMMENT 'Identifier of the message sent via the bot in inline mode, that originated the query',
`data` CHAR(255) NOT NULL DEFAULT '' COMMENT 'Data associated with the callback button.',
`data` CHAR(255) NOT NULL DEFAULT '' COMMENT 'Data associated with the callback button',
`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`)
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`),
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
CREATE TABLE IF NOT EXISTS `message` (
`chat_id` bigint COMMENT 'Chat identifier.',
`chat_id` bigint COMMENT 'Unique chat identifier',
`id` bigint UNSIGNED COMMENT 'Unique message identifier',
`user_id` bigint NULL COMMENT 'User identifier',
`user_id` bigint NULL COMMENT 'Unique 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.',
`forward_from` bigint NULL DEFAULT NULL COMMENT 'Unique user identifier, sender of the original message',
`forward_from_chat` bigint NULL DEFAULT NULL COMMENT 'Unique chat identifier, chat the original message belongs to',
`forward_date` timestamp NULL DEFAULT NULL COMMENT 'date the original message was sent in timestamp format',
`reply_to_chat` bigint NULL DEFAULT NULL COMMENT 'Unique chat identifier',
`reply_to_message` bigint UNSIGNED DEFAULT NULL COMMENT 'Message that this message is reply to',
`text` TEXT DEFAULT NULL COMMENT 'For text messages, the actual UTF-8 text of the message max message length 4096 char utf8mb4',
`entities` TEXT DEFAULT NULL COMMENT 'For text messages, special entities like usernames, URLs, bot commands, etc. that appear in the text',
`audio` TEXT DEFAULT NULL COMMENT 'Audio object. Message is an audio file, information about the file',
......@@ -91,21 +95,22 @@ CREATE TABLE IF NOT EXISTS `message` (
`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',
`venue` TEXT DEFAULT NULL COMMENT 'Venue object. Message is a Venue, information about the Venue',
`new_chat_member` 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_member` 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',
`caption` TEXT DEFAULT NULL COMMENT 'For message with caption, the actual UTF-8 text of the caption',
`new_chat_member` bigint NULL DEFAULT NULL COMMENT 'Unique user identifier, a new member was added to the group, information about them (this member may be bot itself)',
`left_chat_member` bigint NULL DEFAULT NULL COMMENT 'Unique user identifier, 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 chat title was changed to this value',
`new_chat_photo` TEXT DEFAULT NULL COMMENT 'Array of PhotoSize objects. A chat photo was change to this value',
`delete_chat_photo` tinyint(1) DEFAULT 0 COMMENT 'Informs that the chat 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.',
`pinned_message` TEXT NULL DEFAULT NULL COMMENT 'Pinned message, Message object.',
`migrate_to_chat_id` bigint NULL DEFAULT NULL COMMENT 'Migrate to chat identifier. The group has been migrated to a supergroup with the specified identifie',
`migrate_from_chat_id` bigint NULL DEFAULT NULL COMMENT 'Migrate from chat identifier. The supergroup has been migrated from a group with the specified identifier',
`pinned_message` TEXT NULL DEFAULT NULL COMMENT 'Message object. Specified message was pinned',
PRIMARY KEY (`chat_id`, `id`),
KEY `user_id` (`user_id`),
KEY `forward_from` (`forward_from`),
......@@ -125,7 +130,26 @@ CREATE TABLE IF NOT EXISTS `message` (
FOREIGN KEY (`forward_from`) REFERENCES `user` (`id`),
FOREIGN KEY (`new_chat_member`) REFERENCES `user` (`id`),
FOREIGN KEY (`left_chat_member`) REFERENCES `user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
CREATE TABLE IF NOT EXISTS `edited_message` (
`id` bigint UNSIGNED AUTO_INCREMENT COMMENT 'Unique identifier for this entry',
`chat_id` bigint COMMENT 'Unique chat identifier',
`message_id` bigint UNSIGNED COMMENT 'Unique message identifier',
`user_id` bigint NULL COMMENT 'Unique user identifier',
`edit_date` timestamp NULL DEFAULT NULL COMMENT 'Date the message was edited in timestamp format',
`text` TEXT DEFAULT NULL COMMENT 'For text messages, the actual UTF-8 text of the message max message length 4096 char utf8',
`entities` TEXT DEFAULT NULL COMMENT 'For text messages, special entities like usernames, URLs, bot commands, etc. that appear in the text',
`caption` TEXT DEFAULT NULL COMMENT 'For message with caption, the actual UTF-8 text of the caption',
PRIMARY KEY (`id`),
KEY `chat_id` (`chat_id`),
KEY `message_id` (`message_id`),
KEY `user_id` (`user_id`),
FOREIGN KEY (`chat_id`) REFERENCES `chat` (`id`),
FOREIGN KEY (`chat_id`, `message_id`) REFERENCES `message` (`chat_id`, `id`),
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
CREATE TABLE IF NOT EXISTS `edited_message` (
......@@ -148,13 +172,13 @@ CREATE TABLE IF NOT EXISTS `edited_message` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
CREATE TABLE IF NOT EXISTS `telegram_update` (
`id` bigint UNSIGNED COMMENT 'The update\'s unique identifier.',
`chat_id` bigint NULL DEFAULT NULL COMMENT 'Chat identifier.',
`id` bigint UNSIGNED COMMENT 'Update\'s unique identifier',
`chat_id` bigint NULL DEFAULT NULL COMMENT 'Unique chat identifier',
`message_id` bigint UNSIGNED DEFAULT NULL COMMENT 'Unique message identifier',
`inline_query_id` bigint UNSIGNED DEFAULT NULL COMMENT 'The inline query unique identifier.',
`chosen_inline_result_id` bigint UNSIGNED DEFAULT NULL COMMENT 'The chosen query unique identifier.',
`callback_query_id` bigint UNSIGNED DEFAULT NULL COMMENT 'The callback query unique identifier.',
`edited_message_id` bigint UNSIGNED DEFAULT NULL COMMENT 'Unique edited message identifier',
`inline_query_id` bigint UNSIGNED DEFAULT NULL COMMENT 'Unique inline query identifier',
`chosen_inline_result_id` bigint UNSIGNED DEFAULT NULL COMMENT 'Local chosen inline result identifier',
`callback_query_id` bigint UNSIGNED DEFAULT NULL COMMENT 'Unique callback query identifier',
`edited_message_id` bigint UNSIGNED DEFAULT NULL COMMENT 'Local edited message identifier',
PRIMARY KEY (`id`),
KEY `message_id` (`chat_id`, `message_id`),
......@@ -171,11 +195,11 @@ CREATE TABLE IF NOT EXISTS `telegram_update` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
CREATE TABLE IF NOT EXISTS `conversation` (
`id` bigint(20) unsigned AUTO_INCREMENT COMMENT 'Row unique id',
`user_id` bigint NULL DEFAULT NULL COMMENT 'User id',
`chat_id` bigint NULL DEFAULT NULL COMMENT 'Telegram chat_id can be a the user id or the chat id ',
`status` ENUM('active', 'cancelled', 'stopped') NOT NULL DEFAULT 'active' COMMENT 'active conversation is active, cancelled conversation has been truncated before end, stopped conversation has end',
`command` varchar(160) DEFAULT '' COMMENT 'Default Command to execute',
`id` bigint(20) unsigned AUTO_INCREMENT COMMENT 'Unique identifier for this entry',
`user_id` bigint NULL DEFAULT NULL COMMENT 'Unique user identifier',
`chat_id` bigint NULL DEFAULT NULL COMMENT 'Unique user or chat identifier',
`status` ENUM('active', 'cancelled', 'stopped') NOT NULL DEFAULT 'active' COMMENT 'Conversation state',
`command` varchar(160) DEFAULT '' COMMENT 'Default command to execute',
`notes` varchar(1000) DEFAULT 'NULL' COMMENT 'Data stored from command',
`created_at` timestamp NULL DEFAULT NULL COMMENT 'Entry date creation',
`updated_at` timestamp NULL DEFAULT NULL COMMENT 'Entry date update',
......@@ -187,10 +211,10 @@ CREATE TABLE IF NOT EXISTS `conversation` (
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`),
FOREIGN KEY (`chat_id`) REFERENCES `chat` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
CREATE TABLE IF NOT EXISTS `botan_shortener` (
`id` bigint UNSIGNED AUTO_INCREMENT COMMENT 'Unique identifier for this entry.',
`id` bigint UNSIGNED AUTO_INCREMENT COMMENT 'Unique identifier for this entry',
`user_id` bigint NULL DEFAULT NULL COMMENT 'Unique user identifier',
`url` text NOT NULL DEFAULT '' COMMENT 'Original URL',
`short_url` CHAR(255) NOT NULL DEFAULT '' COMMENT 'Shortened URL',
......@@ -199,4 +223,4 @@ CREATE TABLE IF NOT EXISTS `botan_shortener` (
PRIMARY KEY (`id`),
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_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