-
Notifications
You must be signed in to change notification settings - Fork 669
/
Copy pathTelegram.php
664 lines (590 loc) · 18.5 KB
/
Telegram.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
<?php
namespace Irazasyed\Telegram;
use Irazasyed\Telegram\Objects\User;
use Irazasyed\Telegram\Objects\Update;
use Irazasyed\Telegram\Objects\Message;
use Irazasyed\Telegram\Objects\UserProfilePhotos;
use Irazasyed\Telegram\FileUpload\InputFile;
use Irazasyed\Telegram\HttpClients\GuzzleHttpClient;
use Irazasyed\Telegram\Exceptions\TelegramSDKException;
use Irazasyed\Telegram\HttpClients\HttpClientInterface;
/**
* Class Telegram.
*/
class Telegram
{
/**
* @const string Version number of the Telegram Bot PHP SDK.
*/
const VERSION = '0.2.6';
/**
* @const 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;
/**
* Indicates if the request to Telegram will be asynchronous (non-blocking).
*
* @var bool
*/
protected $isAsyncRequest = false;
/**
* 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|TelegramHttpClientInterface $http_client_handler (Optional) Custom HTTP Client Handler.
*
* @throws \Irazasyed\Telegram\Exceptions\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" key 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 Irazasyed\Telegram\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 Telegram
*
* @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;
}
/**
* 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 \Irazasyed\Telegram\Objects\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 KeyboardMarkup $reply_markup
*
* @return \Irazasyed\Telegram\Objects\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 \Irazasyed\Telegram\Objects\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 KeyboardMarkup $reply_markup
*
* @return \Irazasyed\Telegram\Objects\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 audio files.
*
* @link https://core.telegram.org/bots/api#sendaudio
*
* @param int $chat_id
* @param string $audio
* @param int $duration
* @param int $reply_to_message_id
* @param KeyboardMarkup $reply_markup
*
* @return \Irazasyed\Telegram\Objects\Message
*/
public function sendAudio($chat_id, $audio, $duration = null, $reply_to_message_id = null, $reply_markup = null)
{
$params = compact('chat_id', 'audio', 'duration', 'reply_to_message_id', 'reply_markup');
return $this->uploadFile('sendAudio', $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 KeyboardMarkup $reply_markup
*
* @return \Irazasyed\Telegram\Objects\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 KeyboardMarkup $reply_markup
*
* @return \Irazasyed\Telegram\Objects\Message
*
* @throws \Irazasyed\Telegram\Exceptions\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 KeyboardMarkup $reply_markup
*
* @return \Irazasyed\Telegram\Objects\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 KeyboardMarkup $reply_markup
*
* @return \Irazasyed\Telegram\Objects\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 \Irazasyed\Telegram\TelegramResponse
*
* @throws \Irazasyed\Telegram\Exceptions\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 \Irazasyed\Telegram\Objects\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.
*
* @param string $url HTTPS url to send updates to.
*
* @return \Irazasyed\Telegram\TelegramResponse
*
* @throws \Irazasyed\Telegram\Exceptions\TelegramSDKException
*/
public function setWebhook($url)
{
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->get('setWebhook', compact('url'));
}
/**
* Returns webhook updates sent by Telegram.
* Works only if you set a webhook.
*
* @see setWebhook
*
* @return \Irazasyed\Telegram\Objects\Update
*/
public function getWebhookUpdates()
{
$body = json_decode(file_get_contents('php://input'), true);
return new Update($body);
}
/**
* Removes the outgoing webhook (if any).
*
* @return \Irazasyed\Telegram\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 $offset
* @param int $limit
* @param int $timeout
*
* @return \Irazasyed\Telegram\Objects\Update
*/
public function getUpdates($offset = null, $limit = null, $timeout = null)
{
$response = $this->get('getUpdates', compact('offset', 'limit', 'timeout'));
return new Update($response->getDecodedBody());
}
/**
* 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'));
}
/**
* Sends a GET request to Telegram Bot API and returns the result.
*
* @param string $endpoint
* @param array $params
*
* @return TelegramResponse
*
* @throws TelegramSDKException
*/
public 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
*/
public 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 \Irazasyed\Telegram\Objects\Message
*
* @throws \Irazasyed\Telegram\Exceptions\TelegramSDKException
*/
public function uploadFile($endpoint, array $params = [])
{
$i = 0;
$multipart_params = [];
foreach ($params as $name => $contents) {
if (is_null($contents)) {
continue;
}
if (is_file($contents)) {
$contents = (new InputFile($contents))->open();
} else {
$contents = (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
*/
public 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
*/
public 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|\Irazasyed\Telegram\TelegramResponse
*/
public function __call($method, $arguments)
{
$action = substr($method, 0, 3);
if ($action === 'get') {
$class_name = studly_case(substr($method, 3));
$class = 'Irazasyed\Telegram\Objects\\'.$class_name;
$response = $this->get($method, $arguments[0] ?: []);
if (class_exists($class)) {
return new $class($response->getDecodedBody());
}
return $response;
}
return false;
}
}