Skip to content

Commit e936594

Browse files
committed
Add TelegramVenue
1 parent 4d62b85 commit e936594

File tree

7 files changed

+325
-2
lines changed

7 files changed

+325
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ All notable changes to `telegram` will be documented in this file
99
* Drop support for Laravel 10.
1010
* Drop support for PHP 8.1.
1111
* Add support for Laravel 12.
12+
* Add `TelegramVenue` to support `sendVenue` method.
1213
* Add `sticker` method to the `TelegramFile` to send sticker file.
1314
* Add `sendWhen` method to conditionally send a message.
1415
* Add ParseMode Enum and refactor parsing mode setting logic.

README.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ This package makes it easy to send Telegram notification using [Telegram Bot API
2424
- [Attach a Photo](#attach-a-photo)
2525
- [Attach a Document](#attach-a-document)
2626
- [Attach a Location](#attach-a-location)
27+
- [Attach a Venue](#attach-a-venue)
2728
- [Attach a Video](#attach-a-video)
2829
- [Attach a GIF File](#attach-a-gif-file)
2930
- [Routing a Message](#routing-a-message)
@@ -37,6 +38,7 @@ This package makes it easy to send Telegram notification using [Telegram Bot API
3738
- [Common Methods](#common-methods)
3839
- [Telegram Message Methods](#telegram-message-methods)
3940
- [Telegram Location Methods](#telegram-location-methods)
41+
- [Telegram Venue Methods](#telegram-venue-methods)
4042
- [Telegram File Methods](#telegram-file-methods)
4143
- [Telegram Contact Methods](#telegram-contact-methods)
4244
- [Telegram Poll Methods](#telegram-poll-methods)
@@ -310,6 +312,19 @@ Preview:
310312

311313
![Laravel Telegram Location Notification Example](https://user-images.githubusercontent.com/1915268/66616918-54450a80-ebf0-11e9-86ea-d5264fe05ba9.jpg)
312314

315+
### Attach a Venue
316+
317+
```php
318+
public function toTelegram($notifiable)
319+
{
320+
return TelegramVenue::create()
321+
->latitude('40.6892494')
322+
->longitude('-74.0466891')
323+
->title('Sample Venue')
324+
->address('123 Main St.');
325+
}
326+
```
327+
313328
### Attach a Video
314329

315330
```php
@@ -496,13 +511,26 @@ For more information on supported parameters, check out these [docs](https://cor
496511
- `latitude(float|string $latitude)` - Set location latitude.
497512
- `longitude(float|string $longitude)` - Set location longitude.
498513

514+
### Telegram Venue Methods
515+
516+
> Telegram venue messages are used to share a geographical location information about a venue.
517+
518+
- `latitude(float|string $latitude)` - Set venue latitude.
519+
- `longitude(float|string $longitude)` - Set venue longitude.
520+
- `title(string $title)` - Set venue name/title.
521+
- `address(string $address)` - Set venue address.
522+
- `foursquareId(string $foursquareId)` - (Optional) Set Foursquare identifier of the venue.
523+
- `foursquareType(string $foursquareType)` - (Optional) Set Foursquare type of the venue, if known.
524+
- `googlePlaceId(string $googlePlaceId)` - (Optional) Set Google Places identifier of the venue.
525+
- `googlePlaceType(string $googlePlaceType)` - (Optional) Set Google Places type of the venue.
526+
499527
### Telegram File Methods
500528

501529
> Telegram file messages are used to share various types of files with the user.
502530
503531
- `content(string $content)` - Set file caption. Supports markdown.
504532
- `view(string $view, array $data = [], array $mergeData = [])` - Use Blade template for caption.
505-
- `file(string|resource|StreamInterface $file, string $type, string $filename = null)` - Attach file by path/URL. Types: `photo`, `audio`, `document`, `video`, `animation`, `voice`, `video_note`. Use helper methods below for convenience. Filename is optional, ex: `sample.pdf`.
533+
- `file(string|resource|StreamInterface $file, FileType|string $type, string $filename = null)` - Attach file by path/URL. Types: `photo`, `audio`, `document`, `video`, `animation`, `voice`, `video_note`, `sticker` (Use Enum `Enums\FileType`). Use helper methods below for convenience. Filename is optional, ex: `sample.pdf`.
506534

507535
#### Helper Methods:
508536

src/Telegram.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,16 @@ public function sendLocation(array $params): ?ResponseInterface
156156
return $this->sendRequest('sendLocation', $params);
157157
}
158158

159+
/**
160+
* Send a Venue.
161+
*
162+
* @throws CouldNotSendNotification
163+
*/
164+
public function sendVenue(array $params): ?ResponseInterface
165+
{
166+
return $this->sendRequest('sendVenue', $params);
167+
}
168+
159169
/**
160170
* Get HttpClient.
161171
*/

src/TelegramFile.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ public function toArray(): array
206206
/**
207207
* Create multipart array for file uploads.
208208
*/
209-
public function toMultipart(array $payload = null): array
209+
public function toMultipart(?array $payload = null): array
210210
{
211211
$payload = $payload ?? $this->payload;
212212
$data = [];

src/TelegramVenue.php

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<?php
2+
3+
namespace NotificationChannels\Telegram;
4+
5+
use NotificationChannels\Telegram\Contracts\TelegramSenderContract;
6+
use NotificationChannels\Telegram\Exceptions\CouldNotSendNotification;
7+
use Psr\Http\Message\ResponseInterface;
8+
9+
/**
10+
* Class TelegramVenue.
11+
*/
12+
class TelegramVenue extends TelegramBase implements TelegramSenderContract
13+
{
14+
/**
15+
* Telegram Venue constructor.
16+
*/
17+
public function __construct(
18+
float|string $latitude = '',
19+
float|string $longitude = '',
20+
string $title = '',
21+
string $address = ''
22+
) {
23+
parent::__construct();
24+
$this->latitude($latitude);
25+
$this->longitude($longitude);
26+
$this->title($title);
27+
$this->address($address);
28+
}
29+
30+
public static function create(
31+
float|string $latitude = '',
32+
float|string $longitude = '',
33+
string $title = '',
34+
string $address = ''
35+
): self {
36+
return new self($latitude, $longitude, $title, $address);
37+
}
38+
39+
/**
40+
* Venue's latitude.
41+
*
42+
* @return $this
43+
*/
44+
public function latitude(float|string $latitude): self
45+
{
46+
$this->payload['latitude'] = $latitude;
47+
48+
return $this;
49+
}
50+
51+
/**
52+
* Venue's longitude.
53+
*
54+
* @return $this
55+
*/
56+
public function longitude(float|string $longitude): self
57+
{
58+
$this->payload['longitude'] = $longitude;
59+
60+
return $this;
61+
}
62+
63+
/**
64+
* Venue's name/title.
65+
*
66+
* @return $this
67+
*/
68+
public function title(string $title): self
69+
{
70+
$this->payload['title'] = $title;
71+
72+
return $this;
73+
}
74+
75+
/**
76+
* Venue's address.
77+
*
78+
* @return $this
79+
*/
80+
public function address(string $address): self
81+
{
82+
$this->payload['address'] = $address;
83+
84+
return $this;
85+
}
86+
87+
/**
88+
* Optional: Foursquare ID.
89+
*
90+
* @return $this
91+
*/
92+
public function foursquareId(string $foursquareId): self
93+
{
94+
$this->payload['foursquare_id'] = $foursquareId;
95+
96+
return $this;
97+
}
98+
99+
/**
100+
* Optional: Foursquare Type.
101+
*
102+
* @return $this
103+
*/
104+
public function foursquareType(string $foursquareType): self
105+
{
106+
$this->payload['foursquare_type'] = $foursquareType;
107+
108+
return $this;
109+
}
110+
111+
/**
112+
* Optional: Google Place ID.
113+
*
114+
* @return $this
115+
*/
116+
public function googlePlaceId(string $googlePlaceId): self
117+
{
118+
$this->payload['google_place_id'] = $googlePlaceId;
119+
120+
return $this;
121+
}
122+
123+
/**
124+
* Optional: Google Place Type.
125+
*
126+
* @return $this
127+
*/
128+
public function googlePlaceType(string $googlePlaceType): self
129+
{
130+
$this->payload['google_place_type'] = $googlePlaceType;
131+
132+
return $this;
133+
}
134+
135+
/**
136+
* @throws CouldNotSendNotification
137+
*/
138+
public function send(): ?ResponseInterface
139+
{
140+
return $this->telegram->sendVenue($this->toArray());
141+
}
142+
}

tests/Feature/TelegramVenueTest.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
use NotificationChannels\Telegram\TelegramVenue;
4+
use NotificationChannels\Telegram\Tests\TestSupport\TestVenueNotification;
5+
use NotificationChannels\Telegram\Tests\TestSupport\TestNotifiable;
6+
7+
const TEST_LAT = 38.8951;
8+
const TEST_LONG = -77.0364;
9+
const TEST_TITLE = 'Grand Palace';
10+
const TEST_ADDRESS = 'Bangkok, Thailand';
11+
12+
it('accepts content when constructed', function () {
13+
$message = new TelegramVenue(TEST_LAT, TEST_LONG, TEST_TITLE, TEST_ADDRESS);
14+
expect($message->getPayloadValue('latitude'))->toEqual(TEST_LAT)
15+
->and($message->getPayloadValue('longitude'))->toEqual(TEST_LONG)
16+
->and($message->getPayloadValue('title'))->toEqual(TEST_TITLE)
17+
->and($message->getPayloadValue('address'))->toEqual(TEST_ADDRESS);
18+
});
19+
20+
it('accepts content when created', function () {
21+
$message = TelegramVenue::create(TEST_LAT, TEST_LONG, TEST_TITLE, TEST_ADDRESS);
22+
expect($message->getPayloadValue('latitude'))->toEqual(TEST_LAT)
23+
->and($message->getPayloadValue('longitude'))->toEqual(TEST_LONG)
24+
->and($message->getPayloadValue('title'))->toEqual(TEST_TITLE)
25+
->and($message->getPayloadValue('address'))->toEqual(TEST_ADDRESS);
26+
});
27+
28+
test('the recipients chat id can be set', function () {
29+
$message = new TelegramVenue;
30+
$message->to(12345);
31+
expect($message->getPayloadValue('chat_id'))->toEqual(12345);
32+
});
33+
34+
test('the notification latitude can be set', function () {
35+
$message = new TelegramVenue;
36+
$message->latitude(TEST_LAT);
37+
expect($message->getPayloadValue('latitude'))->toEqual(TEST_LAT);
38+
});
39+
40+
test('the notification longitude can be set', function () {
41+
$message = new TelegramVenue;
42+
$message->longitude(TEST_LONG);
43+
expect($message->getPayloadValue('longitude'))->toEqual(TEST_LONG);
44+
});
45+
46+
test('the venue title can be set', function () {
47+
$message = new TelegramVenue;
48+
$message->title(TEST_TITLE);
49+
expect($message->getPayloadValue('title'))->toEqual(TEST_TITLE);
50+
});
51+
52+
test('the venue address can be set', function () {
53+
$message = new TelegramVenue;
54+
$message->address(TEST_ADDRESS);
55+
expect($message->getPayloadValue('address'))->toEqual(TEST_ADDRESS);
56+
});
57+
58+
test('optional foursquare id can be set', function () {
59+
$message = new TelegramVenue;
60+
$message->foursquareId('4sq12345');
61+
expect($message->getPayloadValue('foursquare_id'))->toEqual('4sq12345');
62+
});
63+
64+
test('optional foursquare type can be set', function () {
65+
$message = new TelegramVenue;
66+
$message->foursquareType('coffee_shop');
67+
expect($message->getPayloadValue('foursquare_type'))->toEqual('coffee_shop');
68+
});
69+
70+
test('additional options can be set for the message', function () {
71+
$message = new TelegramVenue;
72+
$message->options(['foo' => 'bar']);
73+
expect($message->getPayloadValue('foo'))->toEqual('bar');
74+
});
75+
76+
it('can determine if the recipient chat id has not been set', function () {
77+
$message = new TelegramVenue;
78+
expect($message->toNotGiven())->toBeTrue();
79+
80+
$message->to(12345);
81+
expect($message->toNotGiven())->toBeFalse();
82+
});
83+
84+
it('can return the payload as an array', function () {
85+
$message = new TelegramVenue(TEST_LAT, TEST_LONG, TEST_TITLE, TEST_ADDRESS);
86+
$message->to(12345);
87+
$message->options(['foo' => 'bar']);
88+
89+
$expected = [
90+
'chat_id' => 12345,
91+
'foo' => 'bar',
92+
'latitude' => TEST_LAT,
93+
'longitude' => TEST_LONG,
94+
'title' => TEST_TITLE,
95+
'address' => TEST_ADDRESS,
96+
];
97+
98+
expect($message->toArray())->toEqual($expected);
99+
});
100+
101+
it('can send a venue', function () {
102+
$notifiable = new TestNotifiable;
103+
$notification = new TestVenueNotification(TEST_LAT, TEST_LONG, TEST_TITLE, TEST_ADDRESS);
104+
105+
$expectedResponse = $this->makeMockResponse([
106+
'venue' => collect($notification->toTelegram($notifiable)->toArray())->except('chat_id')->toArray(),
107+
]);
108+
109+
$actualResponse = $this->sendMockNotification('sendVenue', $notifiable, $notification, $expectedResponse);
110+
111+
expect($actualResponse)->toBe($expectedResponse);
112+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace NotificationChannels\Telegram\Tests\TestSupport;
4+
5+
use Illuminate\Notifications\Notification;
6+
use NotificationChannels\Telegram\TelegramVenue;
7+
8+
/**
9+
* Class TestVenueNotification.
10+
*/
11+
class TestVenueNotification extends Notification
12+
{
13+
public function __construct(
14+
private float|string $latitude,
15+
private float|string $longitude,
16+
private string $title,
17+
private string $address
18+
) {}
19+
20+
public function toTelegram($notifiable): TelegramVenue
21+
{
22+
return TelegramVenue::create()
23+
->to(12345)
24+
->latitude($this->latitude)
25+
->longitude($this->longitude)
26+
->title($this->title)
27+
->address($this->address)
28+
->options(['foursquare_id' => '4sq12345']);
29+
}
30+
}

0 commit comments

Comments
 (0)