Longman\TelegramBot\Entities\Message::getCommand PHP Method

getCommand() public method

Get command
public getCommand ( ) : boolean | string
return boolean | string
    public function getCommand()
    {
        $command = $this->getProperty('command');
        if (!empty($command)) {
            return $command;
        }
        $cmd = $this->getFullCommand();
        if (strpos($cmd, '/') === 0) {
            $cmd = substr($cmd, 1);
            //check if command is follow by botname
            $split_cmd = explode('@', $cmd);
            if (isset($split_cmd[1])) {
                //command is followed by name check if is addressed to me
                if (strtolower($split_cmd[1]) === strtolower($this->bot_name)) {
                    return $split_cmd[0];
                }
            } else {
                //command is not followed by name
                return $cmd;
            }
        }
        return false;
    }

Usage Example

 public function testTextAndCommandRecognise()
 {
     // /command
     $this->message = TestHelpers::getFakeMessageObject(['text' => '/help']);
     $this->assertEquals('/help', $this->message->getFullCommand());
     $this->assertEquals('help', $this->message->getCommand());
     $this->assertEquals('/help', $this->message->getText());
     $this->assertEquals('', $this->message->getText(true));
     // text
     $this->message = TestHelpers::getFakeMessageObject(['text' => 'some text']);
     $this->assertEquals('', $this->message->getFullCommand());
     $this->assertEquals('', $this->message->getCommand());
     $this->assertEquals('some text', $this->message->getText());
     $this->assertEquals('some text', $this->message->getText(true));
     // /command@bot
     $this->message = TestHelpers::getFakeMessageObject(['text' => '/help@testbot']);
     $this->assertEquals('/help@testbot', $this->message->getFullCommand());
     $this->assertEquals('help', $this->message->getCommand());
     $this->assertEquals('/help@testbot', $this->message->getText());
     $this->assertEquals('', $this->message->getText(true));
     // /commmad text
     $this->message = TestHelpers::getFakeMessageObject(['text' => '/help some text']);
     $this->assertEquals('/help', $this->message->getFullCommand());
     $this->assertEquals('help', $this->message->getCommand());
     $this->assertEquals('/help some text', $this->message->getText());
     $this->assertEquals('some text', $this->message->getText(true));
     // /command@bot some text
     $this->message = TestHelpers::getFakeMessageObject(['text' => '/help@testbot some text']);
     $this->assertEquals('/help@testbot', $this->message->getFullCommand());
     $this->assertEquals('help', $this->message->getCommand());
     $this->assertEquals('/help@testbot some text', $this->message->getText());
     $this->assertEquals('some text', $this->message->getText(true));
     // /commmad\n text
     $this->message = TestHelpers::getFakeMessageObject(['text' => "/help\n some text"]);
     $this->assertEquals('/help', $this->message->getFullCommand());
     $this->assertEquals('help', $this->message->getCommand());
     $this->assertEquals("/help\n some text", $this->message->getText());
     $this->assertEquals(' some text', $this->message->getText(true));
     // /command@bot\nsome text
     $this->message = TestHelpers::getFakeMessageObject(['text' => "/help@testbot\nsome text"]);
     $this->assertEquals('/help@testbot', $this->message->getFullCommand());
     $this->assertEquals('help', $this->message->getCommand());
     $this->assertEquals("/help@testbot\nsome text", $this->message->getText());
     $this->assertEquals('some text', $this->message->getText(true));
     // /command@bot \nsome text
     $this->message = TestHelpers::getFakeMessageObject(['text' => "/help@testbot \nsome text"]);
     $this->assertEquals('/help@testbot', $this->message->getFullCommand());
     $this->assertEquals('help', $this->message->getCommand());
     $this->assertEquals("/help@testbot \nsome text", $this->message->getText());
     $this->assertEquals("\nsome text", $this->message->getText(true));
 }