Repository navigation
Description
🐞 Bug Report
Required Information
| ? | ! |
|---|---|
| Operating system | n/a |
| PHP Telegram Bot version | dev after 0.84.0 (4daf9a6) |
| PHP version | 8.5 |
| MySQL version | none |
| Update Method | n/a |
| Self-signed certificate | n/a |
| RAW update (if available) | n/a |
Summary
Since 0.80.0 (#1365) a class FooBarCommand is meant to answer /foo_bar. Which name actually reaches a command class still depends on two things that should not matter: the namespace of the class, and whether it was loaded from a commands path or added with addCommandClass().
Current behaviour
For a class loaded from a commands path:
getCommandsList()registers it underclassNameToCommandName()of the file name, soFooBarCommandisfoo_bar. The$nameproperty is not used./foobaralso reachesFooBarCommand, but only when the class is in a defaultLongman\TelegramBot\Commands\*Commandsnamespace: the fallback ingetCommandObject()builds...\UserCommands\FoobarCommand, and PHP class names are case-insensitive, soclass_exists()findsFooBarCommand.- In a custom namespace there is no such fallback, so a class
BazQuxCommandwithprotected $name = 'bazqux'answers only/baz_qux, and/bazquxfalls through toGenericCommand.
For a class added with addCommandClass(), the $name property is the command name, so the same BazQuxCommand answers /bazqux.
How to reproduce
Two command files, one per directory:
// default/FooBarCommand.php
namespace Longman\TelegramBot\Commands\UserCommands;
class FooBarCommand extends UserCommand { protected $name = 'foobar'; protected $usage = '/foobar'; /* execute() */ }
// custom/BazQuxCommand.php
namespace My\Bot;
class BazQuxCommand extends UserCommand { protected $name = 'bazqux'; protected $usage = '/bazqux'; /* execute() */ }$telegram->addCommandsPaths([__DIR__ . '/default', __DIR__ . '/custom']);
$list = $telegram->getCommandsList();
foreach (['foo_bar', 'foobar', 'baz_qux', 'bazqux'] as $c) {
$o = $list[$c] ?? $telegram->getCommandObject($c);
echo $c, ' -> ', $o ? get_class($o) : 'null', PHP_EOL;
}Output:
foo_bar -> Longman\TelegramBot\Commands\UserCommands\FooBarCommand
foobar -> Longman\TelegramBot\Commands\UserCommands\FooBarCommand
baz_qux -> My\Bot\BazQuxCommand
bazqux -> null
Expected behaviour
One rule for the command name, whatever the namespace and however the class was loaded. Possible directions:
- register path-loaded classes under
getName()when$nameis set, matchingaddCommandClass(), and fall back to the class name otherwise; - or make the class name the only source and stop the case-insensitive fallback from matching
/foobartoFooBarCommand, documenting$nameas display-only.
Either is a behaviour change for some existing bots, so it needs a changelog note. Related: #1312 (types resolving to command classes), #1364 and #1365 (underscore support), #1507 (CLAUDE.md wording).
Activity