skip to Main Content

I’m trying to create telegram bot on Laravel with php-telegram-bot. I have installed webhook and now I get a message:

{"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
}
}

What does it mean? I know what is 301 error mean, but can’t understand what steps needed to solve this problem.

My controller:

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 routes:

Route::group(['prefix' => 'telegram'], function () {
    Route::get('hook/set', 'TelegramController@setWebhook');
    Route::any('hook','TelegramController@hook');
});

Some interesting: /api/telegram/hook correctly works only with ANY or GET option, POST method return empty page with vue, and i don’t know why.

2

Answers


  1. Create a middleware in app/Http/Middleware/PreflightResponse.php

    And paste this code:

    <?php
    namespace AppHttpMiddleware;
    use Closure;
    class PreflightResponse
    {
        public function handle($request, Closure $next )
        {
            if ($request->getMethod() === "OPTIONS") {
                return response('');
            }
    return $next($request);
         }
     }
    

    Then in app/Http/Kernel.php, add in the global variable $middleware:

    protected $middleware = [
        MiddlewarePreflightResponse::class,
    ];
    

    This is a general solution, but you can specify as a named middleware and using in your routes.

    That happens because of CORS and its security measures.
    I hope that could help 😀

    Login or Signup to reply.
  2. You probably have a problem in your webhook url, please check it again, And make sure that is correct and exists in your server.

    Even https://example.com/folder/ is different with https://example.com/folder and you have to use / at the end of the address (In my case, I gave that error because of /).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search