Skip to content

Commit 3fec5c9

Browse files
committed
Add native Polls thanks to Bot API 4.2
1 parent 7444c44 commit 3fec5c9

File tree

9 files changed

+94
-2
lines changed

9 files changed

+94
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
66
## [Unreleased]
77
:exclamation: After updating to this version, you will need to execute the [SQL migration script][unreleased-sql-migration] on your database.
88
### Added
9+
- Bot API 4.2 (Polls).
910
- `getIsMember()` method to `ChatMember` entity.
1011
- `getForwardSenderName()` method to `Message` entity.
1112
- `forward_sender_name` (and forgotten `forward_signature`) DB fields.

src/DB.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ public static function insertMessageRequest(Message $message)
831831
`forward_signature`, `forward_sender_name`, `forward_date`,
832832
`reply_to_chat`, `reply_to_message`, `media_group_id`, `text`, `entities`, `audio`, `document`,
833833
`animation`, `game`, `photo`, `sticker`, `video`, `voice`, `video_note`, `caption`, `contact`,
834-
`location`, `venue`, `new_chat_members`, `left_chat_member`,
834+
`location`, `venue`, `poll`, `new_chat_members`, `left_chat_member`,
835835
`new_chat_title`,`new_chat_photo`, `delete_chat_photo`, `group_chat_created`,
836836
`supergroup_chat_created`, `channel_chat_created`,
837837
`migrate_from_chat_id`, `migrate_to_chat_id`, `pinned_message`, `connected_website`, `passport_data`
@@ -840,7 +840,7 @@ public static function insertMessageRequest(Message $message)
840840
:forward_signature, :forward_sender_name, :forward_date,
841841
:reply_to_chat, :reply_to_message, :media_group_id, :text, :entities, :audio, :document,
842842
:animation, :game, :photo, :sticker, :video, :voice, :video_note, :caption, :contact,
843-
:location, :venue, :new_chat_members, :left_chat_member,
843+
:location, :venue, :poll, :new_chat_members, :left_chat_member,
844844
:new_chat_title, :new_chat_photo, :delete_chat_photo, :group_chat_created,
845845
:supergroup_chat_created, :channel_chat_created,
846846
:migrate_from_chat_id, :migrate_to_chat_id, :pinned_message, :connected_website, :passport_data
@@ -896,6 +896,7 @@ public static function insertMessageRequest(Message $message)
896896
$sth->bindValue(':contact', $message->getContact());
897897
$sth->bindValue(':location', $message->getLocation());
898898
$sth->bindValue(':venue', $message->getVenue());
899+
$sth->bindValue(':poll', $message->getPoll());
899900
$sth->bindValue(':new_chat_members', $new_chat_members_ids);
900901
$sth->bindValue(':left_chat_member', $left_chat_member_id);
901902
$sth->bindValue(':new_chat_title', $message->getNewChatTitle());

src/Entities/Message.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
* @method Contact getContact() Optional. Message is a shared contact, information about the contact
4747
* @method Location getLocation() Optional. Message is a shared location, information about the location
4848
* @method Venue getVenue() Optional. Message is a venue, information about the venue
49+
* @method Poll getPoll() Optional. Message is a native poll, information about the poll
4950
* @method User getLeftChatMember() Optional. A member was removed from the group, information about them (this member may be the bot itself)
5051
* @method string getNewChatTitle() Optional. A chat title was changed to this value
5152
* @method bool getDeleteChatPhoto() Optional. Service message: the chat photo was deleted
@@ -87,6 +88,7 @@ protected function subEntities()
8788
'contact' => Contact::class,
8889
'location' => Location::class,
8990
'venue' => Venue::class,
91+
'poll' => Poll::class,
9092
'new_chat_members' => User::class,
9193
'left_chat_member' => User::class,
9294
'new_chat_photo' => PhotoSize::class,
@@ -296,6 +298,7 @@ public function getType()
296298
'contact',
297299
'location',
298300
'venue',
301+
'poll',
299302
'new_chat_members',
300303
'left_chat_member',
301304
'new_chat_title',

src/Entities/Poll.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* This file is part of the TelegramBot package.
4+
*
5+
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Longman\TelegramBot\Entities;
12+
13+
/**
14+
* Class Poll
15+
*
16+
* This entity contains information about a poll.
17+
*
18+
* @link https://core.telegram.org/bots/api#poll
19+
*
20+
* @method string getId() Unique poll identifier
21+
* @method string getQuestion() Poll question, 1-255 characters
22+
* @method bool getIsClosed() True, if the poll is closed
23+
*/
24+
class Poll extends Entity
25+
{
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
protected function subEntities()
30+
{
31+
return [
32+
'options' => PollOption::class,
33+
];
34+
}
35+
36+
/**
37+
* List of poll options
38+
*
39+
* This method overrides the default getOptions method
40+
* and returns a nice array of PollOption objects.
41+
*
42+
* @return null|PollOption[]
43+
*/
44+
public function getOptions()
45+
{
46+
$pretty_array = $this->makePrettyObjectArray(PollOption::class, 'options');
47+
48+
return empty($pretty_array) ? null : $pretty_array;
49+
}
50+
}

src/Entities/PollOption.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* This file is part of the TelegramBot package.
4+
*
5+
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Longman\TelegramBot\Entities;
12+
13+
/**
14+
* Class PollOption
15+
*
16+
* This entity contains information about one answer option in a poll.
17+
*
18+
* @link https://core.telegram.org/bots/api#polloption
19+
*
20+
* @method string getText() Option text, 1-100 characters
21+
* @method int getVoterCount() Number of users that voted for this option
22+
*/
23+
class PollOption extends Entity
24+
{
25+
26+
}

src/Entities/Update.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* @method CallbackQuery getCallbackQuery() Optional. New incoming callback query
2929
* @method ShippingQuery getShippingQuery() Optional. New incoming shipping query. Only for invoices with flexible price
3030
* @method PreCheckoutQuery getPreCheckoutQuery() Optional. New incoming pre-checkout query. Contains full information about checkout
31+
* @method Poll getPoll() Optional. New poll state. Bots receive only updates about polls, which are sent or stopped by the bot
3132
*/
3233
class Update extends Entity
3334
{
@@ -46,6 +47,7 @@ protected function subEntities()
4647
'callback_query' => CallbackQuery::class,
4748
'shipping_query' => ShippingQuery::class,
4849
'pre_checkout_query' => PreCheckoutQuery::class,
50+
'poll' => Poll::class,
4951
];
5052
}
5153

@@ -66,6 +68,7 @@ public function getUpdateType()
6668
'callback_query',
6769
'shipping_query',
6870
'pre_checkout_query',
71+
'poll',
6972
];
7073
foreach ($types as $type) {
7174
if ($this->getProperty($type)) {

src/Request.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
* @method static ServerResponse stopMessageLiveLocation(array $data) Use this method to stop updating a live location message sent by the bot or via the bot (for inline bots) before live_period expires. On success, if the message was sent by the bot, the sent Message is returned, otherwise True is returned.
4343
* @method static ServerResponse sendVenue(array $data) Use this method to send information about a venue. On success, the sent Message is returned.
4444
* @method static ServerResponse sendContact(array $data) Use this method to send phone contacts. On success, the sent Message is returned.
45+
* @method static ServerResponse sendPoll(array $data) Use this method to send a native poll. A native poll can't be sent to a private chat. On success, the sent Message is returned.
4546
* @method static ServerResponse sendChatAction(array $data) Use this method when you need to tell the user that something is happening on the bot's side. The status is set for 5 seconds or less (when a message arrives from your bot, Telegram clients clear its typing status). Returns True on success.
4647
* @method static ServerResponse getUserProfilePhotos(array $data) Use this method to get a list of profile pictures for a user. Returns a UserProfilePhotos object.
4748
* @method static ServerResponse getFile(array $data) Use this method to get basic info about a file and prepare it for downloading. For the moment, bots can download files of up to 20MB in size. On success, a File object is returned. The file can then be downloaded via the link https://api.telegram.org/file/bot<token>/<file_path>, where <file_path> is taken from the response. It is guaranteed that the link will be valid for at least 1 hour. When the link expires, a new one can be requested by calling getFile again.
@@ -69,6 +70,7 @@
6970
* @method static ServerResponse editMessageCaption(array $data) Use this method to edit captions of messages sent by the bot or via the bot (for inline bots). On success, if edited message is sent by the bot, the edited Message is returned, otherwise True is returned.
7071
* @method static ServerResponse editMessageMedia(array $data) Use this method to edit audio, document, photo, or video messages. On success, if the edited message was sent by the bot, the edited Message is returned, otherwise True is returned.
7172
* @method static ServerResponse editMessageReplyMarkup(array $data) Use this method to edit only the reply markup of messages sent by the bot or via the bot (for inline bots). On success, if edited message is sent by the bot, the edited Message is returned, otherwise True is returned.
73+
* @method static ServerResponse stopPoll(array $data) Use this method to stop a poll which was sent by the bot. On success, the stopped Poll with the final results is returned.
7274
* @method static ServerResponse deleteMessage(array $data) Use this method to delete a message, including service messages, with certain limitations. Returns True on success.
7375
* @method static ServerResponse getStickerSet(array $data) Use this method to get a sticker set. On success, a StickerSet object is returned.
7476
* @method static ServerResponse uploadStickerFile(array $data) Use this method to upload a .png file with a sticker for later use in createNewStickerSet and addStickerToSet methods (can be used multiple times). Returns the uploaded File on success.
@@ -166,6 +168,7 @@ class Request
166168
'stopMessageLiveLocation',
167169
'sendVenue',
168170
'sendContact',
171+
'sendPoll',
169172
'sendChatAction',
170173
'getUserProfilePhotos',
171174
'getFile',
@@ -193,6 +196,7 @@ class Request
193196
'editMessageCaption',
194197
'editMessageMedia',
195198
'editMessageReplyMarkup',
199+
'stopPoll',
196200
'deleteMessage',
197201
'getStickerSet',
198202
'uploadStickerFile',
@@ -807,13 +811,15 @@ private static function limitTelegramRequests($action, array $data = [])
807811
'stopMessageLiveLocation',
808812
'sendVenue',
809813
'sendContact',
814+
'sendPoll',
810815
'sendInvoice',
811816
'sendGame',
812817
'setGameScore',
813818
'editMessageText',
814819
'editMessageCaption',
815820
'editMessageMedia',
816821
'editMessageReplyMarkup',
822+
'stopPoll',
817823
'setChatTitle',
818824
'setChatDescription',
819825
'setChatStickerSet',

structure.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ CREATE TABLE IF NOT EXISTS `message` (
9393
`contact` TEXT COMMENT 'Contact object. Message is a shared contact, information about the contact',
9494
`location` TEXT COMMENT 'Location object. Message is a shared location, information about the location',
9595
`venue` TEXT COMMENT 'Venue object. Message is a Venue, information about the Venue',
96+
`poll` TEXT COMMENT 'Poll object. Message is a native poll, information about the poll',
9697
`caption` TEXT COMMENT 'For message with caption, the actual UTF-8 text of the caption',
9798
`new_chat_members` TEXT COMMENT 'List of unique user identifiers, new member(s) were added to the group, information about them (one of these members may be the bot itself)',
9899
`left_chat_member` bigint NULL DEFAULT NULL COMMENT 'Unique user identifier, a member was removed from the group, information about them (this member may be the bot itself)',

utils/db-schema-update/unreleased.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
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`;
3+
ALTER TABLE `message` ADD COLUMN `poll` TEXT COMMENT 'Poll object. Message is a native poll, information about the poll' AFTER `venue`;

0 commit comments

Comments
 (0)