Skip to content

Commit f52cfe9

Browse files
author
camilo.sperberg
committed
Implements Poll support
1 parent 91bd858 commit f52cfe9

File tree

11 files changed

+269
-35
lines changed

11 files changed

+269
-35
lines changed

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
[![PHP 7 Telegram Bot API](https://github.com/unreal4u/telegram-api/blob/master/examples/binary-test-data/logo-php7-telegram-bot-api-small.png?raw=true)](https://github.com/unreal4u/telegram-api/wiki/100-stars!)
1+
[![PHP 7 Telegram Bot API Library](https://github.com/unreal4u/telegram-api/blob/master/examples/binary-test-data/logo-php7-telegram-bot-api-small.png?raw=true)](https://github.com/unreal4u/telegram-api/wiki/100-stars!)
22

3-
# Telegram API
3+
# Telegram API Library
44

55
[![Latest Stable Version](https://poser.pugx.org/unreal4u/telegram-api/v/stable)](https://packagist.org/packages/unreal4u/telegram-api)
66
[![Total Downloads](https://poser.pugx.org/unreal4u/telegram-api/downloads)](https://packagist.org/packages/unreal4u/telegram-api)
@@ -9,10 +9,11 @@
99
[![Code Coverage](https://scrutinizer-ci.com/g/unreal4u/telegram-api/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/unreal4u/telegram-api/?branch=master)
1010
[![License](https://poser.pugx.org/unreal4u/telegram-api/license)](https://packagist.org/packages/unreal4u/telegram-api)
1111

12-
This is a PHP7 bot API implementation for Telegram implementing the **totality** of [Bot API up until v3.6](https://core.telegram.org/bots/api#february-13-2018)
13-
and the vast majority of the [Bot API up until v4.1](https://core.telegram.org/bots/api#august-27-2018)
12+
This is a PHP7 bot API implementation for Telegram implementing the **vast majority** of
13+
[Bot API up until v4.3](https://core.telegram.org/bots/api#february-13-2018). The only thing it does not implement is
14+
Telegram Passport which was introduced in [Bot API v4.0](https://core.telegram.org/bots/api#july-26-2018).
1415

15-
### About this class
16+
### About this package
1617

1718
* Enables you to anything supported by the Telegram Bot API: messages, stickers, location, inline bots and any other supported method via PHP to a Telegram user (either direct conversation, channel, group or supergroup).
1819
* Respects and implements the default types and methods made by Telegram itself. Have any doubts about any method? [Just check the original documentation](https://core.telegram.org/bots/api), this implementation will not differ too much.
@@ -24,7 +25,8 @@ and the vast majority of the [Bot API up until v4.1](https://core.telegram.org/b
2425

2526
[![Telegram](http://trellobot.doomdns.org/telegrambadge.svg)](https://t.me/PHPBotAPI)
2627

27-
The only thing that is not included in this library (yet) is the Passport support. This will be implemented soon though!
28+
The only thing that is not included in this library (yet) is the Passport support. This was an ongoing development, but
29+
it ended up being a lot more work than initially thought, so if someone wants to pick that up... be my guest!
2830

2931
All other known bugs can be found in the form of issues or pull requests. Found a new bug? Feel free to [submit a PR](https://github.com/unreal4u/telegram-api/pulls) or
3032
[create an issue](https://github.com/unreal4u/telegram-api/issues)! Not sure if you've found a new bug? You can always ask
@@ -34,7 +36,7 @@ in the [special group](https://t.me/PHPBotAPI) :)
3436

3537
- v4 (no branch yet) will be the next major release. [More information](https://github.com/unreal4u/telegram-api/projects/5).
3638
- v3 (master branch) is the current active branch.
37-
- v2 (v2 branch) will be updated with the latest Bot API updates until v4 comes out (no plans for that yet, sorry!).
39+
- v2 (v2 branch) is deprecated and no new work will be done there.
3840
- v1 is deprecated and no new work will be done there.
3941
- v0 is deprecated and no new work will be done there.
4042

src/Abstracts/TraversableCustomType.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,28 @@
44

55
namespace unreal4u\TelegramAPI\Abstracts;
66

7+
use ArrayIterator;
8+
use Generator;
9+
use IteratorAggregate;
10+
use Traversable;
11+
712
/**
8-
* Class TraversableType
13+
* Class TraversableCustomType
914
* @package unreal4u\TelegramAPI\Abstracts
1015
*/
11-
abstract class TraversableCustomType extends CustomType implements \IteratorAggregate
16+
abstract class TraversableCustomType extends CustomType implements IteratorAggregate
1217
{
13-
public function getIterator(): \Traversable
18+
public function getIterator(): Traversable
19+
{
20+
return new ArrayIterator($this->data);
21+
}
22+
23+
/**
24+
* Traverses through our $data, yielding the result set, can return any type of object
25+
* @return Generator|TelegramTypes
26+
*/
27+
final public function traverseObject(): Generator
1428
{
15-
return new \ArrayIterator($this->data);
29+
yield from $this->data;
1630
}
1731
}

src/Telegram/Methods/SendPoll.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace unreal4u\TelegramAPI\Telegram\Methods;
6+
7+
use unreal4u\TelegramAPI\Abstracts\KeyboardMethods;
8+
use unreal4u\TelegramAPI\Abstracts\TelegramMethods;
9+
use function json_encode;
10+
11+
/**
12+
* Use this method to send point on the map. On success, the sent Message is returned.
13+
*
14+
* Objects defined as-is june 2019
15+
*
16+
* @see https://core.telegram.org/bots/api#sendpoll
17+
*/
18+
class SendPoll extends TelegramMethods
19+
{
20+
/**
21+
* Unique identifier for the target chat or username of the target channel (in the format @channelusername). A
22+
* native poll can't be sent to a private chat
23+
* @var string
24+
*/
25+
public $chat_id = '';
26+
27+
/**
28+
* Poll question, 1-255 characters
29+
* @var string
30+
*/
31+
public $question = '';
32+
33+
/**
34+
* List of answer options, 2-10 strings 1-100 characters each
35+
* @var string[]
36+
*/
37+
public $options = [];
38+
39+
/**
40+
* Optional. Sends the message silently. iOS users will not receive a notification, Android users will receive a
41+
* notification with no sound.
42+
* @see https://telegram.org/blog/channels-2-0#silent-messages
43+
* @var bool
44+
*/
45+
public $disable_notification = false;
46+
47+
/**
48+
* If the message is a reply, ID of the original message
49+
* @var int
50+
*/
51+
public $reply_to_message_id = 0;
52+
53+
/**
54+
* Optional. Additional interface options. A JSON-serialized object for a custom reply keyboard, instructions to
55+
* hide keyboard or to force a reply from the user.
56+
* @var KeyboardMethods
57+
*/
58+
public $reply_markup;
59+
60+
public function getMandatoryFields(): array
61+
{
62+
return [
63+
'chat_id',
64+
'question',
65+
'options',
66+
];
67+
}
68+
69+
public function performSpecialConditions(): TelegramMethods
70+
{
71+
$this->options = json_encode($this->options);
72+
return parent::performSpecialConditions();
73+
}
74+
}

src/Telegram/Methods/StopPoll.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace unreal4u\TelegramAPI\Telegram\Methods;
6+
7+
use unreal4u\TelegramAPI\Abstracts\KeyboardMethods;
8+
use unreal4u\TelegramAPI\Abstracts\TelegramMethods;
9+
10+
/**
11+
* Use this method to send point on the map. On success, the sent Message is returned.
12+
*
13+
* Objects defined as-is june 2019
14+
*
15+
* @see https://core.telegram.org/bots/api#stoppoll
16+
*/
17+
class StopPoll extends TelegramMethods
18+
{
19+
/**
20+
* Unique identifier for the target chat or username of the target channel (in the format @channelusername). A
21+
* native poll can't be sent to a private chat
22+
* @var string
23+
*/
24+
public $chat_id = '';
25+
26+
/**
27+
* Identifier of the original message with the poll
28+
* @var int
29+
*/
30+
public $message_id;
31+
32+
/**
33+
* Optional. Additional interface options. A JSON-serialized object for a custom reply keyboard, instructions to
34+
* hide keyboard or to force a reply from the user.
35+
* @var KeyboardMethods
36+
*/
37+
public $reply_markup;
38+
39+
public function getMandatoryFields(): array
40+
{
41+
return [
42+
'chat_id',
43+
'message_id',
44+
];
45+
}
46+
}

src/Telegram/Types/Custom/MessageArray.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,4 @@ public function __construct(array $data = null, LoggerInterface $logger = null)
2222
}
2323
}
2424
}
25-
26-
/**
27-
* Traverses through our $data, yielding the result set
28-
*
29-
* @return Update[]
30-
*/
31-
public function traverseObject()
32-
{
33-
foreach ($this->data as $message) {
34-
yield $message;
35-
}
36-
}
3725
}

src/Telegram/Types/Custom/PassportFileArray.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,4 @@ public function __construct(array $data = null, LoggerInterface $logger = null)
2222
}
2323
}
2424
}
25-
26-
/**
27-
* Traverses through our $data, yielding the result set
28-
*
29-
* @return PhotoSize[]
30-
*/
31-
public function traverseObject(): \Generator
32-
{
33-
foreach ($this->data as $passportFile) {
34-
yield $passportFile;
35-
}
36-
}
3725
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace unreal4u\TelegramAPI\Telegram\Types\Custom;
5+
6+
use Psr\Log\LoggerInterface;
7+
use unreal4u\TelegramAPI\Abstracts\TraversableCustomType;
8+
use unreal4u\TelegramAPI\Telegram\Types\PollOption;
9+
use function count;
10+
11+
/**
12+
* Used for methods that will return an array of messages
13+
*/
14+
class PollOptionArray extends TraversableCustomType
15+
{
16+
public function __construct(array $data = null, LoggerInterface $logger = null)
17+
{
18+
if (count($data) !== 0) {
19+
foreach ($data as $telegramResponse) {
20+
// Create an actual Update object and fill the array
21+
$this->data[] = new PollOption($telegramResponse, $logger);
22+
}
23+
}
24+
}
25+
}

src/Telegram/Types/Message.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,12 @@ class Message extends TelegramTypes
198198
*/
199199
public $venue;
200200

201+
/**
202+
* Optional. Message is a native poll, information about the poll
203+
* @var Poll
204+
*/
205+
public $poll;
206+
201207
/**
202208
* Optional. A new member was added to the group, information about them (this member may be the bot itself)
203209
* @var User
@@ -349,6 +355,8 @@ protected function mapSubObjects(string $key, array $data): TelegramTypes
349355
return new Location($data, $this->logger);
350356
case 'venue':
351357
return new Venue($data, $this->logger);
358+
case 'poll':
359+
return new Poll($data, $this->logger);
352360
case 'invoice':
353361
return new Invoice($data, $this->logger);
354362
case 'successful_payment':

src/Telegram/Types/Poll.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace unreal4u\TelegramAPI\Telegram\Types;
6+
7+
use unreal4u\TelegramAPI\Abstracts\TelegramTypes;
8+
use unreal4u\TelegramAPI\Telegram\Types\Custom\PollOptionArray;
9+
10+
/**
11+
* This object contains information about a poll
12+
*
13+
* Objects defined as-is june 2019
14+
*
15+
* @see https://core.telegram.org/bots/api#poll
16+
*/
17+
class Poll extends TelegramTypes
18+
{
19+
/**
20+
* Unique poll identifier
21+
* @var string
22+
*/
23+
public $id;
24+
25+
/**
26+
* Poll question, 1-255 characters
27+
* @var string
28+
*/
29+
public $question = '';
30+
31+
/**
32+
* List of poll options
33+
* @var PollOption[]
34+
*/
35+
public $options;
36+
37+
/**
38+
* True, if the poll is closed
39+
* @var bool
40+
*/
41+
public $is_closed;
42+
43+
public function mapSubObjects(string $key, array $data): TelegramTypes
44+
{
45+
switch ($key) {
46+
case 'options':
47+
return new PollOptionArray($data, $this->logger);
48+
}
49+
50+
return parent::mapSubObjects($key, $data); // TODO: Change the autogenerated stub
51+
}
52+
}

src/Telegram/Types/PollOption.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace unreal4u\TelegramAPI\Telegram\Types;
6+
7+
use unreal4u\TelegramAPI\Abstracts\TelegramTypes;
8+
9+
/**
10+
* This object contains information about a poll
11+
*
12+
* Objects defined as-is june 2019
13+
*
14+
* @see https://core.telegram.org/bots/api#polloption
15+
*/
16+
class PollOption extends TelegramTypes
17+
{
18+
/**
19+
* Option text, 1-100 characters
20+
* @var string
21+
*/
22+
public $text;
23+
24+
/**
25+
* Number of users that voted for this option
26+
* @var int
27+
*/
28+
public $voter_count;
29+
}

0 commit comments

Comments
 (0)