Passed
Push — master ( 3bc9e8...e4d3c0 )
by Gusev
01:49
created

BotApi::sendAudio()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 21
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 0
cp 0
rs 9.3142
c 0
b 0
f 0
cc 2
eloc 18
nc 1
nop 8
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
     * @param string|null $trackerToken Yandex AppMetrica application api_key
161
     */
162 9
    public function __construct($token, $trackerToken = null)
163
    {
164 9
        $this->curl = curl_init();
165 9
        $this->token = $token;
166
167 9
        if ($trackerToken) {
168
            $this->tracker = new Botan($trackerToken);
169
        }
170 9
    }
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
     * @throws \TelegramBot\Api\InvalidArgumentException
450
     */
451 2
    public function getUpdates($offset = 0, $limit = 100, $timeout = 0)
452
    {
453 2
        $updates = ArrayOfUpdates::fromResponse($this->call('getUpdates', [
454 2
            'offset' => $offset,
455 2
            'limit' => $limit,
456 2
            'timeout' => $timeout,
457 2
        ]));
458
459 2
        if ($this->tracker instanceof Botan) {
460
            foreach ($updates as $update) {
461
                $this->trackUpdate($update);
462
            }
463
        }
464
465 2
        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
        ]));
998
    }
999
1000
    /**
1001
     * Use this method to delete a message, including service messages, with the following limitations:
1002
     *  - A message can only be deleted if it was sent less than 48 hours ago.
1003
     *  - Bots can delete outgoing messages in groups and supergroups.
1004
     *  - Bots granted can_post_messages permissions can delete outgoing messages in channels.
1005
     *  - If the bot is an administrator of a group, it can delete any message there.
1006
     *  - If the bot has can_delete_messages permission in a supergroup or a channel, it can delete any message there.
1007
     *
1008
     * @param int|string $chatId
1009
     * @param int $messageId
1010
     *
1011
     * @return bool
1012
     */
1013
    public function deleteMessage($chatId, $messageId)
1014
    {
1015
        return $this->call('deleteMessage', [
1016
            'chat_id' => $chatId,
1017
            'message_id' => $messageId,
1018
        ]);
1019
    }
1020
1021
    /**
1022
     * Close curl
1023
     */
1024 9
    public function __destruct()
1025
    {
1026 9
        $this->curl && curl_close($this->curl);
1027 9
    }
1028
1029
    /**
1030
     * @return string
1031
     */
1032
    public function getUrl()
1033
    {
1034
        return self::URL_PREFIX.$this->token;
1035
    }
1036
1037
    /**
1038
     * @return string
1039
     */
1040
    public function getFileUrl()
1041
    {
1042
        return self::FILE_URL_PREFIX.$this->token;
1043
    }
1044
1045
    /**
1046
     * @param \TelegramBot\Api\Types\Update $update
1047
     * @param string $eventName
1048
     *
1049
     * @throws \TelegramBot\Api\Exception
1050
     */
1051
    public function trackUpdate(Update $update, $eventName = 'Message')
1052
    {
1053
        if (!in_array($update->getUpdateId(), $this->trackedEvents)) {
1054
            $this->trackedEvents[] = $update->getUpdateId();
1055
1056
            $this->track($update->getMessage(), $eventName);
1057
1058
            if (count($this->trackedEvents) > self::MAX_TRACKED_EVENTS) {
1059
                $this->trackedEvents = array_slice($this->trackedEvents, round(self::MAX_TRACKED_EVENTS / 4));
1060
            }
1061
        }
1062
    }
1063
1064
    /**
1065
     * Wrapper for tracker
1066
     *
1067
     * @param \TelegramBot\Api\Types\Message $message
1068
     * @param string $eventName
1069
     *
1070
     * @throws \TelegramBot\Api\Exception
1071
     */
1072
    public function track(Message $message, $eventName = 'Message')
1073
    {
1074
        if ($this->tracker instanceof Botan) {
1075
            $this->tracker->track($message, $eventName);
1076
        }
1077
    }
1078
1079
    /**
1080
     * Use this method to send invoices. On success, the sent Message is returned.
1081
     *
1082
     * @param int|string $chatId
1083
     * @param string $title
1084
     * @param string $description
1085
     * @param string $payload
1086
     * @param string $providerToken
1087
     * @param string $startParameter
1088
     * @param string $currency
1089
     * @param array $prices
1090
     * @param string|null $photoUrl
1091
     * @param int|null $photoSize
1092
     * @param int|null $photoWidth
1093
     * @param int|null $photoHeight
1094
     * @param bool $needName
1095
     * @param bool $needPhoneNumber
1096
     * @param bool $needEmail
1097
     * @param bool $needShippingAddress
1098
     * @param bool $isFlexible
1099
     * @param int|null $replyToMessageId
1100
     * @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|null $replyMarkup
1101
     * @param bool $disableNotification
1102
     *
1103
     * @return Message
1104
     */
1105
    public function sendInvoice(
1106
        $chatId,
1107
        $title,
1108
        $description,
1109
        $payload,
1110
        $providerToken,
1111
        $startParameter,
1112
        $currency,
1113
        $prices,
1114
        $isFlexible = false,
1115
        $photoUrl = null,
1116
        $photoSize = null,
1117
        $photoWidth = null,
1118
        $photoHeight = null,
1119
        $needName = false,
1120
        $needPhoneNumber = false,
1121
        $needEmail = false,
1122
        $needShippingAddress = false,
1123
        $replyToMessageId = null,
1124
        $replyMarkup = null,
1125
        $disableNotification = false
1126
    ) {
1127
        return Message::fromResponse($this->call('sendInvoice', [
1128
            'chat_id' => $chatId,
1129
            'title' => $title,
1130
            'description' => $description,
1131
            'payload' => $payload,
1132
            'provider_token' => $providerToken,
1133
            'start_parameter' => $startParameter,
1134
            'currency' => $currency,
1135
            'prices' => json_encode($prices),
1136
            'is_flexible' => $isFlexible,
1137
            'photo_url' => $photoUrl,
1138
            'photo_size' => $photoSize,
1139
            'photo_width' => $photoWidth,
1140
            'photo_height' => $photoHeight,
1141
            'need_name' => $needName,
1142
            'need_phone_number' => $needPhoneNumber,
1143
            'need_email' => $needEmail,
1144
            'need_shipping_address' => $needShippingAddress,
1145
            'reply_to_message_id' => $replyToMessageId,
1146
            'reply_markup' => is_null($replyMarkup) ? $replyMarkup : $replyMarkup->toJson(),
1147
            'disable_notification' => (bool)$disableNotification,
1148
        ]));
1149
    }
1150
1151
    /**
1152
     * If you sent an invoice requesting a shipping address and the parameter is_flexible was specified, the Bot API
1153
     * will send an Update with a shipping_query field to the bot. Use this method to reply to shipping queries.
1154
     * On success, True is returned.
1155
     *
1156
     * @param string $shippingQueryId
1157
     * @param bool $ok
1158
     * @param array $shipping_options
1159
     * @param null|string $errorMessage
1160
     *
1161
     * @return bool
1162
     *
1163
     */
1164
    public function answerShippingQuery($shippingQueryId, $ok = true, $shipping_options = [], $errorMessage = null)
1165
    {
1166
        return $this->call('answerShippingQuery', [
1167
            'shipping_query_id' => $shippingQueryId,
1168
            'ok' => (bool)$ok,
1169
            'shipping_options' => json_encode($shipping_options),
1170
            'error_message' => $errorMessage
1171
        ]);
1172
    }
1173
1174
    /**
1175
     * Use this method to respond to such pre-checkout queries. On success, True is returned.
1176
     * Note: The Bot API must receive an answer within 10 seconds after the pre-checkout query was sent.
1177
     *
1178
     * @param string $preCheckoutQueryId
1179
     * @param bool $ok
1180
     * @param null|string $errorMessage
1181
     *
1182
     * @return mixed
1183
     */
1184
    public function answerPreCheckoutQuery($preCheckoutQueryId, $ok = true, $errorMessage = null)
1185
    {
1186
        return $this->call('answerPreCheckoutQuery', [
1187
            'pre_checkout_query_id' => $preCheckoutQueryId,
1188
            'ok' => (bool)$ok,
1189
            'error_message' => $errorMessage
1190
        ]);
1191
    }
1192
1193
    /**
1194
     * Use this method to restrict a user in a supergroup.
1195
     * The bot must be an administrator in the supergroup for this to work and must have the appropriate admin rights.
1196
     * Pass True for all boolean parameters to lift restrictions from a user.
1197
     *
1198
     * @param string|int $chatId Unique identifier for the target chat or username of the target supergroup
1199
     *                   (in the format @supergroupusername)
1200
     * @param int $userId Unique identifier of the target user
1201
     * @param null|integer $untilDate Date when restrictions will be lifted for the user, unix time.
1202
     *                     If user is restricted for more than 366 days or less than 30 seconds from the current time,
1203
     *                     they are considered to be restricted forever
1204
     * @param bool $canSendMessages Pass True, if the user can send text messages, contacts, locations and venues
1205
     * @param bool $canSendMediaMessages No Pass True, if the user can send audios, documents, photos, videos,
1206
     *                                   video notes and voice notes, implies can_send_messages
1207
     * @param bool $canSendOtherMessages Pass True, if the user can send animations, games, stickers and
1208
     *                                   use inline bots, implies can_send_media_messages
1209
     * @param bool $canAddWebPagePreviews Pass True, if the user may add web page previews to their messages,
1210
     *                                    implies can_send_media_messages
1211
     *
1212
     * @return bool
1213
     */
1214
    public function restrictChatMember(
1215
        $chatId,
1216
        $userId,
1217
        $untilDate = null,
1218
        $canSendMessages = false,
1219
        $canSendMediaMessages = false,
1220
        $canSendOtherMessages = false,
1221
        $canAddWebPagePreviews = false
1222
    ) {
1223
        return $this->call('restrictChatMember', [
1224
            'chat_id' => $chatId,
1225
            'user_id' => $userId,
1226
            'until_date' => $untilDate,
1227
            'can_send_messages' => $canSendMessages,
1228
            'can_send_media_messages' => $canSendMediaMessages,
1229
            'can_send_other_messages' => $canSendOtherMessages,
1230
            'can_add_web_page_previews' => $canAddWebPagePreviews
1231
        ]);
1232
    }
1233
1234
    /**
1235
     * Use this method to promote or demote a user in a supergroup or a channel.
1236
     * The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
1237
     * Pass False for all boolean parameters to demote a user.
1238
     *
1239
     * @param string|int $chatId Unique identifier for the target chat or username of the target supergroup
1240
     *                   (in the format @supergroupusername)
1241
     * @param int $userId Unique identifier of the target user
1242
     * @param bool $canChangeInfo Pass True, if the administrator can change chat title, photo and other settings
1243
     * @param bool $canPostMessages Pass True, if the administrator can create channel posts, channels only
1244
     * @param bool $canEditMessages Pass True, if the administrator can edit messages of other users, channels only
1245
     * @param bool $canDeleteMessages Pass True, if the administrator can delete messages of other users
1246
     * @param bool $canInviteUsers Pass True, if the administrator can invite new users to the chat
1247
     * @param bool $canRestrictMembers Pass True, if the administrator can restrict, ban or unban chat members
1248
     * @param bool $canPinMessages Pass True, if the administrator can pin messages, supergroups only
1249
     * @param bool $canPromoteMembers Pass True, if the administrator can add new administrators with a subset of his
1250
     *                                own privileges or demote administrators that he has promoted,directly or
1251
     *                                indirectly (promoted by administrators that were appointed by him)
1252
     *
1253
     * @return bool
1254
     */
1255
    public function promoteChatMember(
1256
        $chatId,
1257
        $userId,
1258
        $canChangeInfo = true,
1259
        $canPostMessages = true,
1260
        $canEditMessages = true,
1261
        $canDeleteMessages = true,
1262
        $canInviteUsers = true,
1263
        $canRestrictMembers = true,
1264
        $canPinMessages = true,
1265
        $canPromoteMembers = true
1266
    ) {
1267
        return $this->call('promoteChatMember', [
1268
            'chat_id' => $chatId,
1269
            'user_id' => $userId,
1270
            'can_change_info' => $canChangeInfo,
1271
            'can_post_messages' => $canPostMessages,
1272
            'can_edit_messages' => $canEditMessages,
1273
            'can_delete_messages' => $canDeleteMessages,
1274
            'can_invite_users' => $canInviteUsers,
1275
            'can_restrict_members' => $canRestrictMembers,
1276
            'can_pin_messages' => $canPinMessages,
1277
            'can_promote_members' => $canPromoteMembers
1278
        ]);
1279
    }
1280
1281
    /**
1282
     * Use this method to export an invite link to a supergroup or a channel.
1283
     * The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
1284
     *
1285
     * @param string|int $chatId Unique identifier for the target chat or username of the target channel
1286
     *                           (in the format @channelusername)
1287
     * @return string
1288
     */
1289
    public function exportChatInviteLink($chatId)
1290
    {
1291
        return $this->call('exportChatInviteLink', [
1292
            'chat_id' => $chatId
1293
        ]);
1294
    }
1295
1296
    /**
1297
     * Use this method to set a new profile photo for the chat. Photos can't be changed for private chats.
1298
     * The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
1299
     *
1300
     * @param string|int $chatId Unique identifier for the target chat or username of the target channel
1301
     *                           (in the format @channelusername)
1302
     * @param \CURLFile|string $photo New chat photo, uploaded using multipart/form-data
1303
     *
1304
     * @return bool
1305
     */
1306
    public function setChatPhoto($chatId, $photo)
1307
    {
1308
        return $this->call('setChatPhoto', [
1309
            'chat_id' => $chatId,
1310
            'photo' => $photo
1311
        ]);
1312
    }
1313
1314
    /**
1315
     * Use this method to delete a chat photo. Photos can't be changed for private chats.
1316
     * The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
1317
     *
1318
     * @param string|int $chatId Unique identifier for the target chat or username of the target channel
1319
     *                           (in the format @channelusername)
1320
     *
1321
     * @return bool
1322
     */
1323
    public function deleteChatPhoto($chatId)
1324
    {
1325
        return $this->call('deleteChatPhoto', [
1326
            'chat_id' => $chatId
1327
        ]);
1328
    }
1329
1330
    /**
1331
     * Use this method to change the title of a chat. Titles can't be changed for private chats.
1332
     * The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
1333
     *
1334
     * @param string|int $chatId Unique identifier for the target chat or username of the target channel
1335
     *                           (in the format @channelusername)
1336
     * @param string $title New chat title, 1-255 characters
1337
     *
1338
     * @return bool
1339
     */
1340
    public function setChatTitle($chatId, $title)
1341
    {
1342
        return $this->call('setChatTitle', [
1343
            'chat_id' => $chatId,
1344
            'title' => $title
1345
        ]);
1346
    }
1347
1348
    /**
1349
     * Use this method to change the description of a supergroup or a channel.
1350
     * The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
1351
     *
1352
     * @param string|int $chatId Unique identifier for the target chat or username of the target channel
1353
     *                   (in the format @channelusername)
1354
     * @param string|null $description New chat description, 0-255 characters
1355
     *
1356
     * @return bool
1357
     */
1358
    public function setChatDescription($chatId, $description = null)
1359
    {
1360
        return $this->call('setChatDescription', [
1361
            'chat_id' => $chatId,
1362
            'title' => $description
1363
        ]);
1364
    }
1365
1366
    /**
1367
     * Use this method to pin a message in a supergroup.
1368
     * The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
1369
     *
1370
     * @param string|int $chatId Unique identifier for the target chat or username of the target channel
1371
     *                   (in the format @channelusername)
1372
     * @param int $messageId Identifier of a message to pin
1373
     * @param bool $disableNotification
1374
     *
1375
     * @return bool
1376
     */
1377
    public function pinChatMessage($chatId, $messageId, $disableNotification = false)
1378
    {
1379
        return $this->call('pinChatMessage', [
1380
            'chat_id' => $chatId,
1381
            'message_id' => $messageId,
1382
            'disable_notification' => $disableNotification
1383
        ]);
1384
    }
1385
1386
    /**
1387
     * Use this method to unpin a message in a supergroup chat.
1388
     * The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
1389
     *
1390
     * @param string|int $chatId Unique identifier for the target chat or username of the target channel
1391
     *                   (in the format @channelusername)
1392
     *
1393
     * @return bool
1394
     */
1395
    public function unpinChatMessage($chatId)
1396
    {
1397
        return $this->call('unpinChatMessage', [
1398
            'chat_id' => $chatId
1399
        ]);
1400
    }
1401
1402
    /**
1403
     * Use this method to get up to date information about the chat
1404
     * (current name of the user for one-on-one conversations, current username of a user, group or channel, etc.).
1405
     *
1406
     * @param string|int $chatId Unique identifier for the target chat or username of the target channel
1407
     *                   (in the format @channelusername)
1408
     *
1409
     * @return Chat
1410
     */
1411
    public function getChat($chatId)
1412
    {
1413
        return Chat::fromResponse($this->call('getChat', [
1414
            'chat_id' => $chatId
1415
        ]));
1416
    }
1417
1418
    /**
1419
     * Use this method to get information about a member of a chat.
1420
     *
1421
     * @param string|int $chatId Unique identifier for the target chat or username of the target channel
1422
     *                   (in the format @channelusername)
1423
     * @param int $userId
1424
     *
1425
     * @return ChatMember
1426
     */
1427
    public function getChatMember($chatId, $userId)
1428
    {
1429
        return ChatMember::fromResponse($this->call('getChatMember', [
1430
            'chat_id' => $chatId,
1431
            'user_id' => $userId
1432
        ]));
1433
    }
1434
1435
    /**
1436
     * Use this method for your bot to leave a group, supergroup or channel.
1437
     *
1438
     * @param string|int $chatId Unique identifier for the target chat or username of the target channel
1439
     *                   (in the format @channelusername)
1440
     *
1441
     * @return bool
1442
     */
1443
    public function leaveChat($chatId)
1444
    {
1445
        return $this->call('leaveChat', [
1446
            'chat_id' => $chatId
1447
        ]);
1448
    }
1449
}
1450