Skip to content

Commit 7263feb

Browse files
authored
add error handler to telegram message
1 parent 9f69d64 commit 7263feb

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,13 @@ class InvoicePaid extends Notification
167167
->button('View Invoice', $url)
168168
->button('Download Invoice', $url)
169169
// (Optional) Inline Button with callback. You can handle callback in your bot instance
170-
->buttonWithCallback('Confirm', 'confirm_invoice ' . $this->invoice->id);
170+
->buttonWithCallback('Confirm', 'confirm_invoice ' . $this->invoice->id)
171+
172+
// (Optional) Register a callback to handle exceptions if they occur.
173+
// This allows you to define a custom action when an exception is thrown.
174+
->onError(function($exception) {
175+
// Custom logic to handle the exception
176+
});
171177
}
172178
}
173179
```

src/TelegramMessage.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use NotificationChannels\Telegram\Contracts\TelegramSenderContract;
88
use NotificationChannels\Telegram\Exceptions\CouldNotSendNotification;
99
use Psr\Http\Message\ResponseInterface;
10+
use Closure;
1011

1112
/**
1213
* Class TelegramMessage.
@@ -15,6 +16,7 @@ class TelegramMessage extends TelegramBase implements TelegramSenderContract
1516
{
1617
/** @var int Message Chunk Size */
1718
public int $chunkSize = 0;
19+
public ?Closure $exceptionHandler = null;
1820

1921
public function __construct(string $content = '')
2022
{
@@ -82,6 +84,22 @@ public function view(string $view, array $data = [], array $mergeData = []): sel
8284
return $this->content(View::make($view, $data, $mergeData)->render());
8385
}
8486

87+
/**
88+
* Registers a callback function to handle exceptions.
89+
*
90+
* This method allows you to define a custom error handler,
91+
* which will be invoked if an exception occurs during the
92+
* notification process. The callback must be a valid Closure.
93+
*
94+
* @param Closure $callback The closure that will handle exceptions.
95+
* @return self
96+
*/
97+
public function onError(Closure $callback): self
98+
{
99+
$this->exceptionHandler = $callback;
100+
return $this;
101+
}
102+
85103
/**
86104
* Chunk message to given size.
87105
*
@@ -110,11 +128,19 @@ public function send(): ResponseInterface|array|null
110128
{
111129
$params = $this->toArray();
112130

113-
if ($this->shouldChunk()) {
114-
return $this->sendChunkedMessage($params);
131+
try {
132+
if ($this->shouldChunk()) {
133+
return $this->sendChunkedMessage($params);
134+
}
135+
136+
return $this->telegram->sendMessage($params);
137+
} catch(Exception $exception) {
138+
if(! $this->exceptionHandler) {
139+
throw $exception;
140+
}
141+
142+
$this->exceptionHandler($exception);
115143
}
116-
117-
return $this->telegram->sendMessage($params);
118144
}
119145

120146
/**

0 commit comments

Comments
 (0)