Completed
Pull Request — master (#526)
by
unknown
14:13
created

Api::getWebhookInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 1
cts 1
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
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 Telegram\Bot\Objects\Webhook;
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
     * A simple method for getting information about webhook configuration
1171
     * Returns basic information about the configuration in form of a Webhook object.
1172
     *
1173
     * @link https://core.telegram.org/bots/api#getwebhookinfo
1174
     *
1175 2
     * @throws TelegramSDKException
1176
     *
1177 2
     * @return Webhook
1178
     */
1179 2
    public function getWebhookInfo()
1180
    {
1181
        $response = $this->post('getWebhookInfo');
1182
1183
        return new Webhook($response->getDecodedBody());
1184
    }
1185
1186
1187
    /**
1188
     * Removes the outgoing webhook (if any).
1189
     *
1190
     * @throws TelegramSDKException
1191
     *
1192
     * @return TelegramResponse
1193
     */
1194
    public function removeWebhook()
1195
    {
1196
        $url = '';
1197
1198
        return $this->post('setWebhook', compact('url'));
1199
    }
1200
1201
    /**
1202
     * Use this method to receive incoming updates using long polling.
1203
     *
1204
     * <code>
1205 6
     * $params = [
1206
     *   'offset'  => '',
1207 6
     *   'limit'   => '',
1208
     *   'timeout' => '',
1209 6
     * ];
1210
     * </code>
1211
     *
1212 6
     * @link https://core.telegram.org/bots/api#getupdates
1213
     *
1214 6
     * @param array  $params
1215 6
     * @param bool   $shouldEmitEvents
1216 6
     * @var int|null $params ['offset']
1217
     * @var int|null $params ['limit']
1218 6
     * @var int|null $params ['timeout']
1219 6
     *
1220 6
     * @throws TelegramSDKException
1221
     *
1222
     * @return Update[]
1223
     */
1224
    public function getUpdates(array $params = [], $shouldEmitEvents = true)
1225
    {
1226
        $response = $this->post('getUpdates', $params);
1227
1228
        return collect($response->getResult())
1229
            ->map(function ($data) use ($shouldEmitEvents) {
1230
1231
                $update = new Update($data);
1232
1233
                if ($shouldEmitEvents) {
1234
                    $this->emitEvent(new UpdateWasReceived($update, $this));
1235
                }
1236
1237
                return $update;
1238
            })
1239
            ->all();
1240
    }
1241
1242
1243
    /**
1244
     * Builds a custom keyboard markup.
1245
     *
1246
     * <code>
1247
     * $params = [
1248
     *   'keyboard'          => '',
1249
     *   'resize_keyboard'   => '',
1250
     *   'one_time_keyboard' => '',
1251
     *   'selective'         => '',
1252
     * ];
1253
     * </code>
1254
     *
1255
     * @deprecated Use Telegram\Bot\Keyboard\Keyboard::make(array $params = []) instead.
1256
     *             To be removed in next major version.
1257
     *
1258
     * @link       https://core.telegram.org/bots/api#replykeyboardmarkup
1259
     *
1260
     * @param array $params
1261
     *
1262
     * @var array   $params ['keyboard']
1263
     * @var bool    $params ['resize_keyboard']
1264
     * @var bool    $params ['one_time_keyboard']
1265
     * @var bool    $params ['selective']
1266
     *
1267
     * @return string
1268
     */
1269
    public function replyKeyboardMarkup(array $params)
1270
    {
1271
        return Keyboard::make($params);
1272
    }
1273
1274
    /**
1275
     * Hide the current custom keyboard and display the default letter-keyboard.
1276
     *
1277
     * <code>
1278
     * $params = [
1279
     *   'hide_keyboard' => true,
1280
     *   'selective'     => false,
1281
     * ];
1282
     * </code>
1283
     *
1284
     * @deprecated Use Telegram\Bot\Keyboard\Keyboard::hide(array $params = []) instead.
1285
     *             To be removed in next major version.
1286
     *
1287
     * @link       https://core.telegram.org/bots/api#replykeyboardhide
1288
     *
1289
     * @param array $params
1290
     *
1291
     * @var bool    $params ['hide_keyboard']
1292
     * @var bool    $params ['selective']
1293
     *
1294
     * @return string
1295
     */
1296
    public static function replyKeyboardHide(array $params = [])
1297
    {
1298
        return Keyboard::hide($params);
1299
    }
1300
1301
    /**
1302
     * Display a reply interface to the user (act as if the user has selected the bot‘s message and tapped ’Reply').
1303
     *
1304
     * <code>
1305
     * $params = [
1306
     *   'force_reply' => true,
1307
     *   'selective'   => false,
1308
     * ];
1309
     * </code>
1310
     *
1311
     * @deprecated Use Telegram\Bot\Keyboard\Keyboard::forceReply(array $params = []) instead.
1312
     *             To be removed in next major version.
1313
     *
1314
     * @link       https://core.telegram.org/bots/api#forcereply
1315
     *
1316 6
     * @param array $params
1317
     *
1318 6
     * @var bool    $params ['force_reply']
1319
     * @var bool    $params ['selective']
1320
     *
1321
     * @return Keyboard
1322
     */
1323
    public static function forceReply(array $params = [])
1324
    {
1325 6
        return Keyboard::forceReply($params);
1326 6
    }
1327
1328 6
    /**
1329 6
     * Processes Inbound Commands.
1330 6
     *
1331 6
     * @param bool  $webhook
1332
     * @param array $params
1333
     *
1334 6
     * @return Update|Update[]
1335 6
     */
1336 6
    public function commandsHandler($webhook = false, array $params = [])
1337 6
    {
1338 6
        if ($webhook) {
1339 6
            $update = $this->getWebhookUpdates();
0 ignored issues
show
Deprecated Code introduced by
The method Telegram\Bot\Api::getWebhookUpdates() has been deprecated with message: Call method getWebhookUpdate (note lack of letter s at end) To be removed in next major version.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1340
            $this->processCommand($update);
1341 6
1342
            return $update;
1343
        }
1344
1345
        $updates = $this->getUpdates($params);
1346
        $highestId = -1;
1347
1348
        foreach ($updates as $update) {
1349 6
            $highestId = $update->getUpdateId();
1350
            $this->processCommand($update);
1351 6
        }
1352
1353 6
        //An update is considered confirmed as soon as getUpdates is called with an offset higher than its update_id.
1354 6
        if ($highestId != -1) {
1355 6
            $params = [];
1356 6
            $params['offset'] = $highestId + 1;
1357
            $params['limit'] = 1;
1358
            $this->markUpdateAsRead($params);
1359
        }
1360
1361
        return $updates;
1362
    }
1363
1364
    /**
1365
     * Check update object for a command and process.
1366
     *
1367
     * @param Update $update
1368
     */
1369
    public function processCommand(Update $update)
1370
    {
1371
        $message = $update->getMessage();
1372
1373
        if ($message !== null && $message->has('text')) {
1374
            $this->getCommandBus()->handler($message->getText(), $update);
1375
        }
1376
    }
1377
1378
    /**
1379
     * Helper to Trigger Commands.
1380
     *
1381
     * @param string $name   Command Name
1382
     * @param Update $update Update Object
1383
     *
1384
     * @return mixed
1385
     */
1386
    public function triggerCommand($name, Update $update)
1387
    {
1388
        return $this->getCommandBus()->execute($name, $update->getMessage()->getText(), $update);
1389
    }
1390
1391
    /**
1392
     * Determine if a given type is the message.
1393
     *
1394
     * @deprecated Call method isType directly on Message object
1395
     *             To be removed in next major version.
1396
     *
1397
     * @param string         $type
1398
     * @param Update|Message $object
1399
     *
1400
     * @throws \ErrorException
1401
     *
1402
     */
1403
    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...
1404
    {
1405
        trigger_error('This method has been deprecated. Use isType() on the Message object instead.', E_USER_DEPRECATED);
1406
    }
1407
1408
    /**
1409
     * Detect Message Type Based on Update or Message Object.
1410
     *
1411
     * @deprecated Call method detectType directly on Message object
1412
     *             To be removed in next major version.
1413
     *
1414
     * @param Update|Message $object
1415
     *
1416
     * @throws \ErrorException
1417
     *
1418
     * @return string|null
1419
     */
1420
    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...
1421
    {
1422
        trigger_error('This method has been deprecated. Use detectType() on the Message object instead.', E_USER_DEPRECATED);
1423
    }
1424
1425
    /**
1426
     * Sends a GET request to Telegram Bot API and returns the result.
1427
     *
1428
     * @param string $endpoint
1429
     * @param array  $params
1430
     *
1431
     * @throws TelegramSDKException
1432
     *
1433
     * @return TelegramResponse
1434
     */
1435
    protected function get($endpoint, $params = [])
1436
    {
1437 40
        if (array_key_exists('reply_markup', $params)) {
1438
            $params['reply_markup'] = (string)$params['reply_markup'];
1439 40
        }
1440 16
1441 16
        return $this->sendRequest(
1442
            'GET',
1443 24
            $endpoint,
1444
            $params
1445
        );
1446
    }
1447 24
1448
    /**
1449
     * Sends a POST request to Telegram Bot API and returns the result.
1450 40
     *
1451 40
     * @param string $endpoint
1452 40
     * @param array  $params
1453
     * @param bool   $fileUpload Set true if a file is being uploaded.
1454 40
     *
1455
     * @return TelegramResponse
1456
     */
1457
    protected function post($endpoint, array $params = [], $fileUpload = false)
1458
    {
1459
        if ($fileUpload) {
1460
            $params = ['multipart' => $params];
1461
        } else {
1462
1463
            if (array_key_exists('reply_markup', $params)) {
1464
                $params['reply_markup'] = (string)$params['reply_markup'];
1465
            }
1466
1467
            $params = ['form_params' => $params];
1468 16
        }
1469
1470 16
        return $this->sendRequest(
1471
            'POST',
1472 16
            $endpoint,
1473 16
            $params
1474 16
        );
1475
    }
1476 16
1477
    /**
1478
     * Sends a multipart/form-data request to Telegram Bot API and returns the result.
1479
     * Used primarily for file uploads.
1480
     *
1481 16
     * @param string $endpoint
1482 16
     * @param array  $params
1483 16
     *
1484 16
     * @throws TelegramSDKException
1485
     *
1486 16
     * @return TelegramResponse
1487 16
     */
1488
    protected function uploadFile($endpoint, array $params = [])
1489 16
    {
1490
        $multipart_params = collect($params)
1491
            ->reject(function ($value) {
1492
                return is_null($value);
1493
            })
1494
            ->map(function ($contents, $name) {
1495
1496
                if (!is_resource($contents) && $this->isValidFileOrUrl($name, $contents)) {
1497
                    $contents = (new InputFile($contents))->open();
1498
                }
1499
1500
                return [
1501
                    'name'     => $name,
1502
                    'contents' => $contents,
1503 40
                ];
1504
            })
1505
            //Reset the keys on the collection
1506
            ->values()
1507
            ->all();
1508 40
1509
        return $this->post($endpoint, $multipart_params, true);
1510 40
    }
1511
1512
    /**
1513
     * Sends a request to Telegram Bot API and returns the result.
1514
     *
1515
     * @param string $method
1516
     * @param string $endpoint
1517
     * @param array  $params
1518
     *
1519
     * @throws TelegramSDKException
1520
     *
1521
     * @return TelegramResponse
1522 40
     */
1523
    protected function sendRequest(
1524
        $method,
1525
        $endpoint,
1526
        array $params = []
1527 40
    ) {
1528 40
        $request = $this->request($method, $endpoint, $params);
1529 40
1530 40
        return $this->lastResponse = $this->client->sendRequest($request);
1531 40
    }
1532 40
1533 40
    /**
1534 40
     * Instantiates a new TelegramRequest entity.
1535 40
     *
1536
     * @param string $method
1537
     * @param string $endpoint
1538
     * @param array  $params
1539
     *
1540
     * @return TelegramRequest
1541
     */
1542
    protected function request(
1543
        $method,
1544
        $endpoint,
1545
        array $params = []
1546
    ) {
1547
        return new TelegramRequest(
1548 2
            $this->getAccessToken(),
1549
            $method,
1550 2
            $endpoint,
1551 2
            $params,
1552
            $this->isAsyncRequest(),
1553
            $this->getTimeOut(),
1554
            $this->getConnectTimeOut()
1555
        );
1556
    }
1557
1558
    /**
1559
     * Magic method to process any "get" requests.
1560
     *
1561
     * @param $method
1562
     * @param $arguments
1563
     *
1564
     * @throws TelegramSDKException
1565
     *
1566
     * @return bool|TelegramResponse|UnknownObject
1567
     */
1568
    public function __call($method, $arguments)
1569
    {
1570
        if (preg_match('/^\w+Commands?/', $method, $matches)) {
1571
            return call_user_func_array([$this->getCommandBus(), $matches[0]], $arguments);
1572
        }
1573
1574
        $action = substr($method, 0, 3);
1575
        if ($action === 'get') {
1576
            /* @noinspection PhpUndefinedFunctionInspection */
1577
            $class_name = studly_case(substr($method, 3));
1578
            $class = 'Telegram\Bot\Objects\\'.$class_name;
1579 2
            $response = $this->post($method, $arguments[0] ?: []);
1580
1581 2
            if (class_exists($class)) {
1582 2
                return new $class($response->getDecodedBody());
1583
            }
1584
1585
            return $response;
1586
        }
1587
        $response = $this->post($method, $arguments[0]);
1588
1589 2
        return new UnknownObject($response->getDecodedBody());
1590
    }
1591 2
1592
    /**
1593
     * Set the IoC Container.
1594
     *
1595
     * @param $container Container instance
1596
     *
1597
     * @return void
1598
     */
1599
    public static function setContainer(Container $container)
1600
    {
1601
        self::$container = $container;
1602
    }
1603
1604
    /**
1605
     * Get the IoC Container.
1606
     *
1607 40
     * @return Container
1608
     */
1609 40
    public function getContainer()
1610
    {
1611
        return self::$container;
1612
    }
1613
1614
    /**
1615
     * Check if IoC Container has been set.
1616
     *
1617 2
     * @return boolean
1618
     */
1619 2
    public function hasContainer()
1620
    {
1621 2
        return self::$container !== null;
1622
    }
1623
1624
    /**
1625
     * @return int
1626
     */
1627 40
    public function getTimeOut()
1628
    {
1629 40
        return $this->timeOut;
1630
    }
1631
1632
    /**
1633
     * @param int $timeOut
1634
     *
1635
     * @return $this
1636
     */
1637 2
    public function setTimeOut($timeOut)
1638
    {
1639 2
        $this->timeOut = $timeOut;
1640
1641 2
        return $this;
1642
    }
1643
1644
    /**
1645
     * @return int
1646
     */
1647
    public function getConnectTimeOut()
1648
    {
1649
        return $this->connectTimeOut;
1650
    }
1651 6
1652
    /**
1653 6
     * @param int $connectTimeOut
1654
     *
1655
     * @return $this
1656
     */
1657
    public function setConnectTimeOut($connectTimeOut)
1658
    {
1659
        $this->connectTimeOut = $connectTimeOut;
1660
1661
        return $this;
1662
    }
1663
1664
    /**
1665 16
     * An alias for getUpdates that helps readability.
1666
     *
1667
     * @param $params
1668 16
     *
1669 2
     * @return Objects\Update[]
1670
     */
1671
    protected function markUpdateAsRead($params)
1672
    {
1673
        return $this->getUpdates($params, false);
1674 14
    }
1675
1676
    /**
1677
     * Determines if the string passed to be uploaded is a valid
1678
     * file on the local file system, or a valid remote URL.
1679 14
     *
1680
     * @param string $name
1681
     * @param string $contents
1682
     *
1683
     * @return bool
1684 14
     */
1685
    protected function isValidFileOrUrl($name, $contents)
1686
    {
1687
        //Don't try to open a url as an actual file when using this method to setWebhook.
1688
        if ($name == 'url') {
1689
            return false;
1690
        }
1691
1692
        //If a certificate name is passed, we must check for the file existing on the local server,
1693
        // otherwise telegram ignores the fact it wasn't sent and no error is thrown.
1694
        if ($name == 'certificate') {
1695
            return true;
1696
        }
1697
1698
        //Is the content a valid file name.
1699
        if (is_readable($contents)) {
1700
            return true;
1701
        }
1702
1703
        //Is the content a valid URL
1704
        return filter_var($contents, FILTER_VALIDATE_URL);
1705
    }
1706
}
1707