Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add error handler to telegram message #201

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,13 @@ class InvoicePaid extends Notification
->button('View Invoice', $url)
->button('Download Invoice', $url)
// (Optional) Inline Button with callback. You can handle callback in your bot instance
->buttonWithCallback('Confirm', 'confirm_invoice ' . $this->invoice->id);
->buttonWithCallback('Confirm', 'confirm_invoice ' . $this->invoice->id)

// (Optional) Register a callback to handle exceptions if they occur.
// This allows you to define a custom action when an exception is thrown.
->onError(function($exception) {
// Custom logic to handle the exception
});
}
}
```
Expand Down
34 changes: 30 additions & 4 deletions src/TelegramMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use NotificationChannels\Telegram\Contracts\TelegramSenderContract;
use NotificationChannels\Telegram\Exceptions\CouldNotSendNotification;
use Psr\Http\Message\ResponseInterface;
use Closure;

/**
* Class TelegramMessage.
Expand All @@ -15,6 +16,7 @@ class TelegramMessage extends TelegramBase implements TelegramSenderContract
{
/** @var int Message Chunk Size */
public int $chunkSize = 0;
public ?Closure $exceptionHandler = null;

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

/**
* Registers a callback function to handle exceptions.
*
* This method allows you to define a custom error handler,
* which will be invoked if an exception occurs during the
* notification process. The callback must be a valid Closure.
*
* @param Closure $callback The closure that will handle exceptions.
* @return self
*/
public function onError(Closure $callback): self
{
$this->exceptionHandler = $callback;
return $this;
}

/**
* Chunk message to given size.
*
Expand Down Expand Up @@ -110,11 +128,19 @@ public function send(): ResponseInterface|array|null
{
$params = $this->toArray();

if ($this->shouldChunk()) {
return $this->sendChunkedMessage($params);
try {
if ($this->shouldChunk()) {
return $this->sendChunkedMessage($params);
}

return $this->telegram->sendMessage($params);
} catch(Exception $exception) {
if($this->exceptionHandler) {
$this->exceptionHandler($exception);
}

throw $exception;
}

return $this->telegram->sendMessage($params);
}

/**
Expand Down