【问题标题】:Error handler telegram bot错误处理程序电报机器人
【发布时间】:2018-03-31 12:51:07
【问题描述】:

我用 laravel 和 sdk 创建了电报机器人 今天有一个问题 - 机器人正在递归地向用户发送相同的消息 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 添加到程序的另一部分或其他内容中?

【问题讨论】:

    标签: php laravel telegram-bot


    【解决方案1】:

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

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

    在你的代码部分你可以使用:

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

    【讨论】:

    • 你的意思是,我添加的正确的 try-catch(有问题)?
    • @sandy,你可以这样做,但更重要的是只返回 200 OK 响应。您可以在控制器的方法中添加return response() ->json(['success'=> true]) 或其他类似的东西
    猜你喜欢
    • 1970-01-01
    • 2015-12-12
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多