17
17
use Longman \TelegramBot \Entities \ChosenInlineResult ;
18
18
use Longman \TelegramBot \Entities \InlineQuery ;
19
19
use Longman \TelegramBot \Entities \Message ;
20
+ use Longman \TelegramBot \Entities \Poll ;
20
21
use Longman \TelegramBot \Entities \ReplyToMessage ;
21
22
use Longman \TelegramBot \Entities \Update ;
22
23
use Longman \TelegramBot \Entities \User ;
@@ -141,6 +142,7 @@ protected static function defineTables()
141
142
'edited_message ' ,
142
143
'inline_query ' ,
143
144
'message ' ,
145
+ 'poll ' ,
144
146
'request_limiter ' ,
145
147
'telegram_update ' ,
146
148
'user ' ,
@@ -309,6 +311,7 @@ public static function entitiesArrayToJson($entities, $default = null)
309
311
* @param string $chosen_inline_result_id
310
312
* @param string $callback_query_id
311
313
* @param string $edited_message_id
314
+ * @param string $poll_id
312
315
*
313
316
* @return bool If the insert was successful
314
317
* @throws TelegramException
@@ -320,10 +323,11 @@ public static function insertTelegramUpdate(
320
323
$ inline_query_id = null ,
321
324
$ chosen_inline_result_id = null ,
322
325
$ callback_query_id = null ,
323
- $ edited_message_id = null
326
+ $ edited_message_id = null ,
327
+ $ poll_id = null
324
328
) {
325
- if ($ message_id === null && $ inline_query_id === null && $ chosen_inline_result_id === null && $ callback_query_id === null && $ edited_message_id === null ) {
326
- throw new TelegramException ('message_id, inline_query_id, chosen_inline_result_id, callback_query_id, edited_message_id are all null ' );
329
+ if ($ message_id === null && $ inline_query_id === null && $ chosen_inline_result_id === null && $ callback_query_id === null && $ edited_message_id === null && $ poll_id === null ) {
330
+ throw new TelegramException ('message_id, inline_query_id, chosen_inline_result_id, callback_query_id, edited_message_id, poll_id are all null ' );
327
331
}
328
332
329
333
if (!self ::isDbConnected ()) {
@@ -333,9 +337,9 @@ public static function insertTelegramUpdate(
333
337
try {
334
338
$ sth = self ::$ pdo ->prepare ('
335
339
INSERT IGNORE INTO ` ' . TB_TELEGRAM_UPDATE . '`
336
- (`id`, `chat_id`, `message_id`, `inline_query_id`, `chosen_inline_result_id`, `callback_query_id`, `edited_message_id`)
340
+ (`id`, `chat_id`, `message_id`, `inline_query_id`, `chosen_inline_result_id`, `callback_query_id`, `edited_message_id`, `poll_id` )
337
341
VALUES
338
- (:id, :chat_id, :message_id, :inline_query_id, :chosen_inline_result_id, :callback_query_id, :edited_message_id)
342
+ (:id, :chat_id, :message_id, :inline_query_id, :chosen_inline_result_id, :callback_query_id, :edited_message_id, :poll_id )
339
343
' );
340
344
341
345
$ sth ->bindValue (':id ' , $ id );
@@ -345,6 +349,7 @@ public static function insertTelegramUpdate(
345
349
$ sth ->bindValue (':inline_query_id ' , $ inline_query_id );
346
350
$ sth ->bindValue (':chosen_inline_result_id ' , $ chosen_inline_result_id );
347
351
$ sth ->bindValue (':callback_query_id ' , $ callback_query_id );
352
+ $ sth ->bindValue (':poll_id ' , $ poll_id );
348
353
349
354
return $ sth ->execute ();
350
355
} catch (PDOException $ e ) {
@@ -599,6 +604,23 @@ public static function insertRequest(Update $update)
599
604
$ callback_query_id
600
605
);
601
606
}
607
+ } elseif ($ update_type === 'poll ' ) {
608
+ $ poll = $ update ->getPoll ();
609
+
610
+ if (self ::insertPollRequest ($ poll )) {
611
+ $ poll_id = $ poll ->getId ();
612
+
613
+ return self ::insertTelegramUpdate (
614
+ $ update_id ,
615
+ null ,
616
+ null ,
617
+ null ,
618
+ null ,
619
+ null ,
620
+ null ,
621
+ $ poll_id
622
+ );
623
+ }
602
624
}
603
625
604
626
return false ;
@@ -759,6 +781,43 @@ public static function insertCallbackQueryRequest(CallbackQuery $callback_query)
759
781
}
760
782
}
761
783
784
+ /**
785
+ * Insert poll request into database
786
+ *
787
+ * @param Poll $poll
788
+ *
789
+ * @return bool If the insert was successful
790
+ * @throws TelegramException
791
+ */
792
+ public static function insertPollRequest (Poll $ poll )
793
+ {
794
+ if (!self ::isDbConnected ()) {
795
+ return false ;
796
+ }
797
+
798
+ try {
799
+ $ sth = self ::$ pdo ->prepare ('
800
+ INSERT INTO ` ' . TB_POLL . '`
801
+ (`id`, `question`, `options`, `is_closed`, `created_at`)
802
+ VALUES
803
+ (:id, :question, :options, :is_closed, :created_at)
804
+ ON DUPLICATE KEY UPDATE
805
+ `options` = VALUES(`options`),
806
+ `is_closed` = VALUES(`is_closed`)
807
+ ' );
808
+
809
+ $ sth ->bindValue (':id ' , $ poll ->getId ());
810
+ $ sth ->bindValue (':question ' , $ poll ->getQuestion ());
811
+ $ sth ->bindValue (':options ' , self ::entitiesArrayToJson ($ poll ->getOptions (), null ));
812
+ $ sth ->bindValue (':is_closed ' , $ poll ->getIsClosed ());
813
+ $ sth ->bindValue (':created_at ' , self ::getTimestamp ());
814
+
815
+ return $ sth ->execute ();
816
+ } catch (PDOException $ e ) {
817
+ throw new TelegramException ($ e ->getMessage ());
818
+ }
819
+ }
820
+
762
821
/**
763
822
* Insert Message request in db
764
823
*
0 commit comments