-
-
Notifications
You must be signed in to change notification settings - Fork 959
/
Copy pathRequest.php
900 lines (805 loc) · 44.4 KB
/
Request.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
<?php
/**
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Stream;
use Longman\TelegramBot\Entities\File;
use Longman\TelegramBot\Entities\InputMedia\InputMedia;
use Longman\TelegramBot\Entities\Message;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Exception\InvalidBotTokenException;
use Longman\TelegramBot\Exception\TelegramException;
use Throwable;
/**
* Class Request
*
* @method static ServerResponse getUpdates(array $data) Use this method to receive incoming updates using long polling (wiki). An Array of Update objects is returned.
* @method static ServerResponse setWebhook(array $data) Use this method to specify a url and receive incoming updates via an outgoing webhook. Whenever there is an update for the bot, we will send an HTTPS POST request to the specified url, containing a JSON-serialized Update. In case of an unsuccessful request, we will give up after a reasonable amount of attempts. Returns true.
* @method static ServerResponse deleteWebhook() Use this method to remove webhook integration if you decide to switch back to getUpdates. Returns True on success. Requires no parameters.
* @method static ServerResponse getWebhookInfo() Use this method to get current webhook status. Requires no parameters. On success, returns a WebhookInfo object. If the bot is using getUpdates, will return an object with the url field empty.
* @method static ServerResponse getMe() A simple method for testing your bot's auth token. Requires no parameters. Returns basic information about the bot in form of a User object.
* @method static ServerResponse logOut() Use this method to log out from the cloud Bot API server before launching the bot locally. Requires no parameters. Returns True on success.
* @method static ServerResponse close() Use this method to close the bot instance before moving it from one local server to another. Requires no parameters. Returns True on success.
* @method static ServerResponse forwardMessage(array $data) Use this method to forward messages of any kind. On success, the sent Message is returned.
* @method static ServerResponse copyMessage(array $data) Use this method to copy messages of any kind. The method is analogous to the method forwardMessages, but the copied message doesn't have a link to the original message. Returns the MessageId of the sent message on success.
* @method static ServerResponse sendPhoto(array $data) Use this method to send photos. On success, the sent Message is returned.
* @method static ServerResponse sendAudio(array $data) Use this method to send audio files, if you want Telegram clients to display them in the music player. Your audio must be in the .mp3 format. On success, the sent Message is returned. Bots can currently send audio files of up to 50 MB in size, this limit may be changed in the future.
* @method static ServerResponse sendDocument(array $data) Use this method to send general files. On success, the sent Message is returned. Bots can currently send files of any type of up to 50 MB in size, this limit may be changed in the future.
* @method static ServerResponse sendSticker(array $data) Use this method to send .webp stickers. On success, the sent Message is returned.
* @method static ServerResponse sendVideo(array $data) Use this method to send video files, Telegram clients support mp4 videos (other formats may be sent as Document). On success, the sent Message is returned. Bots can currently send video files of up to 50 MB in size, this limit may be changed in the future.
* @method static ServerResponse sendAnimation(array $data) Use this method to send animation files (GIF or H.264/MPEG-4 AVC video without sound). On success, the sent Message is returned. Bots can currently send animation files of up to 50 MB in size, this limit may be changed in the future.
* @method static ServerResponse sendVoice(array $data) Use this method to send audio files, if you want Telegram clients to display the file as a playable voice message. For this to work, your audio must be in an .ogg file encoded with OPUS (other formats may be sent as Audio or Document). On success, the sent Message is returned. Bots can currently send voice messages of up to 50 MB in size, this limit may be changed in the future.
* @method static ServerResponse sendVideoNote(array $data) Use this method to send video messages. On success, the sent Message is returned.
* @method static ServerResponse sendMediaGroup(array $data) Use this method to send a group of photos or videos as an album. On success, an array of the sent Messages is returned.
* @method static ServerResponse sendLocation(array $data) Use this method to send point on the map. On success, the sent Message is returned.
* @method static ServerResponse editMessageLiveLocation(array $data) Use this method to edit live location messages sent by the bot or via the bot (for inline bots). A location can be edited until its live_period expires or editing is explicitly disabled by a call to stopMessageLiveLocation. On success, if the edited message was sent by the bot, the edited Message is returned, otherwise True is returned.
* @method static ServerResponse stopMessageLiveLocation(array $data) Use this method to stop updating a live location message sent by the bot or via the bot (for inline bots) before live_period expires. On success, if the message was sent by the bot, the sent Message is returned, otherwise True is returned.
* @method static ServerResponse sendVenue(array $data) Use this method to send information about a venue. On success, the sent Message is returned.
* @method static ServerResponse sendContact(array $data) Use this method to send phone contacts. On success, the sent Message is returned.
* @method static ServerResponse sendPoll(array $data) Use this method to send a native poll. A native poll can't be sent to a private chat. On success, the sent Message is returned.
* @method static ServerResponse sendDice(array $data) Use this method to send a dice, which will have a random value from 1 to 6. On success, the sent Message is returned.
* @method static ServerResponse sendChatAction(array $data) Use this method when you need to tell the user that something is happening on the bot's side. The status is set for 5 seconds or less (when a message arrives from your bot, Telegram clients clear its typing status). Returns True on success.
* @method static ServerResponse getUserProfilePhotos(array $data) Use this method to get a list of profile pictures for a user. Returns a UserProfilePhotos object.
* @method static ServerResponse getFile(array $data) Use this method to get basic info about a file and prepare it for downloading. For the moment, bots can download files of up to 20MB in size. On success, a File object is returned. The file can then be downloaded via the link https://api.telegram.org/file/bot<token>/<file_path>, where <file_path> is taken from the response. It is guaranteed that the link will be valid for at least 1 hour. When the link expires, a new one can be requested by calling getFile again.
* @method static ServerResponse kickChatMember(array $data) Use this method to kick a user from a group, a supergroup or a channel. In the case of supergroups and channels, the user will not be able to return to the group on their own using invite links, etc., unless unbanned first. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. Returns True on success.
* @method static ServerResponse unbanChatMember(array $data) Use this method to unban a previously kicked user in a supergroup or channel. The user will not return to the group or channel automatically, but will be able to join via link, etc. The bot must be an administrator for this to work. Returns True on success.
* @method static ServerResponse restrictChatMember(array $data) Use this method to restrict a user in a supergroup. The bot must be an administrator in the supergroup for this to work and must have the appropriate admin rights. Pass True for all permissions to lift restrictions from a user. Returns True on success.
* @method static ServerResponse promoteChatMember(array $data) Use this method to promote or demote a user in a supergroup or a channel. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. Pass False for all boolean parameters to demote a user. Returns True on success.
* @method static ServerResponse setChatAdministratorCustomTitle(array $data) Use this method to set a custom title for an administrator in a supergroup promoted by the bot. Returns True on success.
* @method static ServerResponse setChatPermissions(array $data) Use this method to set default chat permissions for all members. The bot must be an administrator in the group or a supergroup for this to work and must have the can_restrict_members admin rights. Returns True on success.
* @method static ServerResponse exportChatInviteLink(array $data) Use this method to generate a new invite link for a chat. Any previously generated link is revoked. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. Returns the new invite link as String on success.
* @method static ServerResponse setChatPhoto(array $data) Use this method to set a new profile photo for the chat. Photos can't be changed for private chats. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. Returns True on success.
* @method static ServerResponse deleteChatPhoto(array $data) Use this method to delete a chat photo. Photos can't be changed for private chats. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. Returns True on success.
* @method static ServerResponse setChatTitle(array $data) Use this method to change the title of a chat. Titles can't be changed for private chats. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. Returns True on success.
* @method static ServerResponse setChatDescription(array $data) Use this method to change the description of a group, a supergroup or a channel. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. Returns True on success.
* @method static ServerResponse pinChatMessage(array $data) Use this method to pin a message in a supergroup or a channel. The bot must be an administrator in the chat for this to work and must have the ‘can_pin_messages’ admin right in the supergroup or ‘can_edit_messages’ admin right in the channel. Returns True on success.
* @method static ServerResponse unpinChatMessage(array $data) Use this method to unpin a message in a supergroup or a channel. The bot must be an administrator in the chat for this to work and must have the ‘can_pin_messages’ admin right in the supergroup or ‘can_edit_messages’ admin right in the channel. Returns True on success.
* @method static ServerResponse unpinAllChatMessages(array $data) Use this method to clear the list of pinned messages in a chat. If the chat is not a private chat, the bot must be an administrator in the chat for this to work and must have the 'can_pin_messages' admin right in a supergroup or 'can_edit_messages' admin right in a channel. Returns True on success.
* @method static ServerResponse leaveChat(array $data) Use this method for your bot to leave a group, supergroup or channel. Returns True on success.
* @method static ServerResponse getChat(array $data) 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.
* @method static ServerResponse getChatAdministrators(array $data) Use this method to get a list of administrators in a chat. On success, returns an Array of ChatMember objects that contains information about all chat administrators except other bots. If the chat is a group or a supergroup and no administrators were appointed, only the creator will be returned.
* @method static ServerResponse getChatMembersCount(array $data) Use this method to get the number of members in a chat. Returns Int on success.
* @method static ServerResponse getChatMember(array $data) Use this method to get information about a member of a chat. Returns a ChatMember object on success.
* @method static ServerResponse setChatStickerSet(array $data) Use this method to set a new group sticker set for a supergroup. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. Use the field can_set_sticker_set optionally returned in getChat requests to check if the bot can use this method. Returns True on success.
* @method static ServerResponse deleteChatStickerSet(array $data) Use this method to delete a group sticker set from a supergroup. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. Use the field can_set_sticker_set optionally returned in getChat requests to check if the bot can use this method. Returns True on success.
* @method static ServerResponse answerCallbackQuery(array $data) Use this method to send answers to callback queries sent from inline keyboards. The answer will be displayed to the user as a notification at the top of the chat screen or as an alert. On success, True is returned.
* @method static ServerResponse answerInlineQuery(array $data) Use this method to send answers to an inline query. On success, True is returned.
* @method static ServerResponse setMyCommands(array $data) Use this method to change the list of the bot's commands. Returns True on success.
* @method static ServerResponse getMyCommands() Use this method to get the current list of the bot's commands. Requires no parameters. Returns Array of BotCommand on success.
* @method static ServerResponse editMessageText(array $data) Use this method to edit text and game messages sent by the bot or via the bot (for inline bots). On success, if edited message is sent by the bot, the edited Message is returned, otherwise True is returned.
* @method static ServerResponse editMessageCaption(array $data) Use this method to edit captions of messages sent by the bot or via the bot (for inline bots). On success, if edited message is sent by the bot, the edited Message is returned, otherwise True is returned.
* @method static ServerResponse editMessageMedia(array $data) Use this method to edit audio, document, photo, or video messages. On success, if the edited message was sent by the bot, the edited Message is returned, otherwise True is returned.
* @method static ServerResponse editMessageReplyMarkup(array $data) Use this method to edit only the reply markup of messages sent by the bot or via the bot (for inline bots). On success, if edited message is sent by the bot, the edited Message is returned, otherwise True is returned.
* @method static ServerResponse stopPoll(array $data) Use this method to stop a poll which was sent by the bot. On success, the stopped Poll with the final results is returned.
* @method static ServerResponse deleteMessage(array $data) Use this method to delete a message, including service messages, with certain limitations. Returns True on success.
* @method static ServerResponse getStickerSet(array $data) Use this method to get a sticker set. On success, a StickerSet object is returned.
* @method static ServerResponse uploadStickerFile(array $data) Use this method to upload a .png file with a sticker for later use in createNewStickerSet and addStickerToSet methods (can be used multiple times). Returns the uploaded File on success.
* @method static ServerResponse createNewStickerSet(array $data) Use this method to create new sticker set owned by a user. The bot will be able to edit the created sticker set. Returns True on success.
* @method static ServerResponse addStickerToSet(array $data) Use this method to add a new sticker to a set created by the bot. Returns True on success.
* @method static ServerResponse setStickerPositionInSet(array $data) Use this method to move a sticker in a set created by the bot to a specific position. Returns True on success.
* @method static ServerResponse deleteStickerFromSet(array $data) Use this method to delete a sticker from a set created by the bot. Returns True on success.
* @method static ServerResponse setStickerSetThumb(array $data) Use this method to set the thumbnail of a sticker set. Animated thumbnails can be set for animated sticker sets only. Returns True on success.
* @method static ServerResponse sendInvoice(array $data) Use this method to send invoices. On success, the sent Message is returned.
* @method static ServerResponse answerShippingQuery(array $data) If you sent an invoice requesting a shipping address and the parameter is_flexible was specified, the Bot API will send an Update with a shipping_query field to the bot. Use this method to reply to shipping queries. On success, True is returned.
* @method static ServerResponse answerPreCheckoutQuery(array $data) Once the user has confirmed their payment and shipping details, the Bot API sends the final confirmation in the form of an Update with the field pre_checkout_query. Use this method to respond to such pre-checkout queries. On success, True is returned.
* @method static ServerResponse setPassportDataErrors(array $data) Informs a user that some of the Telegram Passport elements they provided contains errors. The user will not be able to re-submit their Passport to you until the errors are fixed (the contents of the field for which you returned the error must change). Returns True on success. Use this if the data submitted by the user doesn't satisfy the standards your service requires for any reason. For example, if a birthday date seems invalid, a submitted document is blurry, a scan shows evidence of tampering, etc. Supply some details in the error message to make sure the user knows how to correct the issues.
* @method static ServerResponse sendGame(array $data) Use this method to send a game. On success, the sent Message is returned.
* @method static ServerResponse setGameScore(array $data) Use this method to set the score of the specified user in a game. On success, if the message was sent by the bot, returns the edited Message, otherwise returns True. Returns an error, if the new score is not greater than the user's current score in the chat and force is False.
* @method static ServerResponse getGameHighScores(array $data) Use this method to get data for high score tables. Will return the score of the specified user and several of his neighbors in a game. On success, returns an Array of GameHighScore objects.
*/
class Request
{
/**
* Telegram object
*
* @var Telegram
*/
private static $telegram;
/**
* URI of the Telegram API
*
* @var string
*/
private static $api_base_uri = 'https://api.telegram.org';
/**
* Guzzle Client object
*
* @var ClientInterface
*/
private static $client;
/**
* Request limiter
*
* @var bool
*/
private static $limiter_enabled;
/**
* Request limiter's interval between checks
*
* @var float
*/
private static $limiter_interval;
/**
* The current action that is being executed
*
* @var string
*/
private static $current_action = '';
/**
* Available actions to send
*
* This is basically the list of all methods listed on the official API documentation.
*
* @link https://core.telegram.org/bots/api
*
* @var array
*/
private static $actions = [
'getUpdates',
'setWebhook',
'deleteWebhook',
'getWebhookInfo',
'getMe',
'logOut',
'close',
'sendMessage',
'forwardMessage',
'copyMessage',
'sendPhoto',
'sendAudio',
'sendDocument',
'sendSticker',
'sendVideo',
'sendAnimation',
'sendVoice',
'sendVideoNote',
'sendMediaGroup',
'sendLocation',
'editMessageLiveLocation',
'stopMessageLiveLocation',
'sendVenue',
'sendContact',
'sendPoll',
'sendDice',
'sendChatAction',
'getUserProfilePhotos',
'getFile',
'kickChatMember',
'unbanChatMember',
'restrictChatMember',
'promoteChatMember',
'setChatAdministratorCustomTitle',
'setChatPermissions',
'exportChatInviteLink',
'setChatPhoto',
'deleteChatPhoto',
'setChatTitle',
'setChatDescription',
'pinChatMessage',
'unpinChatMessage',
'unpinAllChatMessages',
'leaveChat',
'getChat',
'getChatAdministrators',
'getChatMembersCount',
'getChatMember',
'setChatStickerSet',
'deleteChatStickerSet',
'answerCallbackQuery',
'answerInlineQuery',
'setMyCommands',
'getMyCommands',
'editMessageText',
'editMessageCaption',
'editMessageMedia',
'editMessageReplyMarkup',
'stopPoll',
'deleteMessage',
'getStickerSet',
'uploadStickerFile',
'createNewStickerSet',
'addStickerToSet',
'setStickerPositionInSet',
'deleteStickerFromSet',
'setStickerSetThumb',
'sendInvoice',
'answerShippingQuery',
'answerPreCheckoutQuery',
'setPassportDataErrors',
'sendGame',
'setGameScore',
'getGameHighScores',
];
/**
* Some methods need a dummy param due to certain cURL issues.
*
* @see Request::addDummyParamIfNecessary()
*
* @var array
*/
private static $actions_need_dummy_param = [
'deleteWebhook',
'getWebhookInfo',
'getMe',
'logOut',
'close',
'getMyCommands',
];
/**
* Available fields for InputFile helper
*
* This is basically the list of all fields that allow InputFile objects
* for which input can be simplified by providing local path directly as string.
*
* @var array
*/
private static $input_file_fields = [
'setWebhook' => ['certificate'],
'sendPhoto' => ['photo'],
'sendAudio' => ['audio', 'thumb'],
'sendDocument' => ['document', 'thumb'],
'sendVideo' => ['video', 'thumb'],
'sendAnimation' => ['animation', 'thumb'],
'sendVoice' => ['voice', 'thumb'],
'sendVideoNote' => ['video_note', 'thumb'],
'setChatPhoto' => ['photo'],
'sendSticker' => ['sticker'],
'uploadStickerFile' => ['png_sticker'],
'createNewStickerSet' => ['png_sticker', 'tgs_sticker'],
'addStickerToSet' => ['png_sticker', 'tgs_sticker'],
'setStickerSetThumb' => ['thumb'],
];
/**
* Initialize
*
* @param Telegram $telegram
*/
public static function initialize(Telegram $telegram): void
{
self::$telegram = $telegram;
self::setClient(self::$client ?: new Client(['base_uri' => self::$api_base_uri]));
}
/**
* Set a custom Guzzle HTTP Client object
*
* @param ClientInterface $client
*/
public static function setClient(ClientInterface $client): void
{
self::$client = $client;
}
/**
* Set input from custom input or stdin and return it
*
* @return string
*/
public static function getInput(): string
{
// First check if a custom input has been set, else get the PHP input.
$input = self::$telegram->getCustomInput();
if (empty($input)) {
$input = file_get_contents('php://input');
}
TelegramLog::update($input);
return $input;
}
/**
* Generate general fake server response
*
* @param array $data Data to add to fake response
*
* @return array Fake response data
*/
public static function generateGeneralFakeServerResponse(array $data = []): array
{
//PARAM BINDED IN PHPUNIT TEST FOR TestServerResponse.php
//Maybe this is not the best possible implementation
//No value set in $data ie testing setWebhook
//Provided $data['chat_id'] ie testing sendMessage
$fake_response = ['ok' => true]; // :)
if ($data === []) {
$fake_response['result'] = true;
}
//some data to initialize the class method SendMessage
if (isset($data['chat_id'])) {
$data['message_id'] = '1234';
$data['date'] = '1441378360';
$data['from'] = [
'id' => 123456789,
'first_name' => 'botname',
'username' => 'namebot',
];
$data['chat'] = ['id' => $data['chat_id']];
$fake_response['result'] = $data;
}
return $fake_response;
}
/**
* Properly set up the request params
*
* If any item of the array is a resource, reformat it to a multipart request.
* Else, just return the passed data as form params.
*
* @param array $data
*
* @return array
* @throws TelegramException
*/
private static function setUpRequestParams(array $data): array
{
$has_resource = false;
$multipart = [];
foreach ($data as $key => &$item) {
if ($key === 'media') {
// Magical media input helper.
$item = self::mediaInputHelper($item, $has_resource, $multipart);
} elseif (array_key_exists(self::$current_action, self::$input_file_fields) && in_array($key, self::$input_file_fields[self::$current_action], true)) {
// Allow absolute paths to local files.
if (is_string($item) && file_exists($item)) {
$item = new Stream(self::encodeFile($item));
}
} elseif (is_array($item) || is_object($item)) {
// Convert any nested arrays or objects into JSON strings.
$item = json_encode($item);
}
// Reformat data array in multipart way if it contains a resource
$has_resource = $has_resource || is_resource($item) || $item instanceof Stream;
$multipart[] = ['name' => $key, 'contents' => $item];
}
unset($item);
if ($has_resource) {
return ['multipart' => $multipart];
}
return ['form_params' => $data];
}
/**
* Magical input media helper to simplify passing media.
*
* This allows the following:
* Request::editMessageMedia([
* ...
* 'media' => new InputMediaPhoto([
* 'caption' => 'Caption!',
* 'media' => Request::encodeFile($local_photo),
* ]),
* ]);
* and
* Request::sendMediaGroup([
* 'media' => [
* new InputMediaPhoto(['media' => Request::encodeFile($local_photo_1)]),
* new InputMediaPhoto(['media' => Request::encodeFile($local_photo_2)]),
* new InputMediaVideo(['media' => Request::encodeFile($local_video_1)]),
* ],
* ]);
* and even
* Request::sendMediaGroup([
* 'media' => [
* new InputMediaPhoto(['media' => $local_photo_1]),
* new InputMediaPhoto(['media' => $local_photo_2]),
* new InputMediaVideo(['media' => $local_video_1]),
* ],
* ]);
*
* @param mixed $item
* @param bool $has_resource
* @param array $multipart
*
* @return mixed
* @throws TelegramException
*/
private static function mediaInputHelper($item, bool &$has_resource, array &$multipart)
{
$was_array = is_array($item);
$was_array || $item = [$item];
/** @var InputMedia|null $media_item */
foreach ($item as $media_item) {
if (!($media_item instanceof InputMedia)) {
continue;
}
// Make a list of all possible media that can be handled by the helper.
$possible_medias = array_filter([
'media' => $media_item->getMedia(),
'thumb' => $media_item->getThumb(),
]);
foreach ($possible_medias as $type => $media) {
// Allow absolute paths to local files.
if (is_string($media) && strpos($media, 'attach://') !== 0 && file_exists($media)) {
$media = new Stream(self::encodeFile($media));
}
if (is_resource($media) || $media instanceof Stream) {
$has_resource = true;
$unique_key = uniqid($type . '_', false);
$multipart[] = ['name' => $unique_key, 'contents' => $media];
// We're literally overwriting the passed media type data!
$media_item->$type = 'attach://' . $unique_key;
$media_item->raw_data[$type] = 'attach://' . $unique_key;
}
}
}
$was_array || $item = reset($item);
return json_encode($item);
}
/**
* Get the current action that's being executed
*
* @return string
*/
public static function getCurrentAction(): string
{
return self::$current_action;
}
/**
* Execute HTTP Request
*
* @param string $action Action to execute
* @param array $data Data to attach to the execution
*
* @return string Result of the HTTP Request
* @throws TelegramException
*/
public static function execute(string $action, array $data = []): string
{
$request_params = self::setUpRequestParams($data);
$request_params['debug'] = TelegramLog::getDebugLogTempStream();
try {
$response = self::$client->post(
'/bot' . self::$telegram->getApiKey() . '/' . $action,
$request_params
);
$result = (string) $response->getBody();
//Logging getUpdates Update
if ($action === 'getUpdates') {
TelegramLog::update($result);
}
} catch (RequestException $e) {
$response = null;
$result = $e->getResponse() ? (string) $e->getResponse()->getBody() : '';
}
//Logging verbose debug output
if (TelegramLog::$always_log_request_and_response || $response === null) {
TelegramLog::debug('Request data:' . PHP_EOL . print_r($data, true));
TelegramLog::debug('Response data:' . PHP_EOL . $result);
TelegramLog::endDebugLogTempStream('Verbose HTTP Request output:' . PHP_EOL . '%s' . PHP_EOL);
}
return $result;
}
/**
* Download file
*
* @param File $file
*
* @return bool
* @throws TelegramException
*/
public static function downloadFile(File $file): bool
{
if (empty($download_path = self::$telegram->getDownloadPath())) {
throw new TelegramException('Download path not set!');
}
$tg_file_path = $file->getFilePath();
$file_path = $download_path . '/' . $tg_file_path;
$file_dir = dirname($file_path);
//For safety reasons, first try to create the directory, then check that it exists.
//This is in case some other process has created the folder in the meantime.
if (!@mkdir($file_dir, 0755, true) && !is_dir($file_dir)) {
throw new TelegramException('Directory ' . $file_dir . ' can\'t be created');
}
$debug_handle = TelegramLog::getDebugLogTempStream();
try {
self::$client->get(
'/file/bot' . self::$telegram->getApiKey() . '/' . $tg_file_path,
['debug' => $debug_handle, 'sink' => $file_path]
);
return filesize($file_path) > 0;
} catch (Throwable $e) {
return false;
} finally {
//Logging verbose debug output
TelegramLog::endDebugLogTempStream('Verbose HTTP File Download Request output:' . PHP_EOL . '%s' . PHP_EOL);
}
}
/**
* Encode file
*
* @param string $file
*
* @return resource
* @throws TelegramException
*/
public static function encodeFile(string $file)
{
$fp = fopen($file, 'rb');
if ($fp === false) {
throw new TelegramException('Cannot open "' . $file . '" for reading');
}
return $fp;
}
/**
* Send command
*
* @todo Fake response doesn't need json encoding?
* @todo Write debug entry on failure
*
* @param string $action
* @param array $data
*
* @return ServerResponse
* @throws TelegramException
*/
public static function send(string $action, array $data = []): ServerResponse
{
self::ensureValidAction($action);
self::addDummyParamIfNecessary($action, $data);
$bot_username = self::$telegram->getBotUsername();
if (defined('PHPUNIT_TESTSUITE')) {
$fake_response = self::generateGeneralFakeServerResponse($data);
return new ServerResponse($fake_response, $bot_username);
}
self::ensureNonEmptyData($data);
self::limitTelegramRequests($action, $data);
// Remember which action is currently being executed.
self::$current_action = $action;
$raw_response = self::execute($action, $data);
$response = json_decode($raw_response, true);
if (null === $response) {
TelegramLog::debug($raw_response);
throw new TelegramException('Telegram returned an invalid response!');
}
$response = new ServerResponse($response, $bot_username);
if (!$response->isOk() && $response->getErrorCode() === 401 && $response->getDescription() === 'Unauthorized') {
throw new InvalidBotTokenException();
}
// Special case for sent polls, which need to be saved specially.
// @todo Take into account if DB gets extracted into separate module.
if ($response->isOk() && ($message = $response->getResult()) && ($message instanceof Message) && $poll = $message->getPoll()) {
DB::insertPollRequest($poll);
}
// Reset current action after completion.
self::$current_action = '';
return $response;
}
/**
* Add a dummy parameter if the passed action requires it.
*
* If a method doesn't require parameters, we need to add a dummy one anyway,
* because of some cURL version failed POST request without parameters.
*
* @link https://github.com/php-telegram-bot/core/pull/228
*
* @todo Would be nice to find a better solution for this!
*
* @param string $action
* @param array $data
*/
protected static function addDummyParamIfNecessary(string $action, array &$data): void
{
if (in_array($action, self::$actions_need_dummy_param, true)) {
// Can be anything, using a single letter to minimise request size.
$data = ['d'];
}
}
/**
* Make sure the data isn't empty, else throw an exception
*
* @param array $data
*
* @throws TelegramException
*/
private static function ensureNonEmptyData(array $data): void
{
if (count($data) === 0) {
throw new TelegramException('Data is empty!');
}
}
/**
* Make sure the action is valid, else throw an exception
*
* @param string $action
*
* @throws TelegramException
*/
private static function ensureValidAction(string $action): void
{
if (!in_array($action, self::$actions, true)) {
throw new TelegramException('The action "' . $action . '" doesn\'t exist!');
}
}
/**
* Use this method to send text messages. On success, the last sent Message is returned
*
* All message responses are saved in `$extras['responses']`.
* Custom encoding can be defined in `$extras['encoding']` (default: `mb_internal_encoding()`)
* Custom splitting can be defined in `$extras['split']` (default: 4096)
* `$extras['split'] = null;` // force to not split message at all!
* `$extras['split'] = 200;` // split message into 200 character chunks
*
* @link https://core.telegram.org/bots/api#sendmessage
*
* @todo Splitting formatted text may break the message.
*
* @param array $data
* @param array|null $extras
*
* @return ServerResponse
* @throws TelegramException
*/
public static function sendMessage(array $data, ?array &$extras = []): ServerResponse
{
$extras = array_merge([
'split' => 4096,
'encoding' => mb_internal_encoding(),
], (array) $extras);
$text = $data['text'];
$encoding = $extras['encoding'];
$max_length = $extras['split'] ?: mb_strlen($text, $encoding);
$responses = [];
do {
// Chop off and send the first message.
$data['text'] = mb_substr($text, 0, $max_length, $encoding);
$responses[] = self::send('sendMessage', $data);
// Prepare the next message.
$text = mb_substr($text, $max_length, null, $encoding);
} while ($text !== '');
// Add all response objects to referenced variable.
$extras['responses'] = $responses;
return end($responses);
}
/**
* Any statically called method should be relayed to the `send` method.
*
* @param string $action
* @param array $data
*
* @return ServerResponse
* @throws TelegramException
*/
public static function __callStatic(string $action, array $data): ServerResponse
{
// Only argument should be the data array, ignore any others.
return static::send($action, reset($data) ?: []);
}
/**
* Return an empty Server Response
*
* No request is sent to Telegram.
* This function is used in commands that don't need to fire a message after execution
*
* @return ServerResponse
*/
public static function emptyResponse(): ServerResponse
{
return new ServerResponse(['ok' => true, 'result' => true]);
}
/**
* Send message to all active chats
*
* @param string $callback_function
* @param array $data
* @param array $select_chats_params
*
* @return array
* @throws TelegramException
*/
public static function sendToActiveChats(
string $callback_function,
array $data,
array $select_chats_params
): array {
self::ensureValidAction($callback_function);
$chats = DB::selectChats($select_chats_params);
$results = [];
if (is_array($chats)) {
foreach ($chats as $row) {
$data['chat_id'] = $row['chat_id'];
$results[] = self::send($callback_function, $data);
}
}
return $results;
}
/**
* Enable request limiter
*
* @param bool $enable
* @param array $options
*
* @throws TelegramException
*/
public static function setLimiter(bool $enable = true, array $options = []): void
{
if (DB::isDbConnected()) {
$options_default = [
'interval' => 1,
];
$options = array_merge($options_default, $options);
if (!is_numeric($options['interval']) || $options['interval'] <= 0) {
throw new TelegramException('Interval must be a number and must be greater than zero!');
}
self::$limiter_interval = $options['interval'];
self::$limiter_enabled = $enable;
}
}
/**
* This functions delays API requests to prevent reaching Telegram API limits
* Can be disabled while in execution by 'Request::setLimiter(false)'
*
* @link https://core.telegram.org/bots/faq#my-bot-is-hitting-limits-how-do-i-avoid-this
*
* @param string $action
* @param array $data
*
* @throws TelegramException
*/
private static function limitTelegramRequests(string $action, array $data = []): void
{
if (self::$limiter_enabled) {
$limited_methods = [
'sendMessage',
'forwardMessage',
'copyMessage',
'sendPhoto',
'sendAudio',
'sendDocument',
'sendSticker',
'sendVideo',
'sendAnimation',
'sendVoice',
'sendVideoNote',
'sendMediaGroup',
'sendLocation',
'editMessageLiveLocation',
'stopMessageLiveLocation',
'sendVenue',
'sendContact',
'sendPoll',
'sendDice',
'sendInvoice',
'sendGame',
'setGameScore',
'setMyCommands',
'editMessageText',
'editMessageCaption',
'editMessageMedia',
'editMessageReplyMarkup',
'stopPoll',
'setChatTitle',
'setChatDescription',
'setChatStickerSet',
'deleteChatStickerSet',
'setPassportDataErrors',
];
$chat_id = $data['chat_id'] ?? null;
$inline_message_id = $data['inline_message_id'] ?? null;
if (($chat_id || $inline_message_id) && in_array($action, $limited_methods, true)) {
$timeout = 60;
while (true) {
if ($timeout <= 0) {
throw new TelegramException('Timed out while waiting for a request spot!');
}
if (!($requests = DB::getTelegramRequestCount($chat_id, $inline_message_id))) {
break;
}
// Make sure we're handling integers here.
$requests = array_map('intval', $requests);
$chat_per_second = ($requests['LIMIT_PER_SEC'] === 0); // No more than one message per second inside a particular chat
$global_per_second = ($requests['LIMIT_PER_SEC_ALL'] < 30); // No more than 30 messages per second to different chats
$groups_per_minute = (((is_numeric($chat_id) && $chat_id > 0) || $inline_message_id !== null) || ((!is_numeric($chat_id) || $chat_id < 0) && $requests['LIMIT_PER_MINUTE'] < 20)); // No more than 20 messages per minute in groups and channels
if ($chat_per_second && $global_per_second && $groups_per_minute) {
break;
}
$timeout--;
usleep((int) (self::$limiter_interval * 1000000));
}
DB::insertTelegramRequest($action, $data);
}
}
}
}