如何捕获电报机器人中的任何错误?

how to catch any error in telegram bot?

我想知道如何在电报机器人 API 中捕获任何可能的错误。 因为当发生错误时,电报会坚持下去并且不会回答其他请求。 如果我的代码中的错误或我正在使用的网络服务或阻止机器人或... 我怎样才能避免在电报机器人 API 和 PHP 中坚持一个请求? 我认为我需要的是类似波纹管的代码,但对于任何类型的错误都更通用:

try {

    $telegram->sendMessage([
        'chat_id'                  => '<PERSONS_ID>',
        'text'                     => 'Here is some text',
    ]);
} catch (TelegramResponseException $e) {
    $errorData = $e->getResponseData();

    if ($errorData['ok'] === false) {
        $telegram->sendMessage([
            'chat_id' => '<ADMINISTRATOR ID>',
            'text'    => 'There was an error for a user. ' . $errorData['error_code'] . ' ' . $errorData['description'],
        ]);
    }
}

最后我通过 trick.I 创建了另一个用于错误处理的机器人解决了这个问题。 所以我有一个机器人 X 和一个错误处理机器人 Y。 这里是 POST 方法,我从电报中接收 webhooks:

public function postWebhook(Request $request)

    { .....
        try
        { ....
         bot X token
         everything the bot want to do...
        }
        catch (\Exception $e) 
        {
             bot Y send me the probable problem in my code....
        }
        catch (Throwable $e)
        {
               bot Y send me the probable problem in telegram such 
               as blocking ,..
        }

现在我可以防止出现错误并且机器人运行良好。即使我的网络服务的一部分有问题或我的代码有错误,我也会收到通知。