Commit d2c1fffd authored by Armando Lüscher's avatar Armando Lüscher

Introduce new getLastCommandResponse() method to retrieve the last executed command response.

Fix getUpdate handler to incorporate new Update processing.
execute() method for ALL commands must now return a ServerResponse object, not a bool!!
handle() now returns a bool, handleGetUpdates() still returns a ServerResponse object.
parent 8285b28b
...@@ -130,6 +130,13 @@ class Telegram ...@@ -130,6 +130,13 @@ class Telegram
*/ */
protected $admins_list = []; protected $admins_list = [];
/**
* ServerResponse of the last Command execution
*
* @var Entities\ServerResponse
*/
protected $last_command_response;
/** /**
* Constructor * Constructor
* *
...@@ -340,6 +347,16 @@ class Telegram ...@@ -340,6 +347,16 @@ class Telegram
return $this->input; return $this->input;
} }
/**
* Get the ServerResponse of the last Command execution
*
* @return Entities\ServerResponse
*/
public function getLastCommandResponse()
{
return $this->last_command_response;
}
/** /**
* Handle getUpdates method * Handle getUpdates method
* *
...@@ -353,38 +370,28 @@ class Telegram ...@@ -353,38 +370,28 @@ class Telegram
//DB Query //DB Query
$last_update = DB::selectTelegramUpdate(1); $last_update = DB::selectTelegramUpdate(1);
if (isset($last_update[0]['id'])) { //As explained in the telegram bot api documentation
//As explained in the telegram bot api documentation $offset = (isset($last_update[0]['id'])) ? $last_update[0]['id'] + 1 : null;
$offset = $last_update[0]['id']+1;
} else {
$offset = null;
}
$ServerResponse = Request::getUpdates([ $response = Request::getUpdates([
'offset' => $offset , 'offset' => $offset,
'limit' => $limit, 'limit' => $limit,
'timeout' => $timeout 'timeout' => $timeout,
]); ]);
if ($ServerResponse->isOk()) { if ($response->isOk()) {
$results = ''; //Process all updates
$n_update = count($ServerResponse->getResult()); foreach ((array)$response->getResult() as $result) {
for ($a = 0; $a < $n_update; $a++) { $this->processUpdate($result);
$result = $this->processUpdate($ServerResponse->getResult()[$a]);
} }
} }
return $ServerResponse; return $response;
} }
/** /**
* Handle bot request from webhook * Handle bot request from webhook
* *
* @todo Should return the executed command result (true|false) but we shoud check if all commands return a value.
* Furthermore this function is the twin of handleGetUpdates for webhook, but the first returns the ServerResponse
* instead the latter return if the command has failed or not (true|false).
* We shoud use the same convention for both.
*
* @return bool * @return bool
*/ */
public function handle() public function handle()
...@@ -399,8 +406,7 @@ class Telegram ...@@ -399,8 +406,7 @@ class Telegram
throw new TelegramException('Invalid JSON!'); throw new TelegramException('Invalid JSON!');
} }
$this->update = new Update($post, $this->bot_name); return $this->processUpdate(new Update($post, $this->bot_name))->isOk();
return $this->processUpdate();
} }
/** /**
...@@ -416,24 +422,27 @@ class Telegram ...@@ -416,24 +422,27 @@ class Telegram
} }
/** /**
* Process Handle bot request * Process bot Update request
* *
* @return bool * @param Entities\Update $update
*
* @return Entities\ServerResponse
*/ */
public function processUpdate() public function processUpdate(Update $update)
{ {
$update_type = $this->update->getUpdateType(); $this->update = $update;
//If all else fails, it's a generic message. //If all else fails, it's a generic message.
$command = 'genericmessage'; $command = 'genericmessage';
$update_type = $this->update->getUpdateType();
if (in_array($update_type, ['inline_query', 'chosen_inline_result'])) { if (in_array($update_type, ['inline_query', 'chosen_inline_result'])) {
$command = $this->getCommandFromType($update_type); $command = $this->getCommandFromType($update_type);
} elseif ($update_type === 'message') { } elseif ($update_type === 'message') {
$message = $this->update->getMessage(); $message = $this->update->getMessage();
//Load admin commands //Load admin commands
if ($this->isAdmin($message->getFrom()->getId())) { if ($this->isAdmin()) {
$this->addCommandsPath(BASE_COMMANDS_PATH . '/AdminCommands', false); $this->addCommandsPath(BASE_COMMANDS_PATH . '/AdminCommands', false);
} }
...@@ -476,13 +485,15 @@ class Telegram ...@@ -476,13 +485,15 @@ class Telegram
$command_obj = $this->getCommandObject($command); $command_obj = $this->getCommandObject($command);
if (!$command_obj || !$command_obj->isEnabled()) { if (!$command_obj || !$command_obj->isEnabled()) {
//handle a generic command or non existing one //Handle a generic command or non existing one
return $this->executeCommand('Generic'); $this->last_command_response = $this->executeCommand('Generic');
} else {
//execute() method is executed after preExecute()
//This is to prevent executing a DB query without a valid connection
$this->last_command_response = $command_obj->preExecute();
} }
//execute() methods will be execute after preexecute() methods return $this->last_command_response;
//this for prevent to execute db query without connection
return $command_obj->preExecute();
} }
/** /**
......
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