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

Unable to define commands containing underscore #1364

Closed
mlocati opened this issue Oct 7, 2022 · 2 comments · Fixed by #1365
Closed

Unable to define commands containing underscore #1364

mlocati opened this issue Oct 7, 2022 · 2 comments · Fixed by #1365
Labels

Comments

@mlocati
Copy link
Contributor

mlocati commented Oct 7, 2022

🐞 Bug Report

Required Information

? !
Operating system n/a
PHP Telegram Bot version 0.79.0
PHP version 7.4.32
MySQL version none
Update Method Webhook
Self-signed certificate no
RAW update (if available) n/a

Summary

The specs says this:

Commands can use latin letters, numbers and underscores

But it's not possible to define a command that contains underscores.

For example, let's assume that we'd like to define the command /foo_bar.

If we define it in a file named FooBarCommand.php, at this line we have that $command has a value of Foobar, and $command_name will be foobar.

If we define it in a file named Foo_BarCommand.php, at this line we have that $command has a value of FooBar, and $command_name will still be foobar.

Current behaviour

It's impossible to define a command containing underscores.

How to reproduce

Create a command like this one

<?php

class FooBarCommand extends UserCommand
{
    protected $name = 'foo_bar';
}

It won't be executed with /foo_bar.

Expected behaviour

Invoking /foo_bar should execute a command defined in a file named FooBarCommand.

@mlocati mlocati added the bug label Oct 7, 2022
@jacklul
Copy link
Collaborator

jacklul commented Oct 7, 2022

You can use GenericCommand to create "aliases" or commands with special characters.

Something like this:

<?php
class GenericCommand extends SystemCommand
{
    private static array $aliases = [
        'foo_bar' => 'foobar',
    ];

    public function execute(): ServerResponse
    {
        $command = $this->getUpdate()->getMessage()->getCommand();

        if (isset(self::$aliases[strtolower($command)])) {
            $target_command = self::$aliases[strtolower($command)];
            
            if ($this->getTelegram()->getCommandObject($target_command) !== null) {
                return $this->getTelegram()->executeCommand($target_command);
            }
        }

        return Request::emptyResponse();
    }
}

@mlocati
Copy link
Contributor Author

mlocati commented Oct 7, 2022

@jacklul Thanks! That's a really nice approach!

BTW, I think it should be easier for developers to add commands containing underscores: see #1365

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants