Commit 7238e75f authored by MBoretto's avatar MBoretto

Merge branch 'noplan_conversation' into feature/smartinterface

parents 6dbab388 8c359b2a
...@@ -57,21 +57,19 @@ class SendtochannelCommand extends AdminCommand ...@@ -57,21 +57,19 @@ class SendtochannelCommand extends AdminCommand
// Conversation // Conversation
$this->conversation = new Conversation($user_id, $chat_id, $this->getName()); $this->conversation = new Conversation($user_id, $chat_id, $this->getName());
$session = $this->conversation->getData();
$channels = (array) $this->getConfig('your_channel'); $channels = (array) $this->getConfig('your_channel');
if (!isset($session['state'])) { if (!isset($this->conversation->notes['state'])) {
$state = (count($channels) == 0) ? -1 : 0; $state = (count($channels) == 0) ? -1 : 0;
$session['last_message_id'] = $message->getMessageId(); $this->conversation->notes['last_message_id'] = $message->getMessageId();
} else { } else {
$state = $session['state']; $state = $this->conversation->notes['state'];
} }
switch ($state) { switch ($state) {
case -1: case -1:
// getConfig has not been configured asking for channel to administer // getConfig has not been configured asking for channel to administer
if ($type != 'Message' || empty($text)) { if ($type != 'Message' || empty($text)) {
$session['state'] = -1; $this->conversation->notes['state'] = -1;
$this->conversation->update($session); //$this->conversation->update();
$data['text'] = 'Insert the channel name: (@yourchannel)'; $data['text'] = 'Insert the channel name: (@yourchannel)';
$data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]); $data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]);
...@@ -79,8 +77,8 @@ class SendtochannelCommand extends AdminCommand ...@@ -79,8 +77,8 @@ class SendtochannelCommand extends AdminCommand
break; break;
} }
$session['channel'] = $text; $this->conversation->notes['channel'] = $text;
$session['last_message_id'] = $message->getMessageId(); $this->conversation->notes['last_message_id'] = $message->getMessageId();
// Jump to state 1 // Jump to state 1
goto insert; goto insert;
...@@ -89,8 +87,8 @@ class SendtochannelCommand extends AdminCommand ...@@ -89,8 +87,8 @@ class SendtochannelCommand extends AdminCommand
case 0: case 0:
// getConfig has been configured choose channel // getConfig has been configured choose channel
if ($type != 'Message' || !in_array($text, $channels)) { if ($type != 'Message' || !in_array($text, $channels)) {
$session['state'] = 0; $this->conversation->notes['state'] = 0;
$this->conversation->update($session); //$this->conversation->update();
$keyboard = []; $keyboard = [];
foreach ($channels as $channel) { foreach ($channels as $channel) {
...@@ -112,33 +110,33 @@ class SendtochannelCommand extends AdminCommand ...@@ -112,33 +110,33 @@ class SendtochannelCommand extends AdminCommand
$result = Request::sendMessage($data); $result = Request::sendMessage($data);
break; break;
} }
$session['channel'] = $text; $this->conversation->notes['channel'] = $text;
$session['last_message_id'] = $message->getMessageId(); $this->conversation->notes['last_message_id'] = $message->getMessageId();
// no break // no break
case 1: case 1:
insert: insert:
if ($session['last_message_id'] == $message->getMessageId() || ($type == 'Message' && empty($text))) { if ($this->conversation->notes['last_message_id'] == $message->getMessageId() || ($type == 'Message' && empty($text))) {
$session['state'] = 1; $this->conversation->notes['state'] = 1;
$this->conversation->update($session); //$this->conversation->update();
$data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]); $data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]);
$data['text'] = 'Insert the content you want to share: text, photo, audio...'; $data['text'] = 'Insert the content you want to share: text, photo, audio...';
$result = Request::sendMessage($data); $result = Request::sendMessage($data);
break; break;
} }
$session['last_message_id'] = $message->getMessageId(); $this->conversation->notes['last_message_id'] = $message->getMessageId();
$session['message'] = $message->reflect(); $this->conversation->notes['message'] = $message->reflect();
$session['message_type'] = $type; $this->conversation->notes['message_type'] = $type;
// no break // no break
case 2: case 2:
if ($session['last_message_id'] == $message->getMessageId() || !($text == 'Yes' || $text == 'No')) { if ($this->conversation->notes['last_message_id'] == $message->getMessageId() || !($text == 'Yes' || $text == 'No')) {
$session['state'] = 2; $this->conversation->notes['state'] = 2;
$this->conversation->update($session); //$this->conversation->update();
// Execute this just with object that allow caption // Execute this just with object that allow caption
if ($session['message_type'] == 'Video' || $session['message_type'] == 'Photo') { if ($this->conversation->notes['message_type'] == 'Video' || $this->conversation->notes['message_type'] == 'Photo') {
$keyboard = [['Yes', 'No']]; $keyboard = [['Yes', 'No']];
$reply_keyboard_markup = new ReplyKeyboardMarkup( $reply_keyboard_markup = new ReplyKeyboardMarkup(
[ [
...@@ -151,48 +149,48 @@ class SendtochannelCommand extends AdminCommand ...@@ -151,48 +149,48 @@ class SendtochannelCommand extends AdminCommand
$data['reply_markup'] = $reply_keyboard_markup; $data['reply_markup'] = $reply_keyboard_markup;
$data['text'] = 'Would you insert caption?'; $data['text'] = 'Would you insert caption?';
if ($session['last_message_id'] != $message->getMessageId() && !($text == 'Yes' || $text == 'No')) { if ($this->conversation->notes['last_message_id'] != $message->getMessageId() && !($text == 'Yes' || $text == 'No')) {
$data['text'] = 'Would you insert a caption?' . "\n" . 'Type Yes or No'; $data['text'] = 'Would you insert a caption?' . "\n" . 'Type Yes or No';
} }
$result = Request::sendMessage($data); $result = Request::sendMessage($data);
break; break;
} }
} }
$session['set_caption'] = false; $this->conversation->notes['set_caption'] = false;
if ($text == 'Yes') { if ($text == 'Yes') {
$session['set_caption'] = true; $this->conversation->notes['set_caption'] = true;
} }
$session['last_message_id'] = $message->getMessageId(); $this->conversation->notes['last_message_id'] = $message->getMessageId();
// no break // no break
case 3: case 3:
if (($session['last_message_id'] == $message->getMessageId() || $type != 'Message' ) && $session['set_caption']) { if (($this->conversation->notes['last_message_id'] == $message->getMessageId() || $type != 'Message' ) && $this->conversation->notes['set_caption']) {
$session['state'] = 3; $this->conversation->notes['state'] = 3;
$this->conversation->update($session); //$this->conversation->update();
$data['text'] = 'Insert caption:'; $data['text'] = 'Insert caption:';
$data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]); $data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]);
$result = Request::sendMessage($data); $result = Request::sendMessage($data);
break; break;
} }
$session['last_message_id'] = $message->getMessageId(); $this->conversation->notes['last_message_id'] = $message->getMessageId();
$session['caption'] = $text; $this->conversation->notes['caption'] = $text;
// no break // no break
case 4: case 4:
if ($session['last_message_id'] == $message->getMessageId() || !($text == 'Yes' || $text == 'No')) { if ($this->conversation->notes['last_message_id'] == $message->getMessageId() || !($text == 'Yes' || $text == 'No')) {
$session['state'] = 4; $this->conversation->notes['state'] = 4;
$this->conversation->update($session); //$this->conversation->update();
$data['text'] = 'Message will look like this:'; $data['text'] = 'Message will look like this:';
$result = Request::sendMessage($data); $result = Request::sendMessage($data);
if ($session['message_type'] != 'command') { if ($this->conversation->notes['message_type'] != 'command') {
if ($session['set_caption']) { if ($this->conversation->notes['set_caption']) {
$data['caption'] = $session['caption']; $data['caption'] = $this->conversation->notes['caption'];
} }
$result = $this->sendBack(new Message($session['message'], 'thisbot'), $data); $result = $this->sendBack(new Message($this->conversation->notes['message'], 'thisbot'), $data);
$data['text'] = 'Would you post it?'; $data['text'] = 'Would you post it?';
if ($session['last_message_id'] != $message->getMessageId() && !($text == 'Yes' || $text == 'No')) { if ($this->conversation->notes['last_message_id'] != $message->getMessageId() && !($text == 'Yes' || $text == 'No')) {
$data['text'] = 'Would you post it?' . "\n" . 'Press Yes or No'; $data['text'] = 'Would you post it?' . "\n" . 'Press Yes or No';
} }
$keyboard = [['Yes', 'No']]; $keyboard = [['Yes', 'No']];
...@@ -210,18 +208,18 @@ class SendtochannelCommand extends AdminCommand ...@@ -210,18 +208,18 @@ class SendtochannelCommand extends AdminCommand
break; break;
} }
$session['post_message'] = false; $this->conversation->notes['post_message'] = false;
if ($text == 'Yes') { if ($text == 'Yes') {
$session['post_message'] = true; $this->conversation->notes['post_message'] = true;
} }
$session['last_message_id'] = $message->getMessageId(); $this->conversation->notes['last_message_id'] = $message->getMessageId();
// no break // no break
case 5: case 5:
$this->conversation->stop(); $this->conversation->stop();
$data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]); $data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]);
if ($session['post_message']) { if ($this->conversation->notes['post_message']) {
$data['text'] = $this->publish(new Message($session['message'], 'anystring'), $session['channel'], $session['caption']); $data['text'] = $this->publish(new Message($this->conversation->notes['message'], 'anystring'), $this->conversation->notes['channel'], $this->conversation->notes['caption']);
$result = Request::sendMessage($data); $result = Request::sendMessage($data);
break; break;
} }
......
...@@ -31,12 +31,18 @@ class SurveyCommand extends UserCommand ...@@ -31,12 +31,18 @@ class SurveyCommand extends UserCommand
protected $need_mysql = true; protected $need_mysql = true;
/**#@-*/ /**#@-*/
/**
* Conversation Object
*
* @var Longman\TelegramBot\Conversation
*/
protected $conversation;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function execute() public function execute()
{ {
$update = $this->getUpdate();
$message = $this->getMessage(); $message = $this->getMessage();
$chat = $message->getChat(); $chat = $message->getChat();
...@@ -56,15 +62,14 @@ class SurveyCommand extends UserCommand ...@@ -56,15 +62,14 @@ class SurveyCommand extends UserCommand
} }
$data['chat_id'] = $chat_id; $data['chat_id'] = $chat_id;
//tracking //Conversation start
$conversation = new Conversation($user_id, $chat_id, $this->getName()); $this->conversation = new Conversation($user_id, $chat_id, $this->getName());
//cache data from the tracking session if any //cache data from the tracking session if any
$session = $conversation->getData(); if (!isset($this->conversation->notes['state'])) {
if (!isset($session['state'])) {
$state = '0'; $state = '0';
} else { } else {
$state = $session['state']; $state = $this->conversation->notes['state'];
} }
//state machine //state machine
...@@ -73,35 +78,33 @@ class SurveyCommand extends UserCommand ...@@ -73,35 +78,33 @@ class SurveyCommand extends UserCommand
switch ($state) { switch ($state) {
case 0: case 0:
if (empty($text)) { if (empty($text)) {
$session['state'] = '0'; $this->conversation->notes['state'] = 0;
$conversation->update($session);
$data['text'] = 'Type your name:'; $data['text'] = 'Type your name:';
$data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]); $data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]);
$result = Request::sendMessage($data); $result = Request::sendMessage($data);
break; break;
} }
$session['name'] = $text; $this->conversation->notes['name'] = $text;
$text = ''; $text = '';
// no break // no break
case 1: case 1:
if (empty($text)) { if (empty($text)) {
$session['state'] = 1; $this->conversation->notes['state'] = 1;
$conversation->update($session);
$data['text'] = 'Type your surname:'; $data['text'] = 'Type your surname:';
$result = Request::sendMessage($data); $result = Request::sendMessage($data);
break; break;
} }
$session['surname'] = $text; $this->conversation->notes['surname'] = $text;
++$state; ++$state;
$text = ''; $text = '';
// no break // no break
case 2: case 2:
if (empty($text) || !is_numeric($text)) { if (empty($text) || !is_numeric($text)) {
$session['state'] = '2'; $this->conversation->notes['state'] = 2;
$conversation->update($session);
$data['text'] = 'Type your age:'; $data['text'] = 'Type your age:';
if (!empty($text) && !is_numeric($text)) { if (!empty($text) && !is_numeric($text)) {
$data['text'] = 'Type your age, must be a number'; $data['text'] = 'Type your age, must be a number';
...@@ -109,14 +112,13 @@ class SurveyCommand extends UserCommand ...@@ -109,14 +112,13 @@ class SurveyCommand extends UserCommand
$result = Request::sendMessage($data); $result = Request::sendMessage($data);
break; break;
} }
$session['age'] = $text; $this->conversation->notes['age'] = $text;
$text = ''; $text = '';
// no break // no break
case 3: case 3:
if (empty($text) || !($text == 'M' || $text == 'F')) { if (empty($text) || !($text == 'M' || $text == 'F')) {
$session['state'] = '3'; $this->conversation->notes['state'] = 3;
$conversation->update($session);
$keyboard = [['M','F']]; $keyboard = [['M','F']];
$reply_keyboard_markup = new ReplyKeyboardMarkup( $reply_keyboard_markup = new ReplyKeyboardMarkup(
...@@ -135,14 +137,13 @@ class SurveyCommand extends UserCommand ...@@ -135,14 +137,13 @@ class SurveyCommand extends UserCommand
$result = Request::sendMessage($data); $result = Request::sendMessage($data);
break; break;
} }
$session['gender'] = $text; $this->conversation->notes['gender'] = $text;
$text = ''; $text = '';
// no break // no break
case 4: case 4:
if (is_null($message->getLocation())) { if (is_null($message->getLocation())) {
$session['state'] = '4'; $this->conversation->notes['state'] = 4;
$conversation->update($session);
$data['text'] = 'Insert your home location (need location object):'; $data['text'] = 'Insert your home location (need location object):';
$data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]); $data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]);
...@@ -150,33 +151,32 @@ class SurveyCommand extends UserCommand ...@@ -150,33 +151,32 @@ class SurveyCommand extends UserCommand
break; break;
} }
$session['longitude'] = $message->getLocation()->getLongitude(); $this->conversation->notes['longitude'] = $message->getLocation()->getLongitude();
$session['latitude'] = $message->getLocation()->getLatitude(); $this->conversation->notes['latitude'] = $message->getLocation()->getLatitude();
// no break // no break
case 5: case 5:
if (is_null($message->getPhoto())) { if (is_null($message->getPhoto())) {
$session['state'] = '5'; $this->conversation->notes['state'] = 5;
$conversation->update($session);
$data['text'] = 'Insert your picture:'; $data['text'] = 'Insert your picture:';
$result = Request::sendMessage($data); $result = Request::sendMessage($data);
break; break;
} }
$session['photo_id'] = $message->getPhoto()[0]->getFileId(); $this->conversation->notes['photo_id'] = $message->getPhoto()[0]->getFileId();
// no break // no break
case 6: case 6:
$conversation->stop();
$out_text = '/Survey result:' . "\n"; $out_text = '/Survey result:' . "\n";
unset($session['state']); unset($this->conversation->notes['state']);
foreach ($session as $k => $v) { foreach ($this->conversation->notes as $k => $v) {
$out_text .= "\n" . ucfirst($k).': ' . $v; $out_text .= "\n" . ucfirst($k).': ' . $v;
} }
$data['photo'] = $session['photo_id']; $data['photo'] = $this->conversation->notes['photo_id'];
$data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]); $data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]);
$data['caption'] = $out_text; $data['caption'] = $out_text;
$this->conversation->stop();
$result = Request::sendPhoto($data); $result = Request::sendPhoto($data);
break; break;
} }
......
...@@ -26,11 +26,18 @@ class Conversation ...@@ -26,11 +26,18 @@ class Conversation
protected $conversation = null; protected $conversation = null;
/** /**
* Data stored inside the conversation * Notes stored inside the conversation
* *
* @var array * @var array
*/ */
protected $data = null; protected $protected_notes = null;
/**
* Notes to be stored
*
* @var array
*/
public $notes = null;
/** /**
* Telegram user id * Telegram user id
...@@ -53,6 +60,13 @@ class Conversation ...@@ -53,6 +60,13 @@ class Conversation
*/ */
protected $command; protected $command;
/**
* Command has been provided
*
* @var string
*/
protected $command_is_provided;
/** /**
* Conversation contructor to initialize a new conversation * Conversation contructor to initialize a new conversation
* *
...@@ -66,8 +80,24 @@ class Conversation ...@@ -66,8 +80,24 @@ class Conversation
$this->chat_id = $chat_id; $this->chat_id = $chat_id;
$this->command = $command; $this->command = $command;
$this->command_is_provided = (is_null($command)) ? false : true;
//Try to load an existing conversation if possible //Try to load an existing conversation if possible
$this->load(); if (!$this->load() && !is_null($command)) {
//A new conversation start
$this->start();
}
}
/**
* Conversation destructor update the conversation if not stopped
*/
public function __destruct()
{
//Perform the update when the object go out of the stage and notes has changed after load()
if ($this->command_is_provided && $this->notes !== $this->protected_notes) {
$this->update();
}
} }
/** /**
...@@ -78,7 +108,8 @@ class Conversation ...@@ -78,7 +108,8 @@ class Conversation
protected function load() protected function load()
{ {
$this->conversation = null; $this->conversation = null;
$this->data = null; $this->protected_notes = null;
$this->notes = null;
//Select an active conversation //Select an active conversation
$conversation = ConversationDB::selectConversation($this->user_id, $this->chat_id, 1); $conversation = ConversationDB::selectConversation($this->user_id, $this->chat_id, 1);
...@@ -91,11 +122,13 @@ class Conversation ...@@ -91,11 +122,13 @@ class Conversation
if ($this->command !== $this->conversation['command']) { if ($this->command !== $this->conversation['command']) {
$this->cancel(); $this->cancel();
$this->conversation = null;
return false; return false;
} }
//Load the conversation data //Load the conversation notes
$this->data = json_decode($this->conversation['data'], true); $this->protected_notes = json_decode($this->conversation['notes'], true);
$this->notes = $this->protected_notes;
} }
return $this->exists(); return $this->exists();
...@@ -165,13 +198,12 @@ class Conversation ...@@ -165,13 +198,12 @@ class Conversation
if ($this->exists()) { if ($this->exists()) {
$fields = ['status' => $status]; $fields = ['status' => $status];
$where = [ $where = [
'id' => $this->conversation['id'],
'status' => 'active', 'status' => 'active',
'user_id' => $this->user_id, 'user_id' => $this->user_id,
'chat_id' => $this->chat_id, 'chat_id' => $this->chat_id,
]; ];
if (ConversationDB::updateConversation($fields, $where)) { if (ConversationDB::updateConversation($fields, $where)) {
//Reload the data
$this->load();
return true; return true;
} }
} }
...@@ -182,26 +214,17 @@ class Conversation ...@@ -182,26 +214,17 @@ class Conversation
/** /**
* Store the array/variable in the database with json_encode() function * Store the array/variable in the database with json_encode() function
* *
* @param array $data
*
* @return bool * @return bool
*/ */
public function update($data) public function update()
{ {
if ($this->exists()) { if ($this->exists()) {
$fields = ['data' => json_encode($data)]; $fields = ['notes' => json_encode($this->notes)];
$where = [ //I can update a conversation whatever the state is
'status' => 'active', $where = ['id' => $this->conversation['id']];
'user_id' => $this->user_id,
'chat_id' => $this->chat_id,
];
if (ConversationDB::updateConversation($fields, $where)) { if (ConversationDB::updateConversation($fields, $where)) {
//Reload the data
$this->load();
return true; return true;
} }
} elseif ($this->start()) {
return $this->update($data);
} }
return false; return false;
...@@ -216,14 +239,4 @@ class Conversation ...@@ -216,14 +239,4 @@ class Conversation
{ {
return $this->command; return $this->command;
} }
/**
* Retrieve the data stored in the conversation
*
* @return array|null
*/
public function getData()
{
return $this->data;
}
} }
...@@ -87,22 +87,22 @@ class ConversationDB extends DB ...@@ -87,22 +87,22 @@ class ConversationDB extends DB
try { try {
$sth = self::$pdo->prepare('INSERT INTO `' . TB_CONVERSATION . '` $sth = self::$pdo->prepare('INSERT INTO `' . TB_CONVERSATION . '`
( (
`status`, `user_id`, `chat_id`, `command`, `data`, `created_at`, `updated_at` `status`, `user_id`, `chat_id`, `command`, `notes`, `created_at`, `updated_at`
) )
VALUES ( VALUES (
:status, :user_id, :chat_id, :command, :data, :date, :date :status, :user_id, :chat_id, :command, :notes, :date, :date
) )
'); ');
$active = 'active'; $active = 'active';
//$data = json_encode(''); //$notes = json_encode('');
$data = '""'; $notes = '""';
$created_at = self::getTimestamp(); $created_at = self::getTimestamp();
$sth->bindParam(':status', $active); $sth->bindParam(':status', $active);
$sth->bindParam(':command', $command); $sth->bindParam(':command', $command);
$sth->bindParam(':user_id', $user_id); $sth->bindParam(':user_id', $user_id);
$sth->bindParam(':chat_id', $chat_id); $sth->bindParam(':chat_id', $chat_id);
$sth->bindParam(':data', $data); $sth->bindParam(':notes', $notes);
$sth->bindParam(':date', $created_at); $sth->bindParam(':date', $created_at);
$status = $sth->execute(); $status = $sth->execute();
......
...@@ -141,7 +141,7 @@ CREATE TABLE IF NOT EXISTS `conversation` ( ...@@ -141,7 +141,7 @@ CREATE TABLE IF NOT EXISTS `conversation` (
`chat_id` bigint NULL DEFAULT NULL COMMENT 'Telegram chat_id can be a the user id or the chat id ', `chat_id` bigint NULL DEFAULT NULL COMMENT 'Telegram chat_id can be a the user id or the chat id ',
`status` ENUM('active', 'cancelled', 'stopped') NOT NULL DEFAULT 'active' COMMENT 'active conversation is active, cancelled conversation has been truncated before end, stopped conversation has end', `status` ENUM('active', 'cancelled', 'stopped') NOT NULL DEFAULT 'active' COMMENT 'active conversation is active, cancelled conversation has been truncated before end, stopped conversation has end',
`command` varchar(160) DEFAULT '' COMMENT 'Default Command to execute', `command` varchar(160) DEFAULT '' COMMENT 'Default Command to execute',
`data` varchar(1000) DEFAULT 'NULL' COMMENT 'Data stored from command', `notes` varchar(1000) DEFAULT 'NULL' COMMENT 'Data stored from command',
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
......
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