Commit 02bdd0a4 authored by Armando Lüscher's avatar Armando Lüscher

Let the user know which conversation has been cancelled.

Add more documentation.
Refactor code and adjust output to user.
parent f41e8a52
......@@ -10,13 +10,17 @@
namespace Longman\TelegramBot\Commands\UserCommands;
use Longman\TelegramBot\Conversation;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Conversation;
use Longman\TelegramBot\Entities\ReplyKeyboardHide;
use Longman\TelegramBot\Request;
/**
* User "/cancel" command
*
* This command cancels the currently active conversation and
* returns a message to let the user know which conversation it was.
* If no conversation is active, the returned message says so.
*/
class CancelCommand extends UserCommand
{
......@@ -24,9 +28,10 @@ class CancelCommand extends UserCommand
* {@inheritdoc}
*/
protected $name = 'cancel';
protected $description = 'Exit from a conversation and hide keyboard';
protected $description = 'Cancel the currently active conversation';
protected $usage = '/cancel';
protected $version = '0.1.0';
protected $need_mysql = true;
/**#@-*/
/**
......@@ -34,19 +39,20 @@ class CancelCommand extends UserCommand
*/
public function execute()
{
$message = $this->getMessage();
$user_id = $message->getFrom()->getId();
$chat_id = $message->getChat()->getId();
$text = 'No active conversation!';
//Cancel current conversation if any
(new Conversation($user_id, $chat_id))->cancel();
$conversation = new Conversation(
$this->getMessage()->getFrom()->getId(),
$this->getMessage()->getChat()->getId()
);
$data = [
'reply_markup' => new ReplyKeyboardHide(['selective' => true]),
'chat_id' => $chat_id,
'text' => 'Conversation canceled!',
];
return Request::sendMessage($data);
if ($conversation_command = $conversation->getConversationCommand()) {
$conversation->cancel();
$text = 'Conversation "' . $conversation_command . '" cancelled!';
}
return $this->hideKeyboard($text);
}
/**
......@@ -54,15 +60,24 @@ class CancelCommand extends UserCommand
*/
public function executeNoDB()
{
//Database not setted or without connection
$message = $this->getMessage();
$chat_id = $message->getChat()->getId();
//Output the default message when there is no DB connection
parent::executeNoDB();
return $this->hideKeyboard();
}
$data = [
/**
* Hide the keyboard and output a text
*
* @param string $text
*
* @return Entities\ServerResponse
*/
private function hideKeyboard($text = '')
{
return Request::sendMessage([
'reply_markup' => new ReplyKeyboardHide(['selective' => true]),
'chat_id' => $chat_id,
'text' => 'Keyboard hidden!',
];
return Request::sendMessage($data);
'chat_id' => $this->getMessage()->getChat()->getId(),
'text' => $text,
]);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment