Passed
Pull Request — master (#314)
by Eldar
02:38
created

BotApi::copyMessage()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 25

Duplication

Lines 25
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 25
loc 25
ccs 0
cts 13
cp 0
rs 9.52
c 0
b 0
f 0
cc 2
nc 1
nop 10
crap 6

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace TelegramBot\Api;
4
5
use TelegramBot\Api\Types\ArrayOfBotCommand;
6
use TelegramBot\Api\Types\ArrayOfChatMemberEntity;
7
use TelegramBot\Api\Types\ArrayOfMessageEntity;
8
use TelegramBot\Api\Types\ArrayOfMessages;
9
use TelegramBot\Api\Types\ArrayOfUpdates;
10
use TelegramBot\Api\Types\Chat;
11
use TelegramBot\Api\Types\ChatMember;
12
use TelegramBot\Api\Types\File;
13
use TelegramBot\Api\Types\Inline\QueryResult\AbstractInlineQueryResult;
14
use TelegramBot\Api\Types\InputMedia\ArrayOfInputMedia;
15
use TelegramBot\Api\Types\InputMedia\InputMedia;
16
use TelegramBot\Api\Types\Message;
17
use TelegramBot\Api\Types\Poll;
18
use TelegramBot\Api\Types\Update;
19
use TelegramBot\Api\Types\User;
20
use TelegramBot\Api\Types\UserProfilePhotos;
21
use TelegramBot\Api\Types\WebhookInfo;
22
23
/**
24
 * Class BotApi
25
 *
26
 * @package TelegramBot\Api
27
 */
28
class BotApi
29
{
30
    /**
31
     * HTTP codes
32
     *
33
     * @var array
34
     */
35
    public static $codes = [
36
        // Informational 1xx
37
        100 => 'Continue',
38
        101 => 'Switching Protocols',
39
        102 => 'Processing',            // RFC2518
40
        // Success 2xx
41
        200 => 'OK',
42
        201 => 'Created',
43
        202 => 'Accepted',
44
        203 => 'Non-Authoritative Information',
45
        204 => 'No Content',
46
        205 => 'Reset Content',
47
        206 => 'Partial Content',
48
        207 => 'Multi-Status',          // RFC4918
49
        208 => 'Already Reported',      // RFC5842
50
        226 => 'IM Used',               // RFC3229
51
        // Redirection 3xx
52
        300 => 'Multiple Choices',
53
        301 => 'Moved Permanently',
54
        302 => 'Found', // 1.1
55
        303 => 'See Other',
56
        304 => 'Not Modified',
57
        305 => 'Use Proxy',
58
        // 306 is deprecated but reserved
59
        307 => 'Temporary Redirect',
60
        308 => 'Permanent Redirect',    // RFC7238
61
        // Client Error 4xx
62
        400 => 'Bad Request',
63
        401 => 'Unauthorized',
64
        402 => 'Payment Required',
65
        403 => 'Forbidden',
66
        404 => 'Not Found',
67
        405 => 'Method Not Allowed',
68
        406 => 'Not Acceptable',
69
        407 => 'Proxy Authentication Required',
70
        408 => 'Request Timeout',
71
        409 => 'Conflict',
72
        410 => 'Gone',
73
        411 => 'Length Required',
74
        412 => 'Precondition Failed',
75
        413 => 'Payload Too Large',
76
        414 => 'URI Too Long',
77
        415 => 'Unsupported Media Type',
78
        416 => 'Range Not Satisfiable',
79
        417 => 'Expectation Failed',
80
        422 => 'Unprocessable Entity',                                        // RFC4918
81
        423 => 'Locked',                                                      // RFC4918
82
        424 => 'Failed Dependency',                                           // RFC4918
83
        425 => 'Reserved for WebDAV advanced collections expired proposal',   // RFC2817
84
        426 => 'Upgrade Required',                                            // RFC2817
85
        428 => 'Precondition Required',                                       // RFC6585
86
        429 => 'Too Many Requests',                                           // RFC6585
87
        431 => 'Request Header Fields Too Large',                             // RFC6585
88
        // Server Error 5xx
89
        500 => 'Internal Server Error',
90
        501 => 'Not Implemented',
91
        502 => 'Bad Gateway',
92
        503 => 'Service Unavailable',
93
        504 => 'Gateway Timeout',
94
        505 => 'HTTP Version Not Supported',
95
        506 => 'Variant Also Negotiates (Experimental)',                      // RFC2295
96
        507 => 'Insufficient Storage',                                        // RFC4918
97
        508 => 'Loop Detected',                                               // RFC5842
98
        510 => 'Not Extended',                                                // RFC2774
99
        511 => 'Network Authentication Required',                             // RFC6585
100
    ];
101
102
    private $proxySettings = [];
103
104
    /**
105
     * Default http status code
106
     */
107
    const DEFAULT_STATUS_CODE = 200;
108
109
    /**
110
     * Not Modified http status code
111
     */
112
    const NOT_MODIFIED_STATUS_CODE = 304;
113
114
    /**
115
     * Limits for tracked ids
116
     */
117
    const MAX_TRACKED_EVENTS = 200;
118
119
    /**
120
     * Url prefixes
121
     */
122
    const URL_PREFIX = 'https://api.telegram.org/bot';
123
124
    /**
125
     * Url prefix for files
126
     */
127
    const FILE_URL_PREFIX = 'https://api.telegram.org/file/bot';
128
129
    /**
130
     * CURL object
131
     *
132
     * @var
133
     */
134
    protected $curl;
135
136
    /**
137
     * CURL custom options
138
     *
139
     * @var array
140
     */
141
    protected $customCurlOptions = [];
142
143
    /**
144
     * Bot token
145
     *
146
     * @var string
147
     */
148
    protected $token;
149
150
    /**
151
     * Botan tracker
152
     *
153
     * @var \TelegramBot\Api\Botan
154
     */
155
    protected $tracker;
156
157
    /**
158
     * list of event ids
159
     *
160
     * @var array
161
     */
162
    protected $trackedEvents = [];
163
164
    /**
165
     * Check whether return associative array
166
     *
167
     * @var bool
168
     */
169
    protected $returnArray = true;
170
171
    /**
172
     * Constructor
173
     *
174
     * @param string $token Telegram Bot API token
175
     * @param string|null $trackerToken Yandex AppMetrica application api_key
176
     */
177 9
    public function __construct($token, $trackerToken = null)
178
    {
179 9
        $this->curl = curl_init();
180 9
        $this->token = $token;
181
182 9
        if ($trackerToken) {
183
            $this->tracker = new Botan($trackerToken);
184
        }
185 9
    }
186
187
    /**
188
     * Set return array
189
     *
190
     * @param bool $mode
191
     *
192
     * @return $this
193
     */
194
    public function setModeObject($mode = true)
195
    {
196
        $this->returnArray = !$mode;
197
198
        return $this;
199
    }
200
201
202
    /**
203
     * Call method
204
     *
205
     * @param string $method
206
     * @param array|null $data
207
     *
208
     * @return mixed
209
     * @throws \TelegramBot\Api\Exception
210
     * @throws \TelegramBot\Api\HttpException
211
     * @throws \TelegramBot\Api\InvalidJsonException
212
     */
213
    public function call($method, array $data = null)
214
    {
215
        $options = $this->proxySettings + [
216
            CURLOPT_URL => $this->getUrl().'/'.$method,
217
            CURLOPT_RETURNTRANSFER => true,
218
            CURLOPT_POST => null,
219
            CURLOPT_POSTFIELDS => null,
220
            CURLOPT_TIMEOUT => 5,
221
        ];
222
223
        if ($data) {
224
            $options[CURLOPT_POST] = true;
225
            $options[CURLOPT_POSTFIELDS] = $data;
226
        }
227
228
        if (!empty($this->customCurlOptions) && is_array($this->customCurlOptions)) {
229
            $options = $this->customCurlOptions + $options;
230
        }
231
232
        $response = self::jsonValidate($this->executeCurl($options), $this->returnArray);
233
234
        if ($this->returnArray) {
235
            if (!isset($response['ok']) || !$response['ok']) {
236
                throw new Exception($response['description'], $response['error_code']);
237
            }
238
239
            return $response['result'];
240
        }
241
242
        if (!$response->ok) {
243
            throw new Exception($response->description, $response->error_code);
244
        }
245
246
        return $response->result;
247
    }
248
249
    /**
250
     * curl_exec wrapper for response validation
251
     *
252
     * @param array $options
253
     *
254
     * @return string
255
     *
256
     * @throws \TelegramBot\Api\HttpException
257
     */
258
    protected function executeCurl(array $options)
259
    {
260
        curl_setopt_array($this->curl, $options);
261
262
        $result = curl_exec($this->curl);
263
        self::curlValidate($this->curl, $result);
264
        if ($result === false) {
265
            throw new HttpException(curl_error($this->curl), curl_errno($this->curl));
266
        }
267
268
        return $result;
269
    }
270
271
    /**
272
     * Response validation
273
     *
274
     * @param resource $curl
275
     * @param string $response
276
     * @throws HttpException
277
     */
278
    public static function curlValidate($curl, $response = null)
279
    {
280
        $json = json_decode($response, true)?: [];
281
        if (($httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE))
282
            && !in_array($httpCode, [self::DEFAULT_STATUS_CODE, self::NOT_MODIFIED_STATUS_CODE])
283
        ) {
284
            $errorDescription = array_key_exists('description', $json) ? $json['description'] : self::$codes[$httpCode];
285
            $errorParameters = array_key_exists('parameters', $json) ? $json['parameters'] : [];
286
            throw new HttpException($errorDescription, $httpCode, null, $errorParameters);
287
        }
288
    }
289
290
    /**
291
     * JSON validation
292
     *
293
     * @param string $jsonString
294
     * @param boolean $asArray
295
     *
296
     * @return object|array
297
     * @throws \TelegramBot\Api\InvalidJsonException
298
     */
299
    public static function jsonValidate($jsonString, $asArray)
300
    {
301
        $json = json_decode($jsonString, $asArray);
302
303
        if (json_last_error() != JSON_ERROR_NONE) {
304
            throw new InvalidJsonException(json_last_error_msg(), json_last_error());
305
        }
306
307
        return $json;
308
    }
309
310
    /**
311
     * Use this method to send text messages. On success, the sent \TelegramBot\Api\Types\Message is returned.
312
     *
313
     * @param int|string $chatId
314
     * @param string $text
315
     * @param string|null $parseMode
316
     * @param bool $disablePreview
317
     * @param int|null $replyToMessageId
318
     * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|
319
     *        Types\ReplyKeyboardRemove|null $replyMarkup
320
     * @param bool $disableNotification
321
     *
322
     * @return \TelegramBot\Api\Types\Message
323
     * @throws \TelegramBot\Api\InvalidArgumentException
324
     * @throws \TelegramBot\Api\Exception
325
     */
326
    public function sendMessage(
327
        $chatId,
328
        $text,
329
        $parseMode = null,
330
        $disablePreview = false,
331
        $replyToMessageId = null,
332
        $replyMarkup = null,
333
        $disableNotification = false
334
    ) {
335
        return Message::fromResponse($this->call('sendMessage', [
336
            'chat_id' => $chatId,
337
            'text' => $text,
338
            'parse_mode' => $parseMode,
339
            'disable_web_page_preview' => $disablePreview,
340
            'reply_to_message_id' => (int)$replyToMessageId,
341
            'reply_markup' => is_null($replyMarkup) ? $replyMarkup : $replyMarkup->toJson(),
342
            'disable_notification' => (bool)$disableNotification,
343
        ]));
344
    }
345
346
    /**
347
     * @param int|string $chatId
348
     * @param int|string $fromChatId
349
     * @param int $messageId
350
     * @param string|null $caption
351
     * @param string|null $parseMode
352
     * @param ArrayOfMessageEntity|null $captionEntities
353
     * @param bool $disableNotification
354
     * @param int|null $replyToMessageId
355
     * @param bool $allowSendingWithoutReply
356
     * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|
357
     *        Types\ReplyKeyboardRemove|null $replyMarkup
358
     *
359
     * @return Message
360
     * @throws Exception
361
     * @throws HttpException
362
     * @throws InvalidJsonException
363
     */
364 View Code Duplication
    public function copyMessage(
365
        $chatId,
366
        $fromChatId,
367
        $messageId,
368
        $caption = null,
369
        $parseMode = null,
370
        $captionEntities = null,
371
        $disableNotification = false,
372
        $replyToMessageId = null,
373
        $allowSendingWithoutReply = false,
374
        $replyMarkup = null
375
    ) {
376
        return Message::fromResponse($this->call('copyMessage', [
377
            'chat_id' => $chatId,
378
            'from_chat_id' => $fromChatId,
379
            'message_id' => (int)$messageId,
380
            'caption' => $caption,
381
            'parse_mode' => $parseMode,
382
            'caption_entities' => $captionEntities,
383
            'disable_notification' => (bool)$disableNotification,
384
            'reply_to_message_id' => (int)$replyToMessageId,
385
            'allow_sending_without_reply' => (bool)$allowSendingWithoutReply,
386
            'reply_markup' => is_null($replyMarkup) ? $replyMarkup : $replyMarkup->toJson(),
387
        ]));
388
    }
389
390
    /**
391
     * Use this method to send phone contacts
392
     *
393
     * @param int|string $chatId chat_id or @channel_name
394
     * @param string $phoneNumber
395
     * @param string $firstName
396
     * @param string $lastName
397
     * @param int|null $replyToMessageId
398
     * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|
399
     *        Types\ReplyKeyboardRemove|null $replyMarkup
400
     * @param bool $disableNotification
401
     *
402
     * @return \TelegramBot\Api\Types\Message
403
     * @throws \TelegramBot\Api\Exception
404
     */
405
    public function sendContact(
406
        $chatId,
407
        $phoneNumber,
408
        $firstName,
409
        $lastName = null,
410
        $replyToMessageId = null,
411
        $replyMarkup = null,
412
        $disableNotification = false
413
    ) {
414
        return Message::fromResponse($this->call('sendContact', [
415
            'chat_id' => $chatId,
416
            'phone_number' => $phoneNumber,
417
            'first_name' => $firstName,
418
            'last_name' => $lastName,
419
            'reply_to_message_id' => $replyToMessageId,
420
            'reply_markup' => is_null($replyMarkup) ? $replyMarkup : $replyMarkup->toJson(),
421
            'disable_notification' => (bool)$disableNotification,
422
        ]));
423
    }
424
425
    /**
426
     * Use this method when you need to tell the user that something is happening on the bot's side.
427
     * The status is set for 5 seconds or less (when a message arrives from your bot,
428
     * Telegram clients clear its typing status).
429
     *
430
     * We only recommend using this method when a response from the bot will take a noticeable amount of time to arrive.
431
     *
432
     * Type of action to broadcast. Choose one, depending on what the user is about to receive:
433
     * `typing` for text messages, `upload_photo` for photos, `record_video` or `upload_video` for videos,
434
     * `record_audio` or upload_audio for audio files, `upload_document` for general files,
435
     * `find_location` for location data.
436
     *
437
     * @param int $chatId
438
     * @param string $action
439
     *
440
     * @return bool
441
     * @throws \TelegramBot\Api\Exception
442
     */
443
    public function sendChatAction($chatId, $action)
444
    {
445
        return $this->call('sendChatAction', [
446
            'chat_id' => $chatId,
447
            'action' => $action,
448
        ]);
449
    }
450
451
    /**
452
     * Use this method to get a list of profile pictures for a user.
453
     *
454
     * @param int $userId
455
     * @param int $offset
456
     * @param int $limit
457
     *
458
     * @return \TelegramBot\Api\Types\UserProfilePhotos
459
     * @throws \TelegramBot\Api\Exception
460
     */
461
    public function getUserProfilePhotos($userId, $offset = 0, $limit = 100)
462
    {
463
        return UserProfilePhotos::fromResponse($this->call('getUserProfilePhotos', [
464
            'user_id' => (int)$userId,
465
            'offset' => (int)$offset,
466
            'limit' => (int)$limit,
467
        ]));
468
    }
469
470
    /**
471
     * Use this method to specify a url and receive incoming updates via an outgoing webhook.
472
     * Whenever there is an update for the bot, we will send an HTTPS POST request to the specified url,
473
     * containing a JSON-serialized Update.
474
     * In case of an unsuccessful request, we will give up after a reasonable amount of attempts.
475
     *
476
     * @param string $url HTTPS url to send updates to. Use an empty string to remove webhook integration
477
     * @param \CURLFile|string $certificate Upload your public key certificate
478
     *                                      so that the root certificate in use can be checked
479
     *
480
     * @return string
481
     *
482
     * @throws \TelegramBot\Api\Exception
483
     */
484
    public function setWebhook($url = '', $certificate = null)
485
    {
486
        return $this->call('setWebhook', ['url' => $url, 'certificate' => $certificate]);
487
    }
488
489
490
    /**
491
     * Use this method to clear webhook and use getUpdates again!
492
     *
493
     * @return mixed
494
     *
495
     * @throws \TelegramBot\Api\Exception
496
     */
497
    public function deleteWebhook()
498
    {
499
        return $this->call('deleteWebhook');
500
    }
501
502
    /**
503
     * Use this method to get current webhook status. Requires no parameters.
504
     * On success, returns a WebhookInfo object. If the bot is using getUpdates,
505
     * will return an object with the url field empty.
506
     *
507
     * @return \TelegramBot\Api\Types\WebhookInfo
508
     * @throws \TelegramBot\Api\Exception
509
     * @throws \TelegramBot\Api\InvalidArgumentException
510
     */
511
    public function getWebhookInfo()
512
    {
513
        return WebhookInfo::fromResponse($this->call('getWebhookInfo'));
514
    }
515
516
    /**
517
     * A simple method for testing your bot's auth token.Requires no parameters.
518
     * Returns basic information about the bot in form of a User object.
519
     *
520
     * @return \TelegramBot\Api\Types\User
521
     * @throws \TelegramBot\Api\Exception
522
     * @throws \TelegramBot\Api\InvalidArgumentException
523
     */
524
    public function getMe()
525
    {
526
        return User::fromResponse($this->call('getMe'));
527
    }
528
529
    /**
530
     * Use this method to receive incoming updates using long polling.
531
     * An Array of Update objects is returned.
532
     *
533
     * Notes
534
     * 1. This method will not work if an outgoing webhook is set up.
535
     * 2. In order to avoid getting duplicate updates, recalculate offset after each server response.
536
     *
537
     * @param int $offset
538
     * @param int $limit
539
     * @param int $timeout
540
     *
541
     * @return Update[]
542
     * @throws \TelegramBot\Api\Exception
543
     * @throws \TelegramBot\Api\InvalidArgumentException
544
     */
545 2
    public function getUpdates($offset = 0, $limit = 100, $timeout = 0)
546
    {
547 2
        $updates = ArrayOfUpdates::fromResponse($this->call('getUpdates', [
548 2
            'offset' => $offset,
549 2
            'limit' => $limit,
550 2
            'timeout' => $timeout,
551 2
        ]));
552
553 2
        if ($this->tracker instanceof Botan) {
554
            foreach ($updates as $update) {
555
                $this->trackUpdate($update);
556
            }
557
        }
558
559 2
        return $updates;
560
    }
561
562
    /**
563
     * Use this method to send point on the map. On success, the sent Message is returned.
564
     *
565
     * @param int|string                                                              $chatId
566
     * @param float                                                                   $latitude
567
     * @param float                                                                   $longitude
568
     * @param int|null                                                                $replyToMessageId
569
     * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|
570
     *        Types\ReplyKeyboardRemove|null $replyMarkup
571
     * @param bool                                                                    $disableNotification
572
     *
573
     * @param null|int                                                                $livePeriod
574
     * @return \TelegramBot\Api\Types\Message
575
     */
576
    public function sendLocation(
577
        $chatId,
578
        $latitude,
579
        $longitude,
580
        $replyToMessageId = null,
581
        $replyMarkup = null,
582
        $disableNotification = false,
583
        $livePeriod = null
584
    ) {
585
        return Message::fromResponse($this->call('sendLocation', [
586
            'chat_id'              => $chatId,
587
            'latitude'             => $latitude,
588
            'longitude'            => $longitude,
589
            'live_period'          => $livePeriod,
590
            'reply_to_message_id'  => $replyToMessageId,
591
            'reply_markup'         => is_null($replyMarkup) ? $replyMarkup : $replyMarkup->toJson(),
592
            'disable_notification' => (bool)$disableNotification,
593
        ]));
594
    }
595
596
    /**
597
     * Use this method to edit live location messages sent by the bot or via the bot (for inline bots).
598
     *
599
     * @param int|string                                                              $chatId
600
     * @param int                                                                     $messageId
601
     * @param string                                                                  $inlineMessageId
602
     * @param float                                                                   $latitude
603
     * @param float                                                                   $longitude
604
     * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|
605
     *        Types\ReplyKeyboardRemove|null $replyMarkup
606
     * @return \TelegramBot\Api\Types\Message
607
     */
608
    public function editMessageLiveLocation(
609
        $chatId,
610
        $messageId,
611
        $inlineMessageId,
612
        $latitude,
613
        $longitude,
614
        $replyMarkup = null
615
    ) {
616
        return Message::fromResponse($this->call('sendLocation', [
617
            'chat_id'           => $chatId,
618
            'message_id'        => $messageId,
619
            'inline_message_id' => $inlineMessageId,
620
            'latitude'          => $latitude,
621
            'longitude'         => $longitude,
622
            'reply_markup'      => is_null($replyMarkup) ? $replyMarkup : $replyMarkup->toJson(),
623
        ]));
624
    }
625
626
    /**
627
     * Use this method to stop updating a live location message sent by the bot or via the bot (for inline bots) before
628
     * live_period expires.
629
     *
630
     * @param int|string                                                              $chatId
631
     * @param int                                                                     $messageId
632
     * @param string                                                                  $inlineMessageId
633
     * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|
634
     *        Types\ReplyKeyboardRemove|null $replyMarkup
635
     * @return \TelegramBot\Api\Types\Message
636
     */
637
    public function stopMessageLiveLocation(
638
        $chatId,
639
        $messageId,
640
        $inlineMessageId,
641
        $replyMarkup = null
642
    ) {
643
        return Message::fromResponse($this->call('sendLocation', [
644
            'chat_id'           => $chatId,
645
            'message_id'        => $messageId,
646
            'inline_message_id' => $inlineMessageId,
647
            'reply_markup'      => is_null($replyMarkup) ? $replyMarkup : $replyMarkup->toJson(),
648
        ]));
649
    }
650
651
    /**
652
     * Use this method to send information about a venue. On success, the sent Message is returned.
653
     *
654
     * @param int|string $chatId chat_id or @channel_name
655
     * @param float $latitude
656
     * @param float $longitude
657
     * @param string $title
658
     * @param string $address
659
     * @param string|null $foursquareId
660
     * @param int|null $replyToMessageId
661
     * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|
662
     *        Types\ReplyKeyboardRemove|null $replyMarkup
663
     * @param bool $disableNotification
664
     *
665
     * @return \TelegramBot\Api\Types\Message
666
     * @throws \TelegramBot\Api\Exception
667
     */
668
    public function sendVenue(
669
        $chatId,
670
        $latitude,
671
        $longitude,
672
        $title,
673
        $address,
674
        $foursquareId = null,
675
        $replyToMessageId = null,
676
        $replyMarkup = null,
677
        $disableNotification = false
678
    ) {
679
        return Message::fromResponse($this->call('sendVenue', [
680
            'chat_id' => $chatId,
681
            'latitude' => $latitude,
682
            'longitude' => $longitude,
683
            'title' => $title,
684
            'address' => $address,
685
            'foursquare_id' => $foursquareId,
686
            'reply_to_message_id' => $replyToMessageId,
687
            'reply_markup' => is_null($replyMarkup) ? $replyMarkup : $replyMarkup->toJson(),
688
            'disable_notification' => (bool)$disableNotification,
689
        ]));
690
    }
691
692
    /**
693
     * Use this method to send .webp stickers. On success, the sent Message is returned.
694
     *
695
     * @param int|string $chatId chat_id or @channel_name
696
     * @param \CURLFile|string $sticker
697
     * @param int|null $replyToMessageId
698
     * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|
699
     *        Types\ReplyKeyboardRemove|null $replyMarkup
700
     * @param bool $disableNotification
701
     *
702
     * @return \TelegramBot\Api\Types\Message
703
     * @throws \TelegramBot\Api\InvalidArgumentException
704
     * @throws \TelegramBot\Api\Exception
705
     */
706 View Code Duplication
    public function sendSticker(
707
        $chatId,
708
        $sticker,
709
        $replyToMessageId = null,
710
        $replyMarkup = null,
711
        $disableNotification = false
712
    ) {
713
        return Message::fromResponse($this->call('sendSticker', [
714
            'chat_id' => $chatId,
715
            'sticker' => $sticker,
716
            'reply_to_message_id' => $replyToMessageId,
717
            'reply_markup' => is_null($replyMarkup) ? $replyMarkup : $replyMarkup->toJson(),
718
            'disable_notification' => (bool)$disableNotification,
719
        ]));
720
    }
721
722
    /**
723
     * Use this method to send video files,
724
     * Telegram clients support mp4 videos (other formats may be sent as Document).
725
     * On success, the sent Message is returned.
726
     *
727
     * @param int|string $chatId chat_id or @channel_name
728
     * @param \CURLFile|string $video
729
     * @param int|null $duration
730
     * @param string|null $caption
731
     * @param int|null $replyToMessageId
732
     * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|
733
     *        Types\ReplyKeyboardRemove|null $replyMarkup
734
     * @param bool $disableNotification
735
     * @param bool $supportsStreaming Pass True, if the uploaded video is suitable for streaming
736
     * @param string|null $parseMode
737
     *
738
     * @return \TelegramBot\Api\Types\Message
739
     * @throws \TelegramBot\Api\InvalidArgumentException
740
     * @throws \TelegramBot\Api\Exception
741
     */
742 View Code Duplication
    public function sendVideo(
743
        $chatId,
744
        $video,
745
        $duration = null,
746
        $caption = null,
747
        $replyToMessageId = null,
748
        $replyMarkup = null,
749
        $disableNotification = false,
750
        $supportsStreaming = false,
751
        $parseMode = null
752
    ) {
753
        return Message::fromResponse($this->call('sendVideo', [
754
            'chat_id' => $chatId,
755
            'video' => $video,
756
            'duration' => $duration,
757
            'caption' => $caption,
758
            'reply_to_message_id' => $replyToMessageId,
759
            'reply_markup' => is_null($replyMarkup) ? $replyMarkup : $replyMarkup->toJson(),
760
            'disable_notification' => (bool)$disableNotification,
761
            'supports_streaming' => (bool)$supportsStreaming,
762
            'parse_mode' => $parseMode
763
        ]));
764
    }
765
766
    /**
767
     * Use this method to send animation files (GIF or H.264/MPEG-4 AVC video without sound),
768
     * On success, the sent Message is returned.
769
     * Bots can currently send animation files of up to 50 MB in size, this limit may be changed in the future.
770
     *
771
     * @param int|string $chatId chat_id or @channel_name
772
     * @param \CURLFile|string $animation
773
     * @param int|null $duration
774
     * @param string|null $caption
775
     * @param int|null $replyToMessageId
776
     * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|
777
     *        Types\ReplyKeyboardRemove|null $replyMarkup
778
     * @param bool $disableNotification
779
     * @param string|null $parseMode
780
     *
781
     * @return \TelegramBot\Api\Types\Message
782
     * @throws \TelegramBot\Api\InvalidArgumentException
783
     * @throws \TelegramBot\Api\Exception
784
     */
785
    public function sendAnimation(
786
        $chatId,
787
        $animation,
788
        $duration = null,
789
        $caption = null,
790
        $replyToMessageId = null,
791
        $replyMarkup = null,
792
        $disableNotification = false,
793
        $parseMode = null
794
    ) {
795
        return Message::fromResponse($this->call('sendAnimation', [
796
            'chat_id' => $chatId,
797
            'animation' => $animation,
798
            'duration' => $duration,
799
            'caption' => $caption,
800
            'reply_to_message_id' => $replyToMessageId,
801
            'reply_markup' => is_null($replyMarkup) ? $replyMarkup : $replyMarkup->toJson(),
802
            'disable_notification' => (bool)$disableNotification,
803
            'parse_mode' => $parseMode
804
        ]));
805
    }
806
807
    /**
808
     * Use this method to send audio files,
809
     * if you want Telegram clients to display the file as a playable voice message.
810
     * For this to work, your audio must be in an .ogg file encoded with OPUS
811
     * (other formats may be sent as Audio or Document).
812
     * On success, the sent Message is returned.
813
     * Bots can currently send voice messages of up to 50 MB in size, this limit may be changed in the future.
814
     *
815
     * @param int|string $chatId chat_id or @channel_name
816
     * @param \CURLFile|string $voice
817
     * @param int|null $duration
818
     * @param int|null $replyToMessageId
819
     * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|
820
     *        Types\ReplyKeyboardRemove|null $replyMarkup
821
     * @param bool $disableNotification
822
     * @param string|null $parseMode
823
     *
824
     * @return \TelegramBot\Api\Types\Message
825
     * @throws \TelegramBot\Api\InvalidArgumentException
826
     * @throws \TelegramBot\Api\Exception
827
     */
828
    public function sendVoice(
829
        $chatId,
830
        $voice,
831
        $duration = null,
832
        $replyToMessageId = null,
833
        $replyMarkup = null,
834
        $disableNotification = false,
835
        $parseMode = null
836
    ) {
837
        return Message::fromResponse($this->call('sendVoice', [
838
            'chat_id' => $chatId,
839
            'voice' => $voice,
840
            'duration' => $duration,
841
            'reply_to_message_id' => $replyToMessageId,
842
            'reply_markup' => is_null($replyMarkup) ? $replyMarkup : $replyMarkup->toJson(),
843
            'disable_notification' => (bool)$disableNotification,
844
            'parse_mode' => $parseMode
845
        ]));
846
    }
847
848
    /**
849
     * Use this method to forward messages of any kind. On success, the sent Message is returned.
850
     *
851
     * @param int|string $chatId chat_id or @channel_name
852
     * @param int $fromChatId
853
     * @param int $messageId
854
     * @param bool $disableNotification
855
     *
856
     * @return \TelegramBot\Api\Types\Message
857
     * @throws \TelegramBot\Api\InvalidArgumentException
858
     * @throws \TelegramBot\Api\Exception
859
     */
860
    public function forwardMessage($chatId, $fromChatId, $messageId, $disableNotification = false)
861
    {
862
        return Message::fromResponse($this->call('forwardMessage', [
863
            'chat_id' => $chatId,
864
            'from_chat_id' => $fromChatId,
865
            'message_id' => (int)$messageId,
866
            'disable_notification' => (bool)$disableNotification,
867
        ]));
868
    }
869
870
    /**
871
     * Use this method to send audio files,
872
     * if you want Telegram clients to display them in the music player.
873
     * Your audio must be in the .mp3 format.
874
     * On success, the sent Message is returned.
875
     * Bots can currently send audio files of up to 50 MB in size, this limit may be changed in the future.
876
     *
877
     * For backward compatibility, when the fields title and performer are both empty
878
     * and the mime-type of the file to be sent is not audio/mpeg, the file will be sent as a playable voice message.
879
     * For this to work, the audio must be in an .ogg file encoded with OPUS.
880
     * This behavior will be phased out in the future. For sending voice messages, use the sendVoice method instead.
881
     *
882
     * @deprecated since 20th February. Removed backward compatibility from the method sendAudio.
883
     * Voice messages now must be sent using the method sendVoice.
884
     * There is no more need to specify a non-empty title or performer while sending the audio by file_id.
885
     *
886
     * @param int|string $chatId chat_id or @channel_name
887
     * @param \CURLFile|string $audio
888
     * @param int|null $duration
889
     * @param string|null $performer
890
     * @param string|null $title
891
     * @param int|null $replyToMessageId
892
     * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|
893
     *        Types\ReplyKeyboardRemove|null $replyMarkup
894
     * @param bool $disableNotification
895
     * @param string|null $parseMode
896
     *
897
     * @return \TelegramBot\Api\Types\Message
898
     * @throws \TelegramBot\Api\InvalidArgumentException
899
     * @throws \TelegramBot\Api\Exception
900
     */
901 View Code Duplication
    public function sendAudio(
902
        $chatId,
903
        $audio,
904
        $duration = null,
905
        $performer = null,
906
        $title = null,
907
        $replyToMessageId = null,
908
        $replyMarkup = null,
909
        $disableNotification = false,
910
        $parseMode = null
911
    ) {
912
        return Message::fromResponse($this->call('sendAudio', [
913
            'chat_id' => $chatId,
914
            'audio' => $audio,
915
            'duration' => $duration,
916
            'performer' => $performer,
917
            'title' => $title,
918
            'reply_to_message_id' => $replyToMessageId,
919
            'reply_markup' => is_null($replyMarkup) ? $replyMarkup : $replyMarkup->toJson(),
920
            'disable_notification' => (bool)$disableNotification,
921
            'parse_mode' => $parseMode
922
        ]));
923
    }
924
925
    /**
926
     * Use this method to send photos. On success, the sent Message is returned.
927
     *
928
     * @param int|string $chatId chat_id or @channel_name
929
     * @param \CURLFile|string $photo
930
     * @param string|null $caption
931
     * @param int|null $replyToMessageId
932
     * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|
933
     *        Types\ReplyKeyboardRemove|null $replyMarkup
934
     * @param bool $disableNotification
935
     * @param string|null $parseMode
936
     *
937
     * @return \TelegramBot\Api\Types\Message
938
     * @throws \TelegramBot\Api\InvalidArgumentException
939
     * @throws \TelegramBot\Api\Exception
940
     */
941
    public function sendPhoto(
942
        $chatId,
943
        $photo,
944
        $caption = null,
945
        $replyToMessageId = null,
946
        $replyMarkup = null,
947
        $disableNotification = false,
948
        $parseMode = null
949
    ) {
950
        return Message::fromResponse($this->call('sendPhoto', [
951
            'chat_id' => $chatId,
952
            'photo' => $photo,
953
            'caption' => $caption,
954
            'reply_to_message_id' => $replyToMessageId,
955
            'reply_markup' => is_null($replyMarkup) ? $replyMarkup : $replyMarkup->toJson(),
956
            'disable_notification' => (bool)$disableNotification,
957
            'parse_mode' => $parseMode
958
        ]));
959
    }
960
961
    /**
962
     * Use this method to send general files. On success, the sent Message is returned.
963
     * Bots can currently send files of any type of up to 50 MB in size, this limit may be changed in the future.
964
     *
965
     * @param int|string $chatId chat_id or @channel_name
966
     * @param \CURLFile|string $document
967
     * @param string|null $caption
968
     * @param int|null $replyToMessageId
969
     * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|
970
     *        Types\ReplyKeyboardRemove|null $replyMarkup
971
     * @param bool $disableNotification
972
     * @param string|null $parseMode
973
     *
974
     * @return \TelegramBot\Api\Types\Message
975
     * @throws \TelegramBot\Api\InvalidArgumentException
976
     * @throws \TelegramBot\Api\Exception
977
     */
978
    public function sendDocument(
979
        $chatId,
980
        $document,
981
        $caption = null,
982
        $replyToMessageId = null,
983
        $replyMarkup = null,
984
        $disableNotification = false,
985
        $parseMode = null
986
    ) {
987
        return Message::fromResponse($this->call('sendDocument', [
988
            'chat_id' => $chatId,
989
            'document' => $document,
990
            'caption' => $caption,
991
            'reply_to_message_id' => $replyToMessageId,
992
            'reply_markup' => is_null($replyMarkup) ? $replyMarkup : $replyMarkup->toJson(),
993
            'disable_notification' => (bool)$disableNotification,
994
            'parse_mode' => $parseMode
995
        ]));
996
    }
997
998
    /**
999
     * Use this method to get basic info about a file and prepare it for downloading.
1000
     * For the moment, bots can download files of up to 20MB in size.
1001
     * On success, a File object is returned.
1002
     * The file can then be downloaded via the link https://api.telegram.org/file/bot<token>/<file_path>,
1003
     * where <file_path> is taken from the response.
1004
     * It is guaranteed that the link will be valid for at least 1 hour.
1005
     * When the link expires, a new one can be requested by calling getFile again.
1006
     *
1007
     * @param $fileId
1008
     *
1009
     * @return \TelegramBot\Api\Types\File
1010
     * @throws \TelegramBot\Api\InvalidArgumentException
1011
     * @throws \TelegramBot\Api\Exception
1012
     */
1013
    public function getFile($fileId)
1014
    {
1015
        return File::fromResponse($this->call('getFile', ['file_id' => $fileId]));
1016
    }
1017
1018
    /**
1019
     * Get file contents via cURL
1020
     *
1021
     * @param $fileId
1022
     *
1023
     * @return string
1024
     *
1025
     * @throws \TelegramBot\Api\HttpException
1026
     */
1027
    public function downloadFile($fileId)
1028
    {
1029
        $file = $this->getFile($fileId);
1030
        $options = [
1031
            CURLOPT_HEADER => 0,
1032
            CURLOPT_HTTPGET => 1,
1033
            CURLOPT_RETURNTRANSFER => 1,
1034
            CURLOPT_URL => $this->getFileUrl().'/'.$file->getFilePath(),
1035
        ];
1036
1037
        return $this->executeCurl($options);
1038
    }
1039
1040
    /**
1041
     * Use this method to send answers to an inline query. On success, True is returned.
1042
     * No more than 50 results per query are allowed.
1043
     *
1044
     * @param string $inlineQueryId
1045
     * @param AbstractInlineQueryResult[] $results
1046
     * @param int $cacheTime
1047
     * @param bool $isPersonal
1048
     * @param string $nextOffset
1049
     * @param string $switchPmText
1050
     * @param string $switchPmParameter
1051
     *
1052
     * @return mixed
1053
     * @throws Exception
1054
     */
1055
    public function answerInlineQuery(
1056
        $inlineQueryId,
1057
        $results,
1058
        $cacheTime = 300,
1059
        $isPersonal = false,
1060
        $nextOffset = '',
1061
        $switchPmText = null,
1062
        $switchPmParameter = null
1063
    ) {
1064
        $results = array_map(function ($item) {
1065
            /* @var AbstractInlineQueryResult $item */
1066
            return json_decode($item->toJson(), true);
1067
        }, $results);
1068
1069
        return $this->call('answerInlineQuery', [
1070
            'inline_query_id' => $inlineQueryId,
1071
            'results' => json_encode($results),
1072
            'cache_time' => $cacheTime,
1073
            'is_personal' => $isPersonal,
1074
            'next_offset' => $nextOffset,
1075
            'switch_pm_text' => $switchPmText,
1076
            'switch_pm_parameter' => $switchPmParameter,
1077
        ]);
1078
    }
1079
1080
    /**
1081
     * Use this method to kick a user from a group or a supergroup.
1082
     * In the case of supergroups, the user will not be able to return to the group
1083
     * on their own using invite links, etc., unless unbanned first.
1084
     * The bot must be an administrator in the group for this to work. Returns True on success.
1085
     *
1086
     * @param int|string $chatId Unique identifier for the target group
1087
     * or username of the target supergroup (in the format @supergroupusername)
1088
     * @param int $userId Unique identifier of the target user
1089
     * @param null|int $untilDate Date when the user will be unbanned, unix time.
1090
     *                            If user is banned for more than 366 days or less than 30 seconds from the current time
1091
     *                            they are considered to be banned forever
1092
     *
1093
     * @return bool
1094
     */
1095
    public function kickChatMember($chatId, $userId, $untilDate = null)
1096
    {
1097
        return $this->call('kickChatMember', [
1098
            'chat_id' => $chatId,
1099
            'user_id' => $userId,
1100
            'until_date' => $untilDate
1101
        ]);
1102
    }
1103
1104
    /**
1105
     * Use this method to unban a previously kicked user in a supergroup.
1106
     * The user will not return to the group automatically, but will be able to join via link, etc.
1107
     * The bot must be an administrator in the group for this to work. Returns True on success.
1108
     *
1109
     * @param int|string $chatId Unique identifier for the target group
1110
     * or username of the target supergroup (in the format @supergroupusername)
1111
     * @param int $userId Unique identifier of the target user
1112
     *
1113
     * @return bool
1114
     */
1115
    public function unbanChatMember($chatId, $userId)
1116
    {
1117
        return $this->call('unbanChatMember', [
1118
            'chat_id' => $chatId,
1119
            'user_id' => $userId,
1120
        ]);
1121
    }
1122
1123
    /**
1124
     * Use this method to send answers to callback queries sent from inline keyboards.
1125
     * The answer will be displayed to the user as a notification at the top of the chat screen or as an alert.
1126
     *
1127
     * @param $callbackQueryId
1128
     * @param null $text
1129
     * @param bool $showAlert
1130
     *
1131
     * @return bool
1132
     */
1133
    public function answerCallbackQuery($callbackQueryId, $text = null, $showAlert = false)
1134
    {
1135
        return $this->call('answerCallbackQuery', [
1136
            'callback_query_id' => $callbackQueryId,
1137
            'text' => $text,
1138
            'show_alert' => (bool)$showAlert,
1139
        ]);
1140
    }
1141
1142
    /**
1143
     * Use this method to change the list of the bot's commands. Returns True on success.
1144
     *
1145
     * @param $commands
1146
     *
1147
     * @return mixed
1148
     * @throws Exception
1149
     * @throws HttpException
1150
     * @throws InvalidJsonException
1151
     */
1152
    public function setMyCommands($commands)
1153
    {
1154
        return $this->call(
1155
            'setMyCommands',
1156
            [
1157
                'commands' => json_encode($commands)
1158
            ]
1159
        );
1160
    }
1161
1162
    /**
1163
     * Use this method to get the current list of the bot's commands. Requires no parameters.
1164
     * Returns Array of BotCommand on success.
1165
     *
1166
     * @return mixed
1167
     * @throws Exception
1168
     * @throws HttpException
1169
     * @throws InvalidJsonException
1170
     */
1171
    public function getMyCommands()
1172
    {
1173
        return ArrayOfBotCommand::fromResponse($this->call('getMyCommands'));
1174
    }
1175
1176
    /**
1177
     * Use this method to edit text messages sent by the bot or via the bot
1178
     *
1179
     * @param int|string $chatId
1180
     * @param int $messageId
1181
     * @param string $text
1182
     * @param string $inlineMessageId
1183
     * @param string|null $parseMode
1184
     * @param bool $disablePreview
1185
     * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|
1186
     *        Types\ReplyKeyboardRemove|null $replyMarkup
1187
     * @return Message
1188
     */
1189
    public function editMessageText(
1190
        $chatId,
1191
        $messageId,
1192
        $text,
1193
        $parseMode = null,
1194
        $disablePreview = false,
1195
        $replyMarkup = null,
1196
        $inlineMessageId = null
1197
    ) {
1198
        return Message::fromResponse($this->call('editMessageText', [
1199
            'chat_id' => $chatId,
1200
            'message_id' => $messageId,
1201
            'text' => $text,
1202
            'inline_message_id' => $inlineMessageId,
1203
            'parse_mode' => $parseMode,
1204
            'disable_web_page_preview' => $disablePreview,
1205
            'reply_markup' => is_null($replyMarkup) ? $replyMarkup : $replyMarkup->toJson(),
1206
        ]));
1207
    }
1208
1209
    /**
1210
     * Use this method to edit text messages sent by the bot or via the bot
1211
     *
1212
     * @param int|string $chatId
1213
     * @param int $messageId
1214
     * @param string|null $caption
1215
     * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|
1216
     *        Types\ReplyKeyboardRemove|null $replyMarkup
1217
     * @param string $inlineMessageId
1218
     * @param string|null $parseMode
1219
     *
1220
     * @return \TelegramBot\Api\Types\Message
1221
     * @throws \TelegramBot\Api\InvalidArgumentException
1222
     * @throws \TelegramBot\Api\Exception
1223
     */
1224 View Code Duplication
    public function editMessageCaption(
1225
        $chatId,
1226
        $messageId,
1227
        $caption = null,
1228
        $replyMarkup = null,
1229
        $inlineMessageId = null,
1230
        $parseMode = null
1231
    ) {
1232
        return Message::fromResponse($this->call('editMessageCaption', [
1233
            'chat_id' => $chatId,
1234
            'message_id' => $messageId,
1235
            'inline_message_id' => $inlineMessageId,
1236
            'caption' => $caption,
1237
            'reply_markup' => is_null($replyMarkup) ? $replyMarkup : $replyMarkup->toJson(),
1238
            'parse_mode' => $parseMode
1239
        ]));
1240
    }
1241
1242
    /**
1243
     * Use this method to edit animation, audio, document, photo, or video messages.
1244
     * If a message is a part of a message album, then it can be edited only to a photo or a video.
1245
     * Otherwise, message type can be changed arbitrarily.
1246
     * When inline message is edited, new file can't be uploaded.
1247
     * Use previously uploaded file via its file_id or specify a URL.
1248
     * On success, if the edited message was sent by the bot, the edited Message is returned, otherwise True is returned
1249
     *
1250
     * @param $chatId
1251
     * @param $messageId
1252
     * @param InputMedia $media
1253
     * @param null $inlineMessageId
1254
     * @param null $replyMarkup
1255
     * @return bool|Message
1256
     * @throws Exception
1257
     * @throws HttpException
1258
     * @throws InvalidJsonException
1259
     */
1260 View Code Duplication
    public function editMessageMedia(
1261
        $chatId,
1262
        $messageId,
1263
        InputMedia $media,
1264
        $inlineMessageId = null,
1265
        $replyMarkup = null
1266
    ) {
1267
        return Message::fromResponse($this->call('editMessageMedia', [
1268
            'chat_id' => $chatId,
1269
            'message_id' => $messageId,
1270
            'inline_message_id' => $inlineMessageId,
1271
            'media' => $media->toJson(),
1272
            'reply_markup' => is_null($replyMarkup) ? $replyMarkup : $replyMarkup->toJson(),
1273
        ]));
1274
    }
1275
1276
    /**
1277
     * Use this method to edit only the reply markup of messages sent by the bot or via the bot
1278
     *
1279
     * @param int|string $chatId
1280
     * @param int $messageId
1281
     * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|
1282
     *        Types\ReplyKeyboardRemove|null $replyMarkup
1283
     * @param string $inlineMessageId
1284
     *
1285
     * @return Message
1286
     */
1287
    public function editMessageReplyMarkup(
1288
        $chatId,
1289
        $messageId,
1290
        $replyMarkup = null,
1291
        $inlineMessageId = null
1292
    ) {
1293
        return Message::fromResponse($this->call('editMessageReplyMarkup', [
1294
            'chat_id' => $chatId,
1295
            'message_id' => $messageId,
1296
            'inline_message_id' => $inlineMessageId,
1297
            'reply_markup' => is_null($replyMarkup) ? $replyMarkup : $replyMarkup->toJson(),
1298
        ]));
1299
    }
1300
1301
    /**
1302
     * Use this method to delete a message, including service messages, with the following limitations:
1303
     *  - A message can only be deleted if it was sent less than 48 hours ago.
1304
     *  - Bots can delete outgoing messages in groups and supergroups.
1305
     *  - Bots granted can_post_messages permissions can delete outgoing messages in channels.
1306
     *  - If the bot is an administrator of a group, it can delete any message there.
1307
     *  - If the bot has can_delete_messages permission in a supergroup or a channel, it can delete any message there.
1308
     *
1309
     * @param int|string $chatId
1310
     * @param int $messageId
1311
     *
1312
     * @return bool
1313
     */
1314
    public function deleteMessage($chatId, $messageId)
1315
    {
1316
        return $this->call('deleteMessage', [
1317
            'chat_id' => $chatId,
1318
            'message_id' => $messageId,
1319
        ]);
1320
    }
1321
1322
    /**
1323
     * Close curl
1324
     */
1325 9
    public function __destruct()
1326
    {
1327 9
        $this->curl && curl_close($this->curl);
1328 9
    }
1329
1330
    /**
1331
     * @return string
1332
     */
1333
    public function getUrl()
1334
    {
1335
        return self::URL_PREFIX.$this->token;
1336
    }
1337
1338
    /**
1339
     * @return string
1340
     */
1341
    public function getFileUrl()
1342
    {
1343
        return self::FILE_URL_PREFIX.$this->token;
1344
    }
1345
1346
    /**
1347
     * @param \TelegramBot\Api\Types\Update $update
1348
     * @param string $eventName
1349
     *
1350
     * @throws \TelegramBot\Api\Exception
1351
     */
1352
    public function trackUpdate(Update $update, $eventName = 'Message')
1353
    {
1354
        if (!in_array($update->getUpdateId(), $this->trackedEvents)) {
1355
            $this->trackedEvents[] = $update->getUpdateId();
1356
1357
            $this->track($update->getMessage(), $eventName);
1358
1359
            if (count($this->trackedEvents) > self::MAX_TRACKED_EVENTS) {
1360
                $this->trackedEvents = array_slice($this->trackedEvents, round(self::MAX_TRACKED_EVENTS / 4));
1361
            }
1362
        }
1363
    }
1364
1365
    /**
1366
     * Wrapper for tracker
1367
     *
1368
     * @param \TelegramBot\Api\Types\Message $message
1369
     * @param string $eventName
1370
     *
1371
     * @throws \TelegramBot\Api\Exception
1372
     */
1373
    public function track(Message $message, $eventName = 'Message')
1374
    {
1375
        if ($this->tracker instanceof Botan) {
1376
            $this->tracker->track($message, $eventName);
1377
        }
1378
    }
1379
1380
    /**
1381
     * Use this method to send invoices. On success, the sent Message is returned.
1382
     *
1383
     * @param int|string $chatId
1384
     * @param string $title
1385
     * @param string $description
1386
     * @param string $payload
1387
     * @param string $providerToken
1388
     * @param string $startParameter
1389
     * @param string $currency
1390
     * @param array $prices
1391
     * @param string|null $photoUrl
1392
     * @param int|null $photoSize
1393
     * @param int|null $photoWidth
1394
     * @param int|null $photoHeight
1395
     * @param bool $needName
1396
     * @param bool $needPhoneNumber
1397
     * @param bool $needEmail
1398
     * @param bool $needShippingAddress
1399
     * @param bool $isFlexible
1400
     * @param int|null $replyToMessageId
1401
     * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|
1402
     *        Types\ReplyKeyboardRemove|null $replyMarkup
1403
     * @param bool $disableNotification
1404
     * @param string|null $providerData
1405
     * @param bool $sendPhoneNumberToProvider
1406
     * @param bool $sendEmailToProvider
1407
     *
1408
     * @return Message
1409
     */
1410
    public function sendInvoice(
1411
        $chatId,
1412
        $title,
1413
        $description,
1414
        $payload,
1415
        $providerToken,
1416
        $startParameter,
1417
        $currency,
1418
        $prices,
1419
        $isFlexible = false,
1420
        $photoUrl = null,
1421
        $photoSize = null,
1422
        $photoWidth = null,
1423
        $photoHeight = null,
1424
        $needName = false,
1425
        $needPhoneNumber = false,
1426
        $needEmail = false,
1427
        $needShippingAddress = false,
1428
        $replyToMessageId = null,
1429
        $replyMarkup = null,
1430
        $disableNotification = false,
1431
        $providerData = null,
1432
        $sendPhoneNumberToProvider = false,
1433
        $sendEmailToProvider = false
1434
    ) {
1435
        return Message::fromResponse($this->call('sendInvoice', [
1436
            'chat_id' => $chatId,
1437
            'title' => $title,
1438
            'description' => $description,
1439
            'payload' => $payload,
1440
            'provider_token' => $providerToken,
1441
            'start_parameter' => $startParameter,
1442
            'currency' => $currency,
1443
            'prices' => json_encode($prices),
1444
            'is_flexible' => $isFlexible,
1445
            'photo_url' => $photoUrl,
1446
            'photo_size' => $photoSize,
1447
            'photo_width' => $photoWidth,
1448
            'photo_height' => $photoHeight,
1449
            'need_name' => $needName,
1450
            'need_phone_number' => $needPhoneNumber,
1451
            'need_email' => $needEmail,
1452
            'need_shipping_address' => $needShippingAddress,
1453
            'reply_to_message_id' => $replyToMessageId,
1454
            'reply_markup' => is_null($replyMarkup) ? $replyMarkup : $replyMarkup->toJson(),
1455
            'disable_notification' => (bool)$disableNotification,
1456
            'provider_data' => $providerData,
1457
            'send_phone_number_to_provider' => (bool)$sendPhoneNumberToProvider,
1458
            'send_email_to_provider' => (bool)$sendEmailToProvider
1459
        ]));
1460
    }
1461
1462
    /**
1463
     * If you sent an invoice requesting a shipping address and the parameter is_flexible was specified, the Bot API
1464
     * will send an Update with a shipping_query field to the bot. Use this method to reply to shipping queries.
1465
     * On success, True is returned.
1466
     *
1467
     * @param string $shippingQueryId
1468
     * @param bool $ok
1469
     * @param array $shipping_options
1470
     * @param null|string $errorMessage
1471
     *
1472
     * @return bool
1473
     *
1474
     */
1475
    public function answerShippingQuery($shippingQueryId, $ok = true, $shipping_options = [], $errorMessage = null)
1476
    {
1477
        return $this->call('answerShippingQuery', [
1478
            'shipping_query_id' => $shippingQueryId,
1479
            'ok' => (bool)$ok,
1480
            'shipping_options' => json_encode($shipping_options),
1481
            'error_message' => $errorMessage
1482
        ]);
1483
    }
1484
1485
    /**
1486
     * Use this method to respond to such pre-checkout queries. On success, True is returned.
1487
     * Note: The Bot API must receive an answer within 10 seconds after the pre-checkout query was sent.
1488
     *
1489
     * @param string $preCheckoutQueryId
1490
     * @param bool $ok
1491
     * @param null|string $errorMessage
1492
     *
1493
     * @return mixed
1494
     */
1495
    public function answerPreCheckoutQuery($preCheckoutQueryId, $ok = true, $errorMessage = null)
1496
    {
1497
        return $this->call('answerPreCheckoutQuery', [
1498
            'pre_checkout_query_id' => $preCheckoutQueryId,
1499
            'ok' => (bool)$ok,
1500
            'error_message' => $errorMessage
1501
        ]);
1502
    }
1503
1504
    /**
1505
     * Use this method to restrict a user in a supergroup.
1506
     * The bot must be an administrator in the supergroup for this to work and must have the appropriate admin rights.
1507
     * Pass True for all boolean parameters to lift restrictions from a user.
1508
     *
1509
     * @param string|int $chatId Unique identifier for the target chat or username of the target supergroup
1510
     *                   (in the format @supergroupusername)
1511
     * @param int $userId Unique identifier of the target user
1512
     * @param null|integer $untilDate Date when restrictions will be lifted for the user, unix time.
1513
     *                     If user is restricted for more than 366 days or less than 30 seconds from the current time,
1514
     *                     they are considered to be restricted forever
1515
     * @param bool $canSendMessages Pass True, if the user can send text messages, contacts, locations and venues
1516
     * @param bool $canSendMediaMessages No Pass True, if the user can send audios, documents, photos, videos,
1517
     *                                   video notes and voice notes, implies can_send_messages
1518
     * @param bool $canSendOtherMessages Pass True, if the user can send animations, games, stickers and
1519
     *                                   use inline bots, implies can_send_media_messages
1520
     * @param bool $canAddWebPagePreviews Pass True, if the user may add web page previews to their messages,
1521
     *                                    implies can_send_media_messages
1522
     *
1523
     * @return bool
1524
     */
1525
    public function restrictChatMember(
1526
        $chatId,
1527
        $userId,
1528
        $untilDate = null,
1529
        $canSendMessages = false,
1530
        $canSendMediaMessages = false,
1531
        $canSendOtherMessages = false,
1532
        $canAddWebPagePreviews = false
1533
    ) {
1534
        return $this->call('restrictChatMember', [
1535
            'chat_id' => $chatId,
1536
            'user_id' => $userId,
1537
            'until_date' => $untilDate,
1538
            'can_send_messages' => $canSendMessages,
1539
            'can_send_media_messages' => $canSendMediaMessages,
1540
            'can_send_other_messages' => $canSendOtherMessages,
1541
            'can_add_web_page_previews' => $canAddWebPagePreviews
1542
        ]);
1543
    }
1544
1545
    /**
1546
     * Use this method to promote or demote a user in a supergroup or a channel.
1547
     * The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
1548
     * Pass False for all boolean parameters to demote a user.
1549
     *
1550
     * @param string|int $chatId Unique identifier for the target chat or username of the target supergroup
1551
     *                   (in the format @supergroupusername)
1552
     * @param int $userId Unique identifier of the target user
1553
     * @param bool $canChangeInfo Pass True, if the administrator can change chat title, photo and other settings
1554
     * @param bool $canPostMessages Pass True, if the administrator can create channel posts, channels only
1555
     * @param bool $canEditMessages Pass True, if the administrator can edit messages of other users, channels only
1556
     * @param bool $canDeleteMessages Pass True, if the administrator can delete messages of other users
1557
     * @param bool $canInviteUsers Pass True, if the administrator can invite new users to the chat
1558
     * @param bool $canRestrictMembers Pass True, if the administrator can restrict, ban or unban chat members
1559
     * @param bool $canPinMessages Pass True, if the administrator can pin messages, supergroups only
1560
     * @param bool $canPromoteMembers Pass True, if the administrator can add new administrators with a subset of his
1561
     *                                own privileges or demote administrators that he has promoted,directly or
1562
     *                                indirectly (promoted by administrators that were appointed by him)
1563
     *
1564
     * @return bool
1565
     */
1566
    public function promoteChatMember(
1567
        $chatId,
1568
        $userId,
1569
        $canChangeInfo = true,
1570
        $canPostMessages = true,
1571
        $canEditMessages = true,
1572
        $canDeleteMessages = true,
1573
        $canInviteUsers = true,
1574
        $canRestrictMembers = true,
1575
        $canPinMessages = true,
1576
        $canPromoteMembers = true
1577
    ) {
1578
        return $this->call('promoteChatMember', [
1579
            'chat_id' => $chatId,
1580
            'user_id' => $userId,
1581
            'can_change_info' => $canChangeInfo,
1582
            'can_post_messages' => $canPostMessages,
1583
            'can_edit_messages' => $canEditMessages,
1584
            'can_delete_messages' => $canDeleteMessages,
1585
            'can_invite_users' => $canInviteUsers,
1586
            'can_restrict_members' => $canRestrictMembers,
1587
            'can_pin_messages' => $canPinMessages,
1588
            'can_promote_members' => $canPromoteMembers
1589
        ]);
1590
    }
1591
1592
    /**
1593
     * Use this method to export an invite link to a supergroup or a channel.
1594
     * The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
1595
     *
1596
     * @param string|int $chatId Unique identifier for the target chat or username of the target channel
1597
     *                           (in the format @channelusername)
1598
     * @return string
1599
     */
1600
    public function exportChatInviteLink($chatId)
1601
    {
1602
        return $this->call('exportChatInviteLink', [
1603
            'chat_id' => $chatId
1604
        ]);
1605
    }
1606
1607
    /**
1608
     * Use this method to set a new profile photo for the chat. Photos can't be changed for private chats.
1609
     * The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
1610
     *
1611
     * @param string|int $chatId Unique identifier for the target chat or username of the target channel
1612
     *                           (in the format @channelusername)
1613
     * @param \CURLFile|string $photo New chat photo, uploaded using multipart/form-data
1614
     *
1615
     * @return bool
1616
     */
1617
    public function setChatPhoto($chatId, $photo)
1618
    {
1619
        return $this->call('setChatPhoto', [
1620
            'chat_id' => $chatId,
1621
            'photo' => $photo
1622
        ]);
1623
    }
1624
1625
    /**
1626
     * Use this method to delete a chat photo. Photos can't be changed for private chats.
1627
     * The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
1628
     *
1629
     * @param string|int $chatId Unique identifier for the target chat or username of the target channel
1630
     *                           (in the format @channelusername)
1631
     *
1632
     * @return bool
1633
     */
1634
    public function deleteChatPhoto($chatId)
1635
    {
1636
        return $this->call('deleteChatPhoto', [
1637
            'chat_id' => $chatId
1638
        ]);
1639
    }
1640
1641
    /**
1642
     * Use this method to change the title of a chat. Titles can't be changed for private chats.
1643
     * The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
1644
     *
1645
     * @param string|int $chatId Unique identifier for the target chat or username of the target channel
1646
     *                           (in the format @channelusername)
1647
     * @param string $title New chat title, 1-255 characters
1648
     *
1649
     * @return bool
1650
     */
1651
    public function setChatTitle($chatId, $title)
1652
    {
1653
        return $this->call('setChatTitle', [
1654
            'chat_id' => $chatId,
1655
            'title' => $title
1656
        ]);
1657
    }
1658
1659
    /**
1660
     * Use this method to change the description of a supergroup or a channel.
1661
     * The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
1662
     *
1663
     * @param string|int $chatId Unique identifier for the target chat or username of the target channel
1664
     *                   (in the format @channelusername)
1665
     * @param string|null $description New chat description, 0-255 characters
1666
     *
1667
     * @return bool
1668
     */
1669
    public function setChatDescription($chatId, $description = null)
1670
    {
1671
        return $this->call('setChatDescription', [
1672
            'chat_id' => $chatId,
1673
            'title' => $description
1674
        ]);
1675
    }
1676
1677
    /**
1678
     * Use this method to pin a message in a supergroup.
1679
     * The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
1680
     *
1681
     * @param string|int $chatId Unique identifier for the target chat or username of the target channel
1682
     *                   (in the format @channelusername)
1683
     * @param int $messageId Identifier of a message to pin
1684
     * @param bool $disableNotification
1685
     *
1686
     * @return bool
1687
     */
1688
    public function pinChatMessage($chatId, $messageId, $disableNotification = false)
1689
    {
1690
        return $this->call('pinChatMessage', [
1691
            'chat_id' => $chatId,
1692
            'message_id' => $messageId,
1693
            'disable_notification' => $disableNotification
1694
        ]);
1695
    }
1696
1697
    /**
1698
     * Use this method to unpin a message in a supergroup chat.
1699
     * The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
1700
     *
1701
     * @param string|int $chatId Unique identifier for the target chat or username of the target channel
1702
     *                   (in the format @channelusername)
1703
     *
1704
     * @return bool
1705
     */
1706
    public function unpinChatMessage($chatId)
1707
    {
1708
        return $this->call('unpinChatMessage', [
1709
            'chat_id' => $chatId
1710
        ]);
1711
    }
1712
1713
    /**
1714
     * Use this method to get up to date information about the chat
1715
     * (current name of the user for one-on-one conversations, current username of a user, group or channel, etc.).
1716
     *
1717
     * @param string|int $chatId Unique identifier for the target chat or username of the target channel
1718
     *                   (in the format @channelusername)
1719
     *
1720
     * @return Chat
1721
     */
1722
    public function getChat($chatId)
1723
    {
1724
        return Chat::fromResponse($this->call('getChat', [
1725
            'chat_id' => $chatId
1726
        ]));
1727
    }
1728
1729
    /**
1730
     * Use this method to get information about a member of a chat.
1731
     *
1732
     * @param string|int $chatId Unique identifier for the target chat or username of the target channel
1733
     *                   (in the format @channelusername)
1734
     * @param int $userId
1735
     *
1736
     * @return ChatMember
1737
     */
1738
    public function getChatMember($chatId, $userId)
1739
    {
1740
        return ChatMember::fromResponse($this->call('getChatMember', [
1741
            'chat_id' => $chatId,
1742
            'user_id' => $userId
1743
        ]));
1744
    }
1745
1746
    /**
1747
     * Use this method for your bot to leave a group, supergroup or channel.
1748
     *
1749
     * @param string|int $chatId Unique identifier for the target chat or username of the target channel
1750
     *                   (in the format @channelusername)
1751
     *
1752
     * @return bool
1753
     */
1754
    public function leaveChat($chatId)
1755
    {
1756
        return $this->call('leaveChat', [
1757
            'chat_id' => $chatId
1758
        ]);
1759
    }
1760
1761
    /**
1762
     * Use this method to get the number of members in a chat.
1763
     *
1764
     * @param string|int $chatId Unique identifier for the target chat or username of the target channel
1765
     *                   (in the format @channelusername)
1766
     *
1767
     * @return int
1768
     */
1769
    public function getChatMembersCount($chatId)
1770
    {
1771
        return $this->call(
1772
            'getChatMembersCount',
1773
            [
1774
                'chat_id' => $chatId
1775
            ]
1776
        );
1777
    }
1778
1779
    /**
1780
     * Use this method to get a list of administrators in a chat.
1781
     *
1782
     * @param string|int $chatId Unique identifier for the target chat or username of the target channel
1783
     *                   (in the format @channelusername)
1784
     *
1785
     * @return array
1786
     */
1787
    public function getChatAdministrators($chatId)
1788
    {
1789
        return ArrayOfChatMemberEntity::fromResponse(
1790
            $this->call(
1791
                'getChatAdministrators',
1792
                [
1793
                    'chat_id' => $chatId
1794
                ]
1795
            )
1796
        );
1797
    }
1798
1799
    /**
1800
     * As of v.4.0, Telegram clients support rounded square mp4 videos of up to 1 minute long.
1801
     * Use this method to send video messages.
1802
     * On success, the sent Message is returned.
1803
     *
1804
     * @param int|string $chatId chat_id or @channel_name
1805
     * @param \CURLFile|string $videoNote
1806
     * @param int|null $duration
1807
     * @param int|null $length
1808
     * @param int|null $replyToMessageId
1809
     * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|
1810
     *        Types\ReplyKeyboardRemove|null $replyMarkup
1811
     * @param bool $disableNotification
1812
     *
1813
     * @return \TelegramBot\Api\Types\Message
1814
     * @throws \TelegramBot\Api\InvalidArgumentException
1815
     * @throws \TelegramBot\Api\Exception
1816
     */
1817
    public function sendVideoNote(
1818
        $chatId,
1819
        $videoNote,
1820
        $duration = null,
1821
        $length = null,
1822
        $replyToMessageId = null,
1823
        $replyMarkup = null,
1824
        $disableNotification = false
1825
    ) {
1826
        return Message::fromResponse($this->call('sendVideoNote', [
1827
            'chat_id' => $chatId,
1828
            'video_note' => $videoNote,
1829
            'duration' => $duration,
1830
            'length' => $length,
1831
            'reply_to_message_id' => $replyToMessageId,
1832
            'reply_markup' => is_null($replyMarkup) ? $replyMarkup : $replyMarkup->toJson(),
1833
            'disable_notification' => (bool)$disableNotification
1834
        ]));
1835
    }
1836
1837
    /**
1838
     * Use this method to send a group of photos or videos as an album.
1839
     * On success, the sent \TelegramBot\Api\Types\Message is returned.
1840
     *
1841
     * @param int|string $chatId
1842
     * @param ArrayOfInputMedia $media
1843
     * @param int|null $replyToMessageId
1844
     * @param bool $disableNotification
1845
     *
1846
     * @return array
1847
     * @throws \TelegramBot\Api\Exception
1848
     */
1849
    public function sendMediaGroup(
1850
        $chatId,
1851
        $media,
1852
        $disableNotification = false,
1853
        $replyToMessageId = null
1854
    ) {
1855
        return ArrayOfMessages::fromResponse($this->call('sendMediaGroup', [
1856
            'chat_id' => $chatId,
1857
            'media' => $media->toJson(),
1858
            'reply_to_message_id' => (int)$replyToMessageId,
1859
            'disable_notification' => (bool)$disableNotification
1860
        ]));
1861
    }
1862
1863
    /**
1864
     * Enable proxy for curl requests. Empty string will disable proxy.
1865
     *
1866
     * @param string $proxyString
1867
     *
1868
     * @return BotApi
1869
     */
1870
    public function setProxy($proxyString = '', $socks5 = false)
1871
    {
1872
        if (empty($proxyString)) {
1873
            $this->proxySettings = [];
1874
            return $this;
1875
        }
1876
1877
        $this->proxySettings = [
1878
            CURLOPT_PROXY => $proxyString,
1879
            CURLOPT_HTTPPROXYTUNNEL => true,
1880
        ];
1881
1882
        if ($socks5) {
1883
            $this->proxySettings[CURLOPT_PROXYTYPE] = CURLPROXY_SOCKS5;
1884
        }
1885
        return $this;
1886
    }
1887
1888
1889
    /**
1890
     * Use this method to send a native poll. A native poll can't be sent to a private chat.
1891
     * On success, the sent \TelegramBot\Api\Types\Message is returned.
1892
     *
1893
     * @param $chatId string|int Unique identifier for the target chat or username of the target channel
1894
     *                (in the format @channelusername)
1895
     * @param string $question Poll question, 1-255 characters
1896
     * @param array $options A JSON-serialized list of answer options, 2-10 strings 1-100 characters each
1897
     * @param bool $isAnonymous True, if the poll needs to be anonymous, defaults to True
1898
     * @param null $type Poll type, “quiz” or “regular”, defaults to “regular”
1899
     * @param bool $allowsMultipleAnswers True, if the poll allows multiple answers,
1900
     *                          ignored for polls in quiz mode, defaults to False
1901
     * @param null $correctOptionId 0-based identifier of the correct answer option, required for polls in quiz mode
1902
     * @param bool $isClosed Pass True, if the poll needs to be immediately closed. This can be useful for poll preview.
1903
     * @param bool $disableNotification Sends the message silently. Users will receive a notification with no sound.
1904
     * @param int|null $replyToMessageId If the message is a reply, ID of the original message
1905
     * @param null $replyMarkup Additional interface options. A JSON-serialized object for an inline keyboard,
1906
     *                          custom reply keyboard, instructions to remove reply
1907
     *                          keyboard or to force a reply from the user.
1908
     * @return \TelegramBot\Api\Types\Message
1909
     * @throws Exception
1910
     * @throws HttpException
1911
     * @throws InvalidJsonException
1912
     */
1913
    public function sendPoll(
1914
        $chatId,
1915
        $question,
1916
        $options,
1917
        $isAnonymous = false,
1918
        $type = null,
1919
        $allowsMultipleAnswers = false,
1920
        $correctOptionId = null,
1921
        $isClosed = false,
1922
        $disableNotification = false,
1923
        $replyToMessageId = null,
1924
        $replyMarkup = null
1925
    ) {
1926
        return Message::fromResponse($this->call('sendPoll', [
1927
            'chat_id' => $chatId,
1928
            'question' => $question,
1929
            'options' => json_encode($options),
1930
            'is_anonymous' => (bool) $isAnonymous,
1931
            'type' => (string) $type,
1932
            'allows_multiple_answers' => (bool) $allowsMultipleAnswers,
1933
            'correct_option_id' => (int) $correctOptionId,
1934
            'is_closed' => (bool) $isClosed,
1935
            'disable_notification' => (bool) $disableNotification,
1936
            'reply_to_message_id' => (int) $replyToMessageId,
1937
            'reply_markup' => $replyMarkup === null ? $replyMarkup : $replyMarkup->toJson(),
0 ignored issues
show
Bug introduced by
The method toJson cannot be called on $replyMarkup (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
1938
        ]));
1939
    }
1940
1941
    /**
1942
     * Use this method to send a dice, which will have a random value from 1 to 6.
1943
     * On success, the sent Message is returned. (Yes, we're aware of the “proper” singular of die.
1944
     * But it's awkward, and we decided to help it change. One dice at a time!)
1945
     *
1946
     * @param $chatId string|int Unique identifier for the target chat or username of the target channel
1947
     *                (in the format @channelusername)
1948
     * @param bool $disableNotification Sends the message silently. Users will receive a notification with no sound.
1949
     * @param null $replyToMessageId If the message is a reply, ID of the original message
1950
     * @param null $replyMarkup Additional interface options. A JSON-serialized object for an inline keyboard,
1951
     *                          custom reply keyboard, instructions to remove reply
1952
     *                          keyboard or to force a reply from the user.
1953
     * @return bool|Message
1954
     * @throws Exception
1955
     * @throws HttpException
1956
     * @throws InvalidJsonException
1957
     */
1958 View Code Duplication
    public function sendDice(
1959
        $chatId,
1960
        $disableNotification = false,
1961
        $replyToMessageId = null,
1962
        $replyMarkup = null
1963
    ) {
1964
        return Message::fromResponse($this->call('sendDice', [
1965
            'chat_id' => $chatId,
1966
            'disable_notification' => (bool) $disableNotification,
1967
            'reply_to_message_id' => (int) $replyToMessageId,
1968
            'reply_markup' => $replyMarkup === null ? $replyMarkup : $replyMarkup->toJson(),
0 ignored issues
show
Bug introduced by
The method toJson cannot be called on $replyMarkup (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
1969
        ]));
1970
    }
1971
1972
    /**
1973
     * Use this method to stop a poll which was sent by the bot.
1974
     * On success, the stopped \TelegramBot\Api\Types\Poll with the final results is returned.
1975
     *
1976
     * @param int|string $chatId
1977
     * @param int $messageId
1978
     * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|
1979
     *        Types\ReplyKeyboardRemove|null $replyMarkup
1980
     * @return Poll
1981
     * @throws \TelegramBot\Api\InvalidArgumentException
1982
     * @throws \TelegramBot\Api\Exception
1983
     */
1984
    public function stopPoll(
1985
        $chatId,
1986
        $messageId,
1987
        $replyMarkup = null
1988
    ) {
1989
        return Poll::fromResponse($this->call('stopPoll', [
1990
            'chat_id' => $chatId,
1991
            'message_id' => $messageId,
1992
            'reply_markup' => is_null($replyMarkup) ? $replyMarkup : $replyMarkup->toJson(),
1993
        ]));
1994
    }
1995
1996
    /**
1997
     * Set an option for a cURL transfer
1998
     *
1999
     * @param int $option The CURLOPT_XXX option to set
2000
     * @param mixed $value The value to be set on option
2001
     */
2002
    public function setCurlOption($option, $value)
2003
    {
2004
        $this->customCurlOptions[$option] = $value;
2005
    }
2006
2007
    /**
2008
     * Unset an option for a cURL transfer
2009
     *
2010
     * @param int $option The CURLOPT_XXX option to unset
2011
     */
2012
    public function unsetCurlOption($option)
2013
    {
2014
        unset($this->customCurlOptions[$option]);
2015
    }
2016
2017
    /**
2018
     * Clean custom options
2019
     */
2020
    public function resetCurlOptions()
2021
    {
2022
        $this->customCurlOptions = [];
2023
    }
2024
}
2025