Api::triggerCommand()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 1
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Telegram\Bot;
4
5
use Illuminate\Contracts\Container\Container;
6
use Telegram\Bot\Commands\CommandBus;
7
use Telegram\Bot\Events\EmitsEvents;
8
use Telegram\Bot\Events\UpdateWasReceived;
9
use Telegram\Bot\Exceptions\TelegramSDKException;
10
use Telegram\Bot\FileUpload\InputFile;
11
use Telegram\Bot\HttpClients\HttpClientInterface;
12
use Telegram\Bot\Objects\Chat;
13
use Telegram\Bot\Objects\ChatMember;
14
use Telegram\Bot\Objects\File;
15
use Telegram\Bot\Objects\Message;
16
use Telegram\Bot\Objects\UnknownObject;
17
use Telegram\Bot\Objects\Update;
18
use Telegram\Bot\Objects\User;
19
use Telegram\Bot\Objects\UserProfilePhotos;
20
use Telegram\Bot\Keyboard\Keyboard;
21
use Illuminate\Support\Str;
22
23
/**
24
 * Class Api.
25
 *
26
 * @mixin Commands\CommandBus
27
 */
28
class Api
29
{
30
    use EmitsEvents;
31
32
    /**
33
     * @var string Version number of the Telegram Bot PHP SDK.
34
     */
35
    const VERSION = '3.0.0';
36
37
    /**
38
     * @var string The name of the environment variable that contains the Telegram Bot API Access Token.
39
     */
40
    const BOT_TOKEN_ENV_NAME = 'TELEGRAM_BOT_TOKEN';
41
42
    /**
43
     * @var TelegramClient The Telegram client service.
44
     */
45
    protected $client;
46
47
    /**
48
     * @var string Telegram Bot API Access Token.
49
     */
50
    protected $accessToken = null;
51
52
    /**
53
     * @var TelegramResponse|null Stores the last request made to Telegram Bot API.
54
     */
55
    protected $lastResponse;
56
57
    /**
58
     * @var bool Indicates if the request to Telegram will be asynchronous (non-blocking).
59
     */
60
    protected $isAsyncRequest = false;
61
62
    /**
63
     * @var CommandBus|null Telegram Command Bus.
64
     */
65
    protected $commandBus = null;
66
67
    /**
68
     * @var Container IoC Container
69
     */
70
    protected static $container = null;
71
72
    /**
73
     * Timeout of the request in seconds.
74
     *
75
     * @var int
76
     */
77
    protected $timeOut = 60;
78
79
    /**
80
     * Connection timeout of the request in seconds.
81
     *
82
     * @var int
83
     */
84
    protected $connectTimeOut = 10;
85
86
    /**
87
     * Instantiates a new Telegram super-class object.
88
     *
89
     *
90
     * @param string              $token                      The Telegram Bot API Access Token.
91
     * @param bool                $async                      (Optional) Indicates if the request to Telegram
92
     *                                                        will be asynchronous (non-blocking).
93
     * @param HttpClientInterface $httpClientHandler          (Optional) Custom HTTP Client Handler.
94
     *
95
     * @throws TelegramSDKException
96 66
     */
97
    public function __construct($token = null, $async = false, $httpClientHandler = null)
98 66
    {
99 66
        $this->accessToken = isset($token) ? $token : getenv(static::BOT_TOKEN_ENV_NAME);
100 2
        if (!$this->accessToken) {
101
            throw new TelegramSDKException('Required "token" not supplied in config and could not find fallback environment variable "'.static::BOT_TOKEN_ENV_NAME.'"');
102
        }
103 66
104 66
        if (isset($async)) {
105 66
            $this->setAsyncRequest($async);
106
        }
107 66
108 66
        $this->client = new TelegramClient($httpClientHandler);
109 66
        $this->commandBus = new CommandBus($this);
110
    }
111
112
    /**
113
     * Invoke Bots Manager.
114
     *
115
     * @param $config
116
     *
117
     * @return BotsManager
118
     */
119
    public static function manager($config)
120
    {
121
        return new BotsManager($config);
122
    }
123
124
    /**
125
     * Returns the TelegramClient service.
126
     *
127
     * @return TelegramClient
128 6
     */
129
    public function getClient()
130 6
    {
131
        return $this->client;
132
    }
133
134
    /**
135
     * Returns Telegram Bot API Access Token.
136
     *
137
     * @return string
138 42
     */
139
    public function getAccessToken()
140 42
    {
141
        return $this->accessToken;
142
    }
143
144
    /**
145
     * Returns the last response returned from API request.
146
     *
147
     * @return TelegramResponse
148 2
     */
149
    public function getLastResponse()
150 2
    {
151
        return $this->lastResponse;
152
    }
153
154
    /**
155
     * Sets the bot access token to use with API requests.
156
     *
157
     * @param string $accessToken The bot access token to save.
158
     *
159
     * @throws \InvalidArgumentException
160
     *
161
     * @return Api
162 8
     */
163
    public function setAccessToken($accessToken)
164 8
    {
165 2
        if (is_string($accessToken)) {
166
            $this->accessToken = $accessToken;
167 2
168
            return $this;
169
        }
170 6
171
        throw new \InvalidArgumentException('The Telegram bot access token must be of type "string"');
172
    }
173
174
    /**
175
     * Make this request asynchronous (non-blocking).
176
     *
177
     * @param bool $isAsyncRequest
178
     *
179
     * @return Api
180 66
     */
181
    public function setAsyncRequest($isAsyncRequest)
182 66
    {
183
        $this->isAsyncRequest = $isAsyncRequest;
184 66
185
        return $this;
186
    }
187
188
    /**
189
     * Check if this is an asynchronous request (non-blocking).
190
     *
191
     * @return bool
192 42
     */
193
    public function isAsyncRequest()
194 42
    {
195
        return $this->isAsyncRequest;
196
    }
197
198
    /**
199
     * Returns SDK's Command Bus.
200
     *
201
     * @return CommandBus
202 8
     */
203
    public function getCommandBus()
204 8
    {
205
        return $this->commandBus;
206
    }
207
208
    /**
209
     * A simple method for testing your bot's auth token.
210
     * Returns basic information about the bot in form of a User object.
211
     *
212
     * @link https://core.telegram.org/bots/api#getme
213
     *
214
     * @throws TelegramSDKException
215
     *
216
     * @return User
217 4
     */
218
    public function getMe()
219 4
    {
220
        $response = $this->post('getMe');
221 2
222
        return new User($response->getDecodedBody());
223
    }
224
225
    /**
226
     * Send text messages.
227
     *
228
     * <code>
229
     * $params = [
230
     *   'chat_id'                  => '',
231
     *   'text'                     => '',
232
     *   'parse_mode'               => '',
233
     *   'disable_web_page_preview' => '',
234
     *   'disable_notification'     => '',
235
     *   'reply_to_message_id'      => '',
236
     *   'reply_markup'             => '',
237
     * ];
238
     * </code>
239
     *
240
     * @link https://core.telegram.org/bots/api#sendmessage
241
     *
242
     * @param array    $params
243
     *
244
     * @var int|string $params ['chat_id']
245
     * @var string     $params ['text']
246
     * @var string     $params ['parse_mode']
247
     * @var bool       $params ['disable_web_page_preview']
248
     * @var bool       $params ['disable_notification']
249
     * @var int        $params ['reply_to_message_id']
250
     * @var string     $params ['reply_markup']
251
     *
252
     * @throws TelegramSDKException
253
     *
254
     * @return Message
255 4
     */
256
    public function sendMessage(array $params)
257 4
    {
258
        $response = $this->post('sendMessage', $params);
259 4
260
        return new Message($response->getDecodedBody());
261
    }
262
263
    /**
264
     * Forward messages of any kind.
265
     *
266
     * <code>
267
     * $params = [
268
     *   'chat_id'              => '',
269
     *   'from_chat_id'         => '',
270
     *   'disable_notification' => '',
271
     *   'message_id'           => '',
272
     * ];
273
     * </code>
274
     *
275
     * @link https://core.telegram.org/bots/api#forwardmessage
276
     *
277
     * @param array    $params
278
     *
279
     * @var int|string $params ['chat_id']
280
     * @var int        $params ['from_chat_id']
281
     * @var bool       $params ['disable_notification']
282
     * @var int        $params ['message_id']
283
     *
284
     * @throws TelegramSDKException
285
     *
286
     * @return Message
287 2
     */
288
    public function forwardMessage(array $params)
289 2
    {
290
        $response = $this->post('forwardMessage', $params);
291 2
292
        return new Message($response->getDecodedBody());
293
    }
294
295
    /**
296
     * Send Photos.
297
     *
298
     * <code>
299
     * $params = [
300
     *   'chat_id'              => '',
301
     *   'photo'                => '',
302
     *   'caption'              => '',
303
     *   'disable_notification' => '',
304
     *   'reply_to_message_id'  => '',
305
     *   'reply_markup'         => '',
306
     * ];
307
     * </code>
308
     *
309
     * @link https://core.telegram.org/bots/api#sendphoto
310
     *
311
     * @param array    $params
312
     *
313
     * @var int|string $params ['chat_id']
314
     * @var string     $params ['photo']
315
     * @var string     $params ['caption']
316
     * @var bool       $params ['disable_notification']
317
     * @var int        $params ['reply_to_message_id']
318
     * @var string     $params ['reply_markup']
319
     *
320
     * @throws TelegramSDKException
321
     *
322
     * @return Message
323 4
     */
324
    public function sendPhoto(array $params)
325 4
    {
326
        $response = $this->uploadFile('sendPhoto', $params);
327 4
328
        return new Message($response->getDecodedBody());
329
    }
330
331
    /**
332
     * Send regular audio files.
333
     *
334
     * <code>
335
     * $params = [
336
     *   'chat_id'              => '',
337
     *   'audio'                => '',
338
     *   'duration'             => '',
339
     *   'performer'            => '',
340
     *   'title'                => '',
341
     *   'disable_notification' => '',
342
     *   'reply_to_message_id'  => '',
343
     *   'reply_markup'         => '',
344
     * ];
345
     * </code>
346
     *
347
     * @link https://core.telegram.org/bots/api#sendaudio
348
     *
349
     * @param array    $params
350
     *
351
     * @var int|string $params ['chat_id']
352
     * @var string     $params ['audio']
353
     * @var int        $params ['duration']
354
     * @var string     $params ['performer']
355
     * @var string     $params ['title']
356
     * @var bool       $params ['disable_notification']
357
     * @var int        $params ['reply_to_message_id']
358
     * @var string     $params ['reply_markup']
359
     *
360
     * @throws TelegramSDKException
361
     *
362
     * @return Message
363 2
     */
364
    public function sendAudio(array $params)
365 2
    {
366
        $response = $this->uploadFile('sendAudio', $params);
367 2
368
        return new Message($response->getDecodedBody());
369
    }
370
371
    /**
372
     * Send general files.
373
     *
374
     * <code>
375
     * $params = [
376
     *   'chat_id'              => '',
377
     *   'document'             => '',
378
     *   'caption'              => '',
379
     *   'disable_notification' => '',
380
     *   'reply_to_message_id'  => '',
381
     *   'reply_markup'         => '',
382
     * ];
383
     * </code>
384
     *
385
     * @link https://core.telegram.org/bots/api#senddocument
386
     *
387
     * @param array    $params
388
     *
389
     * @var int|string $params ['chat_id']
390
     * @var string     $params ['document']
391
     * @var string     $params ['caption']
392
     * @var bool       $params ['disable_notification']
393
     * @var int        $params ['reply_to_message_id']
394
     * @var string     $params ['reply_markup']
395
     *
396
     * @throws TelegramSDKException
397
     *
398
     * @return Message
399 2
     */
400
    public function sendDocument(array $params)
401 2
    {
402
        $response = $this->uploadFile('sendDocument', $params);
403 2
404
        return new Message($response->getDecodedBody());
405
    }
406
407
    /**
408
     * Send .webp stickers.
409
     *
410
     * <code>
411
     * $params = [
412
     *   'chat_id'              => '',
413
     *   'sticker'              => '',
414
     *   'disable_notification' => '',
415
     *   'reply_to_message_id'  => '',
416
     *   'reply_markup'         => '',
417
     * ];
418
     * </code>
419
     *
420
     * @link https://core.telegram.org/bots/api#sendsticker
421
     *
422
     * @param array    $params
423
     *
424
     * @var int|string $params ['chat_id']
425
     * @var string     $params ['sticker']
426
     * @var bool       $params ['disable_notification']
427
     * @var int        $params ['reply_to_message_id']
428
     * @var string     $params ['reply_markup']
429
     *
430
     * @throws TelegramSDKException
431
     *
432
     * @return Message
433 2
     */
434
    public function sendSticker(array $params)
435 2
    {
436
        if (is_file($params['sticker']) && (pathinfo($params['sticker'], PATHINFO_EXTENSION) !== 'webp')) {
437
            throw new TelegramSDKException('Invalid Sticker Provided. Supported Format: Webp');
438
        }
439 2
440
        $response = $this->uploadFile('sendSticker', $params);
441 2
442
        return new Message($response->getDecodedBody());
443
    }
444
445
    /**
446
     * Send Video File, Telegram clients support mp4 videos (other formats may be sent as Document).
447
     *
448
     * <code>
449
     * $params = [
450
     *   'chat_id'              => '',
451
     *   'video'                => '',
452
     *   'duration'             => '',
453
     *   'width'                => '',
454
     *   'height'               => '',
455
     *   'caption'              => '',
456
     *   'disable_notification' => '',
457
     *   'reply_to_message_id'  => '',
458
     *   'reply_markup'         => '',
459
     * ];
460
     * </code>
461
     *
462
     * @see  sendDocument
463
     * @link https://core.telegram.org/bots/api#sendvideo
464
     *
465
     * @param array    $params
466
     *
467
     * @var int|string $params ['chat_id']
468
     * @var string     $params ['video']
469
     * @var int        $params ['duration']
470
     * @var int        $params ['width']
471
     * @var int        $params ['height']
472
     * @var string     $params ['caption']
473
     * @var bool       $params ['disable_notification']
474
     * @var int        $params ['reply_to_message_id']
475
     * @var string     $params ['reply_markup']
476
     *
477
     * @throws TelegramSDKException
478
     *
479
     * @return Message
480 2
     */
481
    public function sendVideo(array $params)
482 2
    {
483
        $response = $this->uploadFile('sendVideo', $params);
484 2
485
        return new Message($response->getDecodedBody());
486
    }
487
488
    /**
489
     * Send voice audio files.
490
     *
491
     * <code>
492
     * $params = [
493
     *   'chat_id'              => '',
494
     *   'voice'                => '',
495
     *   'duration'             => '',
496
     *   'disable_notification' => '',
497
     *   'reply_to_message_id'  => '',
498
     *   'reply_markup'         => '',
499
     * ];
500
     * </code>
501
     *
502
     * @link https://core.telegram.org/bots/api#sendaudio
503
     *
504
     * @param array    $params
505
     *
506
     * @var int|string $params ['chat_id']
507
     * @var string     $params ['voice']
508
     * @var int        $params ['duration']
509
     * @var bool       $params ['disable_notification']
510
     * @var int        $params ['reply_to_message_id']
511
     * @var string     $params ['reply_markup']
512
     *
513
     * @throws TelegramSDKException
514
     *
515
     * @return Message
516 2
     */
517
    public function sendVoice(array $params)
518 2
    {
519
        $response = $this->uploadFile('sendVoice', $params);
520 2
521
        return new Message($response->getDecodedBody());
522
    }
523
524
    /**
525
     * Send point on the map.
526
     *
527
     * <code>
528
     * $params = [
529
     *   'chat_id'              => '',
530
     *   'latitude'             => '',
531
     *   'longitude'            => '',
532
     *   'disable_notification' => '',
533
     *   'reply_to_message_id'  => '',
534
     *   'reply_markup'         => '',
535
     * ];
536
     * </code>
537
     *
538
     * @link https://core.telegram.org/bots/api#sendlocation
539
     *
540
     * @param array    $params
541
     *
542
     * @var int|string $params ['chat_id']
543
     * @var float      $params ['latitude']
544
     * @var float      $params ['longitude']
545
     * @var bool       $params ['disable_notification']
546
     * @var int        $params ['reply_to_message_id']
547
     * @var string     $params ['reply_markup']
548
     *
549
     * @throws TelegramSDKException
550
     *
551
     * @return Message
552 2
     */
553
    public function sendLocation(array $params)
554 2
    {
555
        $response = $this->post('sendLocation', $params);
556 2
557
        return new Message($response->getDecodedBody());
558
    }
559
560
    /**
561
     * Send information about a venue.
562
     *
563
     * <code>
564
     * $params = [
565
     *   'chat_id'              => '',
566
     *   'latitude'             => '',
567
     *   'longitude'            => '',
568
     *   'title'                => '',
569
     *   'address'              => '',
570
     *   'foursquare_id'        => '',
571
     *   'disable_notification' => '',
572
     *   'reply_to_message_id'  => '',
573
     *   'reply_markup'         => '',
574
     * ];
575
     * </code>
576
     *
577
     * @link https://core.telegram.org/bots/api#sendvenue
578
     *
579
     * @param array    $params
580
     *
581
     * @var int|string $params ['chat_id']
582
     * @var float      $params ['latitude']
583
     * @var float      $params ['longitude']
584
     * @var string     $params ['title']
585
     * @var string     $params ['address']
586
     * @var string     $params ['foursquare_id']
587
     * @var bool       $params ['disable_notification']
588
     * @var int        $params ['reply_to_message_id']
589
     * @var string     $params ['reply_markup']
590
     *
591
     * @throws TelegramSDKException
592
     *
593
     * @return Message
594
     */
595
    public function sendVenue(array $params)
596
    {
597
        $response = $this->post('sendVenue', $params);
598
599
        return new Message($response->getDecodedBody());
600
    }
601
602
    /**
603
     * Send phone contacts.
604
     *
605
     * <code>
606
     * $params = [
607
     *   'chat_id'              => '',
608
     *   'phone_number'         => '',
609
     *   'first_name'           => '',
610
     *   'last_name'            => '',
611
     *   'disable_notification' => '',
612
     *   'reply_to_message_id'  => '',
613
     *   'reply_markup'         => '',
614
     * ];
615
     * </code>
616
     *
617
     * @link https://core.telegram.org/bots/api#sendcontact
618
     *
619
     * @param array    $params
620
     *
621
     * @var int|string $params ['chat_id']
622
     * @var string     $params ['phone_number']
623
     * @var string     $params ['first_name']
624
     * @var string     $params ['last_name']
625
     * @var bool       $params ['disable_notification']
626
     * @var int        $params ['reply_to_message_id']
627
     * @var string     $params ['reply_markup']
628
     *
629
     * @throws TelegramSDKException
630
     *
631
     * @return Message
632
     */
633
    public function sendContact(array $params)
634
    {
635
        $response = $this->post('sendContact', $params);
636
637
        return new Message($response->getDecodedBody());
638
    }
639
640
    /**
641
     * Broadcast a Chat Action.
642
     *
643
     * <code>
644
     * $params = [
645
     *   'chat_id' => '',
646
     *   'action'  => '',
647
     * ];
648
     * </code>
649
     *
650
     * @link https://core.telegram.org/bots/api#sendchataction
651
     *
652
     * @param array    $params
653
     *
654
     * @var int|string $params ['chat_id']
655
     * @var string     $params ['action']
656
     *
657
     * @throws TelegramSDKException
658
     *
659
     * @return bool
660 4
     */
661
    public function sendChatAction(array $params)
662
    {
663 4
        $validActions = [
664 4
            'typing',
665 4
            'upload_photo',
666 4
            'record_video',
667 4
            'upload_video',
668 4
            'record_audio',
669 4
            'upload_audio',
670 4
            'upload_document',
671 4
            'find_location',
672
        ];
673 4
674 2
        if (isset($params['action']) && in_array($params['action'], $validActions)) {
675
            $this->post('sendChatAction', $params);
676 2
677
            return true;
678
        }
679 2
680
        throw new TelegramSDKException('Invalid Action! Accepted value: '.implode(', ', $validActions));
681
    }
682
683
    /**
684
     * Returns a list of profile pictures for a user.
685
     *
686
     * <code>
687
     * $params = [
688
     *   'user_id' => '',
689
     *   'offset'  => '',
690
     *   'limit'   => '',
691
     * ];
692
     * </code>
693
     *
694
     * @link https://core.telegram.org/bots/api#getuserprofilephotos
695
     *
696
     * @param array $params
697
     *
698
     * @var int     $params ['user_id']
699
     * @var int     $params ['offset']
700
     * @var int     $params ['limit']
701
     *
702
     * @throws TelegramSDKException
703
     *
704
     * @return UserProfilePhotos
705
     */
706
    public function getUserProfilePhotos(array $params)
707
    {
708
        $response = $this->post('getUserProfilePhotos', $params);
709
710
        return new UserProfilePhotos($response->getDecodedBody());
711
    }
712
713
    /**
714
     * Returns basic info about a file and prepare it for downloading.
715
     *
716
     * <code>
717
     * $params = [
718
     *   'file_id' => '',
719
     * ];
720
     * </code>
721
     *
722
     * The file can then be downloaded via the link
723
     * https://api.telegram.org/file/bot<token>/<file_path>,
724
     * where <file_path> is taken from the response.
725
     *
726
     * @link https://core.telegram.org/bots/api#getFile
727
     *
728
     * @param array $params
729
     *
730
     * @var string  $params ['file_id']
731
     *
732
     * @throws TelegramSDKException
733
     *
734
     * @return File
735 2
     */
736
    public function getFile(array $params)
737 2
    {
738
        $response = $this->post('getFile', $params);
739 2
740
        return new File($response->getDecodedBody());
741
    }
742
743
    /**
744
     * Kick a user from a group or a supergroup.
745
     *
746
     * In the case of supergroups, the user will not be able to return to the group on their own using
747
     * invite links etc., unless unbanned first.
748
     *
749
     * The bot must be an administrator in the group for this to work.
750
     *
751
     * <code>
752
     * $params = [
753
     *   'chat_id'              => '',
754
     *   'user_id'              => '',
755
     * ];
756
     * </code>
757
     *
758
     * @link https://core.telegram.org/bots/api#kickchatmember
759
     *
760
     * @param array    $params
761
     *
762
     * @var int|string $params ['chat_id']
763
     * @var int        $params ['user_id']
764
     *
765
     * @throws TelegramSDKException
766
     *
767
     * @return bool
768
     */
769
    public function kickChatMember(array $params)
770
    {
771
        $this->post('kickChatMember', $params);
772
773
        return true;
774
    }
775
776
    /**
777
     * Unban a previously kicked user in a supergroup.
778
     *
779
     * The user will not return to the group automatically, but will be able to join via link, etc.
780
     *
781
     * The bot must be an administrator in the group for this to work.
782
     *
783
     * <code>
784
     * $params = [
785
     *   'chat_id'              => '',
786
     *   'user_id'              => '',
787
     * ];
788
     * </code>
789
     *
790
     * @link https://core.telegram.org/bots/api#unbanchatmember
791
     *
792
     * @param array    $params
793
     *
794
     * @var int|string $params ['chat_id']
795
     * @var int        $params ['user_id']
796
     *
797
     * @throws TelegramSDKException
798
     *
799
     * @return bool
800
     */
801
    public function unbanChatMember(array $params)
802
    {
803
        $this->post('unbanChatMember', $params);
804
805
        return true;
806
    }
807
808
    /**
809
     * Get up to date information about the chat (current name of the user for one-on-one conversations,
810
     * current username of a user, group or channel,
811
     *
812
     * <code>
813
     * $params = [
814
     *   'chat_id'  => '',
815
     * ];
816
     * </code>
817
     *
818
     * @link https://core.telegram.org/bots/api#getchat
819
     *
820
     * @param array $params
821
     *
822
     * @var string|int  $params ['chat_id'] Unique identifier for the target chat or username of the target supergroup or channel (in the format @channelusername)
823
     *
824
     * @throws TelegramSDKException
825
     *
826
     * @return Chat
827
     */
828
    public function getChat(array $params)
829
    {
830
        $response = $this->post('getChat', $params);
831
832
        return new Chat($response->getDecodedBody());
833
    }
834
835
    /**
836
     * Get a list of administrators in a chat.
837
     *
838
     * <code>
839
     * $params = [
840
     *   'chat_id'  => '',
841
     * ];
842
     * </code>
843
     *
844
     * @link https://core.telegram.org/bots/api#getchatadministrators
845
     *
846
     * @param array $params
847
     *
848
     * @var string|int  $params ['chat_id'] Unique identifier for the target chat or username of the target supergroup or channel (in the format @channelusername);
849
     *
850
     * @throws TelegramSDKException
851
     *
852
     * @return ChatMember[]
853
     */
854
    public function getChatAdministrators(array $params)
855
    {
856
        $response = $this->post('getChatAdministrators', $params);
857
858
        return collect($response->getResult())
859
            ->map(function ($admin) {
860
                return new ChatMember($admin);
861
            })
862
            ->all();
863
    }
864
865
    /**
866
     * Get the number of members in a chat
867
     *
868
     * <code>
869
     * $params = [
870
     *   'chat_id'  => '',
871
     * ];
872
     * </code>
873
     *
874
     * @link https://core.telegram.org/bots/api#getchatmemberscount
875
     *
876
     * @param array $params
877
     *
878
     * @var string|int  $params ['chat_id'] Unique identifier for the target chat or username of the target supergroup or channel (in the format @channelusername)
879
     *
880
     * @throws TelegramSDKException
881
     *
882
     * @return int
883
     */
884
    public function getChatMembersCount(array $params)
885
    {
886
        $response = $this->post('getChatMembersCount', $params);
887
888
        return $response->getResult();
889
    }
890
891
    /**
892
     * Get information about a member of a chat.
893
     *
894
     * <code>
895
     * $params = [
896
     *   'chat_id'  => '',
897
     *   'user_id'  => '',
898
     * ];
899
     * </code>
900
     *
901
     * @link https://core.telegram.org/bots/api#getchatmember
902
     *
903
     * @param array $params
904
     *
905
     * @var string|int  $params ['chat_id'] Unique identifier for the target chat or username of the target supergroup or channel (in the format @channelusername)
906
     * @var int         $params ['user_id'] Unique identifier of the target user.
907
     *
908
     * @throws TelegramSDKException
909
     *
910
     * @return ChatMember
911
     */
912
    public function getChatMember(array $params)
913
    {
914
        $response = $this->post('getChatMember', $params);
915
916
        return new ChatMember($response->getDecodedBody());
917
    }
918
919
    /**
920
     * Send answers to callback queries sent from inline keyboards.
921
     *
922
     * he answer will be displayed to the user as a notification at the top of the chat screen or as an alert.
923
     *
924
     * <code>
925
     * $params = [
926
     *   'callback_query_id'  => '',
927
     *   'text'               => '',
928
     *   'show_alert'         => '',
929
     * ];
930
     * </code>
931
     *
932
     * @link https://core.telegram.org/bots/api#answerCallbackQuery
933
     *
934
     * @param array $params
935
     *
936
     * @var string  $params ['callback_query_id']
937
     * @var string  $params ['text']
938
     * @var bool    $params ['show_alert']
939
     *
940
     * @throws TelegramSDKException
941
     *
942
     * @return bool
943
     */
944
    public function answerCallbackQuery(array $params)
945
    {
946
        $this->post('answerCallbackQuery', $params);
947
948
        return true;
949
    }
950
951
952
    /**
953
     * Edit text messages sent by the bot or via the bot (for inline bots).
954
     *
955
     * <code>
956
     * $params = [
957
     *   'chat_id'                  => '',
958
     *   'message_id'               => '',
959
     *   'inline_message_id'        => '',
960
     *   'text'                     => '',
961
     *   'parse_mode'               => '',
962
     *   'disable_web_page_preview' => '',
963
     *   'reply_markup'             => '',
964
     * ];
965
     * </code>
966
     *
967
     * @link https://core.telegram.org/bots/api#editMessageText
968
     *
969
     * @param array    $params
970
     *
971
     * @var int|string $params ['chat_id']
972
     * @var int        $params ['message_id']
973
     * @var string     $params ['inline_message_id']
974
     * @var string     $params ['text']
975
     * @var string     $params ['parse_mode']
976
     * @var bool       $params ['disable_web_page_preview']
977
     * @var string     $params ['reply_markup']
978
     *
979
     * @throws TelegramSDKException
980
     *
981
     * @return Message|bool
982
     */
983
    public function editMessageText(array $params)
984
    {
985
        $response = $this->post('editMessageText', $params);
986
987
        return new Message($response->getDecodedBody());
988
    }
989
990
    /**
991
     * Edit captions of messages sent by the bot or via the bot (for inline bots).
992
     *
993
     * <code>
994
     * $params = [
995
     *   'chat_id'                  => '',
996
     *   'message_id'               => '',
997
     *   'inline_message_id'        => '',
998
     *   'caption'                  => '',
999
     *   'reply_markup'             => '',
1000
     * ];
1001
     * </code>
1002
     *
1003
     * @link https://core.telegram.org/bots/api#editMessageCaption
1004
     *
1005
     * @param array    $params
1006
     *
1007
     * @var int|string $params ['chat_id']
1008
     * @var int        $params ['message_id']
1009
     * @var string     $params ['inline_message_id']
1010
     * @var string     $params ['caption']
1011
     * @var string     $params ['reply_markup']
1012
     *
1013
     * @throws TelegramSDKException
1014
     *
1015
     * @return Message|bool
1016
     */
1017
    public function editMessageCaption(array $params)
1018
    {
1019
        $response = $this->post('editMessageCaption', $params);
1020
1021
        return new Message($response->getDecodedBody());
1022
    }
1023
1024
    /**
1025
     * Edit only the reply markup of messages sent by the bot or via the bot (for inline bots).
1026
     *
1027
     * <code>
1028
     * $params = [
1029
     *   'chat_id'                  => '',
1030
     *   'message_id'               => '',
1031
     *   'inline_message_id'        => '',
1032
     *   'reply_markup'             => '',
1033
     * ];
1034
     * </code>
1035
     *
1036
     * @link https://core.telegram.org/bots/api#editMessageReplyMarkup
1037
     *
1038
     * @param array    $params
1039
     *
1040
     * @var int|string $params ['chat_id']
1041
     * @var int        $params ['message_id']
1042
     * @var string     $params ['inline_message_id']
1043
     * @var string     $params ['reply_markup']
1044
     *
1045
     * @throws TelegramSDKException
1046
     *
1047
     * @return Message|bool
1048
     */
1049
    public function editMessageReplyMarkup(array $params)
1050
    {
1051
        $response = $this->post('editMessageReplyMarkup', $params);
1052
1053
        return new Message($response->getDecodedBody());
1054
    }
1055
1056
    /**
1057
     * Use this method to send answers to an inline query.
1058
     *
1059
     * <code>
1060
     * $params = [
1061
     *   'inline_query_id'      => '',
1062
     *   'results'              => [],
1063
     *   'cache_time'           => 0,
1064
     *   'is_personal'          => false,
1065
     *   'next_offset'          => '',
1066
     *   'switch_pm_text'       => '',
1067
     *   'switch_pm_parameter'  => '',
1068
     * ];
1069
     * </code>
1070
     *
1071
     * @link https://core.telegram.org/bots/api#answerinlinequery
1072
     *
1073
     * @param array     $params
1074
     *
1075
     * @var string      $params ['inline_query_id']
1076
     * @var array       $params ['results']
1077
     * @var int|null    $params ['cache_time']
1078
     * @var bool|null   $params ['is_personal']
1079
     * @var string|null $params ['next_offset']
1080
     * @var string|null $params ['switch_pm_text']
1081
     * @var string|null $params ['switch_pm_parameter']
1082
     *
1083
     * @throws TelegramSDKException
1084
     *
1085
     * @return bool
1086
     */
1087
    public function answerInlineQuery(array $params = [])
1088
    {
1089
        if (is_array($params['results'])) {
1090
            $params['results'] = json_encode($params['results']);
1091
        }
1092
1093
        $this->post('answerInlineQuery', $params);
1094
1095
        return true;
1096
    }
1097
1098
    /**
1099
     * Set a Webhook to receive incoming updates via an outgoing webhook.
1100
     *
1101
     * <code>
1102
     * $params = [
1103
     *   'url'         => '',
1104
     *   'certificate' => '',
1105
     * ];
1106
     * </code>
1107
     *
1108
     * @link https://core.telegram.org/bots/api#setwebhook
1109
     *
1110
     * @param array $params
1111
     *
1112
     * @var string  $params ['url']         HTTPS url to send updates to.
1113
     * @var string  $params ['certificate'] Upload your public key certificate so that the root certificate in
1114
     *                                      use can be checked.
1115
     *
1116
     * @throws TelegramSDKException
1117
     *
1118
     * @return TelegramResponse
1119 6
     */
1120
    public function setWebhook(array $params)
1121 6
    {
1122 2
        if (filter_var($params['url'], FILTER_VALIDATE_URL) === false) {
1123
            throw new TelegramSDKException('Invalid URL Provided');
1124
        }
1125 4
1126 2
        if (parse_url($params['url'], PHP_URL_SCHEME) !== 'https') {
1127
            throw new TelegramSDKException('Invalid URL, should be a HTTPS url.');
1128
        }
1129 2
1130
        return $this->uploadFile('setWebhook', $params);
1131
    }
1132
1133
    /**
1134
     * Returns a webhook update sent by Telegram.
1135
     * Works only if you set a webhook.
1136
     *
1137
     * @see setWebhook
1138
     *
1139
     * @return Update
1140
     */
1141
    public function getWebhookUpdate($shouldEmitEvent = true)
1142
    {
1143
        $body = json_decode(file_get_contents('php://input'), true);
1144
1145
        $update = new Update($body);
1146
1147
        if ($shouldEmitEvent) {
1148
            $this->emitEvent(new UpdateWasReceived($update, $this));
1149
        }
1150
1151
        return $update;
1152
    }
1153
1154
    /**
1155
     * Alias for getWebhookUpdate
1156
     *
1157
     * @deprecated Call method getWebhookUpdate (note lack of letter s at end)
1158
     *             To be removed in next major version.
1159
     *
1160
     * @param bool $shouldEmitEvent
1161
     *
1162
     * @return Update
1163
     */
1164
    public function getWebhookUpdates($shouldEmitEvent = true)
1165
    {
1166
        return $this->getWebhookUpdate($shouldEmitEvent);
1167
    }
1168
1169
    /**
1170
     * Removes the outgoing webhook (if any).
1171
     *
1172
     * @throws TelegramSDKException
1173
     *
1174
     * @return TelegramResponse
1175 2
     */
1176
    public function removeWebhook()
1177 2
    {
1178
        $url = '';
1179 2
1180
        return $this->post('setWebhook', compact('url'));
1181
    }
1182
1183
    /**
1184
     * Use this method to receive incoming updates using long polling.
1185
     *
1186
     * <code>
1187
     * $params = [
1188
     *   'offset'  => '',
1189
     *   'limit'   => '',
1190
     *   'timeout' => '',
1191
     * ];
1192
     * </code>
1193
     *
1194
     * @link https://core.telegram.org/bots/api#getupdates
1195
     *
1196
     * @param array  $params
1197
     * @param bool   $shouldEmitEvents
1198
     * @var int|null $params ['offset']
1199
     * @var int|null $params ['limit']
1200
     * @var int|null $params ['timeout']
1201
     *
1202
     * @throws TelegramSDKException
1203
     *
1204
     * @return Update[]
1205 6
     */
1206
    public function getUpdates(array $params = [], $shouldEmitEvents = true)
1207 6
    {
1208
        $response = $this->post('getUpdates', $params);
1209 6
1210
        return collect($response->getResult())
1211
            ->map(function ($data) use ($shouldEmitEvents) {
1212 6
1213
                $update = new Update($data);
1214 6
1215 6
                if ($shouldEmitEvents) {
1216 6
                    $this->emitEvent(new UpdateWasReceived($update, $this));
1217
                }
1218 6
1219 6
                return $update;
1220 6
            })
1221
            ->all();
1222
    }
1223
1224
1225
    /**
1226
     * Builds a custom keyboard markup.
1227
     *
1228
     * <code>
1229
     * $params = [
1230
     *   'keyboard'          => '',
1231
     *   'resize_keyboard'   => '',
1232
     *   'one_time_keyboard' => '',
1233
     *   'selective'         => '',
1234
     * ];
1235
     * </code>
1236
     *
1237
     * @deprecated Use Telegram\Bot\Keyboard\Keyboard::make(array $params = []) instead.
1238
     *             To be removed in next major version.
1239
     *
1240
     * @link       https://core.telegram.org/bots/api#replykeyboardmarkup
1241
     *
1242
     * @param array $params
1243
     *
1244
     * @var array   $params ['keyboard']
1245
     * @var bool    $params ['resize_keyboard']
1246
     * @var bool    $params ['one_time_keyboard']
1247
     * @var bool    $params ['selective']
1248
     *
1249
     * @return string
1250
     */
1251
    public function replyKeyboardMarkup(array $params)
1252
    {
1253
        return Keyboard::make($params);
1254
    }
1255
1256
    /**
1257
     * Hide the current custom keyboard and display the default letter-keyboard.
1258
     *
1259
     * <code>
1260
     * $params = [
1261
     *   'hide_keyboard' => true,
1262
     *   'selective'     => false,
1263
     * ];
1264
     * </code>
1265
     *
1266
     * @deprecated Use Telegram\Bot\Keyboard\Keyboard::hide(array $params = []) instead.
1267
     *             To be removed in next major version.
1268
     *
1269
     * @link       https://core.telegram.org/bots/api#replykeyboardhide
1270
     *
1271
     * @param array $params
1272
     *
1273
     * @var bool    $params ['hide_keyboard']
1274
     * @var bool    $params ['selective']
1275
     *
1276
     * @return string
1277
     */
1278
    public static function replyKeyboardHide(array $params = [])
1279
    {
1280
        return Keyboard::hide($params);
1281
    }
1282
1283
    /**
1284
     * Display a reply interface to the user (act as if the user has selected the bot‘s message and tapped ’Reply').
1285
     *
1286
     * <code>
1287
     * $params = [
1288
     *   'force_reply' => true,
1289
     *   'selective'   => false,
1290
     * ];
1291
     * </code>
1292
     *
1293
     * @deprecated Use Telegram\Bot\Keyboard\Keyboard::forceReply(array $params = []) instead.
1294
     *             To be removed in next major version.
1295
     *
1296
     * @link       https://core.telegram.org/bots/api#forcereply
1297
     *
1298
     * @param array $params
1299
     *
1300
     * @var bool    $params ['force_reply']
1301
     * @var bool    $params ['selective']
1302
     *
1303
     * @return Keyboard
1304
     */
1305
    public static function forceReply(array $params = [])
1306
    {
1307
        return Keyboard::forceReply($params);
1308
    }
1309
1310
    /**
1311
     * Processes Inbound Commands.
1312
     *
1313
     * @param bool  $webhook
1314
     * @param array $params
1315
     *
1316 6
     * @return Update|Update[]
1317
     */
1318 6
    public function commandsHandler($webhook = false, array $params = [])
1319
    {
1320
        if ($webhook) {
1321
            $update = $this->getWebhookUpdate();
1322
            $this->processCommand($update);
1323
1324
            return $update;
1325 6
        }
1326 6
1327
        $updates = $this->getUpdates($params);
1328 6
        $highestId = -1;
1329 6
1330 6
        foreach ($updates as $update) {
1331 6
            $highestId = $update->getUpdateId();
1332
            $this->processCommand($update);
1333
        }
1334 6
1335 6
        //An update is considered confirmed as soon as getUpdates is called with an offset higher than its update_id.
1336 6
        if ($highestId != -1) {
1337 6
            $params = [];
1338 6
            $params['offset'] = $highestId + 1;
1339 6
            $params['limit'] = 1;
1340
            $this->markUpdateAsRead($params);
1341 6
        }
1342
1343
        return $updates;
1344
    }
1345
1346
    /**
1347
     * Check update object for a command and process.
1348
     *
1349 6
     * @param Update $update
1350
     */
1351 6
    public function processCommand(Update $update)
1352
    {
1353 6
        $message = $update->getMessage();
1354 6
1355 6
        if ($message !== null && $message->has('text')) {
1356 6
            $this->getCommandBus()->handler($message->getText(), $update);
1357
        }
1358
    }
1359
1360
    /**
1361
     * Helper to Trigger Commands.
1362
     *
1363
     * @param string $name   Command Name
1364
     * @param Update $update Update Object
1365
     *
1366
     * @return mixed
1367
     */
1368
    public function triggerCommand($name, Update $update)
1369
    {
1370
        return $this->getCommandBus()->execute($name, $update->getMessage()->getText(), $update);
1371
    }
1372
1373
    /**
1374
     * Determine if a given type is the message.
1375
     *
1376
     * @deprecated Call method isType directly on Message object
1377
     *             To be removed in next major version.
1378
     *
1379
     * @param string         $type
1380
     * @param Update|Message $object
1381
     *
1382
     * @throws \ErrorException
1383
     *
1384
     */
1385
    public function isMessageType($type, $object)
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $object is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1386
    {
1387
        trigger_error('This method has been deprecated. Use isType() on the Message object instead.', E_USER_DEPRECATED);
1388
    }
1389
1390
    /**
1391
     * Detect Message Type Based on Update or Message Object.
1392
     *
1393
     * @deprecated Call method detectType directly on Message object
1394
     *             To be removed in next major version.
1395
     *
1396
     * @param Update|Message $object
1397
     *
1398
     * @throws \ErrorException
1399
     *
1400
     * @return string|null
1401
     */
1402
    public function detectMessageType($object)
0 ignored issues
show
Unused Code introduced by
The parameter $object is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1403
    {
1404
        trigger_error('This method has been deprecated. Use detectType() on the Message object instead.', E_USER_DEPRECATED);
1405
    }
1406
1407
    /**
1408
     * Sends a GET request to Telegram Bot API and returns the result.
1409
     *
1410
     * @param string $endpoint
1411
     * @param array  $params
1412
     *
1413
     * @throws TelegramSDKException
1414
     *
1415
     * @return TelegramResponse
1416
     */
1417
    protected function get($endpoint, $params = [])
1418
    {
1419
        if (array_key_exists('reply_markup', $params)) {
1420
            $params['reply_markup'] = (string)$params['reply_markup'];
1421
        }
1422
1423
        return $this->sendRequest(
1424
            'GET',
1425
            $endpoint,
1426
            $params
1427
        );
1428
    }
1429
1430
    /**
1431
     * Sends a POST request to Telegram Bot API and returns the result.
1432
     *
1433
     * @param string $endpoint
1434
     * @param array  $params
1435
     * @param bool   $fileUpload Set true if a file is being uploaded.
1436
     *
1437 40
     * @return TelegramResponse
1438
     */
1439 40
    protected function post($endpoint, array $params = [], $fileUpload = false)
1440 16
    {
1441 16
        if ($fileUpload) {
1442
            $params = ['multipart' => $params];
1443 24
        } else {
1444
1445
            if (array_key_exists('reply_markup', $params)) {
1446
                $params['reply_markup'] = (string)$params['reply_markup'];
1447 24
            }
1448
1449
            $params = ['form_params' => $params];
1450 40
        }
1451 40
1452 40
        return $this->sendRequest(
1453
            'POST',
1454 40
            $endpoint,
1455
            $params
1456
        );
1457
    }
1458
1459
    /**
1460
     * Sends a multipart/form-data request to Telegram Bot API and returns the result.
1461
     * Used primarily for file uploads.
1462
     *
1463
     * @param string $endpoint
1464
     * @param array  $params
1465
     *
1466
     * @throws TelegramSDKException
1467
     *
1468 16
     * @return TelegramResponse
1469
     */
1470 16
    protected function uploadFile($endpoint, array $params = [])
1471
    {
1472 16
        $multipart_params = collect($params)
1473 16
            ->reject(function ($value) {
1474 16
                return is_null($value);
1475
            })
1476 16
            ->map(function ($contents, $name) {
1477
1478
                if (!is_resource($contents) && $this->isValidFileOrUrl($name, $contents)) {
1479
                    $contents = (new InputFile($contents))->open();
1480
                }
1481 16
1482 16
                return [
1483 16
                    'name'     => $name,
1484 16
                    'contents' => $contents,
1485
                ];
1486 16
            })
1487 16
            //Reset the keys on the collection
1488
            ->values()
1489 16
            ->all();
1490
1491
        return $this->post($endpoint, $multipart_params, true);
1492
    }
1493
1494
    /**
1495
     * Sends a request to Telegram Bot API and returns the result.
1496
     *
1497
     * @param string $method
1498
     * @param string $endpoint
1499
     * @param array  $params
1500
     *
1501
     * @throws TelegramSDKException
1502
     *
1503 40
     * @return TelegramResponse
1504
     */
1505
    protected function sendRequest(
1506
        $method,
1507
        $endpoint,
1508 40
        array $params = []
1509
    ) {
1510 40
        $request = $this->request($method, $endpoint, $params);
1511
1512
        return $this->lastResponse = $this->client->sendRequest($request);
1513
    }
1514
1515
    /**
1516
     * Instantiates a new TelegramRequest entity.
1517
     *
1518
     * @param string $method
1519
     * @param string $endpoint
1520
     * @param array  $params
1521
     *
1522 40
     * @return TelegramRequest
1523
     */
1524
    protected function request(
1525
        $method,
1526
        $endpoint,
1527 40
        array $params = []
1528 40
    ) {
1529 40
        return new TelegramRequest(
1530 40
            $this->getAccessToken(),
1531 40
            $method,
1532 40
            $endpoint,
1533 40
            $params,
1534 40
            $this->isAsyncRequest(),
1535 40
            $this->getTimeOut(),
1536
            $this->getConnectTimeOut()
1537
        );
1538
    }
1539
1540
    /**
1541
     * Magic method to process any "get" requests.
1542
     *
1543
     * @param $method
1544
     * @param $arguments
1545
     *
1546
     * @throws TelegramSDKException
1547
     *
1548 2
     * @return bool|TelegramResponse|UnknownObject
1549
     */
1550 2
    public function __call($method, $arguments)
1551 2
    {
1552
        if (preg_match('/^\w+Commands?/', $method, $matches)) {
1553
            return call_user_func_array([$this->getCommandBus(), $matches[0]], $arguments);
1554
        }
1555
1556
        $action = substr($method, 0, 3);
1557
        if ($action === 'get') {
1558
            /* @noinspection PhpUndefinedFunctionInspection */
1559
            $class_name = Str::studly(substr($method, 3));
1560
            $class = 'Telegram\Bot\Objects\\'.$class_name;
1561
            $response = $this->post($method, $arguments[0] ?: []);
1562
1563
            if (class_exists($class)) {
1564
                return new $class($response->getDecodedBody());
1565
            }
1566
1567
            return $response;
1568
        }
1569
        $response = $this->post($method, $arguments[0]);
1570
1571
        return new UnknownObject($response->getDecodedBody());
1572
    }
1573
1574
    /**
1575
     * Set the IoC Container.
1576
     *
1577
     * @param $container Container instance
1578
     *
1579 2
     * @return void
1580
     */
1581 2
    public static function setContainer(Container $container)
1582 2
    {
1583
        self::$container = $container;
1584
    }
1585
1586
    /**
1587
     * Get the IoC Container.
1588
     *
1589 2
     * @return Container
1590
     */
1591 2
    public function getContainer()
1592
    {
1593
        return self::$container;
1594
    }
1595
1596
    /**
1597
     * Check if IoC Container has been set.
1598
     *
1599
     * @return boolean
1600
     */
1601
    public function hasContainer()
1602
    {
1603
        return self::$container !== null;
1604
    }
1605
1606
    /**
1607 40
     * @return int
1608
     */
1609 40
    public function getTimeOut()
1610
    {
1611
        return $this->timeOut;
1612
    }
1613
1614
    /**
1615
     * @param int $timeOut
1616
     *
1617 2
     * @return $this
1618
     */
1619 2
    public function setTimeOut($timeOut)
1620
    {
1621 2
        $this->timeOut = $timeOut;
1622
1623
        return $this;
1624
    }
1625
1626
    /**
1627 40
     * @return int
1628
     */
1629 40
    public function getConnectTimeOut()
1630
    {
1631
        return $this->connectTimeOut;
1632
    }
1633
1634
    /**
1635
     * @param int $connectTimeOut
1636
     *
1637 2
     * @return $this
1638
     */
1639 2
    public function setConnectTimeOut($connectTimeOut)
1640
    {
1641 2
        $this->connectTimeOut = $connectTimeOut;
1642
1643
        return $this;
1644
    }
1645
1646
    /**
1647
     * An alias for getUpdates that helps readability.
1648
     *
1649
     * @param $params
1650
     *
1651 6
     * @return Objects\Update[]
1652
     */
1653 6
    protected function markUpdateAsRead($params)
1654
    {
1655
        return $this->getUpdates($params, false);
1656
    }
1657
1658
    /**
1659
     * Determines if the string passed to be uploaded is a valid
1660
     * file on the local file system, or a valid remote URL.
1661
     *
1662
     * @param string $name
1663
     * @param string $contents
1664
     *
1665 16
     * @return bool
1666
     */
1667
    protected function isValidFileOrUrl($name, $contents)
1668 16
    {
1669 2
        //Don't try to open a url as an actual file when using this method to setWebhook.
1670
        if ($name == 'url') {
1671
            return false;
1672
        }
1673
1674 14
        //If a certificate name is passed, we must check for the file existing on the local server,
1675
        // otherwise telegram ignores the fact it wasn't sent and no error is thrown.
1676
        if ($name == 'certificate') {
1677
            return true;
1678
        }
1679 14
1680
        //Is the content a valid file name.
1681
        if (is_readable($contents)) {
1682
            return true;
1683
        }
1684 14
1685
        //Is the content a valid URL
1686
        return filter_var($contents, FILTER_VALIDATE_URL);
1687
    }
1688
}
1689