8000 feature #51717 [Notifier] [Telegram] Extend options for `location`, `… · symfony/symfony@cdf30c5 · GitHub
[go: up one dir, main page]

Skip to content

Commit cdf30c5

Browse files
committed
feature #51717 [Notifier] [Telegram] Extend options for location, document, audio, video, venue, photo, animation, sticker & contact (igrizzli)
This PR was squashed before being merged into the 6.4 branch. Discussion ---------- [Notifier] [Telegram] Extend options for `location`, `document`, `audio`, `video`, `venue`, `photo`, `animation`, `sticker` & `contact` | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> Extends #49986 for sending local files as photo Add possibility to send point on map in notification. Commits ------- e223f8e [Notifier] [Telegram] Extend options for `location`, `document`, `audio`, `video`, `venue`, `photo`, `animation`, `sticker` & `contact`
2 parents d591d77 + e223f8e commit cdf30c5

File tree

7 files changed

+1305
-28
lines changed

7 files changed

+1305
-28
lines changed

src/Symfony/Component/Notifier/Bridge/Telegram/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
6.4
5+
---
6+
7+
* Add support for `sendLocation`, `sendAudio`, `sendDocument`, `sendVideo`, `sendAnimation`, `sendVenue`, `sendContact` and `sendSticker` API methods
8+
* Add support for sending local files
9+
410
6.3
511
---
612

src/Symfony/Component/Notifier/Bridge/Telegram/README.md

Lines changed: 177 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,109 @@ $chatMessage->options($telegramOptions);
4747
$chatter->send($chatMessage);
4848
```
4949

50-
Adding Photo to a Message
50+
Adding files to a Message
5151
-------------------------
5252

5353
With a Telegram message, you can use the `TelegramOptions` class to add
5454
[message options](https://core.telegram.org/bots/api).
5555

56+
> :warning: **WARNING**
57+
In one message you can send only one file
58+
59+
[Telegram supports 3 ways](https://core.telegram.org/bots/api#sending-files) for passing files:
60+
61+
* You can send files by passing public http url to option:
62+
* Photo
63+
```php
64+
$telegramOptions = (new TelegramOptions())
65+
->photo('https://localhost/photo.mp4');
66+
```
67+
* Video
68+
```php
69+
$telegramOptions = (new TelegramOptions())
70+
->video('https://localhost/video.mp4');
71+
```
72+
* Animation
73+
```php
74+
$telegramOptions = (new TelegramOptions())
75+
->animation('https://localhost/animation.gif');
76+
```
77+
* Audio
78+
```php
79+
$telegramOptions = (new TelegramOptions())
80+
->audio('https://localhost/audio.ogg');
81+
```
82+
* Document
83+
```php
84+
$telegramOptions = (new TelegramOptions())
85+
->document('https://localhost/document.odt');
86+
```
87+
* Sticker
88+
```php
89+
$telegramOptions = (new TelegramOptions())
90+
->sticker('https://localhost/sticker.webp', '🤖');
91+
```
92+
* You can send files by passing local path to option, in this case file will be sent via multipart/form-data:
93+
* Photo
94+
```php
95+
$telegramOptions = (new TelegramOptions())
96+
->uploadPhoto('files/photo.png');
97+
```
98+
* Video
99+
```php
100+
$telegramOptions = (new TelegramOptions())
101+
->uploadVideo('files/video.mp4');
102+
```
103+
* Animation
104+
```php
105+
$telegramOptions = (new TelegramOptions())
106+
->uploadAnimation('files/animation.gif');
107+
```
108+
* Audio
109+
```php
110+
$telegramOptions = (new TelegramOptions())
111+
->uploadAudio('files/audio.ogg');
112+
```
113+
* Document
114+
```php
115+
$telegramOptions = (new TelegramOptions())
116+
->uploadDocument('files/document.odt');
117+
```
118+
* Sticker
119+
```php
120+
$telegramOptions = (new TelegramOptions())
121+
->uploadSticker('files/sticker.webp', '🤖');
122+
```
123+
* You can send files by passing file_id to option:
124+
* Photo
125+
```php
126+
$telegramOptions = (new TelegramOptions())
127+
->photo('ABCDEF');
128+
```
129+
* Video
130+
```php
131+
$telegramOptions = (new TelegramOptions())
132+
->video('ABCDEF');
133+
```
134+
* Animation
135+
```php
136+
$telegramOptions = (new TelegramOptions())
137+
->animation('ABCDEF');
138+
```
139+
* Audio
140+
```php
141+
$telegramOptions = (new TelegramOptions())
142+
->audio('ABCDEF');
143+
```
144+
* Document
145+
```php
146+
$telegramOptions = (new TelegramOptions())
147+
->document('ABCDEF');
148+
```
149+
* Sticker - *Can't be sent using file_id*
150+
151+
Full example:
56152
```php
57-
use Symfony\Component\Notifier\Bridge\Telegram\Reply\Markup\Button\InlineKeyboardButton;
58-
use Symfony\Component\Notifier\Bridge\Telegram\Reply\Markup\InlineKeyboardMarkup;
59153
use Symfony\Component\Notifier\Bridge\Telegram\TelegramOptions;
60154
use Symfony\Component\Notifier\Message\ChatMessage;
61155

@@ -76,6 +170,86 @@ $chatMessage->options($telegramOptions);
76170
$chatter->send($chatMessage);
77171
```
78172

173+
Adding Location to a Message
174+
----------------------------
175+
176+
With a Telegram message, you can use the `TelegramOptions` class to add
177+
[message options](https://core.telegram.org/bots/api).
178+
179+
```php
180+
use Symfony\Component\Notifier\Bridge\Telegram\TelegramOptions;
181+
use Symfony\Component\Notifier\Message\ChatMessage;
182+
183+
$chatMessage = new ChatMessage('');
184+
185+
// Create Telegram options
186+
$telegramOptions = (new TelegramOptions())
187+
->chatId('@symfonynotifierdev')
188+
->parseMode('MarkdownV2')
189+
->location(48.8566, 2.3522);
190+
191+
// Add the custom options to the chat message and send the message
192+
$chatMessage->options($telegramOptions);
193+
194+
$chatter->send($chatMessage);
195+
```
196+
197+
Adding Venue to a Message
198+
----------------------------
199+
200+
With a Telegram message, you can use the `TelegramOptions` class to add
201+
[message options](https://core.telegram.org/bots/api).
202+
203+
```php
204+
use Symfony\Component\Notifier\Bridge\Telegram\TelegramOptions;
205+
use Symfony\Component\Notifier\Message\ChatMessage;
206+
207+
$chatMessage = new ChatMessage('');
208+
209+
// Create Telegram options
210+
$telegramOptions = (new TelegramOptions())
211+
->chatId('@symfonynotifierdev')
212+
->parseMode('MarkdownV2')
213+
->venue(48.8566, 2.3522, 'Center of Paris', 'France, Paris');
214+
215+
// Add the custom options to the chat message and send the message
216+
$chatMessage->options($telegramOptions);
217+
218+
$chatter->send($chatMessage);
219+
```
220+
221+
Adding Contact to a Message
222+
----------------------------
223+
224+
With a Telegram message, you can use the `TelegramOptions` class to add
225+
[message options](https://core.telegram.org/bots/api).
226+
227+
```php
228+
use Symfony\Component\Notifier\Bridge\Telegram\TelegramOptions;
229+
use Symfony\Component\Notifier\Message\ChatMessage;
230+
231+
$chatMessage = new ChatMessage('');
232+
233+
$vCard = 'BEGIN:VCARD
234+
VERSION:3.0
235+
N:Doe;John;;;
236+
FN:John Doe
237+
EMAIL;type=INTERNET;type=WORK;type=pref:johnDoe@example.org
238+
TEL;type=WORK;type=pref:+330186657200
239+
END:VCARD';
240+
241+
// Create Telegram options
242+
$telegramOptions = (new TelegramOptions())
243+
->chatId('@symfonynotifierdev')
244+
->parseMode('MarkdownV2')
245+
->contact('+330186657200', 'John', 'Doe', $vCard);
246+
247+
// Add the custom options to the chat message and send the message
248+
$chatMessage->options($telegramOptions);
249+
250+
$chatter->send($chatMessage);
251+
```
252+
79253
Updating Messages
80254
-----------------
81255

src/Symfony/Component/Notifier/Bridge/Telegram/TelegramOptions.php

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,16 @@ public function photo(string $url): static
112112
return $this;
113113
}
114114

115+
/**
116+
* @return $this
117+
*/
118+
public function uploadPhoto(string $path): static
119+
{
120+
$this->options['upload']['photo'] = $path;
121+
122+
return $this;
123+
}
124+
115125
/**
116126
* @return $this
117127
*/
@@ -156,4 +166,152 @@ public function answerCallbackQuery(string $callbackQueryId, bool $showAlert = f
156166

157167
return $this;
158168
}
169+
170+
/**
171+
* @return $this
172+
*/
173+
public function location(float $latitude, float $longitude): static
174+
{
175+
$this->options['location'] = ['latitude' => $latitude, 'longitude' => $longitude];
176+
177+
return $this;
178+
}
179+
180+
/**
181+
* @return $this
182+
*/
183+
public function venue(float $latitude, float $longitude, string $title, string $address): static
184+
{
185+
$this->options['venue'] = [
186+
'latitude' => $latitude,
187+
'longitude' => $longitude,
188+
'title' => $title,
189+
'address' => $address,
190+
];
191+
192+
return $this;
193+
}
194+
195+
/**
196+
* @return $this
197+
*/
198+
public function document(string $url): static
199+
{
200+
$this->options['document'] = $url;
201+
202+
return $this;
203+
}
204+
205+
/**
206+
* @return $this
207+
*/
208+
public function uploadDocument(string $path): static
209+
{
210+
$this->options['upload']['document'] = $path;
211+
212+
return $this;
213+
}
214+
215+
/**
216+
* @return $this
217+
*/
218+
public function video(string $url): static
219+
{
220+
$this->options['video'] = $url;
221+
222+
return $this;
223+
}
224+
225+
/**
226+
* @return $this
227+
*/
228+
public function uploadVideo(string $path): static
229+
{
230+
$this->options['upload']['video'] = $path;
231+
232+
return $this;
233+
}
234+
235+
/**
236+
* @return $this
237+
*/
238+
public function audio(string $url): static
239+
{
240+
$this->options['audio'] = $url;
241+
242+
return $this;
243+
}
244+
245+
/**
246+
* @return $this
247+
*/
248+
public function uploadAudio(string $path): static
249+
{
250+
$this->options['upload']['audio'] = $path;
251+
252+
return $this;
253+
}
254+
255+
/**
256+
* @return $this
257+
*/
258+
public function animation(string $url): static
259+
{
260+
$this->options['animation'] = $url;
261+
262+
return $this;
263+
}
264+
265+
/**
266+
* @return $this
267+
*/
268+
public function uploadAnimation(string $path): static
269+
{
270+
$this->options['upload']['animation'] = $path;
271+
272+
return $this;
273+
}
274+
275+
/**
276+
* @return $this
277+
*/
278+
public function sticker(string $url, string $emoji = null): static
279+
{
280+
$this->options['sticker'] = $url;
281+
$this->options['emoji'] = $emoji;
282+
283+
return $this;
284+
}
285+
286+
/**
287+
* @return $this
288+
*/
289+
public function uploadSticker(string $path, string $emoji = null): static
290+
{
291+
$this->options['upload']['sticker'] = $path;
292+
$this->options['emoji'] = $emoji;
293+
294+
return $this;
295+
}
296+
297+
/**
298+
* @return $this
299+
*/
300+
public function contact(string $phoneNumber, string $firstName, string $lastName = null, string $vCard = null): static
301+
{
302+
$this->options['contact'] = [
303+
'phone_number' => $phoneNumber,
304+
'first_name' => $firstName,
305+
];
306+
307+
if (null !== $lastName) {
308+
$this->options['contact']['last_name'] = $lastName;
309+
}
310+
311+
if (null !== $vCard) {
312+
$this->options['contact']['vcard'] = $vCard;
313+
}
314+
315+
return $this;
316+
}
159317
}

0 commit comments

Comments
 (0)
0