错误处理程序电报机器人

Error handler telegram bot

我用 laravel 和 sdk Today there is a problem - the bot is recursively sending the same message to the user like this picture

创建了电报机器人
\Telegram::sendMessage([
                'chat_id' => $chatid,
                'text' => "Считаю...",
            ]);

            $controller = $this->getReportControllerName($text);
            $method = $this->getReportMethodName($text);
            $report_data = new $controller();
            $report_data = $report_data->$method($chatid);

            try
            {
                \Telegram::sendMessage([
                    'chat_id' => $chatid,
                    'text' => $report_data,
                ]);
            }
            catch (TelegramResponseException $e)
            {
                $errorData = $e->getResponseData();

                if ($errorData['ok'] === false) {
                    \Telegram::sendMessage([
                        'chat_id' => '123456789',
                        'text'    => 'There was an error for a user. ' . $errorData['error_code'] . ' ' . $errorData['description'],
                    ]);
                }
            }

Try-catch block检测问题后添加。在我的日志中有很多 499500 错误。我还能做些什么来解决这个问题?将 try-catch 添加到程序的另一部分或其他内容?

您需要在 webhook 上响应 200 OK,否则电报会一次又一次地尝试发送消息。

您需要捕获所有异常并对每个请求响应 200 OK。

在您的代码部分,您可以使用:

try
        {
            return \Telegram::sendMessage([
                'chat_id' => $chatid,
                'text' => $report_data,
            ]);
        }