1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Telegram\Bot; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Promise\PromiseInterface; |
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
7
|
|
|
use Telegram\Bot\Exceptions\TelegramResponseException; |
8
|
|
|
use Telegram\Bot\Exceptions\TelegramSDKException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class TelegramResponse. |
12
|
|
|
* |
13
|
|
|
* Handles the response from Telegram API. |
14
|
|
|
*/ |
15
|
|
|
class TelegramResponse |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var null|int The HTTP status code response from API. |
19
|
|
|
*/ |
20
|
|
|
protected $httpStatusCode; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array The headers returned from API request. |
24
|
|
|
*/ |
25
|
|
|
protected $headers; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string The raw body of the response from API request. |
29
|
|
|
*/ |
30
|
|
|
protected $body; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array The decoded body of the API response. |
34
|
|
|
*/ |
35
|
|
|
protected $decodedBody = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string API Endpoint used to make the request. |
39
|
|
|
*/ |
40
|
|
|
protected $endPoint; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var TelegramRequest The original request that returned this response. |
44
|
|
|
*/ |
45
|
|
|
protected $request; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var TelegramSDKException The exception thrown by this request. |
49
|
|
|
*/ |
50
|
|
|
protected $thrownException; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Gets the relevant data from the Http client. |
54
|
|
|
* |
55
|
|
|
* @param TelegramRequest $request |
56
|
|
|
* @param ResponseInterface|PromiseInterface $response |
57
|
|
|
*/ |
58
|
40 |
|
public function __construct(TelegramRequest $request, $response) |
59
|
|
|
{ |
60
|
40 |
|
if ($response instanceof ResponseInterface) { |
61
|
40 |
|
$this->httpStatusCode = $response->getStatusCode(); |
62
|
40 |
|
$this->body = $response->getBody(); |
63
|
40 |
|
$this->headers = $response->getHeaders(); |
64
|
|
|
|
65
|
40 |
|
$this->decodeBody(); |
66
|
40 |
|
} elseif ($response instanceof PromiseInterface) { |
67
|
|
|
$this->httpStatusCode = null; |
68
|
|
|
} else { |
69
|
|
|
throw new \InvalidArgumentException( |
70
|
|
|
'Second constructor argument "response" must be instance of ResponseInterface or PromiseInterface' |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
40 |
|
$this->request = $request; |
75
|
40 |
|
$this->endPoint = (string) $request->getEndpoint(); |
76
|
40 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Return the original request that returned this response. |
80
|
|
|
* |
81
|
|
|
* @return TelegramRequest |
82
|
|
|
*/ |
83
|
|
|
public function getRequest() |
84
|
|
|
{ |
85
|
|
|
return $this->request; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Gets the HTTP status code. |
90
|
|
|
* Returns NULL if the request was asynchronous since we are not waiting for the response. |
91
|
|
|
* |
92
|
|
|
* @return null|int |
93
|
|
|
*/ |
94
|
2 |
|
public function getHttpStatusCode() |
95
|
|
|
{ |
96
|
2 |
|
return $this->httpStatusCode; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Gets the Request Endpoint used to get the response. |
101
|
|
|
* |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
public function getEndpoint() |
105
|
|
|
{ |
106
|
|
|
return $this->endPoint; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Return the bot access token that was used for this request. |
111
|
|
|
* |
112
|
|
|
* @return string|null |
113
|
|
|
*/ |
114
|
|
|
public function getAccessToken() |
115
|
|
|
{ |
116
|
|
|
return $this->request->getAccessToken(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Return the HTTP headers for this response. |
121
|
|
|
* |
122
|
|
|
* @return array |
123
|
|
|
*/ |
124
|
|
|
public function getHeaders() |
125
|
|
|
{ |
126
|
|
|
return $this->headers; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Return the raw body response. |
131
|
|
|
* |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
|
|
public function getBody() |
135
|
|
|
{ |
136
|
|
|
return $this->body; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Return the decoded body response. |
141
|
|
|
* |
142
|
|
|
* @return array |
143
|
|
|
*/ |
144
|
38 |
|
public function getDecodedBody() |
145
|
|
|
{ |
146
|
38 |
|
return $this->decodedBody; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Helper function to return the payload of a successful response. |
151
|
|
|
* |
152
|
|
|
* @return mixed |
153
|
|
|
*/ |
154
|
2 |
|
public function getResult() |
155
|
|
|
{ |
156
|
2 |
|
return $this->getDecodedBody()['result']; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Checks if response is an error. |
161
|
|
|
* |
162
|
|
|
* @return bool |
163
|
|
|
*/ |
164
|
40 |
|
public function isError() |
165
|
|
|
{ |
166
|
40 |
|
return isset($this->decodedBody['ok']) && ($this->decodedBody['ok'] === false); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Throws the exception. |
171
|
|
|
* |
172
|
|
|
* @throws TelegramSDKException |
173
|
|
|
*/ |
174
|
|
|
public function throwException() |
175
|
|
|
{ |
176
|
|
|
throw $this->thrownException; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Instantiates an exception to be thrown later. |
181
|
|
|
*/ |
182
|
2 |
|
public function makeException() |
183
|
|
|
{ |
184
|
2 |
|
$this->thrownException = TelegramResponseException::create($this); |
185
|
2 |
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Returns the exception that was thrown for this request. |
189
|
|
|
* |
190
|
|
|
* @return TelegramSDKException |
191
|
|
|
*/ |
192
|
2 |
|
public function getThrownException() |
193
|
|
|
{ |
194
|
2 |
|
return $this->thrownException; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Converts raw API response to proper decoded response. |
199
|
|
|
*/ |
200
|
40 |
|
public function decodeBody() |
201
|
|
|
{ |
202
|
40 |
|
$this->decodedBody = json_decode($this->body, true); |
203
|
|
|
|
204
|
40 |
|
if ($this->decodedBody === null) { |
205
|
|
|
$this->decodedBody = []; |
206
|
|
|
parse_str($this->body, $this->decodedBody); |
207
|
|
|
} |
208
|
|
|
|
209
|
40 |
|
if (!is_array($this->decodedBody)) { |
210
|
|
|
$this->decodedBody = []; |
211
|
|
|
} |
212
|
|
|
|
213
|
40 |
|
if ($this->isError()) { |
214
|
2 |
|
$this->makeException(); |
215
|
2 |
|
} |
216
|
40 |
|
} |
217
|
|
|
} |
218
|
|
|
|