Skip to content

Commit 52e1ef1

Browse files
committed
Merge pull request php-telegram-bot#189 from jacklul/dbchanges
chosen_inline_result table and make utf8mb4 default
2 parents 8c66ba4 + 15afa62 commit 52e1ef1

File tree

5 files changed

+104
-86
lines changed

5 files changed

+104
-86
lines changed

.travis.yml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
sudo: false
1+
dist: trusty
2+
sudo: required
3+
4+
addons:
5+
apt:
6+
packages:
7+
- mysql-server-5.6
8+
- mysql-client-core-5.6
9+
- mysql-client-5.6
210

311
language: php
412

@@ -27,7 +35,7 @@ install:
2735
- travis_retry composer install --no-interaction
2836

2937
before_script:
30-
- mysql -e 'create database telegrambot; use telegrambot; source structure.sql;'
38+
- mysql -u root -e 'create database telegrambot; use telegrambot; source structure.sql;'
3139

3240
script:
3341
- ./vendor/bin/phpunit

src/DB.php

Lines changed: 70 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111

1212
namespace Longman\TelegramBot;
1313

14+
use Longman\TelegramBot\Entities\CallbackQuery;
1415
use Longman\TelegramBot\Entities\Chat;
16+
use Longman\TelegramBot\Entities\ChosenInlineResult;
1517
use Longman\TelegramBot\Entities\InlineQuery;
16-
use Longman\TelegramBot\Entities\CallbackQuery;
1718
use Longman\TelegramBot\Entities\Message;
1819
use Longman\TelegramBot\Entities\Update;
1920
use Longman\TelegramBot\Entities\User;
@@ -66,17 +67,18 @@ class DB
6667
* @param array $credentials Database connection details
6768
* @param Telegram $telegram Telegram object to connect with this object
6869
* @param string $table_prefix Table prefix
70+
* @param string $encoding Database character encoding
6971
*
7072
* @return PDO PDO database object
7173
*/
72-
public static function initialize(array $credentials, Telegram $telegram, $table_prefix = null)
74+
public static function initialize(array $credentials, Telegram $telegram, $table_prefix = null, $encoding = 'utf8mb4')
7375
{
7476
if (empty($credentials)) {
7577
throw new TelegramException('MySQL credentials not provided!');
7678
}
7779

7880
$dsn = 'mysql:host=' . $credentials['host'] . ';dbname=' . $credentials['database'];
79-
$options = [\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'];
81+
$options = [\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . $encoding];
8082
try {
8183
$pdo = new \PDO($dsn, $credentials['user'], $credentials['password'], $options);
8284
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);
@@ -135,12 +137,12 @@ protected static function defineTable()
135137
if (!defined('TB_INLINE_QUERY')) {
136138
define('TB_INLINE_QUERY', self::$table_prefix.'inline_query');
137139
}
140+
if (!defined('TB_CHOSEN_INLINE_RESULT')) {
141+
define('TB_CHOSEN_INLINE_RESULT', self::$table_prefix.'chosen_inline_result');
142+
}
138143
if (!defined('TB_CALLBACK_QUERY')) {
139144
define('TB_CALLBACK_QUERY', self::$table_prefix.'callback_query');
140145
}
141-
if (!defined('TB_CHOSEN_INLINE_QUERY')) {
142-
define('TB_CHOSEN_INLINE_QUERY', self::$table_prefix.'chosen_inline_query');
143-
}
144146
if (!defined('TB_USER')) {
145147
define('TB_USER', self::$table_prefix.'user');
146148
}
@@ -256,14 +258,14 @@ protected static function getTimestamp($time = null)
256258
* @param int $chat_id
257259
* @param int $message_id
258260
* @param int $inline_query_id
259-
* @param int $chosen_inline_query_id
261+
* @param int $chosen_inline_result_id
260262
* @param int $callback_query_id
261263
*
262264
* @return bool|null
263265
*/
264-
public static function insertTelegramUpdate($id, $chat_id, $message_id, $inline_query_id, $chosen_inline_query_id, $callback_query_id)
266+
public static function insertTelegramUpdate($id, $chat_id, $message_id, $inline_query_id, $chosen_inline_result_id, $callback_query_id)
265267
{
266-
if (is_null($message_id) && is_null($inline_query_id) && is_null($chosen_inline_query_id) && is_null($callback_query_id)) {
268+
if (is_null($message_id) && is_null($inline_query_id) && is_null($chosen_inline_result_id) && is_null($callback_query_id)) {
267269
throw new TelegramException('Error both query_id and message_id are null');
268270
}
269271

@@ -275,18 +277,18 @@ public static function insertTelegramUpdate($id, $chat_id, $message_id, $inline_
275277
//telegram_update table
276278
$sth_insert_telegram_update = self::$pdo->prepare('INSERT IGNORE INTO `' . TB_TELEGRAM_UPDATE . '`
277279
(
278-
`id`, `chat_id`, `message_id`, `inline_query_id`, `chosen_inline_query_id`, `callback_query_id`
280+
`id`, `chat_id`, `message_id`, `inline_query_id`, `chosen_inline_result_id`, `callback_query_id`
279281
)
280282
VALUES (
281-
:id, :chat_id, :message_id, :inline_query_id, :chosen_inline_query_id, :callback_query_id
283+
:id, :chat_id, :message_id, :inline_query_id, :chosen_inline_result_id, :callback_query_id
282284
)
283285
');
284286

285287
$sth_insert_telegram_update->bindParam(':id', $id, \PDO::PARAM_INT);
286288
$sth_insert_telegram_update->bindParam(':chat_id', $chat_id, \PDO::PARAM_INT);
287289
$sth_insert_telegram_update->bindParam(':message_id', $message_id, \PDO::PARAM_INT);
288290
$sth_insert_telegram_update->bindParam(':inline_query_id', $inline_query_id, \PDO::PARAM_INT);
289-
$sth_insert_telegram_update->bindParam(':chosen_inline_query_id', $chosen_inline_query_id, \PDO::PARAM_INT);
291+
$sth_insert_telegram_update->bindParam(':chosen_inline_result_id', $chosen_inline_result_id, \PDO::PARAM_INT);
290292
$sth_insert_telegram_update->bindParam(':callback_query_id', $callback_query_id, \PDO::PARAM_INT);
291293

292294
$status = $sth_insert_telegram_update->execute();
@@ -436,50 +438,10 @@ public static function insertRequest(Update &$update)
436438
self::insertInlineQueryRequest($inline_query);
437439
return self::insertTelegramUpdate($update_id, null, null, $inline_query_id, null, null);
438440
} elseif ($update->getUpdateType() == 'chosen_inline_result') {
439-
$chosen_inline_query = $update->getChosenInlineResult();
440-
441-
if (!self::isDbConnected()) {
442-
return false;
443-
}
444-
try {
445-
//Inline query Table
446-
$mysql_query = 'INSERT INTO `' . TB_CHOSEN_INLINE_QUERY . '`
447-
(
448-
`result_id`, `user_id`, `location`, `inline_message_id`, `query`, `created_at`
449-
)
450-
VALUES (
451-
:result_id, :user_id, :location, :inline_message_id, :query, :created_at
452-
)';
453-
454-
$sth_insert_chosen_inline_query = self::$pdo->prepare($mysql_query);
455-
456-
$date = self::getTimestamp(time());
457-
$result_id = $chosen_inline_query->getResultId();
458-
$from = $chosen_inline_query->getFrom();
459-
$user_id = null;
460-
if (is_object($from)) {
461-
$user_id = $from->getId();
462-
self::insertUser($from, $date);
463-
}
464-
465-
$location = $chosen_inline_query->getLocation();
466-
$inline_message_id = $chosen_inline_query->getInlineMessageId();
467-
$query = $chosen_inline_query->getQuery();
468-
469-
$sth_insert_chosen_inline_query->bindParam(':result_id', $result_id, \PDO::PARAM_STR);
470-
$sth_insert_chosen_inline_query->bindParam(':user_id', $user_id, \PDO::PARAM_INT);
471-
$sth_insert_chosen_inline_query->bindParam(':location', $location, \PDO::PARAM_INT);
472-
$sth_insert_chosen_inline_query->bindParam(':inline_message_id', $inline_message_id, \PDO::PARAM_INT);
473-
$sth_insert_chosen_inline_query->bindParam(':query', $query, \PDO::PARAM_STR);
474-
$sth_insert_chosen_inline_query->bindParam(':created_at', $date, \PDO::PARAM_STR);
475-
476-
$status = $sth_insert_chosen_inline_query->execute();
477-
$chosen_inline_query_local_id = self::$pdo->lastInsertId();
478-
} catch (PDOException $e) {
479-
throw new TelegramException($e->getMessage());
480-
}
481-
482-
return self::insertTelegramUpdate($update_id, null, null, null, $chosen_inline_query_local_id, null);
441+
$chosen_inline_result = $update->getChosenInlineResult();
442+
self::insertChosenInlineResultRequest($chosen_inline_result);
443+
$chosen_inline_result_local_id = self::$pdo->lastInsertId();
444+
return self::insertTelegramUpdate($update_id, null, null, null, $chosen_inline_result_local_id, null);
483445
} elseif ($update->getUpdateType() == 'callback_query') {
484446
$callback_query = $update->getCallbackQuery();
485447
$callback_query_id = $callback_query->getId();
@@ -540,6 +502,57 @@ public static function insertInlineQueryRequest(InlineQuery &$inline_query)
540502
}
541503
}
542504

505+
/**
506+
* Insert chosen inline result request into database
507+
*
508+
* @param Entities\ChosenInlineResult &$chosen_inline_result
509+
*
510+
* @return bool
511+
*/
512+
public static function insertChosenInlineResultRequest(ChosenInlineResult &$chosen_inline_result)
513+
{
514+
if (!self::isDbConnected()) {
515+
return false;
516+
}
517+
try {
518+
//Chosen inline result Table
519+
$mysql_query = 'INSERT INTO `' . TB_CHOSEN_INLINE_RESULT . '`
520+
(
521+
`result_id`, `user_id`, `location`, `inline_message_id`, `query`, `created_at`
522+
)
523+
VALUES (
524+
:result_id, :user_id, :location, :inline_message_id, :query, :created_at
525+
)';
526+
527+
$sth_insert_chosen_inline_result = self::$pdo->prepare($mysql_query);
528+
529+
$date = self::getTimestamp(time());
530+
$result_id = $chosen_inline_result->getResultId();
531+
$from = $chosen_inline_result->getFrom();
532+
$user_id = null;
533+
if (is_object($from)) {
534+
$user_id = $from->getId();
535+
self::insertUser($from, $date);
536+
}
537+
538+
$location = $chosen_inline_result->getLocation();
539+
$inline_message_id = $chosen_inline_result->getInlineMessageId();
540+
$query = $chosen_inline_result->getQuery();
541+
542+
$sth_insert_chosen_inline_result->bindParam(':result_id', $result_id, \PDO::PARAM_STR);
543+
$sth_insert_chosen_inline_result->bindParam(':user_id', $user_id, \PDO::PARAM_INT);
544+
$sth_insert_chosen_inline_result->bindParam(':location', $location, \PDO::PARAM_INT);
545+
$sth_insert_chosen_inline_result->bindParam(':inline_message_id', $inline_message_id, \PDO::PARAM_INT);
546+
$sth_insert_chosen_inline_result->bindParam(':query', $query, \PDO::PARAM_STR);
547+
$sth_insert_chosen_inline_result->bindParam(':created_at', $date, \PDO::PARAM_STR);
548+
549+
$status = $sth_insert_chosen_inline_result->execute();
550+
} catch (PDOException $e) {
551+
throw new TelegramException($e->getMessage());
552+
}
553+
}
554+
555+
543556
/**
544557
* Insert callback query request into database
545558
*
@@ -711,6 +724,7 @@ public static function insertMessageRequest(Message &$message)
711724
$sth->bindParam(':forward_from', $forward_from, \PDO::PARAM_INT);
712725
$sth->bindParam(':forward_from_chat', $forward_from_chat, \PDO::PARAM_INT);
713726
$sth->bindParam(':forward_date', $forward_date, \PDO::PARAM_STR);
727+
714728
$reply_chat_id = null;
715729
if ($reply_to_message_id) {
716730
$reply_chat_id = $chat_id;

src/Telegram.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,9 @@ public function __construct($api_key, $bot_name)
181181
*
182182
* @return Telegram
183183
*/
184-
public function enableMySql(array $credential, $table_prefix = null)
184+
public function enableMySql(array $credential, $table_prefix = null, $encoding = 'utf8mb4')
185185
{
186-
$this->pdo = DB::initialize($credential, $this, $table_prefix);
186+
$this->pdo = DB::initialize($credential, $this, $table_prefix, $encoding);
187187
ConversationDB::initializeConversation();
188188
$this->mysql_enabled = true;
189189
return $this;

structure.sql

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ CREATE TABLE IF NOT EXISTS `user` (
77
`updated_at` timestamp NULL DEFAULT NULL COMMENT 'Entry date update',
88
PRIMARY KEY (`id`),
99
KEY `username` (`username`)
10-
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
10+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
1111

1212
CREATE TABLE IF NOT EXISTS `chat` (
1313
`id` bigint COMMENT 'Unique user or chat identifier',
@@ -18,7 +18,7 @@ CREATE TABLE IF NOT EXISTS `chat` (
1818
`old_id` bigint DEFAULT NULL COMMENT 'Unique chat identifieri this is filled when a chat is converted to a superchat',
1919
PRIMARY KEY (`id`),
2020
KEY `old_id` (`old_id`)
21-
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
21+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
2222

2323
CREATE TABLE IF NOT EXISTS `user_chat` (
2424
`user_id` bigint COMMENT 'Unique user identifier',
@@ -28,7 +28,7 @@ CREATE TABLE IF NOT EXISTS `user_chat` (
2828
ON DELETE CASCADE ON UPDATE CASCADE,
2929
FOREIGN KEY (`chat_id`) REFERENCES `chat` (`id`)
3030
ON DELETE CASCADE ON UPDATE CASCADE
31-
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
31+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
3232

3333
CREATE TABLE IF NOT EXISTS `inline_query` (
3434
`id` bigint UNSIGNED COMMENT 'Unique identifier for this query.',
@@ -40,12 +40,11 @@ CREATE TABLE IF NOT EXISTS `inline_query` (
4040
PRIMARY KEY (`id`),
4141
KEY `user_id` (`user_id`),
4242

43-
FOREIGN KEY (`user_id`)
44-
REFERENCES `user` (`id`)
43+
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
4544

46-
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
45+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
4746

48-
CREATE TABLE IF NOT EXISTS `chosen_inline_query` (
47+
CREATE TABLE IF NOT EXISTS `chosen_inline_result` (
4948
`id` bigint UNSIGNED AUTO_INCREMENT COMMENT 'Unique identifier for chosen query.',
5049
`result_id` CHAR(255) NOT NULL DEFAULT '' COMMENT 'Id of the chosen result',
5150
`user_id` bigint NULL COMMENT 'Sender',
@@ -56,10 +55,9 @@ CREATE TABLE IF NOT EXISTS `chosen_inline_query` (
5655
PRIMARY KEY (`id`),
5756
KEY `user_id` (`user_id`),
5857

59-
FOREIGN KEY (`user_id`)
60-
REFERENCES `user` (`id`)
58+
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
6159

62-
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
60+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
6361

6462
CREATE TABLE IF NOT EXISTS `callback_query` (
6563
`id` bigint UNSIGNED COMMENT 'Unique identifier for this query.',
@@ -71,10 +69,9 @@ CREATE TABLE IF NOT EXISTS `callback_query` (
7169
PRIMARY KEY (`id`),
7270
KEY `user_id` (`user_id`),
7371

74-
FOREIGN KEY (`user_id`)
75-
REFERENCES `user` (`id`)
72+
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
7673

77-
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
74+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
7875

7976
CREATE TABLE IF NOT EXISTS `message` (
8077
`chat_id` bigint COMMENT 'Chat identifier.',
@@ -86,7 +83,7 @@ CREATE TABLE IF NOT EXISTS `message` (
8683
`forward_date` timestamp NULL DEFAULT NULL COMMENT 'For forwarded messages, date the original message was sent in Unix time',
8784
`reply_to_chat` bigint NULL DEFAULT NULL COMMENT 'Chat identifier.',
8885
`reply_to_message` bigint UNSIGNED DEFAULT NULL COMMENT 'Message is a reply to another message.',
89-
`text` TEXT DEFAULT NULL COMMENT 'For text messages, the actual UTF-8 text of the message max message length 4096 char utf8',
86+
`text` TEXT DEFAULT NULL COMMENT 'For text messages, the actual UTF-8 text of the message max message length 4096 char utf8mb4',
9087
`entities` TEXT DEFAULT NULL COMMENT 'For text messages, special entities like usernames, URLs, bot commands, etc. that appear in the text',
9188
`audio` TEXT DEFAULT NULL COMMENT 'Audio object. Message is an audio file, information about the file',
9289
`document` TEXT DEFAULT NULL COMMENT 'Document object. Message is a general file, information about the file',
@@ -129,27 +126,27 @@ CREATE TABLE IF NOT EXISTS `message` (
129126
FOREIGN KEY (`new_chat_member`) REFERENCES `user` (`id`),
130127
FOREIGN KEY (`left_chat_member`) REFERENCES `user` (`id`)
131128

132-
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
129+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
133130

134131
CREATE TABLE IF NOT EXISTS `telegram_update` (
135132
`id` bigint UNSIGNED COMMENT 'The update\'s unique identifier.',
136133
`chat_id` bigint NULL DEFAULT NULL COMMENT 'Chat identifier.',
137134
`message_id` bigint UNSIGNED DEFAULT NULL COMMENT 'Unique message identifier',
138135
`inline_query_id` bigint UNSIGNED DEFAULT NULL COMMENT 'The inline query unique identifier.',
139-
`chosen_inline_query_id` bigint UNSIGNED DEFAULT NULL COMMENT 'The chosen query unique identifier.',
136+
`chosen_inline_result_id` bigint UNSIGNED DEFAULT NULL COMMENT 'The chosen query unique identifier.',
140137
`callback_query_id` bigint UNSIGNED DEFAULT NULL COMMENT 'The callback query unique identifier.',
141138

142139
PRIMARY KEY (`id`),
143140
KEY `message_id` (`chat_id`, `message_id`),
144141
KEY `inline_query_id` (`inline_query_id`),
145-
KEY `chosen_inline_query_id` (`chosen_inline_query_id`),
142+
KEY `chosen_inline_result_id` (`chosen_inline_result_id`),
146143
KEY `callback_query_id` (`callback_query_id`),
147144

148145
FOREIGN KEY (`chat_id`, `message_id`) REFERENCES `message` (`chat_id`,`id`),
149146
FOREIGN KEY (`inline_query_id`) REFERENCES `inline_query` (`id`),
150-
FOREIGN KEY (`chosen_inline_query_id`) REFERENCES `chosen_inline_query` (`id`),
147+
FOREIGN KEY (`chosen_inline_result_id`) REFERENCES `chosen_inline_result` (`id`),
151148
FOREIGN KEY (`callback_query_id`) REFERENCES `callback_query` (`id`)
152-
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
149+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
153150

154151
CREATE TABLE IF NOT EXISTS `conversation` (
155152
`id` bigint(20) unsigned AUTO_INCREMENT COMMENT 'Row unique id',
@@ -166,11 +163,9 @@ CREATE TABLE IF NOT EXISTS `conversation` (
166163
KEY `chat_id` (`chat_id`),
167164
KEY `status` (`status`),
168165

169-
FOREIGN KEY (`user_id`)
170-
REFERENCES `user` (`id`),
171-
FOREIGN KEY (`chat_id`)
172-
REFERENCES `chat` (`id`)
173-
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
166+
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`),
167+
FOREIGN KEY (`chat_id`) REFERENCES `chat` (`id`)
168+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
174169

175170
CREATE TABLE IF NOT EXISTS `botan_shortener` (
176171
`id` bigint UNSIGNED AUTO_INCREMENT COMMENT 'Unique identifier for this entry.',
@@ -180,5 +175,6 @@ CREATE TABLE IF NOT EXISTS `botan_shortener` (
180175
`created_at` timestamp NULL DEFAULT NULL COMMENT 'Entry date creation',
181176

182177
PRIMARY KEY (`id`),
178+
183179
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
184-
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
180+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;

tests/Unit/ConversationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ protected function setUp()
3535
{
3636
$credentials = [
3737
'host' => '127.0.0.1',
38-
'user' => 'travis',
38+
'user' => 'root',
3939
'password' => '',
4040
'database' => 'telegrambot',
4141
];

0 commit comments

Comments
 (0)