Telegram-bot-sdk: Set webHook URL in Laravel

Created on 14 Mar 2017  路  3Comments  路  Source: irazasyed/telegram-bot-sdk

I just use this SDK in Laravel, It's not working when I set a route as a webHook URL.
For set a webHook I did this:

In Route/web.php:
Route::post('/hook', 'HookController@index');

And this is my index() method in HookController:

public function index()
    {
        try {
            $token = 'mytoken';

            $bot = new Api($token);

            $update = new Update($bot->getWebhookUpdates());

            // I get a chat id directly by getupdates before webhook set just for test...
            $bot->sendMessage(['chat_id' => 72457465, 'text' => 'hi']);

        } catch (Exception $e) {
            $bot->sendMessage(['chat_id' => 72457465, 'text' => 'hi']);
        }
    }

Edit:
And this is the result when I fire getWebhookInfo:

{
"ok": true,
"result": {
    "url": "https://myhost.com/hook",
    "has_custom_certificate": false,
    "pending_update_count": 8,
    "last_error_date": 1489482484,
    "last_error_message": "Wrong response from the webhook: 405 Method Not Allowed",
    "max_connections": 40
}
}

Am I wrong?

Most helpful comment

In Laravel, You don't need to do new Api and stuff. That's totally wrong.

Please follow these instructions:

use Telegram;

// Put this inside either the POST route '/<token>/webhook' closure (see below) or 
// whatever Controller is handling your POST route
$updates = Telegram::getWebhookUpdates();

// Example of POST Route:
Route::post('/<token>/webhook', function () {
    $updates = Telegram::getWebhookUpdates();

    return 'ok';
});

Then do this: https://telegram-bot-sdk.readme.io/docs/webhook-updates#section-laravel-important-

NOTE: <token> above is not your bot token but a random secret string just to protect your webhook. So you know only Telegram is contacting you.

All 3 comments

In Laravel, You don't need to do new Api and stuff. That's totally wrong.

Please follow these instructions:

use Telegram;

// Put this inside either the POST route '/<token>/webhook' closure (see below) or 
// whatever Controller is handling your POST route
$updates = Telegram::getWebhookUpdates();

// Example of POST Route:
Route::post('/<token>/webhook', function () {
    $updates = Telegram::getWebhookUpdates();

    return 'ok';
});

Then do this: https://telegram-bot-sdk.readme.io/docs/webhook-updates#section-laravel-important-

NOTE: <token> above is not your bot token but a random secret string just to protect your webhook. So you know only Telegram is contacting you.

i have the same problem and i do all step. this is my except

protected $except = [
        '/*/webhook',
        '*/webhook',
        'https://subdomin.mydomin.ir/*/webhook',
    ];

webhook url is https://subdomin.mydomin.ir/bottoken/webhook

but its not work. may its happen for subdomin?

LOL,LOL,LOL

Was this page helpful?
0 / 5 - 0 ratings

Related issues

wlinh picture wlinh  路  3Comments

behnamazimi picture behnamazimi  路  4Comments

iMohammadd picture iMohammadd  路  3Comments

b10585 picture b10585  路  4Comments

danilopinotti picture danilopinotti  路  3Comments