Test Failed
Push — master ( 67c9bb...4758b5 )
by
unknown
02:50 queued 38s
created

BotApi::sendInvoice()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 45
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 45
ccs 0
cts 22
cp 0
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 42
nc 1
nop 20
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\ArrayOfUpdates;
6
use TelegramBot\Api\Types\Chat;
7
use TelegramBot\Api\Types\ChatMember;
8
use TelegramBot\Api\Types\File;
9
use TelegramBot\Api\Types\Inline\QueryResult\AbstractInlineQueryResult;
10
use TelegramBot\Api\Types\Message;
11
use TelegramBot\Api\Types\Update;
12
use TelegramBot\Api\Types\User;
13
use TelegramBot\Api\Types\UserProfilePhotos;
14
15
/**
16
 * Class BotApi
17
 *
18
 * @package TelegramBot\Api
19
 */
20
class BotApi
21
{
22
    /**
23
     * HTTP codes
24
     *
25
     * @var array
26
     */
27
    public static $codes = [
28
        // Informational 1xx
29
        100 => 'Continue',
30
        101 => 'Switching Protocols',
31
        102 => 'Processing',            // RFC2518
32
        // Success 2xx
33
        200 => 'OK',
34
        201 => 'Created',
35
        202 => 'Accepted',
36
        203 => 'Non-Authoritative Information',
37
        204 => 'No Content',
38
        205 => 'Reset Content',
39
        206 => 'Partial Content',
40
        207 => 'Multi-Status',          // RFC4918
41
        208 => 'Already Reported',      // RFC5842
42
        226 => 'IM Used',               // RFC3229
43
        // Redirection 3xx
44
        300 => 'Multiple Choices',
45
        301 => 'Moved Permanently',
46
        302 => 'Found', // 1.1
47
        303 => 'See Other',
48
        304 => 'Not Modified',
49
        305 => 'Use Proxy',
50
        // 306 is deprecated but reserved
51
        307 => 'Temporary Redirect',
52
        308 => 'Permanent Redirect',    // RFC7238
53
        // Client Error 4xx
54
        400 => 'Bad Request',
55
        401 => 'Unauthorized',
56
        402 => 'Payment Required',
57
        403 => 'Forbidden',
58
        404 => 'Not Found',
59
        405 => 'Method Not Allowed',
60
        406 => 'Not Acceptable',
61
        407 => 'Proxy Authentication Required',
62
        408 => 'Request Timeout',
63
        409 => 'Conflict',
64
        410 => 'Gone',
65
        411 => 'Length Required',
66
        412 => 'Precondition Failed',
67
        413 => 'Payload Too Large',
68
        414 => 'URI Too Long',
69
        415 => 'Unsupported Media Type',
70
        416 => 'Range Not Satisfiable',
71
        417 => 'Expectation Failed',
72
        422 => 'Unprocessable Entity',                                        // RFC4918
73
        423 => 'Locked',                                                      // RFC4918
74
        424 => 'Failed Dependency',                                           // RFC4918
75
        425 => 'Reserved for WebDAV advanced collections expired proposal',   // RFC2817
76
        426 => 'Upgrade Required',                                            // RFC2817
77
        428 => 'Precondition Required',                                       // RFC6585
78
        429 => 'Too Many Requests',                                           // RFC6585
79
        431 => 'Request Header Fields Too Large',                             // RFC6585
80
        // Server Error 5xx
81
        500 => 'Internal Server Error',
82
        501 => 'Not Implemented',
83
        502 => 'Bad Gateway',
84
        503 => 'Service Unavailable',
85
        504 => 'Gateway Timeout',
86
        505 => 'HTTP Version Not Supported',
87
        506 => 'Variant Also Negotiates (Experimental)',                      // RFC2295
88
        507 => 'Insufficient Storage',                                        // RFC4918
89
        508 => 'Loop Detected',                                               // RFC5842
90
        510 => 'Not Extended',                                                // RFC2774
91
        511 => 'Network Authentication Required',                             // RFC6585
92
    ];
93
94
95
    /**
96
     * Default http status code
97
     */
98
    const DEFAULT_STATUS_CODE = 200;
99
100
    /**
101
     * Not Modified http status code
102
     */
103
    const NOT_MODIFIED_STATUS_CODE = 304;
104
105
    /**
106
     * Limits for tracked ids
107
     */
108
    const MAX_TRACKED_EVENTS = 200;
109
110
    /**
111
     * Url prefixes
112
     */
113
    const URL_PREFIX = 'https://api.telegram.org/bot';
114
115
    /**
116
     * Url prefix for files
117
     */
118
    const FILE_URL_PREFIX = 'https://api.telegram.org/file/bot';
119
120
    /**
121
     * CURL object
122
     *
123
     * @var
124
     */
125
    protected $curl;
126
127
    /**
128
     * Bot token
129
     *
130
     * @var string
131
     */
132
    protected $token;
133
134
    /**
135
     * Botan tracker
136
     *
137
     * @var \TelegramBot\Api\Botan
138
     */
139
    protected $tracker;
140
141
    /**
142
     * list of event ids
143
     *
144
     * @var array
145
     */
146
    protected $trackedEvents = [];
147
148
    /**
149
     * Check whether return associative array
150
     *
151
     * @var bool
152
     */
153
    protected $returnArray = true;
154
155
156
    /**
157
     * Constructor
158
     *
159
     * @param string $token Telegram Bot API token
160 9
     * @param string|null $trackerToken Yandex AppMetrica application api_key
161
     */
162 9
    public function __construct($token, $trackerToken = null)
163 9
    {
164
        $this->curl = curl_init();
165 9
        $this->token = $token;
166
167
        if ($trackerToken) {
168 9
            $this->tracker = new Botan($trackerToken);
169
        }
170
    }
171
172
    /**
173
     * Set return array
174
     *
175
     * @param bool $mode
176
     *
177
     * @return $this
178
     */
179
    public function setModeObject($mode = true)
180
    {
181
        $this->returnArray = !$mode;
182
183
        return $this;
184
    }
185
186
187
    /**
188
     * Call method
189
     *
190
     * @param string $method
191
     * @param array|null $data
192
     *
193
     * @return mixed
194
     * @throws \TelegramBot\Api\Exception
195
     * @throws \TelegramBot\Api\HttpException
196
     * @throws \TelegramBot\Api\InvalidJsonException
197
     */
198
    public function call($method, array $data = null)
199
    {
200
        $options = [
201
            CURLOPT_URL => $this->getUrl().'/'.$method,
202
            CURLOPT_RETURNTRANSFER => true,
203
            CURLOPT_POST => null,
204
            CURLOPT_POSTFIELDS => null,
205
        ];
206
207
        if ($data) {
208
            $options[CURLOPT_POST] = true;
209
            $options[CURLOPT_POSTFIELDS] = $data;
210
        }
211
212
        $response = self::jsonValidate($this->executeCurl($options), $this->returnArray);
213
214
        if ($this->returnArray) {
215
            if (!isset($response['ok'])) {
216
                throw new Exception($response['description'], $response['error_code']);
217
            }
218
219
            return $response['result'];
220
        }
221
222
        if (!$response->ok) {
223
            throw new Exception($response->description, $response->error_code);
224
        }
225
226
        return $response->result;
227
    }
228
229
    /**
230
     * curl_exec wrapper for response validation
231
     *
232
     * @param array $options
233
     *
234
     * @return string
235
     *
236
     * @throws \TelegramBot\Api\HttpException
237
     */
238
    protected function executeCurl(array $options)
239
    {
240
        curl_setopt_array($this->curl, $options);
241
242
        $result = curl_exec($this->curl);
243
        self::curlValidate($this->curl, $result);
244
        if ($result === false) {
245
            throw new HttpException(curl_error($this->curl), curl_errno($this->curl));
246
        }
247
248
        return $result;
249
    }
250
251
    /**
252
     * Response validation
253
     *
254
     * @param resource $curl
255
     * @param string $response
256
     * @throws HttpException
257
     */
258
    public static function curlValidate($curl, $response = null)
259
    {
260
        $json = json_decode($response, true)?: [];
261
        if (($httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE))
262
            && !in_array($httpCode, [self::DEFAULT_STATUS_CODE, self::NOT_MODIFIED_STATUS_CODE])
263
        ) {
264
            $errorDescription = array_key_exists('description', $json) ? $json['description'] : self::$codes[$httpCode];
265
            throw new HttpException($errorDescription, $httpCode);
266
        }
267
    }
268
269
    /**
270
     * JSON validation
271
     *
272
     * @param string $jsonString
273
     * @param boolean $asArray
274
     *
275
     * @return object|array
276
     * @throws \TelegramBot\Api\InvalidJsonException
277
     */
278
    public static function jsonValidate($jsonString, $asArray)
279
    {
280
        $json = json_decode($jsonString, $asArray);
281
282
        if (json_last_error() != JSON_ERROR_NONE) {
283
            throw new InvalidJsonException(json_last_error_msg(), json_last_error());
284
        }
285
286
        return $json;
287
    }
288
289
    /**
290
     * Use this method to send text messages. On success, the sent \TelegramBot\Api\Types\Message is returned.
291
     *
292
     * @param int|string $chatId
293
     * @param string $text
294
     * @param string|null $parseMode
295
     * @param bool $disablePreview
296
     * @param int|null $replyToMessageId
297
     * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|null $replyMarkup
298
     * @param bool $disableNotification
299
     *
300
     * @return \TelegramBot\Api\Types\Message
301
     * @throws \TelegramBot\Api\InvalidArgumentException
302
     * @throws \TelegramBot\Api\Exception
303
     */
304
    public function sendMessage(
305
        $chatId,
306
        $text,
307
        $parseMode = null,
308
        $disablePreview = false,
309
        $replyToMessageId = null,
310
        $replyMarkup = null,
311
        $disableNotification = false
312
    ) {
313
        return Message::fromResponse($this->call('sendMessage', [
314
            'chat_id' => $chatId,
315
            'text' => $text,
316
            'parse_mode' => $parseMode,
317
            'disable_web_page_preview' => $disablePreview,
318
            'reply_to_message_id' => (int)$replyToMessageId,
319
            'reply_markup' => is_null($replyMarkup) ? $replyMarkup : $replyMarkup->toJson(),
320
            'disable_notification' => (bool)$disableNotification,
321
        ]));
322
    }
323
324
    /**
325
     * Use this method to send phone contacts
326
     *
327
     * @param int|string $chatId chat_id or @channel_name
328
     * @param string $phoneNumber
329
     * @param string $firstName
330
     * @param string $lastName
331
     * @param int|null $replyToMessageId
332
     * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|null $replyMarkup
333
     * @param bool $disableNotification
334
     *
335
     * @return \TelegramBot\Api\Types\Message
336
     * @throws \TelegramBot\Api\Exception
337
     */
338 View Code Duplication
    public function sendContact(
339
        $chatId,
340
        $phoneNumber,
341
        $firstName,
342
        $lastName = null,
343
        $replyToMessageId = null,
344
        $replyMarkup = null,
345
        $disableNotification = false
346
    ) {
347
        return Message::fromResponse($this->call('sendContact', [
348
            'chat_id' => $chatId,
349
            'phone_number' => $phoneNumber,
350
            'first_name' => $firstName,
351
            'last_name' => $lastName,
352
            'reply_to_message_id' => $replyToMessageId,
353
            'reply_markup' => is_null($replyMarkup) ? $replyMarkup : $replyMarkup->toJson(),
354
            'disable_notification' => (bool)$disableNotification,
355
        ]));
356
    }
357
358
    /**
359
     * Use this method when you need to tell the user that something is happening on the bot's side.
360
     * The status is set for 5 seconds or less (when a message arrives from your bot,
361
     * Telegram clients clear its typing status).
362
     *
363
     * We only recommend using this method when a response from the bot will take a noticeable amount of time to arrive.
364
     *
365
     * Type of action to broadcast. Choose one, depending on what the user is about to receive:
366
     * `typing` for text messages, `upload_photo` for photos, `record_video` or `upload_video` for videos,
367
     * `record_audio` or upload_audio for audio files, `upload_document` for general files,
368
     * `find_location` for location data.
369
     *
370
     * @param int $chatId
371
     * @param string $action
372
     *
373
     * @return bool
374
     * @throws \TelegramBot\Api\Exception
375
     */
376
    public function sendChatAction($chatId, $action)
377
    {
378
        return $this->call('sendChatAction', [
379
            'chat_id' => $chatId,
380
            'action' => $action,
381
        ]);
382
    }
383
384
    /**
385
     * Use this method to get a list of profile pictures for a user.
386
     *
387
     * @param int $userId
388
     * @param int $offset
389
     * @param int $limit
390
     *
391
     * @return \TelegramBot\Api\Types\UserProfilePhotos
392
     * @throws \TelegramBot\Api\Exception
393
     */
394
    public function getUserProfilePhotos($userId, $offset = 0, $limit = 100)
395
    {
396
        return UserProfilePhotos::fromResponse($this->call('getUserProfilePhotos', [
397
            'user_id' => (int)$userId,
398
            'offset' => (int)$offset,
399
            'limit' => (int)$limit,
400
        ]));
401
    }
402
403
    /**
404
     * Use this method to specify a url and receive incoming updates via an outgoing webhook.
405
     * Whenever there is an update for the bot, we will send an HTTPS POST request to the specified url,
406
     * containing a JSON-serialized Update.
407
     * In case of an unsuccessful request, we will give up after a reasonable amount of attempts.
408
     *
409
     * @param string $url HTTPS url to send updates to. Use an empty string to remove webhook integration
410
     * @param \CURLFile|string $certificate Upload your public key certificate
411
     *                                      so that the root certificate in use can be checked
412
     *
413
     * @return string
414
     *
415
     * @throws \TelegramBot\Api\Exception
416
     */
417
    public function setWebhook($url = '', $certificate = null)
418
    {
419
        return $this->call('setWebhook', ['url' => $url, 'certificate' => $certificate]);
420
    }
421
422
    /**
423
     * A simple method for testing your bot's auth token.Requires no parameters.
424
     * Returns basic information about the bot in form of a User object.
425
     *
426
     * @return \TelegramBot\Api\Types\User
427
     * @throws \TelegramBot\Api\Exception
428
     * @throws \TelegramBot\Api\InvalidArgumentException
429
     */
430
    public function getMe()
431
    {
432
        return User::fromResponse($this->call('getMe'));
433
    }
434
435
    /**
436
     * Use this method to receive incoming updates using long polling.
437
     * An Array of Update objects is returned.
438
     *
439
     * Notes
440
     * 1. This method will not work if an outgoing webhook is set up.
441
     * 2. In order to avoid getting duplicate updates, recalculate offset after each server response.
442
     *
443
     * @param int $offset
444
     * @param int $limit
445
     * @param int $timeout
446
     *
447
     * @return Update[]
448
     * @throws \TelegramBot\Api\Exception
449 2
     * @throws \TelegramBot\Api\InvalidArgumentException
450
     */
451 2
    public function getUpdates($offset = 0, $limit = 100, $timeout = 0)
452 2
    {
453 2
        $updates = ArrayOfUpdates::fromResponse($this->call('getUpdates', [
454 2
            'offset' => $offset,
455 2
            'limit' => $limit,
456
            'timeout' => $timeout,
457 2
        ]));
458
459
        if ($this->tracker instanceof Botan) {
460
            foreach ($updates as $update) {
461
                $this->trackUpdate($update);
462
            }
463 2
        }
464
465
        return $updates;
466
    }
467
468
    /**
469
     * Use this method to send point on the map. On success, the sent Message is returned.
470
     *
471
     * @param int $chatId
472
     * @param float $latitude
473
     * @param float $longitude
474
     * @param int|null $replyToMessageId
475
     * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|null $replyMarkup
476
     * @param bool $disableNotification
477
     *
478
     * @return \TelegramBot\Api\Types\Message
479
     * @throws \TelegramBot\Api\Exception
480
     */
481 View Code Duplication
    public function sendLocation(
482
        $chatId,
483
        $latitude,
484
        $longitude,
485
        $replyToMessageId = null,
486
        $replyMarkup = null,
487
        $disableNotification = false
488
    ) {
489
        return Message::fromResponse($this->call('sendLocation', [
490
            'chat_id' => $chatId,
491
            'latitude' => $latitude,
492
            'longitude' => $longitude,
493
            'reply_to_message_id' => $replyToMessageId,
494
            'reply_markup' => is_null($replyMarkup) ? $replyMarkup : $replyMarkup->toJson(),
495
            'disable_notification' => (bool)$disableNotification,
496
        ]));
497
    }
498
499
    /**
500
     * Use this method to send information about a venue. On success, the sent Message is returned.
501
     *
502
     * @param int|string $chatId chat_id or @channel_name
503
     * @param float $latitude
504
     * @param float $longitude
505
     * @param string $title
506
     * @param string $address
507
     * @param string|null $foursquareId
508
     * @param int|null $replyToMessageId
509
     * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|null $replyMarkup
510
     * @param bool $disableNotification
511
     *
512
     * @return \TelegramBot\Api\Types\Message
513
     * @throws \TelegramBot\Api\Exception
514
     */
515
    public function sendVenue(
516
        $chatId,
517
        $latitude,
518
        $longitude,
519
        $title,
520
        $address,
521
        $foursquareId = null,
522
        $replyToMessageId = null,
523
        $replyMarkup = null,
524
        $disableNotification = false
525
    ) {
526
        return Message::fromResponse($this->call('sendVenue', [
527
            'chat_id' => $chatId,
528
            'latitude' => $latitude,
529
            'longitude' => $longitude,
530
            'title' => $title,
531
            'address' => $address,
532
            'foursquare_id' => $foursquareId,
533
            'reply_to_message_id' => $replyToMessageId,
534
            'reply_markup' => is_null($replyMarkup) ? $replyMarkup : $replyMarkup->toJson(),
535
            'disable_notification' => (bool)$disableNotification,
536
        ]));
537
    }
538
539
    /**
540
     * Use this method to send .webp stickers. On success, the sent Message is returned.
541
     *
542
     * @param int|string $chatId chat_id or @channel_name
543
     * @param \CURLFile|string $sticker
544
     * @param int|null $replyToMessageId
545
     * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|null $replyMarkup
546
     * @param bool $disableNotification
547
     *
548
     * @return \TelegramBot\Api\Types\Message
549
     * @throws \TelegramBot\Api\InvalidArgumentException
550
     * @throws \TelegramBot\Api\Exception
551
     */
552 View Code Duplication
    public function sendSticker(
553
        $chatId,
554
        $sticker,
555
        $replyToMessageId = null,
556
        $replyMarkup = null,
557
        $disableNotification = false
558
    ) {
559
        return Message::fromResponse($this->call('sendSticker', [
560
            'chat_id' => $chatId,
561
            'sticker' => $sticker,
562
            'reply_to_message_id' => $replyToMessageId,
563
            'reply_markup' => is_null($replyMarkup) ? $replyMarkup : $replyMarkup->toJson(),
564
            'disable_notification' => (bool)$disableNotification,
565
        ]));
566
    }
567
568
    /**
569
     * Use this method to send video files,
570
     * Telegram clients support mp4 videos (other formats may be sent as Document).
571
     * On success, the sent Message is returned.
572
     *
573
     * @param int|string $chatId chat_id or @channel_name
574
     * @param \CURLFile|string $video
575
     * @param int|null $duration
576
     * @param string|null $caption
577
     * @param int|null $replyToMessageId
578
     * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|null $replyMarkup
579
     * @param bool $disableNotification
580
     *
581
     * @return \TelegramBot\Api\Types\Message
582
     * @throws \TelegramBot\Api\InvalidArgumentException
583
     * @throws \TelegramBot\Api\Exception
584
     */
585
    public function sendVideo(
586
        $chatId,
587
        $video,
588
        $duration = null,
589
        $caption = null,
590
        $replyToMessageId = null,
591
        $replyMarkup = null,
592
        $disableNotification = false
593
    ) {
594
        return Message::fromResponse($this->call('sendVideo', [
595
            'chat_id' => $chatId,
596
            'video' => $video,
597
            'duration' => $duration,
598
            'caption' => $caption,
599
            'reply_to_message_id' => $replyToMessageId,
600
            'reply_markup' => is_null($replyMarkup) ? $replyMarkup : $replyMarkup->toJson(),
601
            'disable_notification' => (bool)$disableNotification,
602
        ]));
603
    }
604
605
    /**
606
     * Use this method to send audio files,
607
     * if you want Telegram clients to display the file as a playable voice message.
608
     * For this to work, your audio must be in an .ogg file encoded with OPUS
609
     * (other formats may be sent as Audio or Document).
610
     * On success, the sent Message is returned.
611
     * Bots can currently send voice messages of up to 50 MB in size, this limit may be changed in the future.
612
     *
613
     * @param int|string $chatId chat_id or @channel_name
614
     * @param \CURLFile|string $voice
615
     * @param int|null $duration
616
     * @param int|null $replyToMessageId
617
     * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|null $replyMarkup
618
     * @param bool $disableNotification
619
     *
620
     * @return \TelegramBot\Api\Types\Message
621
     * @throws \TelegramBot\Api\InvalidArgumentException
622
     * @throws \TelegramBot\Api\Exception
623
     */
624 View Code Duplication
    public function sendVoice(
625
        $chatId,
626
        $voice,
627
        $duration = null,
628
        $replyToMessageId = null,
629
        $replyMarkup = null,
630
        $disableNotification = false
631
    ) {
632
        return Message::fromResponse($this->call('sendVoice', [
633
            'chat_id' => $chatId,
634
            'voice' => $voice,
635
            'duration' => $duration,
636
            'reply_to_message_id' => $replyToMessageId,
637
            'reply_markup' => is_null($replyMarkup) ? $replyMarkup : $replyMarkup->toJson(),
638
            'disable_notification' => (bool)$disableNotification,
639
        ]));
640
    }
641
642
    /**
643
     * Use this method to forward messages of any kind. On success, the sent Message is returned.
644
     *
645
     * @param int|string $chatId chat_id or @channel_name
646
     * @param int $fromChatId
647
     * @param int $messageId
648
     * @param bool $disableNotification
649
     *
650
     * @return \TelegramBot\Api\Types\Message
651
     * @throws \TelegramBot\Api\InvalidArgumentException
652
     * @throws \TelegramBot\Api\Exception
653
     */
654
    public function forwardMessage($chatId, $fromChatId, $messageId, $disableNotification = false)
655
    {
656
        return Message::fromResponse($this->call('forwardMessage', [
657
            'chat_id' => $chatId,
658
            'from_chat_id' => $fromChatId,
659
            'message_id' => (int)$messageId,
660
            'disable_notification' => (bool)$disableNotification,
661
        ]));
662
    }
663
664
    /**
665
     * Use this method to send audio files,
666
     * if you want Telegram clients to display them in the music player.
667
     * Your audio must be in the .mp3 format.
668
     * On success, the sent Message is returned.
669
     * Bots can currently send audio files of up to 50 MB in size, this limit may be changed in the future.
670
     *
671
     * For backward compatibility, when the fields title and performer are both empty
672
     * and the mime-type of the file to be sent is not audio/mpeg, the file will be sent as a playable voice message.
673
     * For this to work, the audio must be in an .ogg file encoded with OPUS.
674
     * This behavior will be phased out in the future. For sending voice messages, use the sendVoice method instead.
675
     *
676
     * @deprecated since 20th February. Removed backward compatibility from the method sendAudio.
677
     * Voice messages now must be sent using the method sendVoice.
678
     * There is no more need to specify a non-empty title or performer while sending the audio by file_id.
679
     *
680
     * @param int|string $chatId chat_id or @channel_name
681
     * @param \CURLFile|string $audio
682
     * @param int|null $duration
683
     * @param string|null $performer
684
     * @param string|null $title
685
     * @param int|null $replyToMessageId
686
     * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|null $replyMarkup
687
     * @param bool $disableNotification
688
     *
689
     * @return \TelegramBot\Api\Types\Message
690
     * @throws \TelegramBot\Api\InvalidArgumentException
691
     * @throws \TelegramBot\Api\Exception
692
     */
693
    public function sendAudio(
694
        $chatId,
695
        $audio,
696
        $duration = null,
697
        $performer = null,
698
        $title = null,
699
        $replyToMessageId = null,
700
        $replyMarkup = null,
701
        $disableNotification = false
702
    ) {
703
        return Message::fromResponse($this->call('sendAudio', [
704
            'chat_id' => $chatId,
705
            'audio' => $audio,
706
            'duration' => $duration,
707
            'performer' => $performer,
708
            'title' => $title,
709
            'reply_to_message_id' => $replyToMessageId,
710
            'reply_markup' => is_null($replyMarkup) ? $replyMarkup : $replyMarkup->toJson(),
711
            'disable_notification' => (bool)$disableNotification,
712
        ]));
713
    }
714
715
    /**
716
     * Use this method to send photos. On success, the sent Message is returned.
717
     *
718
     * @param int|string $chatId chat_id or @channel_name
719
     * @param \CURLFile|string $photo
720
     * @param string|null $caption
721
     * @param int|null $replyToMessageId
722
     * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|null $replyMarkup
723
     * @param bool $disableNotification
724
     *
725
     * @return \TelegramBot\Api\Types\Message
726
     * @throws \TelegramBot\Api\InvalidArgumentException
727
     * @throws \TelegramBot\Api\Exception
728
     */
729 View Code Duplication
    public function sendPhoto(
730
        $chatId,
731
        $photo,
732
        $caption = null,
733
        $replyToMessageId = null,
734
        $replyMarkup = null,
735
        $disableNotification = false
736
    ) {
737
        return Message::fromResponse($this->call('sendPhoto', [
738
            'chat_id' => $chatId,
739
            'photo' => $photo,
740
            'caption' => $caption,
741
            'reply_to_message_id' => $replyToMessageId,
742
            'reply_markup' => is_null($replyMarkup) ? $replyMarkup : $replyMarkup->toJson(),
743
            'disable_notification' => (bool)$disableNotification,
744
        ]));
745
    }
746
747
    /**
748
     * Use this method to send general files. On success, the sent Message is returned.
749
     * Bots can currently send files of any type of up to 50 MB in size, this limit may be changed in the future.
750
     *
751
     * @param int|string $chatId chat_id or @channel_name
752
     * @param \CURLFile|string $document
753
     * @param string|null $caption
754
     * @param int|null $replyToMessageId
755
     * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|null $replyMarkup
756
     * @param bool $disableNotification
757
     *
758
     * @return \TelegramBot\Api\Types\Message
759
     * @throws \TelegramBot\Api\InvalidArgumentException
760
     * @throws \TelegramBot\Api\Exception
761
     */
762 View Code Duplication
    public function sendDocument(
763
        $chatId,
764
        $document,
765
        $caption = null,
766
        $replyToMessageId = null,
767
        $replyMarkup = null,
768
        $disableNotification = false
769
    ) {
770
        return Message::fromResponse($this->call('sendDocument', [
771
            'chat_id' => $chatId,
772
            'document' => $document,
773
            'caption' => $caption,
774
            'reply_to_message_id' => $replyToMessageId,
775
            'reply_markup' => is_null($replyMarkup) ? $replyMarkup : $replyMarkup->toJson(),
776
            'disable_notification' => (bool)$disableNotification,
777
        ]));
778
    }
779
780
    /**
781
     * Use this method to get basic info about a file and prepare it for downloading.
782
     * For the moment, bots can download files of up to 20MB in size.
783
     * On success, a File object is returned.
784
     * The file can then be downloaded via the link https://api.telegram.org/file/bot<token>/<file_path>,
785
     * where <file_path> is taken from the response.
786
     * It is guaranteed that the link will be valid for at least 1 hour.
787
     * When the link expires, a new one can be requested by calling getFile again.
788
     *
789
     * @param $fileId
790
     *
791
     * @return \TelegramBot\Api\Types\File
792
     * @throws \TelegramBot\Api\InvalidArgumentException
793
     * @throws \TelegramBot\Api\Exception
794
     */
795
    public function getFile($fileId)
796
    {
797
        return File::fromResponse($this->call('getFile', ['file_id' => $fileId]));
798
    }
799
800
    /**
801
     * Get file contents via cURL
802
     *
803
     * @param $fileId
804
     *
805
     * @return string
806
     *
807
     * @throws \TelegramBot\Api\HttpException
808
     */
809
    public function downloadFile($fileId)
810
    {
811
        $file = $this->getFile($fileId);
812
        $options = [
813
            CURLOPT_HEADER => 0,
814
            CURLOPT_HTTPGET => 1,
815
            CURLOPT_RETURNTRANSFER => 1,
816
            CURLOPT_URL => $this->getFileUrl().'/'.$file->getFilePath(),
817
        ];
818
819
        return $this->executeCurl($options);
820
    }
821
822
    /**
823
     * Use this method to send answers to an inline query. On success, True is returned.
824
     * No more than 50 results per query are allowed.
825
     *
826
     * @param string $inlineQueryId
827
     * @param AbstractInlineQueryResult[] $results
828
     * @param int $cacheTime
829
     * @param bool $isPersonal
830
     * @param string $nextOffset
831
     *
832
     * @return mixed
833
     * @throws Exception
834
     */
835
    public function answerInlineQuery($inlineQueryId, $results, $cacheTime = 300, $isPersonal = false, $nextOffset = '')
836
    {
837
        $results = array_map(function ($item) {
838
            /* @var AbstractInlineQueryResult $item */
839
840
            return json_decode($item->toJson(), true);
841
        }, $results);
842
843
        return $this->call('answerInlineQuery', [
844
            'inline_query_id' => $inlineQueryId,
845
            'results' => json_encode($results),
846
            'cache_time' => $cacheTime,
847
            'is_personal' => $isPersonal,
848
            'next_offset' => $nextOffset,
849
        ]);
850
    }
851
852
    /**
853
     * Use this method to kick a user from a group or a supergroup.
854
     * In the case of supergroups, the user will not be able to return to the group
855
     * on their own using invite links, etc., unless unbanned first.
856
     * The bot must be an administrator in the group for this to work. Returns True on success.
857
     *
858
     * @param int|string $chatId Unique identifier for the target group
859
     * or username of the target supergroup (in the format @supergroupusername)
860
     * @param int $userId Unique identifier of the target user
861
     * @param null|int $untilDate Date when the user will be unbanned, unix time.
862
     *                            If user is banned for more than 366 days or less than 30 seconds from the current time
863
     *                            they are considered to be banned forever
864
     *
865
     * @return bool
866
     */
867
    public function kickChatMember($chatId, $userId, $untilDate = null)
868
    {
869
        return $this->call('kickChatMember', [
870
            'chat_id' => $chatId,
871
            'user_id' => $userId,
872
            'until_date' => $untilDate
873
        ]);
874
    }
875
876
    /**
877
     * Use this method to unban a previously kicked user in a supergroup.
878
     * The user will not return to the group automatically, but will be able to join via link, etc.
879
     * The bot must be an administrator in the group for this to work. Returns True on success.
880
     *
881
     * @param int|string $chatId Unique identifier for the target group
882
     * or username of the target supergroup (in the format @supergroupusername)
883
     * @param int $userId Unique identifier of the target user
884
     *
885
     * @return bool
886
     */
887
    public function unbanChatMember($chatId, $userId)
888
    {
889
        return $this->call('unbanChatMember', [
890
            'chat_id' => $chatId,
891
            'user_id' => $userId,
892
        ]);
893
    }
894
895
    /**
896
     * Use this method to send answers to callback queries sent from inline keyboards.
897
     * The answer will be displayed to the user as a notification at the top of the chat screen or as an alert.
898
     *
899
     * @param $callbackQueryId
900
     * @param null $text
901
     * @param bool $showAlert
902
     *
903
     * @return bool
904
     */
905
    public function answerCallbackQuery($callbackQueryId, $text = null, $showAlert = false)
906
    {
907
        return $this->call('answerCallbackQuery', [
908
            'callback_query_id' => $callbackQueryId,
909
            'text' => $text,
910
            'show_alert' => (bool)$showAlert,
911
        ]);
912
    }
913
914
915
    /**
916
     * Use this method to edit text messages sent by the bot or via the bot
917
     *
918
     * @param int|string $chatId
919
     * @param int $messageId
920
     * @param string $text
921
     * @param string $inlineMessageId
922
     * @param string|null $parseMode
923
     * @param bool $disablePreview
924
     * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|null $replyMarkup
925
     * @return Message
926
     */
927 View Code Duplication
    public function editMessageText(
928
        $chatId,
929
        $messageId,
930
        $text,
931
        $parseMode = null,
932
        $disablePreview = false,
933
        $replyMarkup = null,
934
        $inlineMessageId = null
935
    ) {
936
        return Message::fromResponse($this->call('editMessageText', [
937
            'chat_id' => $chatId,
938
            'message_id' => $messageId,
939
            'text' => $text,
940
            'inline_message_id' => $inlineMessageId,
941
            'parse_mode' => $parseMode,
942
            'disable_web_page_preview' => $disablePreview,
943
            'reply_markup' => is_null($replyMarkup) ? $replyMarkup : $replyMarkup->toJson(),
944
        ]));
945
    }
946
947
    /**
948
     * Use this method to edit text messages sent by the bot or via the bot
949
     *
950
     * @param int|string $chatId
951
     * @param int $messageId
952
     * @param string|null $caption
953
     * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|null $replyMarkup
954
     * @param string $inlineMessageId
955
     *
956
     * @return \TelegramBot\Api\Types\Message
957
     * @throws \TelegramBot\Api\InvalidArgumentException
958
     * @throws \TelegramBot\Api\Exception
959
     */
960
    public function editMessageCaption(
961
        $chatId,
962
        $messageId,
963
        $caption = null,
964
        $replyMarkup = null,
965
        $inlineMessageId = null
966
    ) {
967
        return Message::fromResponse($this->call('editMessageCaption', [
968
            'chat_id' => $chatId,
969
            'message_id' => $messageId,
970
            'inline_message_id' => $inlineMessageId,
971
            'caption' => $caption,
972
            'reply_markup' => is_null($replyMarkup) ? $replyMarkup : $replyMarkup->toJson(),
973
        ]));
974
    }
975
976
    /**
977
     * Use this method to edit only the reply markup of messages sent by the bot or via the bot
978
     *
979
     * @param int|string $chatId
980
     * @param int $messageId
981
     * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|null $replyMarkup
982
     * @param string $inlineMessageId
983
     *
984
     * @return Message
985
     */
986 View Code Duplication
    public function editMessageReplyMarkup(
987
        $chatId,
988
        $messageId,
989
        $replyMarkup = null,
990
        $inlineMessageId = null
991
    ) {
992
        return Message::fromResponse($this->call('editMessageReplyMarkup', [
993
            'chat_id' => $chatId,
994
            'message_id' => $messageId,
995
            'inline_message_id' => $inlineMessageId,
996
            'reply_markup' => is_null($replyMarkup) ? $replyMarkup : $replyMarkup->toJson(),
997 9
        ]));
998
    }
999 9
1000 9
    /**
1001
     * Close curl
1002
     */
1003
    public function __destruct()
1004
    {
1005
        $this->curl && curl_close($this->curl);
1006
    }
1007
1008
    /**
1009
     * @return string
1010
     */
1011
    public function getUrl()
1012
    {
1013
        return self::URL_PREFIX.$this->token;
1014
    }
1015
1016
    /**
1017
     * @return string
1018
     */
1019
    public function getFileUrl()
1020
    {
1021
        return self::FILE_URL_PREFIX.$this->token;
1022
    }
1023
1024
    /**
1025
     * @param \TelegramBot\Api\Types\Update $update
1026
     * @param string $eventName
1027
     *
1028
     * @throws \TelegramBot\Api\Exception
1029
     */
1030
    public function trackUpdate(Update $update, $eventName = 'Message')
1031
    {
1032
        if (!in_array($update->getUpdateId(), $this->trackedEvents)) {
1033
            $this->trackedEvents[] = $update->getUpdateId();
1034
1035
            $this->track($update->getMessage(), $eventName);
1036
1037
            if (count($this->trackedEvents) > self::MAX_TRACKED_EVENTS) {
1038
                $this->trackedEvents = array_slice($this->trackedEvents, round(self::MAX_TRACKED_EVENTS / 4));
1039
            }
1040
        }
1041
    }
1042
1043
    /**
1044
     * Wrapper for tracker
1045
     *
1046
     * @param \TelegramBot\Api\Types\Message $message
1047
     * @param string $eventName
1048
     *
1049
     * @throws \TelegramBot\Api\Exception
1050
     */
1051
    public function track(Message $message, $eventName = 'Message')
1052
    {
1053
        if ($this->tracker instanceof Botan) {
1054
            $this->tracker->track($message, $eventName);
1055
        }
1056
    }
1057
1058
    /**
1059
     * Use this method to send invoices. On success, the sent Message is returned.
1060
     *
1061
     * @param int|string $chatId
1062
     * @param string $title
1063
     * @param string $description
1064
     * @param string $payload
1065
     * @param string $providerToken
1066
     * @param string $startParameter
1067
     * @param string $currency
1068
     * @param array $prices
1069
     * @param string|null $photoUrl
1070
     * @param int|null $photoSize
1071
     * @param int|null $photoWidth
1072
     * @param int|null $photoHeight
1073
     * @param bool $needName
1074
     * @param bool $needPhoneNumber
1075
     * @param bool $needEmail
1076
     * @param bool $needShippingAddress
1077
     * @param bool $isFlexible
1078
     * @param int|null $replyToMessageId
1079
     * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|null $replyMarkup
1080
     * @param bool $disableNotification
1081
     *
1082
     * @return Message
1083
     */
1084
    public function sendInvoice(
1085
        $chatId,
1086
        $title,
1087
        $description,
1088
        $payload,
1089
        $providerToken,
1090
        $startParameter,
1091
        $currency,
1092
        $prices,
1093
        $isFlexible = false,
1094
        $photoUrl = null,
1095
        $photoSize = null,
1096
        $photoWidth = null,
1097
        $photoHeight = null,
1098
        $needName = false,
1099
        $needPhoneNumber = false,
1100
        $needEmail = false,
1101
        $needShippingAddress = false,
1102
        $replyToMessageId = null,
1103
        $replyMarkup = null,
1104
        $disableNotification = false
1105
    ) {
1106
        return Message::fromResponse($this->call('sendInvoice', [
1107
            'chat_id' => $chatId,
1108
            'title' => $title,
1109
            'description' => $description,
1110
            'payload' => $payload,
1111
            'provider_token' => $providerToken,
1112
            'start_parameter' => $startParameter,
1113
            'currency' => $currency,
1114
            'prices' => json_encode($prices),
1115
            'is_flexible' => $isFlexible,
1116
            'photo_url' => $photoUrl,
1117
            'photo_size' => $photoSize,
1118
            'photo_width' => $photoWidth,
1119
            'photo_height' => $photoHeight,
1120
            'need_name' => $needName,
1121
            'need_phone_number' => $needPhoneNumber,
1122
            'need_email' => $needEmail,
1123
            'need_shipping_address' => $needShippingAddress,
1124
            'reply_to_message_id' => $replyToMessageId,
1125
            'reply_markup' => is_null($replyMarkup) ? $replyMarkup : $replyMarkup->toJson(),
1126
            'disable_notification' => (bool)$disableNotification,
1127
        ]));
1128
    }
1129
1130
    /**
1131
     * If you sent an invoice requesting a shipping address and the parameter is_flexible was specified, the Bot API
1132
     * will send an Update with a shipping_query field to the bot. Use this method to reply to shipping queries.
1133
     * On success, True is returned.
1134
     *
1135
     * @param string $shippingQueryId
1136
     * @param bool $ok
1137
     * @param array $shipping_options
1138
     * @param null|string $errorMessage
1139
     *
1140
     * @return bool
1141
     *
1142
     */
1143
    public function answerShippingQuery($shippingQueryId, $ok = true, $shipping_options = [], $errorMessage = null)
1144
    {
1145
        return $this->call('answerShippingQuery', [
1146
            'shipping_query_id' => $shippingQueryId,
1147
            'ok' => (bool)$ok,
1148
            'shipping_options' => json_encode($shipping_options),
1149
            'error_message' => $errorMessage
1150
        ]);
1151
    }
1152
1153
    /**
1154
     * Use this method to respond to such pre-checkout queries. On success, True is returned.
1155
     * Note: The Bot API must receive an answer within 10 seconds after the pre-checkout query was sent.
1156
     *
1157
     * @param string $preCheckoutQueryId
1158
     * @param bool $ok
1159
     * @param null|string $errorMessage
1160
     *
1161
     * @return mixed
1162
     */
1163
    public function answerPreCheckoutQuery($preCheckoutQueryId, $ok = true, $errorMessage = null)
1164
    {
1165
        return $this->call('answerPreCheckoutQuery', [
1166
            'pre_checkout_query_id' => $preCheckoutQueryId,
1167
            'ok' => (bool)$ok,
1168
            'error_message' => $errorMessage
1169
        ]);
1170
    }
1171
1172
    /**
1173
     * Use this method to restrict a user in a supergroup.
1174
     * The bot must be an administrator in the supergroup for this to work and must have the appropriate admin rights.
1175
     * Pass True for all boolean parameters to lift restrictions from a user.
1176
     *
1177
     * @param string|int $chatId Unique identifier for the target chat or username of the target supergroup (in the format @supergroupusername)
1178
     * @param int $userId Unique identifier of the target user
1179
     * @param null|integer $untilDate Date when restrictions will be lifted for the user, unix time.
1180
     *                     If user is restricted for more than 366 days or less than 30 seconds from the current time,
1181
     *                     they are considered to be restricted forever
1182
     * @param bool $canSendMessages Pass True, if the user can send text messages, contacts, locations and venues
1183
     * @param bool $canSendMediaMessages No	Pass True, if the user can send audios, documents, photos, videos, video notes and voice notes, implies can_send_messages
1184
     * @param bool $canSendOtherMessages Pass True, if the user can send animations, games, stickers and use inline bots, implies can_send_media_messages
1185
     * @param bool $canAddWebPagePreviews Pass True, if the user may add web page previews to their messages, implies can_send_media_messages
1186
     *
1187
     * @return bool
1188
     */
1189
    public function restrictChatMember(
1190
        $chatId,
1191
        $userId,
1192
        $untilDate = null,
1193
        $canSendMessages = false,
1194
        $canSendMediaMessages = false,
1195
        $canSendOtherMessages = false,
1196
        $canAddWebPagePreviews = false
1197
    ) {
1198
        return $this->call('restrictChatMember', [
1199
            'chat_id' => $chatId,
1200
            'user_id' => $userId,
1201
            'until_date' => $untilDate,
1202
            'can_send_messages' => $canSendMessages,
1203
            'can_send_media_messages' => $canSendMediaMessages,
1204
            'can_send_other_messages' => $canSendOtherMessages,
1205
            'can_add_web_page_previews' => $canAddWebPagePreviews
1206
        ]);
1207
    }
1208
1209
    /**
1210
     * Use this method to promote or demote a user in a supergroup or a channel.
1211
     * The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
1212
     * Pass False for all boolean parameters to demote a user.
1213
     *
1214
     * @param string|int $chatId Unique identifier for the target chat or username of the target supergroup (in the format @supergroupusername)
1215
     * @param int $userId Unique identifier of the target user
1216
     * @param bool $canChangeInfo Pass True, if the administrator can change chat title, photo and other settings
1217
     * @param bool $canPostMessages Pass True, if the administrator can create channel posts, channels only
1218
     * @param bool $canEditMessages Pass True, if the administrator can edit messages of other users, channels only
1219
     * @param bool $canDeleteMessages Pass True, if the administrator can delete messages of other users
1220
     * @param bool $canInviteUsers 	Pass True, if the administrator can invite new users to the chat
1221
     * @param bool $canRestrictMembers Pass True, if the administrator can restrict, ban or unban chat members
1222
     * @param bool $canPinMessages Pass True, if the administrator can pin messages, supergroups only
1223
     * @param bool $canPromoteMembers Pass True, if the administrator can add new administrators with a subset of his own
1224
     *                                privileges or demote administrators that he has promoted, directly or indirectly
1225
     *                                (promoted by administrators that were appointed by him)
1226
     *
1227
     * @return bool
1228
     */
1229
    public function promoteChatMember(
1230
        $chatId,
1231
        $userId,
1232
        $canChangeInfo = true,
1233
        $canPostMessages = true,
1234
        $canEditMessages = true,
1235
        $canDeleteMessages = true,
1236
        $canInviteUsers = true,
1237
        $canRestrictMembers = true,
1238
        $canPinMessages = true,
1239
        $canPromoteMembers = true
1240
    ) {
1241
        return $this->call('promoteChatMember', [
1242
            'chat_id' => $chatId,
1243
            'user_id' => $userId,
1244
            'can_change_info' => $canChangeInfo,
1245
            'can_post_messages' => $canPostMessages,
1246
            'can_edit_messages' => $canEditMessages,
1247
            'can_delete_messages' => $canDeleteMessages,
1248
            'can_invite_users' => $canInviteUsers,
1249
            'can_restrict_members' => $canRestrictMembers,
1250
            'can_pin_messages' => $canPinMessages,
1251
            'can_promote_members' => $canPromoteMembers
1252
        ]);
1253
    }
1254
1255
    /**
1256
     * Use this method to export an invite link to a supergroup or a channel.
1257
     * The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
1258
     *
1259
     * @param string|int $chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
1260
     * @return string
1261
     */
1262
    public function exportChatInviteLink($chatId)
1263
    {
1264
        return $this->call('exportChatInviteLink', [
1265
            'chat_id' => $chatId
1266
        ]);
1267
    }
1268
1269
    /**
1270
     * Use this method to set a new profile photo for the chat. Photos can't be changed for private chats.
1271
     * The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
1272
     *
1273
     * @param string|int $chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
1274
     * @param \CURLFile|string $photo New chat photo, uploaded using multipart/form-data
1275
     *
1276
     * @return bool
1277
     */
1278
    public function setChatPhoto($chatId, $photo)
1279
    {
1280
        return $this->call('setChatPhoto', [
1281
            'chat_id' => $chatId,
1282
            'photo' => $photo
1283
        ]);
1284
    }
1285
1286
    /**
1287
     * Use this method to delete a chat photo. Photos can't be changed for private chats.
1288
     * The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
1289
     *
1290
     * @param string|int $chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
1291
     *
1292
     * @return bool
1293
     */
1294
    public function deleteChatPhoto($chatId)
1295
    {
1296
        return $this->call('deleteChatPhoto', [
1297
            'chat_id' => $chatId
1298
        ]);
1299
    }
1300
1301
    /**
1302
     * Use this method to change the title of a chat. Titles can't be changed for private chats.
1303
     * The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
1304
     *
1305
     * @param string|int $chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
1306
     * @param string $title New chat title, 1-255 characters
1307
     *
1308
     * @return bool
1309
     */
1310
    public function setChatTitle($chatId, $title)
1311
    {
1312
        return $this->call('setChatTitle', [
1313
            'chat_id' => $chatId,
1314
            'title' => $title
1315
        ]);
1316
    }
1317
1318
    /**
1319
     * Use this method to change the description of a supergroup or a channel.
1320
     * The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
1321
     *
1322
     * @param string|int $chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
1323
     * @param string|null $description New chat description, 0-255 characters
1324
     *
1325
     * @return bool
1326
     */
1327
    public function setChatDescription($chatId, $description = null)
1328
    {
1329
        return $this->call('setChatDescription', [
1330
            'chat_id' => $chatId,
1331
            'title' => $description
1332
        ]);
1333
    }
1334
1335
    /**
1336
     * Use this method to pin a message in a supergroup.
1337
     * The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
1338
     *
1339
     * @param string|int $chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
1340
     * @param int $messageId Identifier of a message to pin
1341
     * @param bool $disableNotification
1342
     *
1343
     * @return bool
1344
     */
1345
    public function pinChatMessage($chatId, $messageId, $disableNotification = false)
1346
    {
1347
        return $this->call('pinChatMessage', [
1348
            'chat_id' => $chatId,
1349
            'message_id' => $messageId,
1350
            'disable_notification' => $disableNotification
1351
        ]);
1352
    }
1353
1354
    /**
1355
     * Use this method to unpin a message in a supergroup chat.
1356
     * The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
1357
     *
1358
     * @param string|int $chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
1359
     *
1360
     * @return bool
1361
     */
1362
    public function unpinChatMessage($chatId)
1363
    {
1364
        return $this->call('unpinChatMessage', [
1365
            'chat_id' => $chatId
1366
        ]);
1367
    }
1368
1369
    /**
1370
     * Use this method to get up to date information about the chat
1371
     * (current name of the user for one-on-one conversations, current username of a user, group or channel, etc.).
1372
     *
1373
     * @param string|int $chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
1374
     *
1375
     * @return Chat
1376
     */
1377
    public function getChat($chatId)
1378
    {
1379
        return Chat::fromResponse($this->call('getChat', [
1380
            'chat_id' => $chatId
1381
        ]));
1382
    }
1383
1384
    /**
1385
     * Use this method to get information about a member of a chat.
1386
     *
1387
     * @param string|int $chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
1388
     * @param int $userId
1389
     *
1390
     * @return ChatMember
1391
     */
1392
    public function getChatMember($chatId, $userId)
1393
    {
1394
        return ChatMember::fromResponse($this->call('getChatMember', [
1395
            'chat_id' => $chatId,
1396
            'user_id' => $userId
1397
        ]));
1398
    }
1399
}
1400