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

Merge branch 'develop' into apiupdate

# Conflicts:
#	src/DB.php
#	structure.sql
parents 4854b66d 52e1ef1e
sudo: false
dist: trusty
sudo: required
addons:
apt:
packages:
- mysql-server-5.6
- mysql-client-core-5.6
- mysql-client-5.6
language: php
......@@ -27,7 +35,7 @@ install:
- travis_retry composer install --no-interaction
before_script:
- mysql -e 'create database telegrambot; use telegrambot; source structure.sql;'
- mysql -u root -e 'create database telegrambot; use telegrambot; source structure.sql;'
script:
- ./vendor/bin/phpunit
......
......@@ -11,9 +11,10 @@
namespace Longman\TelegramBot;
use Longman\TelegramBot\Entities\CallbackQuery;
use Longman\TelegramBot\Entities\Chat;
use Longman\TelegramBot\Entities\ChosenInlineResult;
use Longman\TelegramBot\Entities\InlineQuery;
use Longman\TelegramBot\Entities\CallbackQuery;
use Longman\TelegramBot\Entities\Message;
use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\Entities\User;
......@@ -66,17 +67,18 @@ class DB
* @param array $credentials Database connection details
* @param Telegram $telegram Telegram object to connect with this object
* @param string $table_prefix Table prefix
* @param string $encoding Database character encoding
*
* @return PDO PDO database object
*/
public static function initialize(array $credentials, Telegram $telegram, $table_prefix = null)
public static function initialize(array $credentials, Telegram $telegram, $table_prefix = null, $encoding = 'utf8mb4')
{
if (empty($credentials)) {
throw new TelegramException('MySQL credentials not provided!');
}
$dsn = 'mysql:host=' . $credentials['host'] . ';dbname=' . $credentials['database'];
$options = [\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'];
$options = [\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . $encoding];
try {
$pdo = new \PDO($dsn, $credentials['user'], $credentials['password'], $options);
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);
......@@ -138,12 +140,12 @@ class DB
if (!defined('TB_INLINE_QUERY')) {
define('TB_INLINE_QUERY', self::$table_prefix.'inline_query');
}
if (!defined('TB_CHOSEN_INLINE_RESULT')) {
define('TB_CHOSEN_INLINE_RESULT', self::$table_prefix.'chosen_inline_result');
}
if (!defined('TB_CALLBACK_QUERY')) {
define('TB_CALLBACK_QUERY', self::$table_prefix.'callback_query');
}
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');
}
......@@ -259,16 +261,16 @@ class DB
* @param int $chat_id
* @param int $message_id
* @param int $inline_query_id
* @param int $chosen_inline_query_id
* @param int $chosen_inline_result_id
* @param int $callback_query_id
* @param int $edited_message_id
*
* @return bool|null
*/
public static function insertTelegramUpdate($id, $chat_id, $message_id, $inline_query_id, $chosen_inline_query_id, $callback_query_id, $edited_message_id)
public static function insertTelegramUpdate($id, $chat_id, $message_id, $inline_query_id, $chosen_inline_result_id, $callback_query_id, $edited_message_id)
{
if (is_null($message_id) && is_null($inline_query_id) && is_null($chosen_inline_query_id) && is_null($callback_query_id) && is_null($edited_message_id)) {
throw new TelegramException('message_id, inline_query_id, chosen_inline_query_id, callback_query_id, edited_message_id are all null');
if (is_null($message_id) && is_null($inline_query_id) && is_null($chosen_inline_result_id) && is_null($callback_query_id) && is_null($edited_message_id)) {
throw new TelegramException('message_id, inline_query_id, chosen_inline_result_id, callback_query_id, edited_message_id are all null');
}
if (!self::isDbConnected()) {
......@@ -279,10 +281,10 @@ class DB
//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_query_id`, `callback_query_id`, `edited_message_id`
`id`, `chat_id`, `message_id`, `inline_query_id`, `chosen_inline_result_id`, `callback_query_id`, `edited_message_id`
)
VALUES (
:id, :chat_id, :message_id, :inline_query_id, :chosen_inline_query_id, :callback_query_id, :edited_message_id
:id, :chat_id, :message_id, :inline_query_id, :chosen_inline_result_id, :callback_query_id, :edited_message_id
)
');
......@@ -290,7 +292,7 @@ class DB
$sth_insert_telegram_update->bindParam(':chat_id', $chat_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);
$sth_insert_telegram_update->bindParam(':chosen_inline_result_id', $chosen_inline_result_id, \PDO::PARAM_INT);
$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);
......@@ -449,50 +451,10 @@ class DB
self::insertInlineQueryRequest($inline_query);
return self::insertTelegramUpdate($update_id, null, null, $inline_query_id, null, 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, null);
$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);
} elseif ($update->getUpdateType() == 'callback_query') {
$callback_query = $update->getCallbackQuery();
$callback_query_id = $callback_query->getId();
......@@ -553,6 +515,56 @@ class DB
}
}
/**
* Insert chosen inline result request into database
*
* @param Entities\ChosenInlineResult &$chosen_inline_result
*
* @return bool
*/
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`
)
VALUES (
:result_id, :user_id, :location, :inline_message_id, :query, :created_at
)';
$sth_insert_chosen_inline_result = self::$pdo->prepare($mysql_query);
$date = self::getTimestamp(time());
$result_id = $chosen_inline_result->getResultId();
$from = $chosen_inline_result->getFrom();
$user_id = null;
if (is_object($from)) {
$user_id = $from->getId();
self::insertUser($from, $date);
}
$location = $chosen_inline_result->getLocation();
$inline_message_id = $chosen_inline_result->getInlineMessageId();
$query = $chosen_inline_result->getQuery();
$sth_insert_chosen_inline_result->bindParam(':result_id', $result_id, \PDO::PARAM_STR);
$sth_insert_chosen_inline_result->bindParam(':user_id', $user_id, \PDO::PARAM_INT);
$sth_insert_chosen_inline_result->bindParam(':location', $location, \PDO::PARAM_INT);
$sth_insert_chosen_inline_result->bindParam(':inline_message_id', $inline_message_id, \PDO::PARAM_INT);
$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();
} catch (PDOException $e) {
throw new TelegramException($e->getMessage());
}
}
/**
* Insert callback query request into database
*
......@@ -724,6 +736,7 @@ class DB
$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) {
$reply_chat_id = $chat_id;
......
......@@ -181,9 +181,9 @@ class Telegram
*
* @return Telegram
*/
public function enableMySql(array $credential, $table_prefix = null)
public function enableMySql(array $credential, $table_prefix = null, $encoding = 'utf8mb4')
{
$this->pdo = DB::initialize($credential, $this, $table_prefix);
$this->pdo = DB::initialize($credential, $this, $table_prefix, $encoding);
ConversationDB::initializeConversation();
$this->mysql_enabled = true;
return $this;
......@@ -521,7 +521,7 @@ class Telegram
//Botan.io integration
if ($this->botan_enabled) {
Botan::track($this->input, $command);
Botan::track($this->update, $command);
}
}
......
CREATE TABLE IF NOT EXISTS `user` (
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',
......@@ -7,9 +7,9 @@ CREATE TABLE IF NOT EXISTS `user` (
`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;
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
CREATE TABLE IF NOT EXISTS `chat` (
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',
......@@ -18,7 +18,7 @@ CREATE TABLE IF NOT EXISTS `chat` (
`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`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
CREATE TABLE IF NOT EXISTS `user_chat` (
`user_id` bigint COMMENT 'Unique user identifier',
......@@ -28,7 +28,7 @@ CREATE TABLE IF NOT EXISTS `user_chat` (
ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (`chat_id`) REFERENCES `chat` (`id`)
ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
) 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.',
......@@ -40,12 +40,11 @@ CREATE TABLE IF NOT EXISTS `inline_query` (
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`),
FOREIGN KEY (`user_id`)
REFERENCES `user` (`id`)
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
CREATE TABLE IF NOT EXISTS `chosen_inline_query` (
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',
......@@ -56,10 +55,9 @@ CREATE TABLE IF NOT EXISTS `chosen_inline_query` (
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`),
FOREIGN KEY (`user_id`)
REFERENCES `user` (`id`)
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
) 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.',
......@@ -71,12 +69,11 @@ CREATE TABLE IF NOT EXISTS `callback_query` (
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`),
FOREIGN KEY (`user_id`)
REFERENCES `user` (`id`)
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
CREATE TABLE IF NOT EXISTS `message` (
CREATE TABLE IF NOT EXISTS `message` (
`chat_id` bigint COMMENT 'Chat identifier.',
`id` bigint UNSIGNED COMMENT 'Unique message identifier',
`user_id` bigint NULL COMMENT 'User identifier',
......@@ -86,7 +83,7 @@ CREATE TABLE IF NOT EXISTS `message` (
`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.',
`text` TEXT DEFAULT NULL COMMENT 'For text messages, the actual UTF-8 text of the message max message length 4096 char utf8',
`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',
`document` TEXT DEFAULT NULL COMMENT 'Document object. Message is a general file, information about the file',
......@@ -124,12 +121,12 @@ CREATE TABLE IF NOT EXISTS `message` (
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 (`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`),
FOREIGN KEY (`left_chat_member`) REFERENCES `user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
) 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.',
......@@ -148,30 +145,30 @@ CREATE TABLE IF NOT EXISTS `edited_message` (
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=utf8 COLLATE=utf8_general_ci;
) 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.',
`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_query_id` bigint UNSIGNED DEFAULT NULL COMMENT 'The chosen 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',
PRIMARY KEY (`id`),
KEY `message_id` (`chat_id`, `message_id`),
KEY `inline_query_id` (`inline_query_id`),
KEY `chosen_inline_query_id` (`chosen_inline_query_id`),
KEY `chosen_inline_result_id` (`chosen_inline_result_id`),
KEY `callback_query_id` (`callback_query_id`),
KEY `edited_message_id` (`edited_message_id`),
FOREIGN KEY (`chat_id`, `message_id`) REFERENCES `message` (`chat_id`,`id`),
FOREIGN KEY (`chat_id`, `message_id`) REFERENCES `message` (`chat_id`, `id`),
FOREIGN KEY (`inline_query_id`) REFERENCES `inline_query` (`id`),
FOREIGN KEY (`chosen_inline_query_id`) REFERENCES `chosen_inline_query` (`id`),
FOREIGN KEY (`chosen_inline_result_id`) REFERENCES `chosen_inline_result` (`id`),
FOREIGN KEY (`callback_query_id`) REFERENCES `callback_query` (`id`),
FOREIGN KEY (`edited_message_id`) REFERENCES `edited_message` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
) 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',
......@@ -188,11 +185,9 @@ CREATE TABLE IF NOT EXISTS `conversation` (
KEY `chat_id` (`chat_id`),
KEY `status` (`status`),
FOREIGN KEY (`user_id`)
REFERENCES `user` (`id`),
FOREIGN KEY (`chat_id`)
REFERENCES `chat` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`),
FOREIGN KEY (`chat_id`) REFERENCES `chat` (`id`)
) 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.',
......@@ -202,5 +197,6 @@ CREATE TABLE IF NOT EXISTS `botan_shortener` (
`created_at` timestamp NULL DEFAULT NULL COMMENT 'Entry date creation',
PRIMARY KEY (`id`),
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
......@@ -35,7 +35,7 @@ class ConversationTest extends TestCase
{
$credentials = [
'host' => '127.0.0.1',
'user' => 'travis',
'user' => 'root',
'password' => '',
'database' => 'telegrambot',
];
......
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