Commit b5e22037 authored by Marco Boretto's avatar Marco Boretto

Merge pull request #4 from MBoretto/cancel_command

Cancel command
parents 408085f5 93152b3b
<?php
/**
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Commands\UserCommands;
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
{
/**#@+
* {@inheritdoc}
*/
protected $name = 'cancel';
protected $description = 'Cancel the currently active conversation';
protected $usage = '/cancel';
protected $version = '0.1.0';
protected $need_mysql = true;
/**#@-*/
/**
* {@inheritdoc}
*/
public function execute()
{
$text = 'No active conversation!';
//Cancel current conversation if any
$conversation = new Conversation(
$this->getMessage()->getFrom()->getId(),
$this->getMessage()->getChat()->getId()
);
if ($conversation_command = $conversation->getConversationCommand()) {
$conversation->cancel();
$text = 'Conversation "' . $conversation_command . '" cancelled!';
}
return $this->hideKeyboard($text);
}
/**
* {@inheritdoc}
*/
public function executeNoDB()
{
return $this->hideKeyboard();
}
/**
* 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' => $this->getMessage()->getChat()->getId(),
'text' => $text,
]);
}
}
......@@ -121,7 +121,7 @@ class Conversation
}
//A conversation with a different name has been opened, unset the DB one and recreate a new one
ConversationDB::updateConversation(['status' => 'cancelled'], ['chat_id' => $this->chat_id, 'user_id' => $this->user_id, 'status' => 'active']);
$this->cancel();
return false;
}
......@@ -144,35 +144,47 @@ class Conversation
}
/**
* Store the array/variable in the database with json_encode() function
* Delete the conversation from the database
*
* @todo Verify the query before assigning the $data member variable
* Currently the Conversation is not deleted but just set to 'stopped'
*
* @param array $data
* @todo should return something
*/
public function update($data)
public function stop()
{
//Conversation must exist!
if ($this->exist()) {
$fields['data'] = json_encode($data);
ConversationDB::updateConversation(['status' => 'stopped'], ['chat_id' => $this->chat_id, 'user_id' => $this->user_id, 'status' => 'active']);
}
}
ConversationDB::updateConversation($fields, ['chat_id' => $this->chat_id, 'user_id' => $this->user_id, 'status' => 'active']);
//TODO verify query success before convert the private var
$this->data = $data;
/**
* Set to Cancelled the conversation in the database
*
* @todo should return something
*/
public function cancel()
{
if ($this->exist()) {
ConversationDB::updateConversation(['status' => 'cancelled'], ['chat_id' => $this->chat_id, 'user_id' => $this->user_id, 'status' => 'active']);
}
}
/**
* Delete the conversation from the database
* Store the array/variable in the database with json_encode() function
*
* Currently the Conversation is not deleted but just set to 'stopped'
* @todo Verify the query before assigning the $data member variable
*
* @todo should return something
* @param array $data
*/
public function stop()
public function update($data)
{
//Conversation must exist!
if ($this->exist()) {
ConversationDB::updateConversation(['status' => 'stopped'], ['chat_id' => $this->chat_id, 'user_id' => $this->user_id, 'status' => 'active']);
$fields['data'] = json_encode($data);
ConversationDB::updateConversation($fields, ['chat_id' => $this->chat_id, 'user_id' => $this->user_id, 'status' => 'active']);
//TODO verify query success before convert the private var
$this->data = $data;
}
}
......
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