Skip to content

Commit f669c3d

Browse files
authored
Poll support (#130)
1 parent dd7cee4 commit f669c3d

File tree

5 files changed

+163
-0
lines changed

5 files changed

+163
-0
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ This package makes it easy to send Telegram notification using [Telegram Bot API
1717
- [Proxy or Bridge Support](#proxy-or-bridge-support)
1818
- [Usage](#usage)
1919
- [Text Notification](#text-notification)
20+
- [Send a poll](#Send-a-poll)
2021
- [Attach a Photo](#attach-a-photo)
2122
- [Attach a Document](#attach-a-document)
2223
- [Attach a Location](#attach-a-location)
@@ -124,6 +125,22 @@ Here's a screenshot preview of the above notification on Telegram Messenger:
124125

125126
![Laravel Telegram Notification Example](https://user-images.githubusercontent.com/1915268/66616627-39be6180-ebef-11e9-92cc-f2da81da047a.jpg)
126127

128+
### Send a poll
129+
130+
```php
131+
public function toTelegram($notifiable)
132+
{
133+
return TelegramPoll::create()
134+
->to($notifiable)
135+
->question("Aren't Laravel Notification Channels awesome?")
136+
->choices(['Yes', 'YEs', 'YES']);
137+
}
138+
```
139+
140+
Preview:
141+
142+
![Laravel Telegram Poll Example](https://user-images.githubusercontent.com/60013703/143135248-1224a69b-3233-4686-8a59-d41517d8c722.png)
143+
127144
### Attach a Photo
128145

129146
```php

src/Telegram.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,20 @@ public function sendFile(array $params, string $type, bool $multipart = false):
151151
return $this->sendRequest('send'.Str::studly($type), $params, $multipart);
152152
}
153153

154+
/**
155+
* Send a Poll.
156+
*
157+
* @param array $params
158+
*
159+
* @throws CouldNotSendNotification
160+
*
161+
* @return ResponseInterface|null
162+
*/
163+
public function sendPoll(array $params): ?ResponseInterface
164+
{
165+
return $this->sendRequest('sendPoll', $params);
166+
}
167+
154168
/**
155169
* Send a Location.
156170
*

src/TelegramChannel.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ public function send($notifiable, Notification $notification): ?array
109109
$response = $this->telegram->sendLocation($params);
110110
} elseif ($message instanceof TelegramFile) {
111111
$response = $this->telegram->sendFile($params, $message->type, $message->hasFile());
112+
} elseif ($message instanceof TelegramPoll) {
113+
$response = $this->telegram->sendPoll($params);
112114
} else {
113115
return null;
114116
}

src/TelegramPoll.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace NotificationChannels\Telegram;
4+
5+
use JsonSerializable;
6+
use NotificationChannels\Telegram\Traits\HasSharedLogic;
7+
8+
/**
9+
* Class TelegramPoll.
10+
*/
11+
class TelegramPoll implements JsonSerializable
12+
{
13+
use HasSharedLogic;
14+
15+
/**
16+
* @param string $question
17+
*
18+
* @return self
19+
*/
20+
public static function create(string $question = ''): self
21+
{
22+
return new self($question);
23+
}
24+
25+
/**
26+
* Message constructor.
27+
*
28+
* @param string $question
29+
*/
30+
public function __construct(string $question = '')
31+
{
32+
$this->question($question);
33+
}
34+
35+
/**
36+
* Poll question.
37+
*
38+
* @param string $question
39+
*
40+
* @return $this
41+
*/
42+
public function question(string $question): self
43+
{
44+
$this->payload['question'] = $question;
45+
46+
return $this;
47+
}
48+
49+
/**
50+
* Poll choices.
51+
*
52+
* @param array $choices
53+
*
54+
* @return $this
55+
*/
56+
public function choices(array $choices): self
57+
{
58+
$this->payload['options'] = json_encode($choices);
59+
60+
return $this;
61+
}
62+
}

tests/TelegramPollTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace NotificationChannels\Telegram\Test;
4+
5+
use NotificationChannels\Telegram\TelegramPoll;
6+
use PHPUnit\Framework\TestCase;
7+
8+
/**
9+
* Class TelegramPollTest.
10+
*/
11+
class TelegramPollTest extends TestCase
12+
{
13+
/** @test */
14+
public function it_accepts_question_when_constructed(): void
15+
{
16+
$message = new TelegramPoll("Aren't Laravel Notification Channels awesome?");
17+
$this->assertEquals("Aren't Laravel Notification Channels awesome?", $message->getPayloadValue('question'));
18+
}
19+
20+
/** @test */
21+
public function the_recipients_chat_id_can_be_set(): void
22+
{
23+
$message = new TelegramPoll();
24+
$message->to(12345);
25+
$this->assertEquals(12345, $message->getPayloadValue('chat_id'));
26+
}
27+
28+
/** @test */
29+
public function the_question_message_can_be_set(): void
30+
{
31+
$message = new TelegramPoll();
32+
$message->question("Aren't Laravel Notification Channels awesome?");
33+
$this->assertEquals("Aren't Laravel Notification Channels awesome?", $message->getPayloadValue('question'));
34+
}
35+
36+
/** @test */
37+
public function the_options_can_be_set_for_the_question(): void
38+
{
39+
$message = new TelegramPoll();
40+
$message->choices(['Yes', 'No']);
41+
$this->assertEquals('["Yes","No"]', $message->getPayloadValue('options'));
42+
}
43+
44+
/** @test */
45+
public function it_can_determine_if_the_recipient_chat_id_has_not_been_set(): void
46+
{
47+
$message = new TelegramPoll();
48+
$this->assertTrue($message->toNotGiven());
49+
50+
$message->to(12345);
51+
$this->assertFalse($message->toNotGiven());
52+
}
53+
54+
/** @test */
55+
public function it_can_return_the_payload_as_an_array(): void
56+
{
57+
$message = new TelegramPoll("Aren't Laravel Notification Channels awesome?");
58+
$message->to(12345);
59+
$message->choices(['Yes', 'No']);
60+
$expected = [
61+
'chat_id' => 12345,
62+
'question' => "Aren't Laravel Notification Channels awesome?",
63+
'options' => '["Yes","No"]',
64+
];
65+
66+
$this->assertEquals($expected, $message->toArray());
67+
}
68+
}

0 commit comments

Comments
 (0)