Skip to content

Commit

Permalink
Merge pull request #651 from andrey-helldar/patch/2024-10-03/00-12
Browse files Browse the repository at this point in the history
Fix detection of command parameter without passing it
  • Loading branch information
fabio-ivona authored Oct 3, 2024
2 parents 353f210 + b5d066a commit c92602b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/Handlers/WebhookHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,14 +370,18 @@ protected function onFailure(Throwable $throwable): void
*/
protected function parseCommand(Stringable $text): array
{
$command = (string) $text->before('@')->before(' ');
$parameter = (string) $text->after('@')->after(' ');
$command = $text->before('@')->before(' ');

$this->commandPrefixes()->each(function (string $value) use (&$command) {
$command = str($command)->after($value)->toString();
});
foreach ($this->commandPrefixes() as $prefix) {
if ($command->startsWith($prefix)) {
$parameter = $text->after($command)->after('@')->after(' ');
$command = $command->after($prefix);

return [$command, $parameter];
break;
}
}

return [(string) $command, (string) ($parameter ?? '')];
}

/**
Expand Down
9 changes: 9 additions & 0 deletions tests/Unit/Handlers/WebhookHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,15 @@
Facade::assertSent("Hello!! your parameter is [foo bot : :]");
});

it('can handle a command without parameter', function () {
$bot = bot();
Facade::fake();

app(TestWebhookHandler::class)->handle(webhook_command('/hello'), $bot);

Facade::assertSent("Hello!!");
});

it('cannot handle a command with custom start char', function () {
$bot = bot();
Facade::fake();
Expand Down

0 comments on commit c92602b

Please sign in to comment.