Commit 2858ff5d authored by Marco Boretto's avatar Marco Boretto

Merge pull request #9 from noplanman/small_cleanup_fixes

Small cleanup fixes
parents 8a5a5c58 b8e3b4b9
...@@ -215,18 +215,20 @@ class SendtochannelCommand extends AdminCommand ...@@ -215,18 +215,20 @@ class SendtochannelCommand extends AdminCommand
$this->conversation->notes['last_message_id'] = $message->getMessageId(); $this->conversation->notes['last_message_id'] = $message->getMessageId();
// no break // no break
case 5: case 5:
$this->conversation->stop();
$data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]); $data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]);
if ($this->conversation->notes['post_message']) { if ($this->conversation->notes['post_message']) {
$data['text'] = $this->publish(new Message($this->conversation->notes['message'], 'anystring'), $this->conversation->notes['channel'], $this->conversation->notes['caption']); $data['text'] = $this->publish(
$result = Request::sendMessage($data); new Message($this->conversation->notes['message'], 'anystring'),
break; $this->conversation->notes['channel'],
$this->conversation->notes['caption']
);
} else {
$data['text'] = 'Abort by user, message not sent..';
} }
$data['text'] = 'Abort by user, message not sent..'; $this->conversation->stop();
$result = Request::sendMessage($data); $result = Request::sendMessage($data);
break;
} }
return $result; return $result;
} }
......
...@@ -60,13 +60,6 @@ class Conversation ...@@ -60,13 +60,6 @@ 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
* *
...@@ -80,26 +73,35 @@ class Conversation ...@@ -80,26 +73,35 @@ class Conversation
$this->chat_id = $chat_id; $this->chat_id = $chat_id;
$this->command = $command; $this->command = $command;
$this->command_is_provided = ($command !== null);
//Try to load an existing conversation if possible //Try to load an existing conversation if possible
if (!$this->load() && $this->command_is_provided) { if (!$this->load() && $command !== null) {
//A new conversation start //A new conversation start
$this->start(); $this->start();
} }
} }
/** /**
* Load the conversation from the database * Clear all conversation variables.
* *
* @return bool * @return bool Always return true, to allow this method in an if statement.
*/ */
protected function load() protected function clear()
{ {
$this->conversation = null; $this->conversation = null;
$this->protected_notes = null; $this->protected_notes = null;
$this->notes = null; $this->notes = null;
return true;
}
/**
* Load the conversation from the database
*
* @return bool
*/
protected function load()
{
//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);
if (isset($conversation[0])) { if (isset($conversation[0])) {
...@@ -111,7 +113,6 @@ class Conversation ...@@ -111,7 +113,6 @@ 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;
} }
...@@ -162,7 +163,7 @@ class Conversation ...@@ -162,7 +163,7 @@ class Conversation
*/ */
public function stop() public function stop()
{ {
return $this->updateStatus('stopped'); return ($this->updateStatus('stopped') && $this->clear());
} }
/** /**
...@@ -172,7 +173,7 @@ class Conversation ...@@ -172,7 +173,7 @@ class Conversation
*/ */
public function cancel() public function cancel()
{ {
return $this->updateStatus('cancelled'); return ($this->updateStatus('cancelled') && $this->clear());
} }
/** /**
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
namespace Longman\TelegramBot; namespace Longman\TelegramBot;
use Longman\TelegramBot\DB; use Longman\TelegramBot\DB;
use Longman\TelegramBot\Exception\TelegramException;
/** /**
* Class ConversationDB * Class ConversationDB
...@@ -63,7 +64,7 @@ class ConversationDB extends DB ...@@ -63,7 +64,7 @@ class ConversationDB extends DB
$results = $sth->fetchAll(\PDO::FETCH_ASSOC); $results = $sth->fetchAll(\PDO::FETCH_ASSOC);
} catch (PDOException $e) { } catch (\Exception $e) {
throw new TelegramException($e->getMessage()); throw new TelegramException($e->getMessage());
} }
return $results; return $results;
...@@ -106,7 +107,7 @@ class ConversationDB extends DB ...@@ -106,7 +107,7 @@ class ConversationDB extends DB
$sth->bindParam(':date', $created_at); $sth->bindParam(':date', $created_at);
$status = $sth->execute(); $status = $sth->execute();
} catch (PDOException $e) { } catch (\Exception $e) {
throw new TelegramException($e->getMessage()); throw new TelegramException($e->getMessage());
} }
return $status; return $status;
...@@ -178,7 +179,7 @@ class ConversationDB extends DB ...@@ -178,7 +179,7 @@ class ConversationDB extends DB
try { try {
$sth = self::$pdo->prepare($query); $sth = self::$pdo->prepare($query);
$status = $sth->execute($tokens); $status = $sth->execute($tokens);
} catch (PDOException $e) { } catch (\Exception $e) {
throw new TelegramException($e->getMessage()); throw new TelegramException($e->getMessage());
} }
return $status; return $status;
......
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