irazasyed/telegram-bot-sdk get update object on commands

Viewed 408

I was using irazasyed/telegram-bot-sdk for a Telegram bot in Laravel. I just wanted to access the update object on my StartCommand class and send a welcome message to a user with his name. The StartCommand class looks like this:

<?php

namespace App\TelegramCommands;

use Telegram\Bot\Commands\Command;

class StartCommand extends Command
{
    protected $name = "start";

    protected $description = "Lets you get started";

    public function handle()
    {
        $this->replyWithMessage(['text' => 'Welcome ']);
    }
}

And the route (which is inside api.php) is:

Route::post('/'.env('TELEGRAM_BOT_TOKEN').'/webhook', function (Request $request) {
    $update = Telegram::commandsHandler(true);
    return 'ok';
});

I just wanted to access users data when he sends /start command and replay with a message like, "Welcome [his name]". Thank you in advance.

1 Answers

I just have got the answer. It was mainly adding the following to the handle method of StartCommand class:

$update= $this->getUpdate();

So the final file will look like:

<?php

namespace App\TelegramCommands;

use Telegram\Bot\Commands\Command;

class StartCommand extends Command
{
    protected $name = "start";

    protected $description = "Lets you get started";

    public function handle()
    {
        $update= $this->getUpdate();
        $name = $update['message']['from']['first_name'];
        $this->replyWithMessage(['text' => 'Welcome '.$name]);
    }
}

A good tutorial is here

Related