Completed
Pull Request — master (#698)
by
unknown
11:42
created

Api::deleteMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
     * Delete a message, including service messages.
954
     *
955
     * <code>
956
     * $params = [
957
     *   'chat_id'                  => '',
958
     *   'message_id'               => '',
959
     * ];
960
     * </code>
961
     *
962
     * @link https://core.telegram.org/bots/api#editmessagereplymarkup
963
     *
964
     * @param array $params [
965
     *
966
     * @var int|string $chat_id           Required. Unique identifier for the target chat or username of the target channel (in the format @channelusername)
967
     * @var int        $message_id        Required. Identifier of the message to delete.
968
     *
969
     * ]
970
     *
971
     * @throws TelegramSDKException
972
     *
973
     * @return Message|bool
974
     */
975
    public function deleteMessage(array $params)
976
    {
977
        $response = $this->post('deleteMessage', $params);
978
979
        return new Message($response->getDecodedBody());
980
    }
981
982
983
    /**
984
     * Edit text messages sent by the bot or via the bot (for inline bots).
985
     *
986
     * <code>
987
     * $params = [
988
     *   'chat_id'                  => '',
989
     *   'message_id'               => '',
990
     *   'inline_message_id'        => '',
991
     *   'text'                     => '',
992
     *   'parse_mode'               => '',
993
     *   'disable_web_page_preview' => '',
994
     *   'reply_markup'             => '',
995
     * ];
996
     * </code>
997
     *
998
     * @link https://core.telegram.org/bots/api#editMessageText
999
     *
1000
     * @param array    $params
1001
     *
1002
     * @var int|string $params ['chat_id']
1003
     * @var int        $params ['message_id']
1004
     * @var string     $params ['inline_message_id']
1005
     * @var string     $params ['text']
1006
     * @var string     $params ['parse_mode']
1007
     * @var bool       $params ['disable_web_page_preview']
1008
     * @var string     $params ['reply_markup']
1009
     *
1010
     * @throws TelegramSDKException
1011
     *
1012
     * @return Message|bool
1013
     */
1014
    public function editMessageText(array $params)
1015
    {
1016
        $response = $this->post('editMessageText', $params);
1017
1018
        return new Message($response->getDecodedBody());
1019
    }
1020
1021
    /**
1022
     * Edit captions of messages sent by the bot or via the bot (for inline bots).
1023
     *
1024
     * <code>
1025
     * $params = [
1026
     *   'chat_id'                  => '',
1027
     *   'message_id'               => '',
1028
     *   'inline_message_id'        => '',
1029
     *   'caption'                  => '',
1030
     *   'reply_markup'             => '',
1031
     * ];
1032
     * </code>
1033
     *
1034
     * @link https://core.telegram.org/bots/api#editMessageCaption
1035
     *
1036
     * @param array    $params
1037
     *
1038
     * @var int|string $params ['chat_id']
1039
     * @var int        $params ['message_id']
1040
     * @var string     $params ['inline_message_id']
1041
     * @var string     $params ['caption']
1042
     * @var string     $params ['reply_markup']
1043
     *
1044
     * @throws TelegramSDKException
1045
     *
1046
     * @return Message|bool
1047
     */
1048
    public function editMessageCaption(array $params)
1049
    {
1050
        $response = $this->post('editMessageCaption', $params);
1051
1052
        return new Message($response->getDecodedBody());
1053
    }
1054
1055
    /**
1056
     * Edit only the reply markup of messages sent by the bot or via the bot (for inline bots).
1057
     *
1058
     * <code>
1059
     * $params = [
1060
     *   'chat_id'                  => '',
1061
     *   'message_id'               => '',
1062
     *   'inline_message_id'        => '',
1063
     *   'reply_markup'             => '',
1064
     * ];
1065
     * </code>
1066
     *
1067
     * @link https://core.telegram.org/bots/api#editMessageReplyMarkup
1068
     *
1069
     * @param array    $params
1070
     *
1071
     * @var int|string $params ['chat_id']
1072
     * @var int        $params ['message_id']
1073
     * @var string     $params ['inline_message_id']
1074
     * @var string     $params ['reply_markup']
1075
     *
1076
     * @throws TelegramSDKException
1077
     *
1078
     * @return Message|bool
1079
     */
1080
    public function editMessageReplyMarkup(array $params)
1081
    {
1082
        $response = $this->post('editMessageReplyMarkup', $params);
1083
1084
        return new Message($response->getDecodedBody());
1085
    }
1086
1087
    /**
1088
     * Use this method to send answers to an inline query.
1089
     *
1090
     * <code>
1091
     * $params = [
1092
     *   'inline_query_id'      => '',
1093
     *   'results'              => [],
1094
     *   'cache_time'           => 0,
1095
     *   'is_personal'          => false,
1096
     *   'next_offset'          => '',
1097
     *   'switch_pm_text'       => '',
1098
     *   'switch_pm_parameter'  => '',
1099
     * ];
1100
     * </code>
1101
     *
1102
     * @link https://core.telegram.org/bots/api#answerinlinequery
1103
     *
1104
     * @param array     $params
1105
     *
1106
     * @var string      $params ['inline_query_id']
1107
     * @var array       $params ['results']
1108
     * @var int|null    $params ['cache_time']
1109
     * @var bool|null   $params ['is_personal']
1110
     * @var string|null $params ['next_offset']
1111
     * @var string|null $params ['switch_pm_text']
1112
     * @var string|null $params ['switch_pm_parameter']
1113
     *
1114
     * @throws TelegramSDKException
1115
     *
1116
     * @return bool
1117
     */
1118
    public function answerInlineQuery(array $params = [])
1119 6
    {
1120
        if (is_array($params['results'])) {
1121 6
            $params['results'] = json_encode($params['results']);
1122 2
        }
1123
1124
        $this->post('answerInlineQuery', $params);
1125 4
1126 2
        return true;
1127
    }
1128
1129 2
    /**
1130
     * Set a Webhook to receive incoming updates via an outgoing webhook.
1131
     *
1132
     * <code>
1133
     * $params = [
1134
     *   'url'         => '',
1135
     *   'certificate' => '',
1136
     * ];
1137
     * </code>
1138
     *
1139
     * @link https://core.telegram.org/bots/api#setwebhook
1140
     *
1141
     * @param array $params
1142
     *
1143
     * @var string  $params ['url']         HTTPS url to send updates to.
1144
     * @var string  $params ['certificate'] Upload your public key certificate so that the root certificate in
1145
     *                                      use can be checked.
1146
     *
1147
     * @throws TelegramSDKException
1148
     *
1149
     * @return TelegramResponse
1150
     */
1151
    public function setWebhook(array $params)
1152
    {
1153
        if (filter_var($params['url'], FILTER_VALIDATE_URL) === false) {
1154
            throw new TelegramSDKException('Invalid URL Provided');
1155
        }
1156
1157
        if (parse_url($params['url'], PHP_URL_SCHEME) !== 'https') {
1158
            throw new TelegramSDKException('Invalid URL, should be a HTTPS url.');
1159
        }
1160
1161
        return $this->uploadFile('setWebhook', $params);
1162
    }
1163
1164
    /**
1165
     * Returns a webhook update sent by Telegram.
1166
     * Works only if you set a webhook.
1167
     *
1168
     * @see setWebhook
1169
     *
1170
     * @return Update
1171
     */
1172
    public function getWebhookUpdate($shouldEmitEvent = true)
1173
    {
1174
        $body = json_decode(file_get_contents('php://input'), true);
1175 2
1176
        $update = new Update($body);
1177 2
1178
        if ($shouldEmitEvent) {
1179 2
            $this->emitEvent(new UpdateWasReceived($update, $this));
1180
        }
1181
1182
        return $update;
1183
    }
1184
1185
    /**
1186
     * Alias for getWebhookUpdate
1187
     *
1188
     * @deprecated Call method getWebhookUpdate (note lack of letter s at end)
1189
     *             To be removed in next major version.
1190
     *
1191
     * @param bool $shouldEmitEvent
1192
     *
1193
     * @return Update
1194
     */
1195
    public function getWebhookUpdates($shouldEmitEvent = true)
1196
    {
1197
        return $this->getWebhookUpdate($shouldEmitEvent);
1198
    }
1199
1200
    /**
1201
     * Removes the outgoing webhook (if any).
1202
     *
1203
     * @throws TelegramSDKException
1204
     *
1205 6
     * @return TelegramResponse
1206
     */
1207 6
    public function removeWebhook()
1208
    {
1209 6
        $url = '';
1210
1211
        return $this->post('setWebhook', compact('url'));
1212 6
    }
1213
1214 6
    /**
1215 6
     * Use this method to receive incoming updates using long polling.
1216 6
     *
1217
     * <code>
1218 6
     * $params = [
1219 6
     *   'offset'  => '',
1220 6
     *   'limit'   => '',
1221
     *   'timeout' => '',
1222
     * ];
1223
     * </code>
1224
     *
1225
     * @link https://core.telegram.org/bots/api#getupdates
1226
     *
1227
     * @param array  $params
1228
     * @param bool   $shouldEmitEvents
1229
     * @var int|null $params ['offset']
1230
     * @var int|null $params ['limit']
1231
     * @var int|null $params ['timeout']
1232
     *
1233
     * @throws TelegramSDKException
1234
     *
1235
     * @return Update[]
1236
     */
1237
    public function getUpdates(array $params = [], $shouldEmitEvents = true)
1238
    {
1239
        $response = $this->post('getUpdates', $params);
1240
1241
        return collect($response->getResult())
1242
            ->map(function ($data) use ($shouldEmitEvents) {
1243
1244
                $update = new Update($data);
1245
1246
                if ($shouldEmitEvents) {
1247
                    $this->emitEvent(new UpdateWasReceived($update, $this));
1248
                }
1249
1250
                return $update;
1251
            })
1252
            ->all();
1253
    }
1254
1255
1256
    /**
1257
     * Builds a custom keyboard markup.
1258
     *
1259
     * <code>
1260
     * $params = [
1261
     *   'keyboard'          => '',
1262
     *   'resize_keyboard'   => '',
1263
     *   'one_time_keyboard' => '',
1264
     *   'selective'         => '',
1265
     * ];
1266
     * </code>
1267
     *
1268
     * @deprecated Use Telegram\Bot\Keyboard\Keyboard::make(array $params = []) instead.
1269
     *             To be removed in next major version.
1270
     *
1271
     * @link       https://core.telegram.org/bots/api#replykeyboardmarkup
1272
     *
1273
     * @param array $params
1274
     *
1275
     * @var array   $params ['keyboard']
1276
     * @var bool    $params ['resize_keyboard']
1277
     * @var bool    $params ['one_time_keyboard']
1278
     * @var bool    $params ['selective']
1279
     *
1280
     * @return string
1281
     */
1282
    public function replyKeyboardMarkup(array $params)
1283
    {
1284
        return Keyboard::make($params);
1285
    }
1286
1287
    /**
1288
     * Hide the current custom keyboard and display the default letter-keyboard.
1289
     *
1290
     * <code>
1291
     * $params = [
1292
     *   'hide_keyboard' => true,
1293
     *   'selective'     => false,
1294
     * ];
1295
     * </code>
1296
     *
1297
     * @deprecated Use Telegram\Bot\Keyboard\Keyboard::hide(array $params = []) instead.
1298
     *             To be removed in next major version.
1299
     *
1300
     * @link       https://core.telegram.org/bots/api#replykeyboardhide
1301
     *
1302
     * @param array $params
1303
     *
1304
     * @var bool    $params ['hide_keyboard']
1305
     * @var bool    $params ['selective']
1306
     *
1307
     * @return string
1308
     */
1309
    public static function replyKeyboardHide(array $params = [])
1310
    {
1311
        return Keyboard::hide($params);
1312
    }
1313
1314
    /**
1315
     * Display a reply interface to the user (act as if the user has selected the bot‘s message and tapped ’Reply').
1316 6
     *
1317
     * <code>
1318 6
     * $params = [
1319
     *   'force_reply' => true,
1320
     *   'selective'   => false,
1321
     * ];
1322
     * </code>
1323
     *
1324
     * @deprecated Use Telegram\Bot\Keyboard\Keyboard::forceReply(array $params = []) instead.
1325 6
     *             To be removed in next major version.
1326 6
     *
1327
     * @link       https://core.telegram.org/bots/api#forcereply
1328 6
     *
1329 6
     * @param array $params
1330 6
     *
1331 6
     * @var bool    $params ['force_reply']
1332
     * @var bool    $params ['selective']
1333
     *
1334 6
     * @return Keyboard
1335 6
     */
1336 6
    public static function forceReply(array $params = [])
1337 6
    {
1338 6
        return Keyboard::forceReply($params);
1339 6
    }
1340
1341 6
    /**
1342
     * Processes Inbound Commands.
1343
     *
1344
     * @param bool  $webhook
1345
     * @param array $params
1346
     *
1347
     * @return Update|Update[]
1348
     */
1349 6
    public function commandsHandler($webhook = false, array $params = [])
1350
    {
1351 6
        if ($webhook) {
1352
            $update = $this->getWebhookUpdate();
1353 6
            $this->processCommand($update);
1354 6
1355 6
            return $update;
1356 6
        }
1357
1358
        $updates = $this->getUpdates($params);
1359
        $highestId = -1;
1360
1361
        foreach ($updates as $update) {
1362
            $highestId = $update->getUpdateId();
1363
            $this->processCommand($update);
1364
        }
1365
1366
        //An update is considered confirmed as soon as getUpdates is called with an offset higher than its update_id.
1367
        if ($highestId != -1) {
1368
            $params = [];
1369
            $params['offset'] = $highestId + 1;
1370
            $params['limit'] = 1;
1371
            $this->markUpdateAsRead($params);
1372
        }
1373
1374
        return $updates;
1375
    }
1376
1377
    /**
1378
     * Check update object for a command and process.
1379
     *
1380
     * @param Update $update
1381
     */
1382
    public function processCommand(Update $update)
1383
    {
1384
        $message = $update->getMessage();
1385
1386
        if ($message !== null && $message->has('text')) {
1387
            $this->getCommandBus()->handler($message->getText(), $update);
1388
        }
1389
    }
1390
1391
    /**
1392
     * Helper to Trigger Commands.
1393
     *
1394
     * @param string $name   Command Name
1395
     * @param Update $update Update Object
1396
     *
1397
     * @return mixed
1398
     */
1399
    public function triggerCommand($name, Update $update)
1400
    {
1401
        return $this->getCommandBus()->execute($name, $update->getMessage()->getText(), $update);
1402
    }
1403
1404
    /**
1405
     * Determine if a given type is the message.
1406
     *
1407
     * @deprecated Call method isType directly on Message object
1408
     *             To be removed in next major version.
1409
     *
1410
     * @param string         $type
1411
     * @param Update|Message $object
1412
     *
1413
     * @throws \ErrorException
1414
     *
1415
     */
1416
    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...
1417
    {
1418
        trigger_error('This method has been deprecated. Use isType() on the Message object instead.', E_USER_DEPRECATED);
1419
    }
1420
1421
    /**
1422
     * Detect Message Type Based on Update or Message Object.
1423
     *
1424
     * @deprecated Call method detectType directly on Message object
1425
     *             To be removed in next major version.
1426
     *
1427
     * @param Update|Message $object
1428
     *
1429
     * @throws \ErrorException
1430
     *
1431
     * @return string|null
1432
     */
1433
    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...
1434
    {
1435
        trigger_error('This method has been deprecated. Use detectType() on the Message object instead.', E_USER_DEPRECATED);
1436
    }
1437 40
1438
    /**
1439 40
     * Sends a GET request to Telegram Bot API and returns the result.
1440 16
     *
1441 16
     * @param string $endpoint
1442
     * @param array  $params
1443 24
     *
1444
     * @throws TelegramSDKException
1445
     *
1446
     * @return TelegramResponse
1447 24
     */
1448
    protected function get($endpoint, $params = [])
1449
    {
1450 40
        if (array_key_exists('reply_markup', $params)) {
1451 40
            $params['reply_markup'] = (string)$params['reply_markup'];
1452 40
        }
1453
1454 40
        return $this->sendRequest(
1455
            'GET',
1456
            $endpoint,
1457
            $params
1458
        );
1459
    }
1460
1461
    /**
1462
     * Sends a POST request to Telegram Bot API and returns the result.
1463
     *
1464
     * @param string $endpoint
1465
     * @param array  $params
1466
     * @param bool   $fileUpload Set true if a file is being uploaded.
1467
     *
1468 16
     * @return TelegramResponse
1469
     */
1470 16
    protected function post($endpoint, array $params = [], $fileUpload = false)
1471
    {
1472 16
        if ($fileUpload) {
1473 16
            $params = ['multipart' => $params];
1474 16
        } else {
1475
1476 16
            if (array_key_exists('reply_markup', $params)) {
1477
                $params['reply_markup'] = (string)$params['reply_markup'];
1478
            }
1479
1480
            $params = ['form_params' => $params];
1481 16
        }
1482 16
1483 16
        return $this->sendRequest(
1484 16
            'POST',
1485
            $endpoint,
1486 16
            $params
1487 16
        );
1488
    }
1489 16
1490
    /**
1491
     * Sends a multipart/form-data request to Telegram Bot API and returns the result.
1492
     * Used primarily for file uploads.
1493
     *
1494
     * @param string $endpoint
1495
     * @param array  $params
1496
     *
1497
     * @throws TelegramSDKException
1498
     *
1499
     * @return TelegramResponse
1500
     */
1501
    protected function uploadFile($endpoint, array $params = [])
1502
    {
1503 40
        $multipart_params = collect($params)
1504
            ->reject(function ($value) {
1505
                return is_null($value);
1506
            })
1507
            ->map(function ($contents, $name) {
1508 40
1509
                if (!is_resource($contents) && $this->isValidFileOrUrl($name, $contents)) {
1510 40
                    $contents = (new InputFile($contents))->open();
1511
                }
1512
1513
                return [
1514
                    'name'     => $name,
1515
                    'contents' => $contents,
1516
                ];
1517
            })
1518
            //Reset the keys on the collection
1519
            ->values()
1520
            ->all();
1521
1522 40
        return $this->post($endpoint, $multipart_params, true);
1523
    }
1524
1525
    /**
1526
     * Sends a request to Telegram Bot API and returns the result.
1527 40
     *
1528 40
     * @param string $method
1529 40
     * @param string $endpoint
1530 40
     * @param array  $params
1531 40
     *
1532 40
     * @throws TelegramSDKException
1533 40
     *
1534 40
     * @return TelegramResponse
1535 40
     */
1536
    protected function sendRequest(
1537
        $method,
1538
        $endpoint,
1539
        array $params = []
1540
    ) {
1541
        $request = $this->request($method, $endpoint, $params);
1542
1543
        return $this->lastResponse = $this->client->sendRequest($request);
1544
    }
1545
1546
    /**
1547
     * Instantiates a new TelegramRequest entity.
1548 2
     *
1549
     * @param string $method
1550 2
     * @param string $endpoint
1551 2
     * @param array  $params
1552
     *
1553
     * @return TelegramRequest
1554
     */
1555
    protected function request(
1556
        $method,
1557
        $endpoint,
1558
        array $params = []
1559
    ) {
1560
        return new TelegramRequest(
1561
            $this->getAccessToken(),
1562
            $method,
1563
            $endpoint,
1564
            $params,
1565
            $this->isAsyncRequest(),
1566
            $this->getTimeOut(),
1567
            $this->getConnectTimeOut()
1568
        );
1569
    }
1570
1571
    /**
1572
     * Magic method to process any "get" requests.
1573
     *
1574
     * @param $method
1575
     * @param $arguments
1576
     *
1577
     * @throws TelegramSDKException
1578
     *
1579 2
     * @return bool|TelegramResponse|UnknownObject
1580
     */
1581 2
    public function __call($method, $arguments)
1582 2
    {
1583
        if (preg_match('/^\w+Commands?/', $method, $matches)) {
1584
            return call_user_func_array([$this->getCommandBus(), $matches[0]], $arguments);
1585
        }
1586
1587
        $action = substr($method, 0, 3);
1588
        if ($action === 'get') {
1589 2
            /* @noinspection PhpUndefinedFunctionInspection */
1590
            $class_name = Str::studly(substr($method, 3));
1591 2
            $class = 'Telegram\Bot\Objects\\'.$class_name;
1592
            $response = $this->post($method, $arguments[0] ?: []);
1593
1594
            if (class_exists($class)) {
1595
                return new $class($response->getDecodedBody());
1596
            }
1597
1598
            return $response;
1599
        }
1600
        $response = $this->post($method, $arguments[0]);
1601
1602
        return new UnknownObject($response->getDecodedBody());
1603
    }
1604
1605
    /**
1606
     * Set the IoC Container.
1607 40
     *
1608
     * @param $container Container instance
1609 40
     *
1610
     * @return void
1611
     */
1612
    public static function setContainer(Container $container)
1613
    {
1614
        self::$container = $container;
1615
    }
1616
1617 2
    /**
1618
     * Get the IoC Container.
1619 2
     *
1620
     * @return Container
1621 2
     */
1622
    public function getContainer()
1623
    {
1624
        return self::$container;
1625
    }
1626
1627 40
    /**
1628
     * Check if IoC Container has been set.
1629 40
     *
1630
     * @return boolean
1631
     */
1632
    public function hasContainer()
1633
    {
1634
        return self::$container !== null;
1635
    }
1636
1637 2
    /**
1638
     * @return int
1639 2
     */
1640
    public function getTimeOut()
1641 2
    {
1642
        return $this->timeOut;
1643
    }
1644
1645
    /**
1646
     * @param int $timeOut
1647
     *
1648
     * @return $this
1649
     */
1650
    public function setTimeOut($timeOut)
1651 6
    {
1652
        $this->timeOut = $timeOut;
1653 6
1654
        return $this;
1655
    }
1656
1657
    /**
1658
     * @return int
1659
     */
1660
    public function getConnectTimeOut()
1661
    {
1662
        return $this->connectTimeOut;
1663
    }
1664
1665 16
    /**
1666
     * @param int $connectTimeOut
1667
     *
1668 16
     * @return $this
1669 2
     */
1670
    public function setConnectTimeOut($connectTimeOut)
1671
    {
1672
        $this->connectTimeOut = $connectTimeOut;
1673
1674 14
        return $this;
1675
    }
1676
1677
    /**
1678
     * An alias for getUpdates that helps readability.
1679 14
     *
1680
     * @param $params
1681
     *
1682
     * @return Objects\Update[]
1683
     */
1684 14
    protected function markUpdateAsRead($params)
1685
    {
1686
        return $this->getUpdates($params, false);
1687
    }
1688
1689
    /**
1690
     * Determines if the string passed to be uploaded is a valid
1691
     * file on the local file system, or a valid remote URL.
1692
     *
1693
     * @param string $name
1694
     * @param string $contents
1695
     *
1696
     * @return bool
1697
     */
1698
    protected function isValidFileOrUrl($name, $contents)
1699
    {
1700
        //Don't try to open a url as an actual file when using this method to setWebhook.
1701
        if ($name == 'url') {
1702
            return false;
1703
        }
1704
1705
        //If a certificate name is passed, we must check for the file existing on the local server,
1706
        // otherwise telegram ignores the fact it wasn't sent and no error is thrown.
1707
        if ($name == 'certificate') {
1708
            return true;
1709
        }
1710
1711
        //Is the content a valid file name.
1712
        if (is_readable($contents)) {
1713
            return true;
1714
        }
1715
1716
        //Is the content a valid URL
1717
        return filter_var($contents, FILTER_VALIDATE_URL);
1718
    }
1719
}
1720