Skip to content

Commit

Permalink
Merge pull request #4 from MBoretto/cancel_command
Browse files Browse the repository at this point in the history
Cancel command
  • Loading branch information
MBoretto committed Feb 27, 2016
2 parents 408085f + 93152b3 commit b5e2203
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 15 deletions.
81 changes: 81 additions & 0 deletions src/Commands/UserCommands/CancelCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?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,
]);
}
}
42 changes: 27 additions & 15 deletions src/Conversation.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ protected function exist()
}

//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;
}

Expand All @@ -144,35 +144,47 @@ public function start()
}

/**
* 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;
}
}

Expand Down

0 comments on commit b5e2203

Please sign in to comment.