Skip to content

Commit 2499c61

Browse files
committed
Remember the current action being executed and return correct objects for requests.
1 parent c599d54 commit 2499c61

File tree

3 files changed

+56
-36
lines changed

3 files changed

+56
-36
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
1111
- Botan.io service has been discontinued.
1212
### Removed
1313
### Fixed
14+
- Return correct objects for requests.
1415
### Security
1516

1617
## [0.55.1] - 2019-01-06

src/Entities/ServerResponse.php

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
namespace Longman\TelegramBot\Entities;
1010

11+
use Longman\TelegramBot\Entities\Games\GameHighScore;
12+
use Longman\TelegramBot\Request;
13+
1114
/**
1215
* Class ServerResponse
1316
*
@@ -103,32 +106,27 @@ public function printError($return = false)
103106
* @param string $bot_username
104107
*
105108
* @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
106-
* @throws \Longman\TelegramBot\Exception\TelegramException
107109
*/
108-
private function createResultObject($result, $bot_username)
110+
private function createResultObject(array $result, $bot_username)
109111
{
112+
$action = Request::getCurrentAction();
113+
110114
// We don't need to save the raw_data of the response object!
111115
$result['raw_data'] = null;
112116

113117
$result_object_types = [
114-
'total_count' => 'UserProfilePhotos', //Response from getUserProfilePhotos
115-
'stickers' => 'StickerSet', //Response from getStickerSet
116-
'file_id' => 'File', //Response from getFile
117-
'title' => 'Chat', //Response from getChat
118-
'username' => 'User', //Response from getMe
119-
'user' => 'ChatMember', //Response from getChatMember
120-
'url' => 'WebhookInfo', //Response from getWebhookInfo
118+
'getChat' => Chat::class,
119+
'getChatMember' => ChatMember::class,
120+
'getFile' => File::class,
121+
'getMe' => User::class,
122+
'getStickerSet' => StickerSet::class,
123+
'getUserProfilePhotos' => UserProfilePhotos::class,
124+
'getWebhookInfo' => WebhookInfo::class,
121125
];
122-
foreach ($result_object_types as $type => $object_class) {
123-
if (isset($result[$type])) {
124-
$object_class = __NAMESPACE__ . '\\' . $object_class;
125126

126-
return new $object_class($result);
127-
}
128-
}
127+
$object_class = array_key_exists($action, $result_object_types) ? $result_object_types[$action] : Message::class;
129128

130-
//Response from sendMessage
131-
return new Message($result, $bot_username);
129+
return new $object_class($result, $bot_username);
132130
}
133131

134132
/**
@@ -137,28 +135,26 @@ private function createResultObject($result, $bot_username)
137135
* @param array $result
138136
* @param string $bot_username
139137
*
140-
* @return null|\Longman\TelegramBot\Entities\ChatMember[]|\Longman\TelegramBot\Entities\Update[]
141-
* @throws \Longman\TelegramBot\Exception\TelegramException
138+
* @return \Longman\TelegramBot\Entities\ChatMember[]|\Longman\TelegramBot\Entities\Games\GameHighScore[]|\Longman\TelegramBot\Entities\Message[]|\Longman\TelegramBot\Entities\Update[]
142139
*/
143-
private function createResultObjects($result, $bot_username)
140+
private function createResultObjects(array $result, $bot_username)
144141
{
145142
$results = [];
146-
if (isset($result[0]['user'])) {
147-
//Response from getChatAdministrators
148-
foreach ($result as $user) {
149-
// We don't need to save the raw_data of the response object!
150-
$user['raw_data'] = null;
143+
$action = Request::getCurrentAction();
151144

152-
$results[] = new ChatMember($user);
153-
}
154-
} else {
155-
//Get Update
156-
foreach ($result as $update) {
157-
// We don't need to save the raw_data of the response object!
158-
$update['raw_data'] = null;
145+
$result_object_types = [
146+
'getChatAdministrators' => ChatMember::class,
147+
'getGameHighScores' => GameHighScore::class,
148+
'sendMediaGroup' => Message::class,
149+
];
159150

160-
$results[] = new Update($update, $bot_username);
161-
}
151+
$object_class = array_key_exists($action, $result_object_types) ? $result_object_types[$action] : Update::class;
152+
153+
foreach ($result as $data) {
154+
// We don't need to save the raw_data of the response object!
155+
$data['raw_data'] = null;
156+
157+
$results[] = new $object_class($data, $bot_username);
162158
}
163159

164160
return $results;

src/Request.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,13 @@ class Request
128128
*/
129129
private static $limiter_interval;
130130

131+
/**
132+
* Get the current action that is being executed
133+
*
134+
* @var string
135+
*/
136+
private static $current_action;
137+
131138
/**
132139
* Available actions to send
133140
*
@@ -400,6 +407,16 @@ private static function mediaInputHelper($item, &$has_resource, array &$multipar
400407
return json_encode($item);
401408
}
402409

410+
/**
411+
* Get the current action that's being executed
412+
*
413+
* @return string
414+
*/
415+
public static function getCurrentAction()
416+
{
417+
return self::$current_action;
418+
}
419+
403420
/**
404421
* Execute HTTP Request
405422
*
@@ -432,7 +449,7 @@ public static function execute($action, array $data = [])
432449
TelegramLog::update($result);
433450
}
434451
} catch (RequestException $e) {
435-
$result = ($e->getResponse()) ? (string) $e->getResponse()->getBody() : '';
452+
$result = $e->getResponse() ? (string) $e->getResponse()->getBody() : '';
436453
} finally {
437454
//Logging verbose debug output
438455
TelegramLog::endDebugLogTempStream('Verbose HTTP Request output:' . PHP_EOL . '%s' . PHP_EOL);
@@ -529,8 +546,11 @@ public static function send($action, array $data = [])
529546

530547
self::limitTelegramRequests($action, $data);
531548

549+
// Remember which action is currently being executed.
550+
self::$current_action = $action;
551+
532552
$raw_response = self::execute($action, $data);
533-
$response = json_decode($raw_response, true);
553+
$response = json_decode($raw_response, true);
534554

535555
if (null === $response) {
536556
TelegramLog::debug($raw_response);
@@ -543,6 +563,9 @@ public static function send($action, array $data = [])
543563
throw new InvalidBotTokenException();
544564
}
545565

566+
// Reset current action after completion.
567+
self::$current_action = null;
568+
546569
return $response;
547570
}
548571

0 commit comments

Comments
 (0)