irazasyed / telegram-bot-sdk

🤖 Telegram Bot API PHP SDK. Lets you build Telegram Bots easily! Supports Laravel out of the box.
https://telegram-bot-sdk.com
BSD 3-Clause "New" or "Revised" License
2.99k stars 660 forks source link

Call command outside #576

Closed Stajor closed 4 years ago

Stajor commented 6 years ago

How can I call command outside

composer.json

"irazasyed/telegram-bot-sdk": "^3.0"

Webhook controller

        $update = \Telegram\Bot\Laravel\Facades\Telegram::commandsHandler(true);

        if ($update->isType('callback_query')) {
...
            \Telegram\Bot\Laravel\Facades\Telegram::triggerCommand('language', $update);
        }

Stack trace:

#0 /vendor/irazasyed/telegram-bot-sdk/src/Commands/Command.php(307): Telegram\Bot\Commands\Command->allCommandOffsets()
#1 /vendor/irazasyed/telegram-bot-sdk/src/Commands/Command.php(229): Telegram\Bot\Commands\Command->relevantMessageSubString()
#2 /vendor/irazasyed/telegram-bot-sdk/src/Commands/Command.php(183): Telegram\Bot\Commands\Command->parseCommandArguments()
#3 /vendor/irazasyed/telegram-bot-sdk/src/Commands/CommandBus.php(232): Telegram\Bot\Commands\Command->make(Object(Telegram\Bot\Api), Object(Telegram\Bot\Objects\Update), Array)
#4 [internal function]: Telegram\Bot\Commands\CommandBus->execute('language', Object(Telegram\Bot\Objects\Update), Array)
#5 /vendor/irazasyed/telegram-bot-sdk/src/Answers/AnswerBus.php(27): call_user_func_array(Array, Array)
#6 /vendor/irazasyed/telegram-bot-sdk/src/Traits/CommandsHandler.php(117): Telegram\Bot\Answers\AnswerBus->__call('execute', Array)
#7 [internal function]: Telegram\Bot\Api->triggerCommand('language', Object(Telegram\Bot\Objects\Update))
#8 /vendor/irazasyed/telegram-bot-sdk/src/BotsManager.php(281): call_user_func_array(Array, Array)
#9 /vendor/illuminate/support/Facades/Facade.php(221): Telegram\Bot\BotsManager->__call('triggerCommand', Array)
#10 /app/Http/Controllers/WebhookController.php(16): Illuminate\Support\Facades\Facade::__callStatic('triggerCommand', Array)
#11 [internal function]: App\Http\Controllers\WebhookController->create(Object(Illuminate\Http\Request))
#12 /vendor/illuminate/container/BoundMethod.php(29): call_user_func_array(Array, Array)
#13 /vendor/illuminate/container/BoundMethod.php(87): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#14 /vendor/illuminate/container/BoundMethod.php(31): Illuminate\Container\BoundMethod::callBoundMethod(Object(Laravel\Lumen\Application), Array, Object(Closure))
#15 /vendor/illuminate/container/Container.php(564): Illuminate\Container\BoundMethod::call(Object(Laravel\Lumen\Application), Array, Array, NULL)
#16 /vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(373): Illuminate\Container\Container->call(Array, Array)
#17 /vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(339): Laravel\Lumen\Application->callControllerCallable(Array, Array)
#18 /vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(313): Laravel\Lumen\Application->callLumenController(Object(App\Http\Controllers\WebhookController), 'create', Array)
#19 /vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(275): Laravel\Lumen\Application->callControllerAction(Array)
#20 /vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(260): Laravel\Lumen\Application->callActionOnArrayBasedRoute(Array)
#21 /vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(160): Laravel\Lumen\Application->handleFoundRoute(Array)
#22 [internal function]: Laravel\Lumen\Application->Laravel\Lumen\Concerns\{closure}(Object(Illuminate\Http\Request))
#23 /vendor/laravel/lumen-framework/src/Routing/Pipeline.php(52): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#24 /app/Http/Middleware/CorsMiddleware.php(28): Laravel\Lumen\Routing\Pipeline->Laravel\Lumen\Routing\{closure}(Object(Illuminate\Http\Request))
#25 /vendor/illuminate/pipeline/Pipeline.php(151): App\Http\Middleware\CorsMiddleware->handle(Object(Illuminate\Http\Request), Object(Closure))
#26 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#27 /vendor/laravel/lumen-framework/src/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#28 /vendor/illuminate/pipeline/Pipeline.php(104): Laravel\Lumen\Routing\Pipeline->Laravel\Lumen\Routing\{closure}(Object(Illuminate\Http\Request))
#29 /vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(410): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#30 /vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(166): Laravel\Lumen\Application->sendThroughPipeline(Array, Object(Closure))
#31 /vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(107): Laravel\Lumen\Application->dispatch(NULL)
#32 /public/index.php(28): Laravel\Lumen\Application->run()
#33 {main} {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Call to a member function filter() on null at /vendor/irazasyed/telegram-bot-sdk/src/Commands/Command.php:341)
welcome[bot] commented 6 years ago

👋 Thanks for opening your first issue here! If you're reporting a 🐞 bug, please make sure you include steps to reproduce it. We get a lot of issues on this repo, so please be patient and we will get back to you as soon as we can.

To help make it easier for us to investigate your issue, please follow the contributing guidelines.

zeetabit commented 6 years ago

also have this problem, maybe it because all new methods tested with only getUpdates without webhook I'm rolled back to ^2.0 ver or "dev-master" is last

Rattone commented 5 years ago

UP @irazasyed why is this happening? can it be solved?

isrkar commented 5 years ago

It happens because CommandBus is trying to parse command in update->message->text using message entity bot_command. In callback query there is no entities, so it gives error. I resolved this problem very simple, but it is not best practice, just temporary fix.

In file scr/Commands/Command.php add if (!$this->update->isType('callback_query')) on line 183 for method make, like this:

public function make(Api $telegram, Update $update, array $entity)
    {
        $this->telegram = $telegram;
        $this->update = $update;
        $this->entity = $entity;
        if (!$this->update->isType('callback_query'))
            $this->arguments = $this->parseCommandArguments();

        return call_user_func_array([$this, 'handle'], $this->getArguments());
    }

and now you can call triggerCommand for callback queries, but without command arguments (i resolved it by sending callback data as json, i.e. command=start, parameters=[param1=val1...])

ghost commented 5 years ago

It happens because CommandBus is trying to parse command in update->message->text using message entity bot_command. In callback query there is no entities, so it gives error. I resolved this problem very simple, but it is not best practice, just temporary fix.

In file scr/Commands/Command.php add if (!$this->update->isType('callback_query')) on line 183 for method make, like this:

public function make(Api $telegram, Update $update, array $entity)
    {
        $this->telegram = $telegram;
        $this->update = $update;
        $this->entity = $entity;
        if (!$this->update->isType('callback_query'))
            $this->arguments = $this->parseCommandArguments();

        return call_user_func_array([$this, 'handle'], $this->getArguments());
    }

and now you can call triggerCommand for callback queries, but without command arguments (i resolved it by sending callback data as json, i.e. command=start, parameters=[param1=val1...])

That's exactly it! Thank you!

irazasyed commented 4 years ago

These kinda cases have been taken care in upcoming version which will be released in sometime.