1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the TelegramBot package. |
4
|
|
|
* |
5
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Longman\TelegramBot; |
12
|
|
|
|
13
|
|
|
use GuzzleHttp\Client; |
14
|
|
|
use GuzzleHttp\Exception\RequestException; |
15
|
|
|
use Longman\TelegramBot\Entities\File; |
16
|
|
|
use Longman\TelegramBot\Entities\ServerResponse; |
17
|
|
|
use Longman\TelegramBot\Exception\TelegramException; |
18
|
|
|
|
19
|
|
|
class Request |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Telegram object |
23
|
|
|
* |
24
|
|
|
* @var \Longman\TelegramBot\Telegram |
25
|
|
|
*/ |
26
|
|
|
private static $telegram; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* URI of the Telegram API |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private static $api_base_uri = 'https://api.telegram.org'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Guzzle Client object |
37
|
|
|
* |
38
|
|
|
* @var \GuzzleHttp\Client |
39
|
|
|
*/ |
40
|
|
|
private static $client; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Input value of the request |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
private static $input; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Available actions to send |
51
|
|
|
* |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
private static $actions = [ |
55
|
|
|
'getUpdates', |
56
|
|
|
'setWebhook', |
57
|
|
|
'deleteWebhook', |
58
|
|
|
'getMe', |
59
|
|
|
'sendMessage', |
60
|
|
|
'forwardMessage', |
61
|
|
|
'sendPhoto', |
62
|
|
|
'sendAudio', |
63
|
|
|
'sendDocument', |
64
|
|
|
'sendSticker', |
65
|
|
|
'sendVideo', |
66
|
|
|
'sendVoice', |
67
|
|
|
'sendLocation', |
68
|
|
|
'sendVenue', |
69
|
|
|
'sendContact', |
70
|
|
|
'sendChatAction', |
71
|
|
|
'getUserProfilePhotos', |
72
|
|
|
'getFile', |
73
|
|
|
'kickChatMember', |
74
|
|
|
'leaveChat', |
75
|
|
|
'unbanChatMember', |
76
|
|
|
'getChat', |
77
|
|
|
'getChatAdministrators', |
78
|
|
|
'getChatMember', |
79
|
|
|
'getChatMembersCount', |
80
|
|
|
'answerCallbackQuery', |
81
|
|
|
'answerInlineQuery', |
82
|
|
|
'editMessageText', |
83
|
|
|
'editMessageCaption', |
84
|
|
|
'editMessageReplyMarkup', |
85
|
|
|
'getWebhookInfo', |
86
|
|
|
]; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Initialize |
90
|
|
|
* |
91
|
|
|
* @param \Longman\TelegramBot\Telegram $telegram |
92
|
|
|
* |
93
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
94
|
|
|
*/ |
95
|
39 |
|
public static function initialize(Telegram $telegram) |
96
|
|
|
{ |
97
|
39 |
|
if (is_object($telegram)) { |
98
|
39 |
|
self::$telegram = $telegram; |
99
|
39 |
|
self::$client = new Client(['base_uri' => self::$api_base_uri]); |
100
|
|
|
} else { |
101
|
|
|
throw new TelegramException('Telegram pointer is empty!'); |
102
|
|
|
} |
103
|
39 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Set input from custom input or stdin and return it |
107
|
|
|
* |
108
|
|
|
* @return string |
109
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
110
|
|
|
*/ |
111
|
|
|
public static function getInput() |
112
|
|
|
{ |
113
|
|
|
// First check if a custom input has been set, else get the PHP input. |
114
|
|
|
if (!($input = self::$telegram->getCustomInput())) { |
115
|
|
|
$input = file_get_contents('php://input'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// Make sure we have a string to work with. |
119
|
|
|
if (is_string($input)) { |
120
|
|
|
self::$input = $input; |
121
|
|
|
} else { |
122
|
|
|
throw new TelegramException('Input must be a string!'); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
TelegramLog::update(self::$input); |
126
|
|
|
|
127
|
|
|
return self::$input; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Generate general fake server response |
132
|
|
|
* |
133
|
|
|
* @param array $data Data to add to fake response |
134
|
|
|
* |
135
|
|
|
* @return array Fake response data |
136
|
|
|
*/ |
137
|
6 |
|
public static function generateGeneralFakeServerResponse(array $data = []) |
138
|
|
|
{ |
139
|
|
|
//PARAM BINDED IN PHPUNIT TEST FOR TestServerResponse.php |
140
|
|
|
//Maybe this is not the best possible implementation |
141
|
|
|
|
142
|
|
|
//No value set in $data ie testing setWebhook |
143
|
|
|
//Provided $data['chat_id'] ie testing sendMessage |
144
|
|
|
|
145
|
6 |
|
$fake_response = ['ok' => true]; // :) |
146
|
|
|
|
147
|
6 |
|
if ($data === []) { |
148
|
1 |
|
$fake_response['result'] = true; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
//some data to let iniatilize the class method SendMessage |
152
|
6 |
|
if (isset($data['chat_id'])) { |
153
|
6 |
|
$data['message_id'] = '1234'; |
154
|
6 |
|
$data['date'] = '1441378360'; |
155
|
6 |
|
$data['from'] = [ |
156
|
|
|
'id' => 123456789, |
157
|
|
|
'first_name' => 'botname', |
158
|
|
|
'username' => 'namebot', |
159
|
|
|
]; |
160
|
6 |
|
$data['chat'] = ['id' => $data['chat_id']]; |
161
|
|
|
|
162
|
6 |
|
$fake_response['result'] = $data; |
163
|
|
|
} |
164
|
|
|
|
165
|
6 |
|
return $fake_response; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Properly set up the request params |
170
|
|
|
* |
171
|
|
|
* If any item of the array is a resource, reformat it to a multipart request. |
172
|
|
|
* Else, just return the passed data as form params. |
173
|
|
|
* |
174
|
|
|
* @param array $data |
175
|
|
|
* |
176
|
|
|
* @return array |
177
|
|
|
*/ |
178
|
|
|
private static function setUpRequestParams(array $data) |
179
|
|
|
{ |
180
|
|
|
$has_resource = false; |
181
|
|
|
$multipart = []; |
182
|
|
|
|
183
|
|
|
//Reformat data array in multipart way if it contains a resource |
184
|
|
|
foreach ($data as $key => $item) { |
185
|
|
|
$has_resource |= is_resource($item); |
186
|
|
|
$multipart[] = ['name' => $key, 'contents' => $item]; |
187
|
|
|
} |
188
|
|
|
if ($has_resource) { |
189
|
|
|
return ['multipart' => $multipart]; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return ['form_params' => $data]; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Execute HTTP Request |
197
|
|
|
* |
198
|
|
|
* @param string $action Action to execute |
199
|
|
|
* @param array $data Data to attach to the execution |
200
|
|
|
* |
201
|
|
|
* @return string Result of the HTTP Request |
202
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
203
|
|
|
*/ |
204
|
|
|
public static function execute($action, array $data = []) |
205
|
|
|
{ |
206
|
|
|
//Fix so that the keyboard markup is a string, not an object |
207
|
|
|
if (isset($data['reply_markup'])) { |
208
|
|
|
$data['reply_markup'] = json_encode($data['reply_markup']); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
$request_params = self::setUpRequestParams($data); |
212
|
|
|
|
213
|
|
|
$debug_handle = TelegramLog::getDebugLogTempStream(); |
214
|
|
|
$request_params['debug'] = $debug_handle; |
215
|
|
|
|
216
|
|
|
try { |
217
|
|
|
$response = self::$client->post( |
218
|
|
|
'/bot' . self::$telegram->getApiKey() . '/' . $action, |
219
|
|
|
$request_params |
220
|
|
|
); |
221
|
|
|
$result = (string)$response->getBody(); |
222
|
|
|
|
223
|
|
|
//Logging getUpdates Update |
224
|
|
|
if ($action === 'getUpdates') { |
225
|
|
|
TelegramLog::update($result); |
226
|
|
|
} |
227
|
|
|
} catch (RequestException $e) { |
228
|
|
|
$result = (string)$e->getResponse()->getBody(); |
229
|
|
|
} finally { |
230
|
|
|
//Logging verbose debug output |
231
|
|
|
TelegramLog::endDebugLogTempStream("Verbose HTTP Request output:\n%s\n"); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
return $result; |
|
|
|
|
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Download file |
239
|
|
|
* |
240
|
|
|
* @param \Longman\TelegramBot\Entities\File $file |
241
|
|
|
* |
242
|
|
|
* @return boolean |
243
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
244
|
|
|
*/ |
245
|
|
|
public static function downloadFile(File $file) |
246
|
|
|
{ |
247
|
|
|
$tg_file_path = $file->getFilePath(); |
248
|
|
|
$file_path = self::$telegram->getDownloadPath() . '/' . $tg_file_path; |
249
|
|
|
|
250
|
|
|
$file_dir = dirname($file_path); |
251
|
|
|
//For safety reasons, first try to create the directory, then check that it exists. |
252
|
|
|
//This is in case some other process has created the folder in the meantime. |
253
|
|
|
if (!@mkdir($file_dir, 0755, true) && !is_dir($file_dir)) { |
254
|
|
|
throw new TelegramException('Directory ' . $file_dir . ' can\'t be created'); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
$debug_handle = TelegramLog::getDebugLogTempStream(); |
258
|
|
|
|
259
|
|
|
try { |
260
|
|
|
self::$client->get( |
261
|
|
|
'/file/bot' . self::$telegram->getApiKey() . '/' . $tg_file_path, |
262
|
|
|
['debug' => $debug_handle, 'sink' => $file_path] |
263
|
|
|
); |
264
|
|
|
|
265
|
|
|
return filesize($file_path) > 0; |
266
|
|
|
} catch (RequestException $e) { |
267
|
|
|
return (string)$e->getResponse()->getBody(); |
268
|
|
|
} finally { |
269
|
|
|
//Logging verbose debug output |
270
|
|
|
TelegramLog::endDebugLogTempStream("Verbose HTTP File Download Request output:\n%s\n"); |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Encode file |
276
|
|
|
* |
277
|
|
|
* @param string $file |
278
|
|
|
* |
279
|
|
|
* @return resource |
280
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
281
|
|
|
*/ |
282
|
|
|
protected static function encodeFile($file) |
283
|
|
|
{ |
284
|
|
|
$fp = fopen($file, 'r'); |
285
|
|
|
if ($fp === false) { |
286
|
|
|
throw new TelegramException('Cannot open "' . $file . '" for reading'); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
return $fp; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Send command |
294
|
|
|
* |
295
|
|
|
* @todo Fake response doesn't need json encoding? |
296
|
|
|
* |
297
|
|
|
* @param string $action |
298
|
|
|
* @param array $data |
299
|
|
|
* |
300
|
|
|
* @return \Longman\TelegramBot\Entities\ServerResponse |
301
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
302
|
|
|
*/ |
303
|
5 |
|
public static function send($action, array $data = []) |
304
|
|
|
{ |
305
|
5 |
|
self::ensureValidAction($action); |
306
|
|
|
|
307
|
5 |
|
$bot_name = self::$telegram->getBotName(); |
308
|
|
|
|
309
|
5 |
|
if (defined('PHPUNIT_TESTSUITE')) { |
310
|
5 |
|
$fake_response = self::generateGeneralFakeServerResponse($data); |
311
|
|
|
|
312
|
5 |
|
return new ServerResponse($fake_response, $bot_name); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
self::ensureNonEmptyData($data); |
316
|
|
|
|
317
|
|
|
$response = json_decode(self::execute($action, $data), true); |
318
|
|
|
|
319
|
|
|
if (null === $response) { |
320
|
|
|
throw new TelegramException('Telegram returned an invalid response! Please review your bot name and API key.'); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
return new ServerResponse($response, $bot_name); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Make sure the data isn't empty, else throw an exception |
328
|
|
|
* |
329
|
|
|
* @param array $data |
330
|
|
|
* |
331
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
332
|
|
|
*/ |
333
|
|
|
private static function ensureNonEmptyData(array $data) |
334
|
|
|
{ |
335
|
|
|
if (count($data) === 0) { |
336
|
|
|
throw new TelegramException('Data is empty!'); |
337
|
|
|
} |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* Make sure the action is valid, else throw an exception |
342
|
|
|
* |
343
|
|
|
* @param string $action |
344
|
|
|
* |
345
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
346
|
|
|
*/ |
347
|
5 |
|
private static function ensureValidAction($action) |
348
|
|
|
{ |
349
|
5 |
|
if (!in_array($action, self::$actions, true)) { |
350
|
|
|
throw new TelegramException('The action "' . $action . '" doesn\'t exist!'); |
351
|
|
|
} |
352
|
5 |
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* Assign an encoded file to a data array |
356
|
|
|
* |
357
|
|
|
* @param array $data |
358
|
|
|
* @param string $field |
359
|
|
|
* @param string $file |
360
|
|
|
* |
361
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
362
|
|
|
*/ |
363
|
|
|
private static function assignEncodedFile(&$data, $field, $file) |
364
|
|
|
{ |
365
|
|
|
if ($file !== null && $file !== '') { |
366
|
|
|
$data[$field] = self::encodeFile($file); |
367
|
|
|
} |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* Returns basic information about the bot in form of a User object |
372
|
|
|
* |
373
|
|
|
* @link https://core.telegram.org/bots/api#getme |
374
|
|
|
* |
375
|
|
|
* @return \Longman\TelegramBot\Entities\ServerResponse |
376
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
377
|
|
|
*/ |
378
|
|
|
public static function getMe() |
379
|
|
|
{ |
380
|
|
|
// Added fake parameter, because of some cURL version failed POST request without parameters |
381
|
|
|
// see https://github.com/akalongman/php-telegram-bot/pull/228 |
382
|
|
|
return self::send('getMe', ['whoami']); |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* Use this method to send text messages. On success, the sent Message is returned |
387
|
|
|
* |
388
|
|
|
* @link https://core.telegram.org/bots/api#sendmessage |
389
|
|
|
* |
390
|
|
|
* @param array $data |
391
|
|
|
* |
392
|
|
|
* @return \Longman\TelegramBot\Entities\ServerResponse |
393
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
394
|
|
|
*/ |
395
|
5 |
|
public static function sendMessage(array $data) |
396
|
|
|
{ |
397
|
5 |
|
$text = $data['text']; |
398
|
|
|
|
399
|
|
|
do { |
400
|
|
|
//Chop off and send the first message |
401
|
5 |
|
$data['text'] = mb_substr($text, 0, 4096); |
402
|
5 |
|
$response = self::send('sendMessage', $data); |
403
|
|
|
|
404
|
|
|
//Prepare the next message |
405
|
5 |
|
$text = mb_substr($text, 4096); |
406
|
5 |
|
} while (mb_strlen($text, 'UTF-8') > 0); |
407
|
|
|
|
408
|
5 |
|
return $response; |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* Use this method to forward messages of any kind. On success, the sent Message is returned |
413
|
|
|
* |
414
|
|
|
* @link https://core.telegram.org/bots/api#forwardmessage |
415
|
|
|
* |
416
|
|
|
* @param array $data |
417
|
|
|
* |
418
|
|
|
* @return \Longman\TelegramBot\Entities\ServerResponse |
419
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
420
|
|
|
*/ |
421
|
|
|
public static function forwardMessage(array $data) |
422
|
|
|
{ |
423
|
|
|
return self::send('forwardMessage', $data); |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
/** |
427
|
|
|
* Use this method to send photos. On success, the sent Message is returned |
428
|
|
|
* |
429
|
|
|
* @link https://core.telegram.org/bots/api#sendphoto |
430
|
|
|
* |
431
|
|
|
* @param array $data |
432
|
|
|
* @param string $file |
433
|
|
|
* |
434
|
|
|
* @return \Longman\TelegramBot\Entities\ServerResponse |
435
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
436
|
|
|
*/ |
437
|
|
|
public static function sendPhoto(array $data, $file = null) |
438
|
|
|
{ |
439
|
|
|
self::assignEncodedFile($data, 'photo', $file); |
440
|
|
|
|
441
|
|
|
return self::send('sendPhoto', $data); |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* Use this method to send audio files |
446
|
|
|
* |
447
|
|
|
* Your audio must be in the .mp3 format. On success, the sent Message is returned. |
448
|
|
|
* Bots can currently send audio files of up to 50 MB in size, this limit may be changed in the future. |
449
|
|
|
* For sending voice messages, use the sendVoice method instead. |
450
|
|
|
* |
451
|
|
|
* @link https://core.telegram.org/bots/api#sendaudio |
452
|
|
|
* |
453
|
|
|
* @param array $data |
454
|
|
|
* @param string $file |
455
|
|
|
* |
456
|
|
|
* @return \Longman\TelegramBot\Entities\ServerResponse |
457
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
458
|
|
|
*/ |
459
|
|
|
public static function sendAudio(array $data, $file = null) |
460
|
|
|
{ |
461
|
|
|
self::assignEncodedFile($data, 'audio', $file); |
462
|
|
|
|
463
|
|
|
return self::send('sendAudio', $data); |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
/** |
467
|
|
|
* Use this method to send general files. On success, the sent Message is returned. |
468
|
|
|
* |
469
|
|
|
* Bots can currently send files of any type of up to 50 MB in size, this limit may be changed in the future. |
470
|
|
|
* |
471
|
|
|
* @link https://core.telegram.org/bots/api#senddocument |
472
|
|
|
* |
473
|
|
|
* @param array $data |
474
|
|
|
* @param string $file |
475
|
|
|
* |
476
|
|
|
* @return \Longman\TelegramBot\Entities\ServerResponse |
477
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
478
|
|
|
*/ |
479
|
|
|
public static function sendDocument(array $data, $file = null) |
480
|
|
|
{ |
481
|
|
|
self::assignEncodedFile($data, 'document', $file); |
482
|
|
|
|
483
|
|
|
return self::send('sendDocument', $data); |
484
|
|
|
} |
485
|
|
|
|
486
|
|
|
/** |
487
|
|
|
* Use this method to send .webp stickers. On success, the sent Message is returned. |
488
|
|
|
* |
489
|
|
|
* @link https://core.telegram.org/bots/api#sendsticker |
490
|
|
|
* |
491
|
|
|
* @param array $data |
492
|
|
|
* @param string $file |
493
|
|
|
* |
494
|
|
|
* @return \Longman\TelegramBot\Entities\ServerResponse |
495
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
496
|
|
|
*/ |
497
|
|
|
public static function sendSticker(array $data, $file = null) |
498
|
|
|
{ |
499
|
|
|
self::assignEncodedFile($data, 'sticker', $file); |
500
|
|
|
|
501
|
|
|
return self::send('sendSticker', $data); |
502
|
|
|
} |
503
|
|
|
|
504
|
|
|
/** |
505
|
|
|
* Use this method to send video files. On success, the sent Message is returned. |
506
|
|
|
* |
507
|
|
|
* Telegram clients support mp4 videos (other formats may be sent as Document). |
508
|
|
|
* Bots can currently send video files of up to 50 MB in size, this limit may be changed in the future. |
509
|
|
|
* |
510
|
|
|
* @link https://core.telegram.org/bots/api#sendvideo |
511
|
|
|
* |
512
|
|
|
* @param array $data |
513
|
|
|
* @param string $file |
514
|
|
|
* |
515
|
|
|
* @return \Longman\TelegramBot\Entities\ServerResponse |
516
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
517
|
|
|
*/ |
518
|
|
|
public static function sendVideo(array $data, $file = null) |
519
|
|
|
{ |
520
|
|
|
self::assignEncodedFile($data, 'video', $file); |
521
|
|
|
|
522
|
|
|
return self::send('sendVideo', $data); |
523
|
|
|
} |
524
|
|
|
|
525
|
|
|
/** |
526
|
|
|
* Use this method to send audio files. On success, the sent Message is returned. |
527
|
|
|
* |
528
|
|
|
* Telegram clients will display the file as a playable voice message. |
529
|
|
|
* For this to work, your audio must be in an .ogg file encoded with OPUS (other formats may be sent as Audio or Document). |
530
|
|
|
* Bots can currently send voice messages of up to 50 MB in size, this limit may be changed in the future. |
531
|
|
|
* |
532
|
|
|
* @link https://core.telegram.org/bots/api#sendvoice |
533
|
|
|
* |
534
|
|
|
* @param array $data |
535
|
|
|
* @param string $file |
536
|
|
|
* |
537
|
|
|
* @return \Longman\TelegramBot\Entities\ServerResponse |
538
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
539
|
|
|
*/ |
540
|
|
|
public static function sendVoice(array $data, $file = null) |
541
|
|
|
{ |
542
|
|
|
self::assignEncodedFile($data, 'voice', $file); |
543
|
|
|
|
544
|
|
|
return self::send('sendVoice', $data); |
545
|
|
|
} |
546
|
|
|
|
547
|
|
|
/** |
548
|
|
|
* Use this method to send point on the map. On success, the sent Message is returned. |
549
|
|
|
* |
550
|
|
|
* @link https://core.telegram.org/bots/api#sendlocation |
551
|
|
|
* |
552
|
|
|
* @param array $data |
553
|
|
|
* |
554
|
|
|
* @return \Longman\TelegramBot\Entities\ServerResponse |
555
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
556
|
|
|
*/ |
557
|
|
|
public static function sendLocation(array $data) |
558
|
|
|
{ |
559
|
|
|
return self::send('sendLocation', $data); |
560
|
|
|
} |
561
|
|
|
|
562
|
|
|
/** |
563
|
|
|
* Use this method to send information about a venue. On success, the sent Message is returned. |
564
|
|
|
* |
565
|
|
|
* @link https://core.telegram.org/bots/api#sendvenue |
566
|
|
|
* |
567
|
|
|
* @param array $data |
568
|
|
|
* |
569
|
|
|
* @return \Longman\TelegramBot\Entities\ServerResponse |
570
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
571
|
|
|
*/ |
572
|
|
|
public static function sendVenue(array $data) |
573
|
|
|
{ |
574
|
|
|
return self::send('sendVenue', $data); |
575
|
|
|
} |
576
|
|
|
|
577
|
|
|
/** |
578
|
|
|
* Use this method to send phone contacts. On success, the sent Message is returned. |
579
|
|
|
* |
580
|
|
|
* @link https://core.telegram.org/bots/api#sendcontact |
581
|
|
|
* |
582
|
|
|
* @param array $data |
583
|
|
|
* |
584
|
|
|
* @return \Longman\TelegramBot\Entities\ServerResponse |
585
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
586
|
|
|
*/ |
587
|
|
|
public static function sendContact(array $data) |
588
|
|
|
{ |
589
|
|
|
return self::send('sendContact', $data); |
590
|
|
|
} |
591
|
|
|
|
592
|
|
|
/** |
593
|
|
|
* Use this method when you need to tell the user that something is happening on the bot's side. |
594
|
|
|
* |
595
|
|
|
* The status is set for 5 seconds or less. |
596
|
|
|
* (when a message arrives from your bot, Telegram clients clear its typing status) |
597
|
|
|
* |
598
|
|
|
* @link https://core.telegram.org/bots/api#sendchataction |
599
|
|
|
* |
600
|
|
|
* @param array $data |
601
|
|
|
* |
602
|
|
|
* @return \Longman\TelegramBot\Entities\ServerResponse |
603
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
604
|
|
|
*/ |
605
|
|
|
public static function sendChatAction(array $data) |
606
|
|
|
{ |
607
|
|
|
return self::send('sendChatAction', $data); |
608
|
|
|
} |
609
|
|
|
|
610
|
|
|
/** |
611
|
|
|
* Use this method to get a list of profile pictures for a user. Returns a UserProfilePhotos object. |
612
|
|
|
* |
613
|
|
|
* @param array $data |
614
|
|
|
* |
615
|
|
|
* @return \Longman\TelegramBot\Entities\ServerResponse |
616
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
617
|
|
|
*/ |
618
|
|
|
public static function getUserProfilePhotos(array $data) |
619
|
|
|
{ |
620
|
|
|
return self::send('getUserProfilePhotos', $data); |
621
|
|
|
} |
622
|
|
|
|
623
|
|
|
/** |
624
|
|
|
* Use this method to get basic info about a file and prepare it for downloading. On success, a File object is returned. |
625
|
|
|
* |
626
|
|
|
* For the moment, bots can download files of up to 20MB in size. |
627
|
|
|
* The file can then be downloaded via the link https://api.telegram.org/file/bot<token>/<file_path>, |
628
|
|
|
* where <file_path> is taken from the response. |
629
|
|
|
* It is guaranteed that the link will be valid for at least 1 hour. |
630
|
|
|
* When the link expires, a new one can be requested by calling getFile again. |
631
|
|
|
* |
632
|
|
|
* @link https://core.telegram.org/bots/api#getfile |
633
|
|
|
* |
634
|
|
|
* @param array $data |
635
|
|
|
* |
636
|
|
|
* @return \Longman\TelegramBot\Entities\ServerResponse |
637
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
638
|
|
|
*/ |
639
|
|
|
public static function getFile(array $data) |
640
|
|
|
{ |
641
|
|
|
return self::send('getFile', $data); |
642
|
|
|
} |
643
|
|
|
|
644
|
|
|
/** |
645
|
|
|
* Use this method to kick a user from a group or a supergroup. Returns True on success. |
646
|
|
|
* |
647
|
|
|
* In the case of supergroups, the user will not be able to return to the group on their own using invite links, etc., unless unbanned first. |
648
|
|
|
* The bot must be an administrator in the group for this to work. |
649
|
|
|
* |
650
|
|
|
* @link https://core.telegram.org/bots/api#kickchatmember |
651
|
|
|
* |
652
|
|
|
* @param array $data |
653
|
|
|
* |
654
|
|
|
* @return \Longman\TelegramBot\Entities\ServerResponse |
655
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
656
|
|
|
*/ |
657
|
|
|
public static function kickChatMember(array $data) |
658
|
|
|
{ |
659
|
|
|
return self::send('kickChatMember', $data); |
660
|
|
|
} |
661
|
|
|
|
662
|
|
|
/** |
663
|
|
|
* Use this method for your bot to leave a group, supergroup or channel. Returns True on success. |
664
|
|
|
* |
665
|
|
|
* @link https://core.telegram.org/bots/api#leavechat |
666
|
|
|
* |
667
|
|
|
* @param array $data |
668
|
|
|
* |
669
|
|
|
* @return \Longman\TelegramBot\Entities\ServerResponse |
670
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
671
|
|
|
*/ |
672
|
|
|
public static function leaveChat(array $data) |
673
|
|
|
{ |
674
|
|
|
return self::send('leaveChat', $data); |
675
|
|
|
} |
676
|
|
|
|
677
|
|
|
/** |
678
|
|
|
* Use this method to unban a previously kicked user in a supergroup. Returns True on success. |
679
|
|
|
* |
680
|
|
|
* The user will not return to the group automatically, but will be able to join via link, etc. |
681
|
|
|
* The bot must be an administrator in the group for this to work. |
682
|
|
|
* |
683
|
|
|
* @link https://core.telegram.org/bots/api#unbanchatmember |
684
|
|
|
* |
685
|
|
|
* @param array $data |
686
|
|
|
* |
687
|
|
|
* @return \Longman\TelegramBot\Entities\ServerResponse |
688
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
689
|
|
|
*/ |
690
|
|
|
public static function unbanChatMember(array $data) |
691
|
|
|
{ |
692
|
|
|
return self::send('unbanChatMember', $data); |
693
|
|
|
} |
694
|
|
|
|
695
|
|
|
/** |
696
|
|
|
* Use this method to get up to date information about the chat (current name of the user for one-on-one conversations, current username of a user, group or channel, etc.). Returns a Chat object on success. |
697
|
|
|
* |
698
|
|
|
* @todo add get response in ServerResponse.php? |
699
|
|
|
* |
700
|
|
|
* @link https://core.telegram.org/bots/api#getchat |
701
|
|
|
* |
702
|
|
|
* @param array $data |
703
|
|
|
* |
704
|
|
|
* @return \Longman\TelegramBot\Entities\ServerResponse |
705
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
706
|
|
|
*/ |
707
|
|
|
public static function getChat(array $data) |
708
|
|
|
{ |
709
|
|
|
return self::send('getChat', $data); |
710
|
|
|
} |
711
|
|
|
|
712
|
|
|
/** |
713
|
|
|
* Use this method to get a list of administrators in a chat. |
714
|
|
|
* |
715
|
|
|
* On success, returns an Array of ChatMember objects that contains information about all chat administrators except other bots. |
716
|
|
|
* If the chat is a group or a supergroup and no administrators were appointed, only the creator will be returned. |
717
|
|
|
* |
718
|
|
|
* @todo add get response in ServerResponse.php? |
719
|
|
|
* |
720
|
|
|
* @link https://core.telegram.org/bots/api#getchatadministrators |
721
|
|
|
* |
722
|
|
|
* @param array $data |
723
|
|
|
* |
724
|
|
|
* @return \Longman\TelegramBot\Entities\ServerResponse |
725
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
726
|
|
|
*/ |
727
|
|
|
public static function getChatAdministrators(array $data) |
728
|
|
|
{ |
729
|
|
|
return self::send('getChatAdministrators', $data); |
730
|
|
|
} |
731
|
|
|
|
732
|
|
|
/** |
733
|
|
|
* Use this method to get the number of members in a chat. Returns Int on success. |
734
|
|
|
* |
735
|
|
|
* @todo add get response in ServerResponse.php? |
736
|
|
|
* |
737
|
|
|
* @link https://core.telegram.org/bots/api#getchatmemberscount |
738
|
|
|
* |
739
|
|
|
* @param array $data |
740
|
|
|
* |
741
|
|
|
* @return \Longman\TelegramBot\Entities\ServerResponse |
742
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
743
|
|
|
*/ |
744
|
|
|
public static function getChatMembersCount(array $data) |
745
|
|
|
{ |
746
|
|
|
return self::send('getChatMembersCount', $data); |
747
|
|
|
} |
748
|
|
|
|
749
|
|
|
/** |
750
|
|
|
* Use this method to get information about a member of a chat. Returns a ChatMember object on success. |
751
|
|
|
* |
752
|
|
|
* @todo add get response in ServerResponse.php? |
753
|
|
|
* |
754
|
|
|
* @link https://core.telegram.org/bots/api#getchatmember |
755
|
|
|
* |
756
|
|
|
* @param array $data |
757
|
|
|
* |
758
|
|
|
* @return \Longman\TelegramBot\Entities\ServerResponse |
759
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
760
|
|
|
*/ |
761
|
|
|
public static function getChatMember(array $data) |
762
|
|
|
{ |
763
|
|
|
return self::send('getChatMember', $data); |
764
|
|
|
} |
765
|
|
|
|
766
|
|
|
/** |
767
|
|
|
* Use this method to send answers to callback queries sent from inline keyboards. On success, True is returned. |
768
|
|
|
* |
769
|
|
|
* The answer will be displayed to the user as a notification at the top of the chat screen or as an alert. |
770
|
|
|
* |
771
|
|
|
* @link https://core.telegram.org/bots/api#answercallbackquery |
772
|
|
|
* |
773
|
|
|
* @param array $data |
774
|
|
|
* |
775
|
|
|
* @return \Longman\TelegramBot\Entities\ServerResponse |
776
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
777
|
|
|
*/ |
778
|
|
|
public static function answerCallbackQuery(array $data) |
779
|
|
|
{ |
780
|
|
|
return self::send('answerCallbackQuery', $data); |
781
|
|
|
} |
782
|
|
|
|
783
|
|
|
/** |
784
|
|
|
* Get updates |
785
|
|
|
* |
786
|
|
|
* @link https://core.telegram.org/bots/api#getupdates |
787
|
|
|
* |
788
|
|
|
* @param array $data |
789
|
|
|
* |
790
|
|
|
* @return \Longman\TelegramBot\Entities\ServerResponse |
791
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
792
|
|
|
*/ |
793
|
|
|
public static function getUpdates(array $data) |
794
|
|
|
{ |
795
|
|
|
return self::send('getUpdates', $data); |
796
|
|
|
} |
797
|
|
|
|
798
|
|
|
/** |
799
|
|
|
* Set webhook |
800
|
|
|
* |
801
|
|
|
* @link https://core.telegram.org/bots/api#setwebhook |
802
|
|
|
* |
803
|
|
|
* @param string $url |
804
|
|
|
* @param array $data Optional parameters. |
805
|
|
|
* |
806
|
|
|
* @return \Longman\TelegramBot\Entities\ServerResponse |
807
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
808
|
|
|
*/ |
809
|
|
|
public static function setWebhook($url = '', array $data = []) |
810
|
|
|
{ |
811
|
|
|
$data = array_intersect_key($data, array_flip([ |
812
|
|
|
'certificate', |
813
|
|
|
'max_connections', |
814
|
|
|
'allowed_updates', |
815
|
|
|
])); |
816
|
|
|
$data['url'] = $url; |
817
|
|
|
|
818
|
|
|
if (isset($data['certificate'])) { |
819
|
|
|
self::assignEncodedFile($data, 'certificate', $data['certificate']); |
820
|
|
|
} |
821
|
|
|
|
822
|
|
|
return self::send('setWebhook', $data); |
823
|
|
|
} |
824
|
|
|
|
825
|
|
|
/** |
826
|
|
|
* Delete webhook |
827
|
|
|
* |
828
|
|
|
* @link https://core.telegram.org/bots/api#deletewebhook |
829
|
|
|
* |
830
|
|
|
* @return \Longman\TelegramBot\Entities\ServerResponse |
831
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
832
|
|
|
*/ |
833
|
|
|
public static function deleteWebhook() |
834
|
|
|
{ |
835
|
|
|
// Must send some arbitrary data for this to work for now... |
836
|
|
|
return self::send('deleteWebhook', ['delete']); |
837
|
|
|
} |
838
|
|
|
|
839
|
|
|
/** |
840
|
|
|
* Use this method to edit text and game messages sent by the bot or via the bot (for inline bots). |
841
|
|
|
* |
842
|
|
|
* On success, if edited message is sent by the bot, the edited Message is returned, otherwise True is returned. |
843
|
|
|
* |
844
|
|
|
* @link https://core.telegram.org/bots/api#editmessagetext |
845
|
|
|
* |
846
|
|
|
* @param array $data |
847
|
|
|
* |
848
|
|
|
* @return \Longman\TelegramBot\Entities\ServerResponse |
849
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
850
|
|
|
*/ |
851
|
|
|
public static function editMessageText(array $data) |
852
|
|
|
{ |
853
|
|
|
return self::send('editMessageText', $data); |
854
|
|
|
} |
855
|
|
|
|
856
|
|
|
/** |
857
|
|
|
* Use this method to edit captions of messages sent by the bot or via the bot (for inline bots). |
858
|
|
|
* |
859
|
|
|
* On success, if edited message is sent by the bot, the edited Message is returned, otherwise True is returned. |
860
|
|
|
* |
861
|
|
|
* @link https://core.telegram.org/bots/api#editmessagecaption |
862
|
|
|
* |
863
|
|
|
* @param array $data |
864
|
|
|
* |
865
|
|
|
* @return \Longman\TelegramBot\Entities\ServerResponse |
866
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
867
|
|
|
*/ |
868
|
|
|
public static function editMessageCaption(array $data) |
869
|
|
|
{ |
870
|
|
|
return self::send('editMessageCaption', $data); |
871
|
|
|
} |
872
|
|
|
|
873
|
|
|
/** |
874
|
|
|
* Use this method to edit only the reply markup of messages sent by the bot or via the bot (for inline bots). |
875
|
|
|
* |
876
|
|
|
* On success, if edited message is sent by the bot, the edited Message is returned, otherwise True is returned. |
877
|
|
|
* |
878
|
|
|
* @link https://core.telegram.org/bots/api#editmessagereplymarkup |
879
|
|
|
* |
880
|
|
|
* @param array $data |
881
|
|
|
* |
882
|
|
|
* @return \Longman\TelegramBot\Entities\ServerResponse |
883
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
884
|
|
|
*/ |
885
|
|
|
public static function editMessageReplyMarkup(array $data) |
886
|
|
|
{ |
887
|
|
|
return self::send('editMessageReplyMarkup', $data); |
888
|
|
|
} |
889
|
|
|
|
890
|
|
|
/** |
891
|
|
|
* Use this method to send answers to an inline query. On success, True is returned. |
892
|
|
|
* |
893
|
|
|
* No more than 50 results per query are allowed. |
894
|
|
|
* |
895
|
|
|
* @link https://core.telegram.org/bots/api#answerinlinequery |
896
|
|
|
* |
897
|
|
|
* @param array $data |
898
|
|
|
* |
899
|
|
|
* @return \Longman\TelegramBot\Entities\ServerResponse |
900
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
901
|
|
|
*/ |
902
|
|
|
public static function answerInlineQuery(array $data) |
903
|
|
|
{ |
904
|
|
|
return self::send('answerInlineQuery', $data); |
905
|
|
|
} |
906
|
|
|
|
907
|
|
|
/** |
908
|
|
|
* Return an empty Server Response |
909
|
|
|
* |
910
|
|
|
* No request to telegram are sent, this function is used in commands that |
911
|
|
|
* don't need to fire a message after execution |
912
|
|
|
* |
913
|
|
|
* @return \Longman\TelegramBot\Entities\ServerResponse |
914
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
915
|
|
|
*/ |
916
|
|
|
public static function emptyResponse() |
917
|
|
|
{ |
918
|
|
|
return new ServerResponse(['ok' => true, 'result' => true], null); |
919
|
|
|
} |
920
|
|
|
|
921
|
|
|
/** |
922
|
|
|
* Send message to all active chats |
923
|
|
|
* |
924
|
|
|
* @param string $callback_function |
925
|
|
|
* @param array $data |
926
|
|
|
* @param boolean $send_groups |
927
|
|
|
* @param boolean $send_super_groups |
928
|
|
|
* @param boolean $send_users |
929
|
|
|
* @param string $date_from |
930
|
|
|
* @param string $date_to |
931
|
|
|
* |
932
|
|
|
* @return array |
933
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
934
|
|
|
*/ |
935
|
|
|
public static function sendToActiveChats( |
936
|
|
|
$callback_function, |
937
|
|
|
array $data, |
938
|
|
|
$send_groups = true, |
939
|
|
|
$send_super_groups = true, |
940
|
|
|
$send_users = true, |
941
|
|
|
$date_from = null, |
942
|
|
|
$date_to = null |
943
|
|
|
) { |
944
|
|
|
$callback_path = __NAMESPACE__ . '\Request'; |
945
|
|
|
if (!method_exists($callback_path, $callback_function)) { |
946
|
|
|
throw new TelegramException('Method "' . $callback_function . '" not found in class Request.'); |
947
|
|
|
} |
948
|
|
|
|
949
|
|
|
$chats = DB::selectChats($send_groups, $send_super_groups, $send_users, $date_from, $date_to); |
950
|
|
|
|
951
|
|
|
$results = []; |
952
|
|
|
if (is_array($chats)) { |
953
|
|
|
foreach ($chats as $row) { |
954
|
|
|
$data['chat_id'] = $row['chat_id']; |
955
|
|
|
$results[] = call_user_func_array($callback_path . '::' . $callback_function, [$data]); |
956
|
|
|
} |
957
|
|
|
} |
958
|
|
|
|
959
|
|
|
return $results; |
960
|
|
|
} |
961
|
|
|
|
962
|
|
|
/** |
963
|
|
|
* Use this method to get current webhook status. |
964
|
|
|
* |
965
|
|
|
* @link https://core.telegram.org/bots/api#getwebhookinfo |
966
|
|
|
* |
967
|
|
|
* @return Entities\ServerResponse |
968
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
969
|
|
|
*/ |
970
|
|
|
public static function getWebhookInfo() |
971
|
|
|
{ |
972
|
|
|
// Must send some arbitrary data for this to work for now... |
973
|
|
|
return self::send('getWebhookInfo', ['info']); |
974
|
|
|
} |
975
|
|
|
} |
976
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: