11
11
12
12
namespace Longman \TelegramBot ;
13
13
14
+ use Longman \TelegramBot \Entities \CallbackQuery ;
14
15
use Longman \TelegramBot \Entities \Chat ;
16
+ use Longman \TelegramBot \Entities \ChosenInlineResult ;
15
17
use Longman \TelegramBot \Entities \InlineQuery ;
16
- use Longman \TelegramBot \Entities \CallbackQuery ;
17
18
use Longman \TelegramBot \Entities \Message ;
18
19
use Longman \TelegramBot \Entities \Update ;
19
20
use Longman \TelegramBot \Entities \User ;
@@ -66,17 +67,18 @@ class DB
66
67
* @param array $credentials Database connection details
67
68
* @param Telegram $telegram Telegram object to connect with this object
68
69
* @param string $table_prefix Table prefix
70
+ * @param string $encoding Database character encoding
69
71
*
70
72
* @return PDO PDO database object
71
73
*/
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 ' )
73
75
{
74
76
if (empty ($ credentials )) {
75
77
throw new TelegramException ('MySQL credentials not provided! ' );
76
78
}
77
79
78
80
$ 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 ];
80
82
try {
81
83
$ pdo = new \PDO ($ dsn , $ credentials ['user ' ], $ credentials ['password ' ], $ options );
82
84
$ pdo ->setAttribute (\PDO ::ATTR_ERRMODE , \PDO ::ERRMODE_WARNING );
@@ -135,12 +137,12 @@ protected static function defineTable()
135
137
if (!defined ('TB_INLINE_QUERY ' )) {
136
138
define ('TB_INLINE_QUERY ' , self ::$ table_prefix .'inline_query ' );
137
139
}
140
+ if (!defined ('TB_CHOSEN_INLINE_RESULT ' )) {
141
+ define ('TB_CHOSEN_INLINE_RESULT ' , self ::$ table_prefix .'chosen_inline_result ' );
142
+ }
138
143
if (!defined ('TB_CALLBACK_QUERY ' )) {
139
144
define ('TB_CALLBACK_QUERY ' , self ::$ table_prefix .'callback_query ' );
140
145
}
141
- if (!defined ('TB_CHOSEN_INLINE_QUERY ' )) {
142
- define ('TB_CHOSEN_INLINE_QUERY ' , self ::$ table_prefix .'chosen_inline_query ' );
143
- }
144
146
if (!defined ('TB_USER ' )) {
145
147
define ('TB_USER ' , self ::$ table_prefix .'user ' );
146
148
}
@@ -256,14 +258,14 @@ protected static function getTimestamp($time = null)
256
258
* @param int $chat_id
257
259
* @param int $message_id
258
260
* @param int $inline_query_id
259
- * @param int $chosen_inline_query_id
261
+ * @param int $chosen_inline_result_id
260
262
* @param int $callback_query_id
261
263
*
262
264
* @return bool|null
263
265
*/
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 )
265
267
{
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 )) {
267
269
throw new TelegramException ('Error both query_id and message_id are null ' );
268
270
}
269
271
@@ -275,18 +277,18 @@ public static function insertTelegramUpdate($id, $chat_id, $message_id, $inline_
275
277
//telegram_update table
276
278
$ sth_insert_telegram_update = self ::$ pdo ->prepare ('INSERT IGNORE INTO ` ' . TB_TELEGRAM_UPDATE . '`
277
279
(
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`
279
281
)
280
282
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
282
284
)
283
285
' );
284
286
285
287
$ sth_insert_telegram_update ->bindParam (':id ' , $ id , \PDO ::PARAM_INT );
286
288
$ sth_insert_telegram_update ->bindParam (':chat_id ' , $ chat_id , \PDO ::PARAM_INT );
287
289
$ sth_insert_telegram_update ->bindParam (':message_id ' , $ message_id , \PDO ::PARAM_INT );
288
290
$ 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 );
290
292
$ sth_insert_telegram_update ->bindParam (':callback_query_id ' , $ callback_query_id , \PDO ::PARAM_INT );
291
293
292
294
$ status = $ sth_insert_telegram_update ->execute ();
@@ -436,50 +438,10 @@ public static function insertRequest(Update &$update)
436
438
self ::insertInlineQueryRequest ($ inline_query );
437
439
return self ::insertTelegramUpdate ($ update_id , null , null , $ inline_query_id , null , null );
438
440
} 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 );
483
445
} elseif ($ update ->getUpdateType () == 'callback_query ' ) {
484
446
$ callback_query = $ update ->getCallbackQuery ();
485
447
$ callback_query_id = $ callback_query ->getId ();
@@ -540,6 +502,57 @@ public static function insertInlineQueryRequest(InlineQuery &$inline_query)
540
502
}
541
503
}
542
504
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
+
543
556
/**
544
557
* Insert callback query request into database
545
558
*
@@ -711,6 +724,7 @@ public static function insertMessageRequest(Message &$message)
711
724
$ sth ->bindParam (':forward_from ' , $ forward_from , \PDO ::PARAM_INT );
712
725
$ sth ->bindParam (':forward_from_chat ' , $ forward_from_chat , \PDO ::PARAM_INT );
713
726
$ sth ->bindParam (':forward_date ' , $ forward_date , \PDO ::PARAM_STR );
727
+
714
728
$ reply_chat_id = null ;
715
729
if ($ reply_to_message_id ) {
716
730
$ reply_chat_id = $ chat_id ;
0 commit comments