Skip to content

Commit

Permalink
Automatic dependencies resolution for callback action method
Browse files Browse the repository at this point in the history
Co-authored-by: armivit
  • Loading branch information
fabio-ivona committed Jan 22, 2024
1 parent a929798 commit a9a722a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Handlers/WebhookHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use DefStudio\Telegraph\Models\TelegraphChat;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use Illuminate\Support\Stringable;
Expand Down Expand Up @@ -64,7 +65,8 @@ private function handleCallbackQuery(): void
return;
}

$this->$action();
/** @phpstan-ignore-next-line */
App::call([$this, $action], $this->data->toArray());
}

private function handleCommand(Stringable $text): void
Expand Down
5 changes: 5 additions & 0 deletions tests/Support/TestWebhookHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public function hello($parameter = null): void
$this->chat->html("Hello!!")->send();
}

public function param_injection(string $foo = 'not set'): void
{
$this->chat->html("Foo is [$foo]")->send();
}

public function reply_to_command(): void
{
$this->reply('foo');
Expand Down
17 changes: 17 additions & 0 deletions tests/Unit/Handlers/WebhookHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use DefStudio\Telegraph\Facades\Telegraph as Facade;
use DefStudio\Telegraph\Telegraph;
use DefStudio\Telegraph\Tests\Support\TestWebhookHandler;
use Illuminate\Support\Facades\Config;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

it('rejects unknown chat queries', function () {
Expand Down Expand Up @@ -63,6 +64,22 @@
expect(TestWebhookHandler::$calls_count)->toBe(1);
});

it('can handle a registered action with parameters', function () {
Config::set('telegraph.security.allow_callback_queries_from_unknown_chats', true);
Config::set('telegraph.security.allow_messages_from_unknown_chats', true);

$bot = make_bot();
Facade::fake();

app(TestWebhookHandler::class)->handle(webhook_request('param_injection'), $bot);

Facade::assertSent("Foo is [not set]");

app(TestWebhookHandler::class)->handle(webhook_request('param_injection;foo:bar'), $bot);

Facade::assertSent("Foo is [bar]");
});

it('rejects unregistered actions', function () {
Config::set('telegraph.security.allow_callback_queries_from_unknown_chats', true);
Config::set('telegraph.security.allow_messages_from_unknown_chats', true);
Expand Down

0 comments on commit a9a722a

Please sign in to comment.