Remember the current action being executed and return correct objects for requests.

parent c599d545
...@@ -11,6 +11,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c ...@@ -11,6 +11,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
- Botan.io service has been discontinued. - Botan.io service has been discontinued.
### Removed ### Removed
### Fixed ### Fixed
- Return correct objects for requests.
### Security ### Security
## [0.55.1] - 2019-01-06 ## [0.55.1] - 2019-01-06
......
...@@ -8,6 +8,9 @@ ...@@ -8,6 +8,9 @@
namespace Longman\TelegramBot\Entities; namespace Longman\TelegramBot\Entities;
use Longman\TelegramBot\Entities\Games\GameHighScore;
use Longman\TelegramBot\Request;
/** /**
* Class ServerResponse * Class ServerResponse
* *
...@@ -103,32 +106,27 @@ class ServerResponse extends Entity ...@@ -103,32 +106,27 @@ class ServerResponse extends Entity
* @param string $bot_username * @param string $bot_username
* *
* @return \Longman\TelegramBot\Entities\Chat|\Longman\TelegramBot\Entities\ChatMember|\Longman\TelegramBot\Entities\File|\Longman\TelegramBot\Entities\Message|\Longman\TelegramBot\Entities\User|\Longman\TelegramBot\Entities\UserProfilePhotos|\Longman\TelegramBot\Entities\WebhookInfo * @return \Longman\TelegramBot\Entities\Chat|\Longman\TelegramBot\Entities\ChatMember|\Longman\TelegramBot\Entities\File|\Longman\TelegramBot\Entities\Message|\Longman\TelegramBot\Entities\User|\Longman\TelegramBot\Entities\UserProfilePhotos|\Longman\TelegramBot\Entities\WebhookInfo
* @throws \Longman\TelegramBot\Exception\TelegramException
*/ */
private function createResultObject($result, $bot_username) private function createResultObject(array $result, $bot_username)
{ {
$action = Request::getCurrentAction();
// We don't need to save the raw_data of the response object! // We don't need to save the raw_data of the response object!
$result['raw_data'] = null; $result['raw_data'] = null;
$result_object_types = [ $result_object_types = [
'total_count' => 'UserProfilePhotos', //Response from getUserProfilePhotos 'getChat' => Chat::class,
'stickers' => 'StickerSet', //Response from getStickerSet 'getChatMember' => ChatMember::class,
'file_id' => 'File', //Response from getFile 'getFile' => File::class,
'title' => 'Chat', //Response from getChat 'getMe' => User::class,
'username' => 'User', //Response from getMe 'getStickerSet' => StickerSet::class,
'user' => 'ChatMember', //Response from getChatMember 'getUserProfilePhotos' => UserProfilePhotos::class,
'url' => 'WebhookInfo', //Response from getWebhookInfo 'getWebhookInfo' => WebhookInfo::class,
]; ];
foreach ($result_object_types as $type => $object_class) {
if (isset($result[$type])) {
$object_class = __NAMESPACE__ . '\\' . $object_class;
return new $object_class($result); $object_class = array_key_exists($action, $result_object_types) ? $result_object_types[$action] : Message::class;
}
}
//Response from sendMessage return new $object_class($result, $bot_username);
return new Message($result, $bot_username);
} }
/** /**
...@@ -137,28 +135,26 @@ class ServerResponse extends Entity ...@@ -137,28 +135,26 @@ class ServerResponse extends Entity
* @param array $result * @param array $result
* @param string $bot_username * @param string $bot_username
* *
* @return null|\Longman\TelegramBot\Entities\ChatMember[]|\Longman\TelegramBot\Entities\Update[] * @return \Longman\TelegramBot\Entities\ChatMember[]|\Longman\TelegramBot\Entities\Games\GameHighScore[]|\Longman\TelegramBot\Entities\Message[]|\Longman\TelegramBot\Entities\Update[]
* @throws \Longman\TelegramBot\Exception\TelegramException
*/ */
private function createResultObjects($result, $bot_username) private function createResultObjects(array $result, $bot_username)
{ {
$results = []; $results = [];
if (isset($result[0]['user'])) { $action = Request::getCurrentAction();
//Response from getChatAdministrators
foreach ($result as $user) {
// We don't need to save the raw_data of the response object!
$user['raw_data'] = null;
$results[] = new ChatMember($user); $result_object_types = [
} 'getChatAdministrators' => ChatMember::class,
} else { 'getGameHighScores' => GameHighScore::class,
//Get Update 'sendMediaGroup' => Message::class,
foreach ($result as $update) { ];
$object_class = array_key_exists($action, $result_object_types) ? $result_object_types[$action] : Update::class;
foreach ($result as $data) {
// We don't need to save the raw_data of the response object! // We don't need to save the raw_data of the response object!
$update['raw_data'] = null; $data['raw_data'] = null;
$results[] = new Update($update, $bot_username); $results[] = new $object_class($data, $bot_username);
}
} }
return $results; return $results;
......
...@@ -128,6 +128,13 @@ class Request ...@@ -128,6 +128,13 @@ class Request
*/ */
private static $limiter_interval; private static $limiter_interval;
/**
* Get the current action that is being executed
*
* @var string
*/
private static $current_action;
/** /**
* Available actions to send * Available actions to send
* *
...@@ -400,6 +407,16 @@ class Request ...@@ -400,6 +407,16 @@ class Request
return json_encode($item); return json_encode($item);
} }
/**
* Get the current action that's being executed
*
* @return string
*/
public static function getCurrentAction()
{
return self::$current_action;
}
/** /**
* Execute HTTP Request * Execute HTTP Request
* *
...@@ -432,7 +449,7 @@ class Request ...@@ -432,7 +449,7 @@ class Request
TelegramLog::update($result); TelegramLog::update($result);
} }
} catch (RequestException $e) { } catch (RequestException $e) {
$result = ($e->getResponse()) ? (string) $e->getResponse()->getBody() : ''; $result = $e->getResponse() ? (string) $e->getResponse()->getBody() : '';
} finally { } finally {
//Logging verbose debug output //Logging verbose debug output
TelegramLog::endDebugLogTempStream('Verbose HTTP Request output:' . PHP_EOL . '%s' . PHP_EOL); TelegramLog::endDebugLogTempStream('Verbose HTTP Request output:' . PHP_EOL . '%s' . PHP_EOL);
...@@ -529,6 +546,9 @@ class Request ...@@ -529,6 +546,9 @@ class Request
self::limitTelegramRequests($action, $data); self::limitTelegramRequests($action, $data);
// Remember which action is currently being executed.
self::$current_action = $action;
$raw_response = self::execute($action, $data); $raw_response = self::execute($action, $data);
$response = json_decode($raw_response, true); $response = json_decode($raw_response, true);
...@@ -543,6 +563,9 @@ class Request ...@@ -543,6 +563,9 @@ class Request
throw new InvalidBotTokenException(); throw new InvalidBotTokenException();
} }
// Reset current action after completion.
self::$current_action = null;
return $response; return $response;
} }
......
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