我正在尝试用php-telegram-bot在Laravel上创建电报机器人。我已经安装了webhook,现在我收到一条消息:
{"ok": true,
"result": {
"url": "https://.../api/telegram/hook/",
"has_custom_certificate": true,
"pending_update_count": 15,
"last_error_date": 1549043620,
"last_error_message": "Wrong response from the webhook: 301 Moved Permanently",
"max_connections": 40
}
}什么意思?我知道301错误是什么意思,但我不明白解决这个问题需要什么步骤。
我的控制器:
public function hook()
{
$commands_paths = [
app_path() . '/Services/Telegram/Bots/Bitbd_BOT/Commands',
];
try {
// Create Telegram API object
$telegram = new Telegram($this->bot_api_key, $this->bot_username);
TelegramLog::initErrorLog(storage_path() . "/{$this->bot_username}_error.log");
TelegramLog::initDebugLog(storage_path() . "/{$this->bot_username}_debug.log");
TelegramLog::initUpdateLog(storage_path() . "/{$this->bot_username}_update.log");
// Add commands paths containing your custom commands
$telegram->addCommandsPaths($commands_paths);
$telegram->enableLimiter();
// Handle telegram webhook request
$handle = $telegram->handle();
} catch (TelegramException $e) {
echo $e->getMessage();
}
}API路由:
Route::group(['prefix' => 'telegram'], function () {
Route::get('hook/set', 'TelegramController@setWebhook');
Route::any('hook','TelegramController@hook');
});有趣的是: /api/telegram/hook只适用于任何或GET选项,POST方法返回带有vue的空页,我不知道为什么。
发布于 2019-02-02 02:19:06
在app/Http/ middleware /PrefightResponse.php中创建中间件
并粘贴以下代码:
<?php
namespace App\Http\Middleware;
use Closure;
class PreflightResponse
{
public function handle($request, Closure $next )
{
if ($request->getMethod() === "OPTIONS") {
return response('');
}
return $next($request);
}
}然后在app/Http/Kernel.php中,添加全局变量$middleware:
protected $middleware = [
Middleware\PreflightResponse::class,
];这是一个通用的解决方案,但是您可以指定为命名中间件,并在您的路由中使用。
这种情况的发生是因为CORS及其安全措施。我希望这能有所帮助:D
发布于 2020-11-08 19:33:05
您的webhook url可能有问题,请再次检查,并确保它是正确的并且存在于您的服务器中。
即使https://example.com/folder/和https://example.com/folder也不同,你必须在地址的末尾使用/ (在我的例子中,我是因为/才给出这个错误的)。
https://stackoverflow.com/questions/54484909
复制相似问题