【问题标题】:how to catch any error in telegram bot?如何捕捉电报机器人中的任何错误?
【发布时间】:2017-09-15 05:43:58
【问题描述】:

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

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'],
        ]);
    }
}

【问题讨论】:

  • 你能给我们看看Minimal, Complete, and Verifiable example吗?
  • @Ohgod你为什么费心阅读我的问题甚至电报机器人 API?您链接了用于开发电报客户端应用程序的电报核心 API。

标签: php laravel try-catch telegram-bot


【解决方案1】:

最后我通过一个技巧解决了这个问题。我创建了另一个机器人来处理错误。 所以我有一个机器人 X 和一个错误处理机器人 Y。 这里是我从电报接收 webhook 的 POST 方法:

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 ,..
        }

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

【讨论】:

    猜你喜欢
    • 2018-08-31
    • 2015-12-12
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-18
    相关资源
    最近更新 更多