westacks / telebot

Easy way to create Telegram bots in PHP
https://westacks.github.io/telebot/
MIT License
282 stars 44 forks source link

Error Handling #46

Closed FallenKnight85 closed 2 years ago

FallenKnight85 commented 2 years ago

Hi,

The exceptions property is set to true but I still cannot handle the error because no error was thrown but only logged. image

On my investigation, the telegram notification channel is just handling the error and logging a warning on send, then returns a null image

To share, what I'd like to do why I wanted to catch an errors is because I wanted to remove the registered telegram ID of a user if I get a request exception that contains "user deactivated" or "bot was blocked"

punyflash commented 2 years ago

Hello. In fact, in most cases you would not want any exception thrown by laravel's notification, but I do agree that there is no detection except log if notification was failed. I'll try to rework this, but for now you may create a custom channel that will serve your needs and send notification using it.

FallenKnight85 commented 2 years ago

maybe you could just fire an event like "MessageFailed" Event where users can listen to. On the event pass some contexts [$e, $notifiable]; so users who'd like to handle a FailedMessage event can listen and handle the event via Listeners.

punyflash commented 2 years ago

maybe you could just fire an event like "MessageFailed" Event where users can listen to. On the event pass some contexts [$e, $notifiable]; so users who'd like to handle a FailedMessage event can listen and handle the event via Listeners.

Yeah, that first I thought about too, but I'll take some time to see may be there some kind of way to deal with that in laravel already

punyflash commented 2 years ago

Updated in 2.0.1 release. Now instead of writing exceptions to the log, channel will emit events on sending, success and failure. If needed in 1.x version, gladly will accept pull request.

/**
 * Send Telegram Notification.
 *
 * @param  mixed        $notifiable
 * @param  Notification $notification
 * @return mixed
 */
public function send($notifiable, Notification $notification)
{
    $data = call_user_func([$notification, 'toTelegram'], $notifiable);
    $data = new TelegramNotification((string) $data);
    $data = $data->jsonSerialize();
    $bot = $this->botmanager->bot($data['bot'] ?? null);

    $this->dispatcher->dispatch(new NotificationSending($notifiable, $notification, static::class));

    $promises = [];
    $errors = [];

    foreach (($data['actions'] ?? []) as $action) {
        $promises[] = $bot->async()->exceptions()
            ->{$action['method']}($action['arguments'])
            ->otherwise(function (Exception $exception) use (&$errors) {
                $errors[] = $exception;
                return $exception;
            });
    }

    $results = Utils::unwrap($promises);
    $report = count($errors) ?
        new NotificationFailed($notifiable, $notification, static::class, $results) :
        new NotificationSent($notifiable, $notification, static::class, $results);

    $this->dispatcher->dispatch($report);
    return $results;
}