Skip to content

Commit 50d732e

Browse files
committed
Save polls to DB.
1 parent 3fec5c9 commit 50d732e

File tree

3 files changed

+84
-5
lines changed

3 files changed

+84
-5
lines changed

src/DB.php

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Longman\TelegramBot\Entities\ChosenInlineResult;
1818
use Longman\TelegramBot\Entities\InlineQuery;
1919
use Longman\TelegramBot\Entities\Message;
20+
use Longman\TelegramBot\Entities\Poll;
2021
use Longman\TelegramBot\Entities\ReplyToMessage;
2122
use Longman\TelegramBot\Entities\Update;
2223
use Longman\TelegramBot\Entities\User;
@@ -141,6 +142,7 @@ protected static function defineTables()
141142
'edited_message',
142143
'inline_query',
143144
'message',
145+
'poll',
144146
'request_limiter',
145147
'telegram_update',
146148
'user',
@@ -309,6 +311,7 @@ public static function entitiesArrayToJson($entities, $default = null)
309311
* @param string $chosen_inline_result_id
310312
* @param string $callback_query_id
311313
* @param string $edited_message_id
314+
* @param string $poll_id
312315
*
313316
* @return bool If the insert was successful
314317
* @throws TelegramException
@@ -320,10 +323,11 @@ public static function insertTelegramUpdate(
320323
$inline_query_id = null,
321324
$chosen_inline_result_id = null,
322325
$callback_query_id = null,
323-
$edited_message_id = null
326+
$edited_message_id = null,
327+
$poll_id = null
324328
) {
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');
327331
}
328332

329333
if (!self::isDbConnected()) {
@@ -333,9 +337,9 @@ public static function insertTelegramUpdate(
333337
try {
334338
$sth = self::$pdo->prepare('
335339
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`)
337341
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)
339343
');
340344

341345
$sth->bindValue(':id', $id);
@@ -345,6 +349,7 @@ public static function insertTelegramUpdate(
345349
$sth->bindValue(':inline_query_id', $inline_query_id);
346350
$sth->bindValue(':chosen_inline_result_id', $chosen_inline_result_id);
347351
$sth->bindValue(':callback_query_id', $callback_query_id);
352+
$sth->bindValue(':poll_id', $poll_id);
348353

349354
return $sth->execute();
350355
} catch (PDOException $e) {
@@ -599,6 +604,23 @@ public static function insertRequest(Update $update)
599604
$callback_query_id
600605
);
601606
}
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+
}
602624
}
603625

604626
return false;
@@ -759,6 +781,43 @@ public static function insertCallbackQueryRequest(CallbackQuery $callback_query)
759781
}
760782
}
761783

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+
762821
/**
763822
* Insert Message request in db
764823
*

structure.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ CREATE TABLE IF NOT EXISTS `chosen_inline_result` (
6565
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
6666
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
6767

68+
CREATE TABLE IF NOT EXISTS `poll` (
69+
`id` bigint UNSIGNED COMMENT 'Unique poll identifier',
70+
`question` char(255) NOT NULL COMMENT 'Poll question',
71+
`options` text NOT NULL COMMENT 'List of poll options',
72+
`is_closed` tinyint(1) DEFAULT 0 COMMENT 'True, if the poll is closed',
73+
`created_at` timestamp NULL DEFAULT NULL COMMENT 'Entry date creation',
74+
75+
PRIMARY KEY (`id`)
76+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
77+
6878
CREATE TABLE IF NOT EXISTS `message` (
6979
`chat_id` bigint COMMENT 'Unique chat identifier',
7080
`id` bigint UNSIGNED COMMENT 'Unique message identifier',

utils/db-schema-update/unreleased.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
ALTER TABLE `message` ADD COLUMN `forward_signature` TEXT NULL DEFAULT NULL COMMENT 'For messages forwarded from channels, signature of the post author if present' AFTER `forward_from_message_id`;
22
ALTER TABLE `message` ADD COLUMN `forward_sender_name` TEXT NULL DEFAULT NULL COMMENT 'Sender''s name for messages forwarded from users who disallow adding a link to their account in forwarded messages' AFTER `forward_signature`;
33
ALTER TABLE `message` ADD COLUMN `poll` TEXT COMMENT 'Poll object. Message is a native poll, information about the poll' AFTER `venue`;
4+
5+
CREATE TABLE IF NOT EXISTS `poll` (
6+
`id` bigint UNSIGNED COMMENT 'Unique poll identifier',
7+
`question` char(255) NOT NULL COMMENT 'Poll question',
8+
`options` text NOT NULL COMMENT 'List of poll options',
9+
`is_closed` tinyint(1) DEFAULT 0 COMMENT 'True, if the poll is closed',
10+
`created_at` timestamp NULL DEFAULT NULL COMMENT 'Entry date creation',
11+
12+
PRIMARY KEY (`id`)
13+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;

0 commit comments

Comments
 (0)