Skip to content

API 4.2 - Polls #948

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
Exclamation symbols (:exclamation:) note something of importance e.g. breaking changes. Click them to learn more.

## [Unreleased]
:exclamation: After updating to this version, you will need to execute the [SQL migration script][unreleased-sql-migration] on your database.
### Added
- Bot API 4.2 (Polls).
- `getIsMember()` method to `ChatMember` entity.
- `getForwardSenderName()` method to `Message` entity.
- `forward_sender_name` (and forgotten `forward_signature`) DB fields.
### Changed
### Deprecated
### Removed
Expand Down Expand Up @@ -249,6 +254,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
### Deprecated
- Move `hideKeyboard` to `removeKeyboard`.

[unreleased-sql-migration]: https://github.com/php-telegram-bot/core/tree/develop/utils/db-schema-update/unreleased.sql
[0.55.0-sql-migration]: https://github.com/php-telegram-bot/core/tree/master/utils/db-schema-update/0.54.1-0.55.0.sql
[0.55.0-bc-move-animation-out-of-games-namespace]: https://github.com/php-telegram-bot/core/wiki/Breaking-backwards-compatibility#move-animation-out-of-games-namespace
[0.54.0-sql-migration]: https://github.com/php-telegram-bot/core/tree/master/utils/db-schema-update/0.53.0-0.54.0.sql
Expand Down
82 changes: 73 additions & 9 deletions src/DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Longman\TelegramBot\Entities\ChosenInlineResult;
use Longman\TelegramBot\Entities\InlineQuery;
use Longman\TelegramBot\Entities\Message;
use Longman\TelegramBot\Entities\Poll;
use Longman\TelegramBot\Entities\ReplyToMessage;
use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\Entities\User;
Expand Down Expand Up @@ -141,6 +142,7 @@ protected static function defineTables()
'edited_message',
'inline_query',
'message',
'poll',
'request_limiter',
'telegram_update',
'user',
Expand Down Expand Up @@ -309,6 +311,7 @@ public static function entitiesArrayToJson($entities, $default = null)
* @param string $chosen_inline_result_id
* @param string $callback_query_id
* @param string $edited_message_id
* @param string $poll_id
*
* @return bool If the insert was successful
* @throws TelegramException
Expand All @@ -320,10 +323,11 @@ public static function insertTelegramUpdate(
$inline_query_id = null,
$chosen_inline_result_id = null,
$callback_query_id = null,
$edited_message_id = null
$edited_message_id = null,
$poll_id = null
) {
if ($message_id === null && $inline_query_id === null && $chosen_inline_result_id === null && $callback_query_id === null && $edited_message_id === null) {
throw new TelegramException('message_id, inline_query_id, chosen_inline_result_id, callback_query_id, edited_message_id are all null');
if ($message_id === null && $inline_query_id === null && $chosen_inline_result_id === null && $callback_query_id === null && $edited_message_id === null && $poll_id === null) {
throw new TelegramException('message_id, inline_query_id, chosen_inline_result_id, callback_query_id, edited_message_id, poll_id are all null');
}

if (!self::isDbConnected()) {
Expand All @@ -333,9 +337,9 @@ public static function insertTelegramUpdate(
try {
$sth = self::$pdo->prepare('
INSERT IGNORE INTO `' . TB_TELEGRAM_UPDATE . '`
(`id`, `chat_id`, `message_id`, `inline_query_id`, `chosen_inline_result_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`, `poll_id`)
VALUES
(:id, :chat_id, :message_id, :inline_query_id, :chosen_inline_result_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, :poll_id)
');

$sth->bindValue(':id', $id);
Expand All @@ -345,6 +349,7 @@ public static function insertTelegramUpdate(
$sth->bindValue(':inline_query_id', $inline_query_id);
$sth->bindValue(':chosen_inline_result_id', $chosen_inline_result_id);
$sth->bindValue(':callback_query_id', $callback_query_id);
$sth->bindValue(':poll_id', $poll_id);

return $sth->execute();
} catch (PDOException $e) {
Expand Down Expand Up @@ -599,6 +604,23 @@ public static function insertRequest(Update $update)
$callback_query_id
);
}
} elseif ($update_type === 'poll') {
$poll = $update->getPoll();

if (self::insertPollRequest($poll)) {
$poll_id = $poll->getId();

return self::insertTelegramUpdate(
$update_id,
null,
null,
null,
null,
null,
null,
$poll_id
);
}
}

return false;
Expand Down Expand Up @@ -759,6 +781,43 @@ public static function insertCallbackQueryRequest(CallbackQuery $callback_query)
}
}

/**
* Insert poll request into database
*
* @param Poll $poll
*
* @return bool If the insert was successful
* @throws TelegramException
*/
public static function insertPollRequest(Poll $poll)
{
if (!self::isDbConnected()) {
return false;
}

try {
$sth = self::$pdo->prepare('
INSERT INTO `' . TB_POLL . '`
(`id`, `question`, `options`, `is_closed`, `created_at`)
VALUES
(:id, :question, :options, :is_closed, :created_at)
ON DUPLICATE KEY UPDATE
`options` = VALUES(`options`),
`is_closed` = VALUES(`is_closed`)
');

$sth->bindValue(':id', $poll->getId());
$sth->bindValue(':question', $poll->getQuestion());
$sth->bindValue(':options', self::entitiesArrayToJson($poll->getOptions(), null));
$sth->bindValue(':is_closed', $poll->getIsClosed());
$sth->bindValue(':created_at', self::getTimestamp());

return $sth->execute();
} catch (PDOException $e) {
throw new TelegramException($e->getMessage());
}
}

/**
* Insert Message request in db
*
Expand Down Expand Up @@ -828,17 +887,19 @@ public static function insertMessageRequest(Message $message)
INSERT IGNORE INTO `' . TB_MESSAGE . '`
(
`id`, `user_id`, `chat_id`, `date`, `forward_from`, `forward_from_chat`, `forward_from_message_id`,
`forward_date`, `reply_to_chat`, `reply_to_message`, `media_group_id`, `text`, `entities`, `audio`, `document`,
`forward_signature`, `forward_sender_name`, `forward_date`,
`reply_to_chat`, `reply_to_message`, `media_group_id`, `text`, `entities`, `audio`, `document`,
`animation`, `game`, `photo`, `sticker`, `video`, `voice`, `video_note`, `caption`, `contact`,
`location`, `venue`, `new_chat_members`, `left_chat_member`,
`location`, `venue`, `poll`, `new_chat_members`, `left_chat_member`,
`new_chat_title`,`new_chat_photo`, `delete_chat_photo`, `group_chat_created`,
`supergroup_chat_created`, `channel_chat_created`,
`migrate_from_chat_id`, `migrate_to_chat_id`, `pinned_message`, `connected_website`, `passport_data`
) VALUES (
:message_id, :user_id, :chat_id, :date, :forward_from, :forward_from_chat, :forward_from_message_id,
:forward_date, :reply_to_chat, :reply_to_message, :media_group_id, :text, :entities, :audio, :document,
:forward_signature, :forward_sender_name, :forward_date,
:reply_to_chat, :reply_to_message, :media_group_id, :text, :entities, :audio, :document,
:animation, :game, :photo, :sticker, :video, :voice, :video_note, :caption, :contact,
:location, :venue, :new_chat_members, :left_chat_member,
:location, :venue, :poll, :new_chat_members, :left_chat_member,
:new_chat_title, :new_chat_photo, :delete_chat_photo, :group_chat_created,
:supergroup_chat_created, :channel_chat_created,
:migrate_from_chat_id, :migrate_to_chat_id, :pinned_message, :connected_website, :passport_data
Expand Down Expand Up @@ -867,6 +928,8 @@ public static function insertMessageRequest(Message $message)
$sth->bindValue(':forward_from', $forward_from);
$sth->bindValue(':forward_from_chat', $forward_from_chat);
$sth->bindValue(':forward_from_message_id', $message->getForwardFromMessageId());
$sth->bindValue(':forward_signature', $message->getForwardSignature());
$sth->bindValue(':forward_sender_name', $message->getForwardSenderName());
$sth->bindValue(':forward_date', $forward_date);

$reply_to_chat_id = null;
Expand All @@ -892,6 +955,7 @@ public static function insertMessageRequest(Message $message)
$sth->bindValue(':contact', $message->getContact());
$sth->bindValue(':location', $message->getLocation());
$sth->bindValue(':venue', $message->getVenue());
$sth->bindValue(':poll', $message->getPoll());
$sth->bindValue(':new_chat_members', $new_chat_members_ids);
$sth->bindValue(':left_chat_member', $left_chat_member_id);
$sth->bindValue(':new_chat_title', $message->getNewChatTitle());
Expand Down
1 change: 1 addition & 0 deletions src/Entities/ChatMember.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* @method bool getCanRestrictMembers() Optional. Administrators only. True, if the administrator can restrict, ban or unban chat members
* @method bool getCanPinMessages() Optional. Administrators only. True, if the administrator can pin messages, supergroups only
* @method bool getCanPromoteMembers() Optional. Administrators only. True, if the administrator can add new administrators with a subset of his own privileges or demote administrators that he has promoted, directly or indirectly (promoted by administrators that were appointed by the user)
* @method bool getIsMember() Optional. Restricted only. True, if the user is a member of the chat at the moment of the request
* @method bool getCanSendMessages() Optional. Restricted only. True, if the user can send text messages, contacts, locations and venues
* @method bool getCanSendMediaMessages() Optional. Restricted only. True, if the user can send audios, documents, photos, videos, video notes and voice notes, implies can_send_messages
* @method bool getCanSendOtherMessages() Optional. Restricted only. True, if the user can send animations, games, stickers and use inline bots, implies can_send_media_messages
Expand Down
4 changes: 4 additions & 0 deletions src/Entities/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* @method Chat getForwardFromChat() Optional. For messages forwarded from a channel, information about the original channel
* @method int getForwardFromMessageId() Optional. For forwarded channel posts, identifier of the original message in the channel
* @method string getForwardSignature() Optional. For messages forwarded from channels, signature of the post author if present
* @method string getForwardSenderName() Optional. Sender's name for messages forwarded from users who disallow adding a link to their account in forwarded messages
* @method int getForwardDate() Optional. For forwarded messages, date the original message was sent in Unix time
* @method Message getReplyToMessage() Optional. For replies, the original message. Note that the Message object in this field will not contain further reply_to_message fields even if it itself is a reply.
* @method int getEditDate() Optional. Date the message was last edited in Unix time
Expand All @@ -45,6 +46,7 @@
* @method Contact getContact() Optional. Message is a shared contact, information about the contact
* @method Location getLocation() Optional. Message is a shared location, information about the location
* @method Venue getVenue() Optional. Message is a venue, information about the venue
* @method Poll getPoll() Optional. Message is a native poll, information about the poll
* @method User getLeftChatMember() Optional. A member was removed from the group, information about them (this member may be the bot itself)
* @method string getNewChatTitle() Optional. A chat title was changed to this value
* @method bool getDeleteChatPhoto() Optional. Service message: the chat photo was deleted
Expand Down Expand Up @@ -86,6 +88,7 @@ protected function subEntities()
'contact' => Contact::class,
'location' => Location::class,
'venue' => Venue::class,
'poll' => Poll::class,
'new_chat_members' => User::class,
'left_chat_member' => User::class,
'new_chat_photo' => PhotoSize::class,
Expand Down Expand Up @@ -295,6 +298,7 @@ public function getType()
'contact',
'location',
'venue',
'poll',
'new_chat_members',
'left_chat_member',
'new_chat_title',
Expand Down
50 changes: 50 additions & 0 deletions src/Entities/Poll.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Longman\TelegramBot\Entities;

/**
* Class Poll
*
* This entity contains information about a poll.
*
* @link https://core.telegram.org/bots/api#poll
*
* @method string getId() Unique poll identifier
* @method string getQuestion() Poll question, 1-255 characters
* @method bool getIsClosed() True, if the poll is closed
*/
class Poll extends Entity
{
/**
* {@inheritdoc}
*/
protected function subEntities()
{
return [
'options' => PollOption::class,
];
}

/**
* List of poll options
*
* This method overrides the default getOptions method
* and returns a nice array of PollOption objects.
*
* @return null|PollOption[]
*/
public function getOptions()
{
$pretty_array = $this->makePrettyObjectArray(PollOption::class, 'options');

return empty($pretty_array) ? null : $pretty_array;
}
}
26 changes: 26 additions & 0 deletions src/Entities/PollOption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Longman\TelegramBot\Entities;

/**
* Class PollOption
*
* This entity contains information about one answer option in a poll.
*
* @link https://core.telegram.org/bots/api#polloption
*
* @method string getText() Option text, 1-100 characters
* @method int getVoterCount() Number of users that voted for this option
*/
class PollOption extends Entity
{

}
3 changes: 3 additions & 0 deletions src/Entities/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* @method CallbackQuery getCallbackQuery() Optional. New incoming callback query
* @method ShippingQuery getShippingQuery() Optional. New incoming shipping query. Only for invoices with flexible price
* @method PreCheckoutQuery getPreCheckoutQuery() Optional. New incoming pre-checkout query. Contains full information about checkout
* @method Poll getPoll() Optional. New poll state. Bots receive only updates about polls, which are sent or stopped by the bot
*/
class Update extends Entity
{
Expand All @@ -46,6 +47,7 @@ protected function subEntities()
'callback_query' => CallbackQuery::class,
'shipping_query' => ShippingQuery::class,
'pre_checkout_query' => PreCheckoutQuery::class,
'poll' => Poll::class,
];
}

Expand All @@ -66,6 +68,7 @@ public function getUpdateType()
'callback_query',
'shipping_query',
'pre_checkout_query',
'poll',
];
foreach ($types as $type) {
if ($this->getProperty($type)) {
Expand Down
Loading