Is there any way to get command description of an active conversation?
Assume that I want when the user cancels the survey command using /cancel see a message like:
Conversation "Survery for bot users" cancelled!,
which the Survery for bot users is the description of Survey command.
Hi!
You can use that:
$this->getDescription();
And your CancelCommand will look like this:
...
public function execute()
{
$text = 'Nothing to cancel';
//Cancel current conversation if any
$conversation = new Conversation(
$this->getMessage()->getFrom()->getId(),
$this->getMessage()->getChat()->getId()
);
if ($conversation_command = $conversation->getCommand()) {
$conversation->cancel();
$text = 'Conversation ' . $this->getDescription() . ' cancelled!';
}
return $this->removeKeyboard($text);
}
...
Thank you @MyZik, but it seems that I wasn't clear enough about my problem.
The thing is that I want to get the description of the conversation which is going to be canceled.
I've updated the issue to clarify my point.
You mean the command description, which is indicated at the beginning of the file?
Yes
@ttvd94 so I answered your question. https://github.com/php-telegram-bot/core/issues/660#issuecomment-332750130
Use it wherever you need and u will get the command description from beginning of the file
$this->getDescription();
$this->getDescription(); does not work in my case as I expected. I'm gonna use it inside CancelCommand.php which controls cancel command.
When I go your way, I always get this:
Conversation "Cancel the currently active conversation" cancelled!
(since "$this" refers to CancelCommand class.)
What I need is something like $conversation->getDescription(); but there's no such method.
It would be ideal if the command member variables ($name, $description, $version, etc.) were static, as they never change. Then it would also be easier to access them.
But for now, you can do it like so:
if ($conversation_command = $conversation->getCommand()) {
$conversation->cancel();
$conversation_command_obj = $this->getTelegram()->getCommandObject($conversation_command);
$text = 'Conversation "' . $conversation_command_obj->getDescription() . '" cancelled!';
}
If they are static, you would also need to have a method to return the class with namespace from command, like getCommandClass()
Yes and no, as you'd have to have the class including namespace to actually access that method in the first place 馃槙
Neat would be something like:
$command = Command::getCommandObject('command');
Then simply:
$description = $command->getDescription();
Having the properties static would make sense for callback things, where no command object is created, but some static method inside the command itself makes sense for data retrieval etc.
Being able to access the command info statically would allow that, or the method above, by implementing the object retrieval directly in the Command class.
Dependency injection would need to be cleaned up then, allowing commands to be invoked without any Telegram or Update object. I haven't really thought this through, just popping out ideas.
What do you think of that?
We'd need a new issue for this though, as it's going a bit off topic 馃榿
I think that even more stuff should not be static, like Request taking telegram object into a static property, database connection and more.
However, command properties can be considered static, there could be some methods to get them without needing the object.
Like the one I suggested above - getCommandClass(), this could return the command class with namespace and getCommandObject() could use that to return the object.
Thanks @noplanman, your solution solved my problem.
By the way, happy to see discussions going on inspired by my question.