-
Notifications
You must be signed in to change notification settings - Fork 674
/
Copy pathApi.php
843 lines (747 loc) · 21.9 KB
/
Api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
<?php
namespace Telegram\Bot;
use Telegram\Bot\Objects\User;
use Telegram\Bot\Objects\Update;
use Telegram\Bot\Objects\Message;
use Telegram\Bot\Objects\UserProfilePhotos;
use Telegram\Bot\FileUpload\InputFile;
use Telegram\Bot\Commands\CommandBus;
use Telegram\Bot\Commands\CommandInterface;
use Telegram\Bot\HttpClients\GuzzleHttpClient;
use Telegram\Bot\Exceptions\TelegramSDKException;
use Telegram\Bot\HttpClients\HttpClientInterface;
/**
* Class Api
*
* @package Telegram\Bot
*/
class Api
{
/**
* @var string Version number of the Telegram Bot PHP SDK.
*/
const VERSION = '1.0.0';
/**
* @var string The name of the environment variable that contains the Telegram Bot API Access Token.
*/
const BOT_TOKEN_ENV_NAME = 'TELEGRAM_BOT_TOKEN';
/**
* @var TelegramClient The Telegram client service.
*/
protected $client;
/**
* @var string Telegram Bot API Access Token.
*/
protected $accessToken = null;
/**
* @var TelegramResponse|null Stores the last request made to Telegram Bot API.
*/
protected $lastResponse;
/**
* @var bool Indicates if the request to Telegram will be asynchronous (non-blocking).
*/
protected $isAsyncRequest = false;
/**
* @var CommandBus|null Telegram Command Bus.
*/
protected $commandBus = null;
/**
* Instantiates a new Telegram super-class object.
*
*
* @param string $token The Telegram Bot API Access Token.
* @param bool $async (Optional) Indicates if the request to Telegram
* will be asynchronous (non-blocking).
* @param string|HttpClientInterface $http_client_handler (Optional) Custom HTTP Client Handler.
*
* @throws TelegramSDKException
*/
public function __construct($token = null, $async = false, $http_client_handler = null)
{
$this->accessToken = isset($token) ? $token : getenv(static::BOT_TOKEN_ENV_NAME);
if (!$this->accessToken) {
throw new TelegramSDKException('Required "token" not supplied in config and could not find fallback environment variable "'.static::BOT_TOKEN_ENV_NAME.'"');
}
$httpClientHandler = null;
if (isset($http_client_handler)) {
if ($http_client_handler instanceof HttpClientInterface) {
$httpClientHandler = $http_client_handler;
} elseif ($http_client_handler === 'guzzle') {
$httpClientHandler = new GuzzleHttpClient();
} else {
throw new \InvalidArgumentException('The HTTP Client Handler must be set to "guzzle", or be an instance of Telegram\Bot\HttpClients\HttpClientInterface');
}
}
if (isset($async)) {
$this->setAsyncRequest($async);
}
$this->client = new TelegramClient($httpClientHandler);
}
/**
* Returns the TelegramClient service.
*
* @return TelegramClient
*/
public function getClient()
{
return $this->client;
}
/**
* Returns Telegram Bot API Access Token.
*
* @return string
*/
public function getAccessToken()
{
return $this->accessToken;
}
/**
* Returns the last response returned from API request.
*
* @return TelegramResponse
*/
public function getLastResponse()
{
return $this->lastResponse;
}
/**
* Sets the bot access token to use with API requests.
*
* @param string $accessToken The bot access token to save.
*
* @return Api
*
* @throws \InvalidArgumentException
*/
public function setAccessToken($accessToken)
{
if (is_string($accessToken)) {
$this->accessToken = $accessToken;
return $this;
}
throw new \InvalidArgumentException('The Telegram bot access token must be of type "string"');
}
/**
* Make this request asynchronous (non-blocking).
*
* @param bool $isAsyncRequest
*
* @return TelegramRequest
*/
public function setAsyncRequest($isAsyncRequest)
{
$this->isAsyncRequest = $isAsyncRequest;
return $this;
}
/**
* Check if this is an asynchronous request (non-blocking).
*
* @return bool
*/
public function isAsyncRequest()
{
return $this->isAsyncRequest;
}
/**
* Returns SDK's Command Bus.
*
* @return CommandBus
*/
public function getCommandBus()
{
if (is_null($this->commandBus)) {
return $this->commandBus = new CommandBus($this);
}
return $this->commandBus;
}
/**
* Add Telegram Command to the Command Bus.
*
* @param CommandInterface|string $command
*
* @return CommandBus
*/
public function addCommand($command)
{
return $this->getCommandBus()->addCommand($command);
}
/**
* Add Telegram Commands to the Command Bus.
*
* @param array $commands
*
* @return CommandBus
*/
public function addCommands(array $commands)
{
return $this->getCommandBus()->addCommands($commands);
}
/**
* Returns list of available commands.
*
* @return array
*/
public function getCommands()
{
return $this->getCommandBus()->getCommands();
}
/**
* A simple method for testing your bot's auth token.
* Returns basic information about the bot in form of a User object.
*
* @link https://core.telegram.org/bots/api#getme
*
* @return User
*/
public function getMe()
{
$response = $this->get('getMe');
return new User($response->getDecodedBody());
}
/**
* Send text messages.
*
* @link https://core.telegram.org/bots/api#sendmessage
*
* @param int $chat_id
* @param string $text
* @param bool $disable_web_page_preview
* @param int $reply_to_message_id
* @param string $reply_markup
*
* @return Message
*/
public function sendMessage(
$chat_id,
$text,
$disable_web_page_preview = false,
$reply_to_message_id = null,
$reply_markup = null
) {
$params = compact('chat_id', 'text', 'disable_web_page_preview', 'reply_to_message_id', 'reply_markup');
$response = $this->get('sendMessage', $params);
return new Message($response->getDecodedBody());
}
/**
* Forward messages of any kind.
*
* @link https://core.telegram.org/bots/api#forwardmessage
*
* @param int $chat_id
* @param int $from_chat_id
* @param int $message_id
*
* @return Message
*/
public function forwardMessage($chat_id, $from_chat_id, $message_id)
{
$params = compact('chat_id', 'from_chat_id', 'message_id');
$response = $this->get('forwardMessage', $params);
return new Message($response->getDecodedBody());
}
/**
* Send Photos.
*
* @link https://core.telegram.org/bots/api#sendphoto
*
* @param int $chat_id
* @param string $photo
* @param string $caption
* @param int $reply_to_message_id
* @param string $reply_markup
*
* @return Message
*/
public function sendPhoto($chat_id, $photo, $caption = null, $reply_to_message_id = null, $reply_markup = null)
{
$params = compact('chat_id', 'photo', 'caption', 'reply_to_message_id', 'reply_markup');
return $this->uploadFile('sendPhoto', $params);
}
/**
* Send regular audio files.
*
* @link https://core.telegram.org/bots/api#sendaudio
*
* @param int $chat_id
* @param string $audio
* @param int $duration
* @param string $performer
* @param string $title
* @param int $reply_to_message_id
* @param string $reply_markup
*
* @return Message
*/
public function sendAudio(
$chat_id,
$audio,
$duration = null,
$performer = null,
$title = null,
$reply_to_message_id = null,
$reply_markup = null
) {
$params = compact('chat_id', 'audio', 'duration', 'performer', 'title', 'reply_to_message_id', 'reply_markup');
return $this->uploadFile('sendAudio', $params);
}
/**
* Send voice audio files.
*
* @link https://core.telegram.org/bots/api#sendaudio
*
* @param int $chat_id
* @param string $voice
* @param int $duration
* @param int $reply_to_message_id
* @param string $reply_markup
*
* @return Message
*/
public function sendVoice($chat_id, $voice, $duration = null, $reply_to_message_id = null, $reply_markup = null)
{
$params = compact('chat_id', 'voice', 'duration', 'reply_to_message_id', 'reply_markup');
return $this->uploadFile('sendVoice', $params);
}
/**
* Send general files.
*
* @link https://core.telegram.org/bots/api#senddocument
*
* @param int $chat_id
* @param string $document
* @param int $reply_to_message_id
* @param string $reply_markup
*
* @return Message
*/
public function sendDocument($chat_id, $document, $reply_to_message_id = null, $reply_markup = null)
{
$params = compact('chat_id', 'document', 'reply_to_message_id', 'reply_markup');
return $this->uploadFile('sendDocument', $params);
}
/**
* Send .webp stickers.
*
* @link https://core.telegram.org/bots/api#sendsticker
*
* @param int $chat_id
* @param string $sticker
* @param int $reply_to_message_id
* @param string $reply_markup
*
* @return Message
*
* @throws TelegramSDKException
*/
public function sendSticker($chat_id, $sticker, $reply_to_message_id = null, $reply_markup = null)
{
if (is_file($sticker) && (pathinfo($sticker, PATHINFO_EXTENSION) !== 'webp')) {
throw new TelegramSDKException('Invalid Sticker Provided. Supported Format: Webp');
}
$params = compact('chat_id', 'sticker', 'reply_to_message_id', 'reply_markup');
return $this->uploadFile('sendSticker', $params);
}
/**
* Send Video File, Telegram clients support mp4 videos (other formats may be sent as Document).
*
* @see sendDocument
* @link https://core.telegram.org/bots/api#sendvideo
*
* @param int $chat_id
* @param string $video
* @param int $duration
* @param string $caption
* @param int $reply_to_message_id
* @param string $reply_markup
*
* @return Message
*/
public function sendVideo(
$chat_id,
$video,
$duration = null,
$caption = null,
$reply_to_message_id = null,
$reply_markup = null
) {
$params = compact('chat_id', 'video', 'duration', 'caption', 'reply_to_message_id', 'reply_markup');
return $this->uploadFile('sendVideo', $params);
}
/**
* Send point on the map.
*
* @link https://core.telegram.org/bots/api#sendlocation
*
* @param int $chat_id
* @param float $latitude
* @param float $longitude
* @param int $reply_to_message_id
* @param string $reply_markup
*
* @return Message
*/
public function sendLocation($chat_id, $latitude, $longitude, $reply_to_message_id = null, $reply_markup = null)
{
$params = compact('chat_id', 'latitude', 'longitude', 'reply_to_message_id', 'reply_markup');
$response = $this->get('sendLocation', $params);
return new Message($response->getDecodedBody());
}
/**
* Broadcast a Chat Action.
*
* @link https://core.telegram.org/bots/api#sendchataction
*
* @param int $chat_id
* @param string $action
*
* @return TelegramResponse
*
* @throws TelegramSDKException
*/
public function sendChatAction($chat_id, $action)
{
$validActions = [
'typing',
'upload_photo',
'record_video',
'upload_video',
'record_audio',
'upload_audio',
'upload_document',
'find_location',
];
if (isset($action) && in_array($action, $validActions)) {
return $this->get('sendChatAction', compact('chat_id', 'action'));
}
throw new TelegramSDKException('Invalid Action! Accepted value: '.implode(', ', $validActions));
}
/**
* Returns a list of profile pictures for a user.
*
* @link https://core.telegram.org/bots/api#getuserprofilephotos
*
* @param int $user_id
* @param int $offset
* @param int $limit
*
* @return UserProfilePhotos
*/
public function getUserProfilePhotos($user_id, $offset = null, $limit = null)
{
$response = $this->get('getUserProfilePhotos', compact('user_id', 'offset', 'limit'));
return new UserProfilePhotos($response->getDecodedBody());
}
/**
* Set a Webhook to receive incoming updates via an outgoing webhook.
*
* @link https://core.telegram.org/bots/api#setwebhook
*
* @param string $url HTTPS url to send updates to.
* @param string $certificate Upload your public key certificate so that the root certificate in use can be checked.
*
* @return TelegramResponse
*
* @throws TelegramSDKException
*/
public function setWebhook($url, $certificate = null)
{
if (filter_var($url, FILTER_VALIDATE_URL) === false) {
throw new TelegramSDKException('Invalid URL Provided');
}
if (parse_url($url, PHP_URL_SCHEME) !== 'https') {
throw new TelegramSDKException('Invalid URL, should be a HTTPS url.');
}
return $this->uploadFile('setWebhook', compact('url', 'certificate'));
}
/**
* Returns webhook updates sent by Telegram.
* Works only if you set a webhook.
*
* @see setWebhook
*
* @return Update
*/
public function getWebhookUpdates()
{
$body = json_decode(file_get_contents('php://input'), true);
return new Update($body);
}
/**
* Removes the outgoing webhook (if any).
*
* @return TelegramResponse
*/
public function removeWebhook()
{
$url = '';
return $this->get('setWebhook', compact('url'));
}
/**
* Use this method to receive incoming updates using long polling.
*
* @link https://core.telegram.org/bots/api#getupdates
*
* @param int|null $offset
* @param int|null $limit
* @param int|null $timeout
*
* @return Update[]
*/
public function getUpdates($offset = null, $limit = null, $timeout = null)
{
$response = $this->get('getUpdates', compact('offset', 'limit', 'timeout'));
$updates = $response->getDecodedBody();
$data = [];
foreach ($updates['result'] as $update) {
$data[] = new Update($update);
}
return $data;
}
/**
* Builds a custom keyboard markup.
*
* @link https://core.telegram.org/bots/api#replykeyboardmarkup
*
* @param array $keyboard
* @param bool $resize_keyboard
* @param bool $one_time_keyboard
* @param bool $selective
*
* @return string
*/
public function replyKeyboardMarkup(
$keyboard,
$resize_keyboard = false,
$one_time_keyboard = false,
$selective = false
) {
return json_encode(compact('keyboard', 'resize_keyboard', 'one_time_keyboard', 'selective'));
}
/**
* Hide the current custom keyboard and display the default letter-keyboard.
*
* @link https://core.telegram.org/bots/api#replykeyboardhide
*
* @param bool $selective
*
* @return string
*/
public static function replyKeyboardHide($selective = false)
{
$hide_keyboard = true;
return json_encode(compact('hide_keyboard', 'selective'));
}
/**
* Display a reply interface to the user (act as if the user has selected the bot‘s message and tapped ’Reply').
*
* @link https://core.telegram.org/bots/api#forcereply
*
* @param bool $selective
*
* @return string
*/
public static function forceReply($selective = false)
{
$force_reply = true;
return json_encode(compact('force_reply', 'selective'));
}
/**
* Processes Inbound Commands.
*
* @param bool $webhook
*
* @return Update|Objects\Update[]
*/
public function commandsHandler($webhook = false)
{
if ($webhook) {
$update = $this->getWebhookUpdates();
$this->processCommand($update);
return $update;
}
$updates = $this->getUpdates();
$highestId = -1;
foreach ($updates as $update) {
$highestId = $update->getUpdateId();
$this->processCommand($update);
}
//An update is considered confirmed as soon as getUpdates is called with an offset higher than its update_id.
if ($highestId != -1) {
$this->getUpdates($highestId + 1, 1);
}
return $updates;
}
/**
* Check update object for a command and process.
*
* @param $update
*/
protected function processCommand(Update $update)
{
$message = $update->getMessage();
if ($message->has('text')) {
$this->getCommandBus()->handler($message->getText(), $update);
}
}
/**
* Determine if a given type is the message.
*
* @param string $type
* @param Update $update
*
* @return bool
*/
public function isMessageType($type, Update $update)
{
if ($update->getMessage()->has(strtolower($type))) {
return true;
}
return $this->detectMessageType($update) === $type;
}
/**
* Detect Message Type Based on Update Object.
*
* @param Update $update
*
* @return string|null
*/
public function detectMessageType(Update $update)
{
$types = ['audio', 'document', 'photo', 'sticker', 'video', 'voice', 'contact', 'location', 'text'];
return $update->getMessage()
->keys()
->intersect($types)
->pop();
}
/**
* Sends a GET request to Telegram Bot API and returns the result.
*
* @param string $endpoint
* @param array $params
*
* @return TelegramResponse
*
* @throws TelegramSDKException
*/
protected function get($endpoint, $params = [])
{
return $this->sendRequest(
'GET',
$endpoint,
$params
);
}
/**
* Sends a POST request to Telegram Bot API and returns the result.
*
* @param string $endpoint
* @param array $params
*
* @return TelegramResponse
*
* @throws TelegramSDKException
*/
protected function post($endpoint, array $params = [])
{
return $this->sendRequest(
'POST',
$endpoint,
$params
);
}
/**
* Sends a multipart/form-data request to Telegram Bot API and returns the result.
* Used primarily for file uploads.
*
* @param string $endpoint
* @param array $params
*
* @return Message
*
* @throws TelegramSDKException
*/
protected function uploadFile($endpoint, array $params = [])
{
$i = 0;
$multipart_params = [];
foreach ($params as $name => $contents) {
if (is_null($contents)) {
continue;
}
if (!is_resource($contents)) {
$contents = is_file($contents) ? (new InputFile($contents))->open() : (string)$contents;
}
$multipart_params[$i]['name'] = $name;
$multipart_params[$i]['contents'] = $contents;
++$i;
}
$response = $this->post($endpoint, [
'multipart' => $multipart_params,
]);
return new Message($response->getDecodedBody());
}
/**
* Sends a request to Telegram Bot API and returns the result.
*
* @param string $method
* @param string $endpoint
* @param array $params
*
* @return TelegramResponse
*
* @throws TelegramSDKException
*/
protected function sendRequest(
$method,
$endpoint,
array $params = []
) {
$request = $this->request($method, $endpoint, $params);
return $this->lastResponse = $this->client->sendRequest($request);
}
/**
* Instantiates a new TelegramRequest entity.
*
* @param string $method
* @param string $endpoint
* @param array $params
*
* @return TelegramRequest
*/
protected function request(
$method,
$endpoint,
array $params = []
) {
return new TelegramRequest(
$this->getAccessToken(),
$method,
$endpoint,
$params,
$this->isAsyncRequest()
);
}
/**
* Magic method to process any "get" requests.
*
* @param $method
* @param $arguments
*
* @return bool|TelegramResponse
*/
public function __call($method, $arguments)
{
$action = substr($method, 0, 3);
if ($action === 'get') {
/** @noinspection PhpUndefinedFunctionInspection */
$class_name = studly_case(substr($method, 3));
$class = 'Telegram\Bot\Objects\\'.$class_name;
$response = $this->get($method, $arguments[0] ?: []);
if (class_exists($class)) {
return new $class($response->getDecodedBody());
}
return $response;
}
return false;
}
}