Skip to content

Commit 442d3d3

Browse files
Allow to handle response from Telegram (#67)
* Allow to handle response from Telegram * Return response as array * Adjust code style * Add tests for returned response
1 parent ba6e323 commit 442d3d3

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

src/TelegramChannel.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ public function __construct(Telegram $telegram)
3131
* @param mixed $notifiable
3232
* @param Notification $notification
3333
*
34+
* @return null|array
35+
*
3436
* @throws CouldNotSendNotification
3537
*/
36-
public function send($notifiable, Notification $notification): void
38+
public function send($notifiable, Notification $notification): ?array
3739
{
3840
$message = $notification->toTelegram($notifiable);
3941

@@ -52,11 +54,15 @@ public function send($notifiable, Notification $notification): void
5254
$params = $message->toArray();
5355

5456
if ($message instanceof TelegramMessage) {
55-
$this->telegram->sendMessage($params);
57+
$response = $this->telegram->sendMessage($params);
5658
} elseif ($message instanceof TelegramLocation) {
57-
$this->telegram->sendLocation($params);
59+
$response = $this->telegram->sendLocation($params);
5860
} elseif ($message instanceof TelegramFile) {
59-
$this->telegram->sendFile($params, $message->type, $message->hasFile());
61+
$response = $this->telegram->sendFile($params, $message->type, $message->hasFile());
62+
} else {
63+
return null;
6064
}
65+
66+
return json_decode($response->getBody()->getContents(), true);
6167
}
6268
}

tests/TelegramChannelTest.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace NotificationChannels\Telegram\Test;
44

5+
use GuzzleHttp\Psr7\Response;
56
use Illuminate\Notifications\Notifiable;
67
use Illuminate\Notifications\Notification;
78
use Mockery;
@@ -38,12 +39,18 @@ public function tearDown(): void
3839
/** @test */
3940
public function it_can_send_a_message(): void
4041
{
42+
$expectedResponse = ['ok' => true, 'result' => ['message_id' => 123, 'chat' => ['id' => 12345]]];
43+
4144
$this->telegram->shouldReceive('sendMessage')->once()->with([
42-
'text' => 'Laravel Notification Channels are awesome!',
45+
'text' => 'Laravel Notification Channels are awesome!',
4346
'parse_mode' => 'Markdown',
44-
'chat_id' => 12345,
45-
]);
46-
$this->channel->send(new TestNotifiable(), new TestNotification());
47+
'chat_id' => 12345,
48+
])
49+
->andReturns(new Response(200, [], json_encode($expectedResponse)));
50+
51+
$actualResponse = $this->channel->send(new TestNotifiable(), new TestNotification());
52+
53+
self::assertSame($expectedResponse, $actualResponse);
4754
}
4855

4956
/** @test */

0 commit comments

Comments
 (0)