|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Telegram\Bot; |
|
4
|
|
|
|
|
5
|
|
|
use Telegram\Bot\Exceptions\TelegramSDKException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class TelegramRequest. |
|
9
|
|
|
* |
|
10
|
|
|
* Builds Telegram Bot API Request Entity. |
|
11
|
|
|
*/ |
|
12
|
|
|
class TelegramRequest |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var string|null The bot access token to use for this request. |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $accessToken; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string The HTTP method for this request. |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $method; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string The API endpoint for this request. |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $endpoint; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var array The headers to send with this request. |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $headers = []; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var array The parameters to send with this request. |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $params = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var array The files to send with this request. |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $files = []; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Indicates if the request to Telegram will be asynchronous (non-blocking). |
|
46
|
|
|
* |
|
47
|
|
|
* @var bool |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $isAsyncRequest = false; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Timeout of the request in seconds. |
|
53
|
|
|
* |
|
54
|
|
|
* @var int |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $timeOut = 30; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Connection timeout of the request in seconds. |
|
60
|
|
|
* |
|
61
|
|
|
* @var int |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $connectTimeOut = 10; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Creates a new Request entity. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string|null $accessToken |
|
69
|
|
|
* @param string|null $method |
|
70
|
|
|
* @param string|null $endpoint |
|
71
|
|
|
* @param array|null $params |
|
72
|
|
|
* @param bool $isAsyncRequest |
|
73
|
|
|
* @param int $timeOut |
|
74
|
|
|
* @param int $connectTimeOut |
|
75
|
|
|
*/ |
|
76
|
40 |
|
public function __construct( |
|
77
|
|
|
$accessToken = null, |
|
78
|
|
|
$method = null, |
|
79
|
|
|
$endpoint = null, |
|
80
|
|
|
array $params = [], |
|
81
|
|
|
$isAsyncRequest = false, |
|
82
|
|
|
$timeOut = 60, |
|
83
|
|
|
$connectTimeOut = 10 |
|
84
|
|
|
) { |
|
85
|
40 |
|
$this->setAccessToken($accessToken); |
|
86
|
40 |
|
$this->setMethod($method); |
|
87
|
40 |
|
$this->setEndpoint($endpoint); |
|
88
|
40 |
|
$this->setParams($params); |
|
89
|
40 |
|
$this->setAsyncRequest($isAsyncRequest); |
|
90
|
40 |
|
$this->setTimeOut($timeOut); |
|
91
|
40 |
|
$this->setConnectTimeOut($connectTimeOut); |
|
92
|
40 |
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Set the bot access token for this request. |
|
96
|
|
|
* |
|
97
|
|
|
* @param string |
|
98
|
|
|
* |
|
99
|
|
|
* @return TelegramRequest |
|
100
|
|
|
*/ |
|
101
|
40 |
|
public function setAccessToken($accessToken) |
|
102
|
|
|
{ |
|
103
|
40 |
|
$this->accessToken = $accessToken; |
|
104
|
|
|
|
|
105
|
40 |
|
return $this; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Return the bot access token for this request. |
|
110
|
|
|
* |
|
111
|
|
|
* @return string|null |
|
112
|
|
|
*/ |
|
113
|
40 |
|
public function getAccessToken() |
|
114
|
|
|
{ |
|
115
|
40 |
|
return $this->accessToken; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Validate that bot access token exists for this request. |
|
120
|
|
|
* |
|
121
|
|
|
* @throws TelegramSDKException |
|
122
|
|
|
*/ |
|
123
|
|
|
public function validateAccessToken() |
|
124
|
|
|
{ |
|
125
|
|
|
$accessToken = $this->getAccessToken(); |
|
126
|
|
|
if ($accessToken === null) { |
|
127
|
|
|
throw new TelegramSDKException('You must provide your bot access token to make any API requests.'); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Set the HTTP method for this request. |
|
133
|
|
|
* |
|
134
|
|
|
* @param string |
|
135
|
|
|
* |
|
136
|
|
|
* @return TelegramRequest |
|
137
|
|
|
*/ |
|
138
|
40 |
|
public function setMethod($method) |
|
139
|
|
|
{ |
|
140
|
40 |
|
$this->method = strtoupper($method); |
|
141
|
|
|
|
|
142
|
40 |
|
return $this; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Return the HTTP method for this request. |
|
147
|
|
|
* |
|
148
|
|
|
* @return string |
|
149
|
|
|
*/ |
|
150
|
40 |
|
public function getMethod() |
|
151
|
|
|
{ |
|
152
|
40 |
|
return $this->method; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Validate that the HTTP method is set. |
|
157
|
|
|
* |
|
158
|
|
|
* @throws TelegramSDKException |
|
159
|
|
|
*/ |
|
160
|
|
|
public function validateMethod() |
|
161
|
|
|
{ |
|
162
|
|
|
if (!$this->method) { |
|
163
|
|
|
throw new TelegramSDKException('HTTP method not specified.'); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
if (!in_array($this->method, ['GET', 'POST'])) { |
|
167
|
|
|
throw new TelegramSDKException('Invalid HTTP method specified.'); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Set the endpoint for this request. |
|
173
|
|
|
* |
|
174
|
|
|
* @param string $endpoint |
|
175
|
|
|
* |
|
176
|
|
|
* @return TelegramRequest |
|
177
|
|
|
*/ |
|
178
|
40 |
|
public function setEndpoint($endpoint) |
|
179
|
|
|
{ |
|
180
|
40 |
|
$this->endpoint = $endpoint; |
|
181
|
|
|
|
|
182
|
40 |
|
return $this; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Return the API Endpoint for this request. |
|
187
|
|
|
* |
|
188
|
|
|
* @return string |
|
189
|
|
|
*/ |
|
190
|
40 |
|
public function getEndpoint() |
|
191
|
|
|
{ |
|
192
|
40 |
|
return $this->endpoint; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Set the params for this request. |
|
197
|
|
|
* |
|
198
|
|
|
* @param array $params |
|
199
|
|
|
* |
|
200
|
|
|
* @return TelegramRequest |
|
201
|
|
|
*/ |
|
202
|
40 |
|
public function setParams(array $params = []) |
|
203
|
|
|
{ |
|
204
|
40 |
|
$this->params = array_merge($this->params, $params); |
|
205
|
|
|
|
|
206
|
40 |
|
return $this; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Return the params for this request. |
|
211
|
|
|
* |
|
212
|
|
|
* @return array |
|
213
|
|
|
*/ |
|
214
|
40 |
|
public function getParams() |
|
215
|
|
|
{ |
|
216
|
40 |
|
return $this->params; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Set the headers for this request. |
|
221
|
|
|
* |
|
222
|
|
|
* @param array $headers |
|
223
|
|
|
* |
|
224
|
|
|
* @return TelegramRequest |
|
225
|
|
|
*/ |
|
226
|
|
|
public function setHeaders(array $headers) |
|
227
|
|
|
{ |
|
228
|
|
|
$this->headers = array_merge($this->headers, $headers); |
|
229
|
|
|
|
|
230
|
|
|
return $this; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Return the headers for this request. |
|
235
|
|
|
* |
|
236
|
|
|
* @return array |
|
237
|
|
|
*/ |
|
238
|
40 |
|
public function getHeaders() |
|
239
|
|
|
{ |
|
240
|
40 |
|
$headers = $this->getDefaultHeaders(); |
|
241
|
|
|
|
|
242
|
40 |
|
return array_merge($this->headers, $headers); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* Make this request asynchronous (non-blocking). |
|
247
|
|
|
* |
|
248
|
|
|
* @param $isAsyncRequest |
|
249
|
|
|
* |
|
250
|
|
|
* @return TelegramRequest |
|
251
|
|
|
*/ |
|
252
|
40 |
|
public function setAsyncRequest($isAsyncRequest) |
|
253
|
|
|
{ |
|
254
|
40 |
|
$this->isAsyncRequest = $isAsyncRequest; |
|
255
|
|
|
|
|
256
|
40 |
|
return $this; |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* Check if this is an asynchronous request (non-blocking). |
|
261
|
|
|
* |
|
262
|
|
|
* @return bool |
|
263
|
|
|
*/ |
|
264
|
40 |
|
public function isAsyncRequest() |
|
265
|
|
|
{ |
|
266
|
40 |
|
return $this->isAsyncRequest; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* Only return params on POST requests. |
|
271
|
|
|
* |
|
272
|
|
|
* @return array |
|
273
|
|
|
*/ |
|
274
|
40 |
|
public function getPostParams() |
|
275
|
|
|
{ |
|
276
|
40 |
|
if ($this->getMethod() === 'POST') { |
|
277
|
40 |
|
return $this->getParams(); |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
return []; |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* The default headers used with every request. |
|
285
|
|
|
* |
|
286
|
|
|
* @return array |
|
287
|
|
|
*/ |
|
288
|
40 |
|
public function getDefaultHeaders() |
|
289
|
|
|
{ |
|
290
|
|
|
return [ |
|
291
|
40 |
|
'User-Agent' => 'Telegram Bot PHP SDK v'.Api::VERSION.' - (https://github.com/irazasyed/telegram-bot-sdk)', |
|
292
|
40 |
|
]; |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
/** |
|
296
|
|
|
* @return int |
|
297
|
|
|
*/ |
|
298
|
40 |
|
public function getTimeOut() |
|
299
|
|
|
{ |
|
300
|
40 |
|
return $this->timeOut; |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
/** |
|
304
|
|
|
* @param int $timeOut |
|
305
|
|
|
* |
|
306
|
|
|
* @return $this |
|
307
|
|
|
*/ |
|
308
|
40 |
|
public function setTimeOut($timeOut) |
|
309
|
|
|
{ |
|
310
|
40 |
|
$this->timeOut = $timeOut; |
|
311
|
|
|
|
|
312
|
40 |
|
return $this; |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
/** |
|
316
|
|
|
* @return int |
|
317
|
|
|
*/ |
|
318
|
40 |
|
public function getConnectTimeOut() |
|
319
|
|
|
{ |
|
320
|
40 |
|
return $this->connectTimeOut; |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
/** |
|
324
|
|
|
* @param int $connectTimeOut |
|
325
|
|
|
* |
|
326
|
|
|
* @return $this |
|
327
|
|
|
*/ |
|
328
|
40 |
|
public function setConnectTimeOut($connectTimeOut) |
|
329
|
|
|
{ |
|
330
|
40 |
|
$this->connectTimeOut = $connectTimeOut; |
|
331
|
|
|
|
|
332
|
40 |
|
return $this; |
|
333
|
|
|
} |
|
334
|
|
|
} |
|
335
|
|
|
|