Skip to content

Commit 177b5b8

Browse files
committed
6.0
1 parent 9f69d64 commit 177b5b8

14 files changed

+329
-254
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
vendor
22
build
3-
.phpunit.result.cache
3+
.phpunit.cache
44
composer.lock
55
phpunit.xml
66
phpstan.neon

CHANGELOG.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,25 @@
22

33
All notable changes to `telegram` will be documented in this file
44

5+
## 6.0 - (Unreleased)
6+
7+
### What's Changed
8+
9+
* Drop support for Laravel 10.
10+
* Drop support for PHP 8.1.
11+
* Add `sendWhen` method to conditionally send a message.
12+
* Add ParseMode Enum and refactor parsing mode setting logic.
13+
* Add `buttonWithWebApp` method to open web app from a button.
14+
* Add `onError` method to handle exceptions. Based of https://github.com/laravel-notification-channels/telegram/pull/201
15+
* Refactor `escapedLine` method.
16+
* Refactor `HasSharedLogic` trait.
17+
* Refactor classes to use PHP 8.2 features.
18+
* Revise `keyboard` method parameters to `$requestLocation` and `$requestContact` to be consistent.
19+
20+
### New Contributors
21+
22+
* @Hesammousavi made their first contribution in https://github.com/laravel-notification-channels/telegram/pull/201
23+
524
## 5.0 - 2024-03-12
625

726
### What's Changed

README.md

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ use NotificationChannels\Telegram\TelegramUpdates;
8787
$updates = TelegramUpdates::create()
8888
// (Optional). Get's the latest update. NOTE: All previous updates will be forgotten using this method.
8989
// ->latest()
90-
90+
9191
// (Optional). Limit to 2 updates (By default, updates starting with the earliest unconfirmed update are returned).
9292
->limit(2)
93-
93+
9494
// (Optional). Add more params to the request.
9595
->options([
9696
'timeout' => 0,
@@ -154,6 +154,7 @@ class InvoicePaid extends Notification
154154
return TelegramMessage::create()
155155
// Optional recipient user id.
156156
->to($notifiable->telegram_user_id)
157+
157158
// Markdown supported.
158159
->content("Hello there!")
159160
->line("Your invoice has been *PAID*")
@@ -165,9 +166,16 @@ class InvoicePaid extends Notification
165166

166167
// (Optional) Inline Buttons
167168
->button('View Invoice', $url)
168-
->button('Download Invoice', $url)
169+
->button('Download Invoice', $url);
170+
171+
// (Optional) Conditional notification. Only send if amount is greater than 0. Otherwise, don't send.
172+
// ->sendWhen($notifiable->amount > 0)
173+
174+
// (Optional) Inline Button with Web App
175+
// ->buttonWithWebApp('Open Web App', $url)
176+
169177
// (Optional) Inline Button with callback. You can handle callback in your bot instance
170-
->buttonWithCallback('Confirm', 'confirm_invoice ' . $this->invoice->id);
178+
// ->buttonWithCallback('Confirm', 'confirm_invoice ' . $this->invoice->id)
171179
}
172180
}
173181
```
@@ -362,10 +370,10 @@ Notification::route('telegram', 'TELEGRAM_CHAT_ID')
362370

363371
Using the [notification facade][link-notification-facade] you can send a notification to multiple recipients at once.
364372

365-
> If you're sending bulk notifications to multiple users, the Telegram Bot API will not allow more than 30 messages per second or so.
373+
> If you're sending bulk notifications to multiple users, the Telegram Bot API will not allow more than 30 messages per second or so.
366374
> Consider spreading out notifications over large intervals of 8—12 hours for best results.
367375
>
368-
> Also note that your bot will not be able to send more than 20 messages per minute to the same group.
376+
> Also note that your bot will not be able to send more than 20 messages per minute to the same group.
369377
>
370378
> If you go over the limit, you'll start getting `429` errors. For more details, refer Telegram Bots [FAQ](https://core.telegram.org/bots/faq#broadcasting-to-users).
371379
@@ -386,8 +394,11 @@ Notification::send($recipients, new InvoicePaid());
386394
- `token(string $token)`: Bot token if you wish to override the default token for a specific notification.
387395
- `button(string $text, string $url, int $columns = 2)`: Adds an inline "Call to Action" button. You can add as many as you want, and they'll be placed 2 in a row by default.
388396
- `buttonWithCallback(string $text, string $callback_data, int $columns = 2)`: Adds an inline button with the given callback data. You can add as many as you want, and they'll be placed 2 in a row by default.
397+
- `buttonWithWebApp(string $text, string $url, int $columns = 2)`: Adds an inline button that opens a web application when clicked. This button can be used to direct users to a specific web app.
389398
- `disableNotification(bool $disableNotification = true)`: Send the message silently. Users will receive a notification with no sound.
390399
- `options(array $options)`: Allows you to add additional params or override the payload.
400+
- `sendWhen(bool $condition)`: Sets a condition for sending the notification. If the condition is true, the notification will be sent; otherwise, it will not.
401+
- `onError(Closure $callback)`: Sets a callback function to handle exceptions. Callback receives an array with `to`, `request`, `exception` keys.
391402
- `getPayloadValue(string $key)`: Get payload value for given key.
392403

393404
### Telegram Message methods

composer.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,23 @@
1919
],
2020
"homepage": "https://github.com/laravel-notification-channels/telegram",
2121
"require": {
22-
"php": "^8.1",
22+
"php": "^8.2",
2323
"ext-json": "*",
24-
"guzzlehttp/guzzle": "^7.2",
25-
"illuminate/contracts": "^10 || ^11.0",
26-
"illuminate/notifications": "^10 || ^11.0",
27-
"illuminate/support": "^10 || ^11.0"
24+
"guzzlehttp/guzzle": "^7.8",
25+
"illuminate/contracts": "^11.0",
26+
"illuminate/notifications": "^11.0",
27+
"illuminate/support": "^11.0"
2828
},
2929
"require-dev": {
3030
"larastan/larastan": "^2.9",
3131
"mockery/mockery": "^1.4.4",
3232
"orchestra/testbench": "^8.0 || ^9.0",
33-
"pestphp/pest": "^2.34",
34-
"pestphp/pest-plugin-laravel": "^2.3",
33+
"pestphp/pest": "^3.0",
34+
"pestphp/pest-plugin-laravel": "^3.0",
3535
"phpstan/extension-installer": "^1.2",
3636
"phpstan/phpstan-deprecation-rules": "^1.1",
3737
"phpstan/phpstan-phpunit": "^1.3",
38-
"phpunit/phpunit": "^10.5 || ^11.0"
38+
"phpunit/phpunit": "^11.0"
3939
},
4040
"minimum-stability": "dev",
4141
"prefer-stable": true,

phpstan.neon.dist

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,3 @@ parameters:
66
paths:
77
- src
88
tmpDir: build/phpstan
9-
checkMissingIterableValueType: false

phpunit.xml.dist.bak

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/Enums/ParseMode.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace NotificationChannels\Telegram\Enums;
4+
5+
enum ParseMode: string
6+
{
7+
case Markdown = 'Markdown';
8+
case HTML = 'HTML';
9+
case MarkdownV2 = 'MarkdownV2';
10+
}

src/Telegram.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,22 @@
1515
class Telegram
1616
{
1717
/** @var HttpClient HTTP Client */
18-
protected HttpClient $http;
18+
protected readonly HttpClient $http;
1919

2020
/** @var null|string Telegram Bot API Token. */
2121
protected ?string $token;
2222

2323
/** @var string Telegram Bot API Base URI */
2424
protected string $apiBaseUri;
2525

26-
public function __construct(?string $token = null, ?HttpClient $httpClient = null, ?string $apiBaseUri = null)
27-
{
26+
public function __construct(
27+
?string $token = null,
28+
HttpClient $httpClient = new HttpClient(),
29+
string $apiBaseUri = 'https://api.telegram.org'
30+
) {
2831
$this->token = $token;
29-
$this->http = $httpClient ?? new HttpClient();
30-
$this->setApiBaseUri($apiBaseUri ?? 'https://api.telegram.org');
32+
$this->http = $httpClient;
33+
$this->setApiBaseUri($apiBaseUri);
3134
}
3235

3336
/**
@@ -112,9 +115,9 @@ public function sendMessage(array $params): ?ResponseInterface
112115
*
113116
* @throws CouldNotSendNotification
114117
*/
115-
public function sendFile(array $params, string $type, bool $multipart = false): ?ResponseInterface
118+
public function sendFile(array $params, string|array $type, bool $multipart = false): ?ResponseInterface
116119
{
117-
return $this->sendRequest('send'.Str::studly($type), $params, $multipart);
120+
return $this->sendRequest('send'.Str::studly((array)$type[0]), $params, $multipart);
118121
}
119122

120123
/**

src/TelegramChannel.php

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,9 @@
1313
*/
1414
class TelegramChannel
1515
{
16-
private Dispatcher $dispatcher;
17-
18-
/**
19-
* Channel constructor.
20-
*/
21-
public function __construct(Dispatcher $dispatcher)
22-
{
23-
$this->dispatcher = $dispatcher;
24-
}
16+
public function __construct(
17+
private readonly Dispatcher $dispatcher
18+
) {}
2519

2620
/**
2721
* Send the given notification.
@@ -38,33 +32,44 @@ public function send(mixed $notifiable, Notification $notification): ?array
3832
$message = TelegramMessage::create($message);
3933
}
4034

41-
if ($message->toNotGiven()) {
42-
$to = $notifiable->routeNotificationFor('telegram', $notification)
43-
?? $notifiable->routeNotificationFor(self::class, $notification);
35+
if (!$message->canSend()) {
36+
return null;
37+
}
4438

45-
if (! $to) {
46-
return null;
47-
}
39+
$to = $message->getPayloadValue('chat_id') ?:
40+
($notifiable->routeNotificationFor('telegram', $notification) ?:
41+
$notifiable->routeNotificationFor(self::class, $notification));
4842

49-
$message->to($to);
43+
if (! $to) {
44+
return null;
5045
}
5146

47+
$message->to($to);
48+
5249
if ($message->hasToken()) {
5350
$message->telegram->setToken($message->token);
5451
}
5552

5653
try {
5754
$response = $message->send();
5855
} catch (CouldNotSendNotification $exception) {
59-
$this->dispatcher->dispatch(new NotificationFailed($notifiable, $notification, 'telegram', [
56+
$data = [
6057
'to' => $message->getPayloadValue('chat_id'),
6158
'request' => $message->toArray(),
6259
'exception' => $exception,
63-
]));
60+
];
61+
62+
if ($message->exceptionHandler) {
63+
($message->exceptionHandler)($data);
64+
}
65+
66+
$this->dispatcher->dispatch(new NotificationFailed($notifiable, $notification, 'telegram', $data));
6467

6568
throw $exception;
6669
}
6770

68-
return $response instanceof Response ? json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR) : $response;
71+
return $response instanceof Response
72+
? json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR)
73+
: $response;
6974
}
7075
}

0 commit comments

Comments
 (0)