Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing support for underscores in bot command names when used with bot username set #578

Merged
merged 3 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Telegram/Types/Message/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ class Message extends BaseType
public function getParsedCommand(?string $username = null): ?string
{
$tag = $username !== null ? "(@$username)?" : '';
$pattern = sprintf("/^(?<name>\\/[a-z]+)%s(?<args> .+)?\$/i", $tag);
$pattern = sprintf("/^(?<name>\\/[a-z_]+)%s(?<args> .+)?\$/i", $tag);

if ($this->text !== null && preg_match($pattern, $this->text, $matches)) {
return $matches['name'].($matches['args'] ?? '');
Expand Down
4 changes: 4 additions & 0 deletions src/Testing/Asserts.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public function assertReply(string|array $method, ?array $expected = null, ?int

$reqRes = $this->testingHistory[$index];

if ($reqRes === null) {
PHPUnit::fail('No request found');
}

/** @var Request $request */
[$request,] = array_values($reqRes);

Expand Down
13 changes: 13 additions & 0 deletions tests/Feature/HandlerTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use GuzzleHttp\Psr7\Response;
use SergiX44\Nutgram\Configuration;
use SergiX44\Nutgram\Exception\StatusFinalizedException;
use SergiX44\Nutgram\Handlers\Type\Command;
use SergiX44\Nutgram\Nutgram;
Expand Down Expand Up @@ -963,3 +964,15 @@ public function handle(Nutgram $bot): void
],
]);
});

it('calls onCommand() handler with bot command with underscore', function () {
$bot = Nutgram::fake(config: new Configuration(botName: 'foo_bot'));

$bot->onCommand('my_test', function (Nutgram $bot) {
$bot->sendMessage('foo');
});

$bot->hearText('/my_test@foo_bot')
->reply()
->assertReplyText('foo');
});
2 changes: 1 addition & 1 deletion tests/Feature/Handlers/MessageHandlersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
})->with('command');

it('calls onCommand() handler with different tags', function ($update, $valid) {
$bot = Nutgram::fake($update, config: new Configuration(botName: 'foo'));
$bot = Nutgram::fake($update, config: new Configuration(botName: 'foo_bot'));
$bot->onCommand('test', function (Nutgram $bot) {
$bot->set('called', true);
});
Expand Down
4 changes: 2 additions & 2 deletions tests/Fixtures/Updates/command_tag_valid.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
"type": "private"
},
"date": 1632176928,
"text": "/test@foo",
"text": "/test@foo_bot",
"entities": [
{
"offset": 0,
"length": 9,
"length": 13,
"type": "bot_command"
}
]
Expand Down
Loading