forked from Eleirbag89/TelegramBotPHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Telegram.php
927 lines (872 loc) · 37.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
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
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
<?php
/**
* Telegram Bot Class.
* @author Gabriele Grillo <gabry.grillo@alice.it>
*/
class Telegram {
private $bot_id = "";
private $data = array();
private $updates = array();
/// Class constructor
/**
* Create a Telegram instance from the bot token
* \param bot_id the bot token
* \return an instance of the class
*/
public function __construct($bot_id) {
$this->bot_id = $bot_id;
$this->data = $this->getData();
}
/// Do requests to Telegram Bot API
/**
* Contacts the various API's endpoints
* \param api the API endpoint
* \param $content the request parameters as array
* \param $post boolean tells if $content needs to be sends
* \return the JSON Telegram's reply
*/
public function endpoint($api, array $content, $post = true) {
$url = 'https://api.telegram.org/bot' . $this->bot_id . '/' . $api;
if ($post)
$reply = $this->sendAPIRequest($url, $content);
else
$reply = $this->sendAPIRequest($url, array(), false);
return json_decode($reply, true);
}
/// A method for testing your bot.
/**
* A simple method for testing your bot's auth token. Requires no parameters.
* Returns basic information about the bot in form of a User object.
* \return the JSON Telegram's reply
*/
public function getMe() {
return $this->endpoint("getMe", array(), false);
}
/// Send a message
/**
* Contacts the various API's endpoints<br/>Values inside $content:<br/>
* <table>
* <tr>
* <td><strong>Parameters</strong></td>
* <td><strong>Type</strong></td>
* <td><strong>Required</strong></td>
* <td><strong>Description</strong></td>
* </tr>
* <tr>
* <td>chat_id</td>
* <td>Integer</td>
* <td>Yes</td>
* <td>Unique identifier for the message recipient — User or GroupChat id</td>
* * </tr>
* <tr>
* <td>text</td>
* <td>String</td>
* <td>Yes</td>
* <td>Text of the message to be sent</td>
* </tr>
* <tr>
* <td>parse_mode</td>
* <td>String</td>
* <td>Optional</td>
* <td>Send <em>Markdown</em>, if you want Telegram apps to show bold, italic and inline URLs in your bot's message. For the moment, only Telegram for Android supports this.</td>
* </tr>
* <tr>
* <td>disable_web_page_preview</td>
* <td>Boolean</td>
* <td>Optional</td>
* <td>Disables link previews for links in this message</td>
* </tr>
* <tr>
* <td>reply_to_message_id</td>
* <td>Integer</td>
* <td>Optional</td>
* <td>If the message is a reply, ID of the original message</td>
* </tr>
* <tr>
* <td>reply_markup</td>
* <td>ReplyKeyboardMarkup or ReplyKeyboardHide or ForceReply</td>
* <td>Optional</td>
* <td>Additional interface options. A JSON-serialized object for a custom reply keyboard, instructions to hide keyboard or to force a reply from the user.</td>
* </tr>
* </table>
* \param $content the request parameters as array
* \return the JSON Telegram's reply
*/
public function sendMessage(array $content) {
return $this->endpoint("sendMessage", $content);
}
/// Answer a callback Query
/**
* Use this method to send answers to callback queries sent from inline keyboards. The answer will be displayed to the user as a notification at the top of the chat screen or as an alert. On success, <em>True</em> is returned.<br/>Values inside $content:<br/>
* <table>
* <tr>
* <td><strong>Parameters</strong></td>
* <td><strong>Type</strong></td>
* <td><strong>Required</strong></td>
* <td><strong>Description</strong></td>
* </tr>
* <tr>
* <td>callback_query_id</td>
* <td>String</td>
* <td>Yes</td>
* <td>Unique identifier for the query to be answered</td>
* </tr>
* <tr>
* <td>text</td>
* <td>String</td>
* <td>Optional</td>
* <td>Text of the notification. If not specified, nothing will be shown to the user</td>
* </tr>
* <tr>
* <td>show_alert</td>
* <td>Boolean</td>
* <td>Optional</td>
* <td>If <em>true</em>, an alert will be shown by the client instead of a notification at the top of the chat screen. Defaults to <em>false</em>.</td>
* </tr>
* </table>
* \param $content the request parameters as array
* \return the JSON Telegram's reply
*/
public function answerCallbackQuery(array $content) {
return $this->endpoint("answerCallbackQuery", $content);
}
/// Forward a message
/**
* Use this method to forward messages of any kind. On success, the sent Message is returned<br/>Values inside $content:<br/>
* <table>
* <tr>
* <td><strong>Parameters</strong></td>
* <td><strong>Type</strong></td>
* <td><strong>Required</strong></td>
* <td><strong>Description</strong></td>
* </tr>
* <tr>
* <td>chat_id</td>
* <td>Integer</td>
* <td>Yes</td>
* <td>Unique identifier for the message recipient — User or GroupChat id</td>
* </tr>
* <tr>
* <td>from_chat_id</td>
* <td>Integer</td>
* <td>Yes</td>
* <td>Unique identifier for the chat where the original message was sent — User or GroupChat id</td>
* </tr>
* <tr>
* <td>message_id</td>
* <td>Integer</td>
* <td>Yes</td>
* <td>Unique message identifier</td>
* </tr>
* </table>
* \param $content the request parameters as array
* \return the JSON Telegram's reply
*/
public function forwardMessage(array $content) {
return $this->endpoint("forwardMessage", $content);
}
/// Send a photo
/**
* Use this method to send photos. On success, the sent Message is returned.<br/>Values inside $content:<br/>
* <table>
* <tr>
* <td><strong>Parameters</strong></td>
* <td><strong>Type</strong></td>
* <td><strong>Required</strong></td>
* <td><strong>Description</strong></td>
* </tr>
* <tr>
* <td>chat_id</td>
* <td>Integer</td>
* <td>Yes</td>
* <td>Unique identifier for the message recipient — User or GroupChat id</td>
* </tr>
* <tr>
* <td>photo</td>
* <td><a href="#inputfile">InputFile</a> or String</td>
* <td>Yes</td>
* <td>Photo to send. You can either pass a <em>file_id</em> as String to resend a photo that is already on the Telegram servers, or upload a new photo using multipart/form-data.</td>
* </tr>
* <tr>
* <td>caption</td>
* <td>String</td>
* <td>Optional</td>
* <td>Photo caption (may also be used when resending photos by <em>file_id</em>).</td>
* </tr>
* <tr>
* <td>reply_to_message_id</td>
* <td>Integer</td>
* <td>Optional</td>
* <td>If the message is a reply, ID of the original message</td>
* </tr>
* <tr>
* <td>reply_markup</td>
* <td>ReplyKeyboardMarkup or >ReplyKeyboardHide or ForceReply</td>
* <td>Optional</td>
* <td>Additional interface options. A JSON-serialized object for a custom reply keyboard, instructions to hide keyboard or to force a reply from the user.</td>
* </tr>
* </table>
* \param $content the request parameters as array
* \return the JSON Telegram's reply
*/
public function sendPhoto(array $content) {
return $this->endpoint("sendPhoto", $content);
}
/// Send an audio
/**
* Use this method to send audio files, if you want Telegram clients to display them in the music player. Your audio must be in the .mp3 format. On success, the sent Message is returned. Bots can currently send audio files of up to 50 MB in size, this limit may be changed in the future.
* For backward compatibility, when the fields title and performer are both empty and the mime-type of the file to be sent is not audio/mpeg, the file will be sent as a playable voice message. For this to work, the audio must be in an .ogg file encoded with OPUS. This behavior will be phased out in the future. For sending voice messages, use the sendVoice method instead.<br/>Values inside $content:<br/>
* <table>
* <tr>
* <td><strong>Parameters</strong></td>
* <td><strong>Type</strong></td>
* <td><strong>Required</strong></td>
* <td><strong>Description</strong></td>
* </tr>
* <tr>
* <td>chat_id</td>
* <td>Integer</td>
* <td>Yes</td>
* <td>Unique identifier for the message recipient — User or GroupChat id</td>
* </tr>
* <tr>
* <td>audio</td>
* <td><a href="#inputfile">InputFile</a> or String</td>
* <td>Yes</td>
* <td>Audio file to send. You can either pass a <em>file_id</em> as String to resend an audio that is already on the Telegram servers, or upload a new audio file using <strong>multipart/form-data</strong>.</td>
* </tr>
* <tr>
* <td>duration</td>
* <td>Integer</td>
* <td>Optional</td>
* <td>Duration of the audio in seconds</td>
* </tr>
* <tr>
* <td>performer</td>
* <td>String</td>
* <td>Optional</td>
* <td>Performer</td>
* </tr>
* <tr>
* <td>title</td>
* <td>String</td>
* <td>Optional</td>
* <td>Track name</td>
* </tr>
* <tr>
* <td>reply_to_message_id</td>
* <td>Integer</td>
* <td>Optional</td>
* <td>If the message is a reply, ID of the original message</td>
* </tr>
* <tr>
* <td>reply_markup</td>
* <td>ReplyKeyboardMarkup or ReplyKeyboardHide or ForceReply</td>
* <td>Optional</td>
* <td>Additional interface options. A JSON-serialized object for a custom reply keyboard, instructions to hide keyboard or to force a reply from the user.</td>
* </tr>
* </table>
* \param $content the request parameters as array
* \return the JSON Telegram's reply
*/
public function sendAudio(array $content) {
return $this->endpoint("sendAudio", $content);
}
/// Send a document
/**
* Use this method to send general files. On success, the sent Message is returned. Bots can currently send files of any type of up to 50 MB in size, this limit may be changed in the future.<br/>Values inside $content:<br/>
* <table>
* <tr>
* <td><strong>Parameters</strong></td>
* <td><strong>Type</strong></td>
* <td><strong>Required</strong></td>
* <td><strong>Description</strong></td>
* </tr>
* <tr>
* <td>chat_id</td>
* <td>Integer</td>
* <td>Yes</td>
* <td>Unique identifier for the message recipient — User or GroupChat id</td>
* </tr>
* <tr>
* <td>document</td>
* <td>InputFile or String</td>
* <td>Yes</td>
* <td>File to send. You can either pass a <em>file_id</em> as String to resend a file that is already on the Telegram servers, or upload a new file using <strong>multipart/form-data</strong>.</td>
* </tr>
* <tr>
* <td>reply_to_message_id</td>
* <td>Integer</td>
* <td>Optional</td>
* <td>If the message is a reply, ID of the original message</td>
* </tr>
* <tr>
* <td>reply_markup</td>
* <td>ReplyKeyboardMarkup or ReplyKeyboardHide or ForceReply</td>
* <td>Optional</td>
* <td>Additional interface options. A JSON-serialized object for a custom reply keyboard, instructions to hide keyboard or to force a reply from the user.</td>
* </tr>
* </table>
* \param $content the request parameters as array
* \return the JSON Telegram's reply
*/
public function sendDocument(array $content) {
return $this->endpoint("sendDocument", $content);
}
/// Send a sticker
/**
* Use this method to send .webp stickers. On success, the sent Message is returned.<br/>Values inside $content:<br/>
* <table>
* <tr>
* <td><strong>Parameters</strong></td>
* <td><strong>Type</strong></td>
* <td><strong>Required</strong></td>
* <td><strong>Description</strong></td>
* </tr>
* <tr>
* <td>chat_id</td>
* <td>Integer</td>
* <td>Yes</td>
* <td>Unique identifier for the message recipient — User or GroupChat id</td>
* </tr>
* <tr>
* <td>sticker</td>
* <td><a href="#inputfile">InputFile</a> or String</td>
* <td>Yes</td>
* <td>Sticker to send. You can either pass a <em>file_id</em> as String to resend a sticker that is already on the Telegram servers, or upload a new sticker using <strong>multipart/form-data</strong>.</td>
* </tr>
* <tr>
* <td>reply_to_message_id</td>
* <td>Integer</td>
* <td>Optional</td>
* <td>If the message is a reply, ID of the original message</td>
* </tr>
* <tr>
* <td>reply_markup</td>
* <td>ReplyKeyboardMarkup or ReplyKeyboardHide or ForceReply</td>
* <td>Optional</td>
* <td>Additional interface options. A JSON-serialized object for a custom reply keyboard, instructions to hide keyboard or to force a reply from the user.</td>
* </tr>
* </table>
* \param $content the request parameters as array
* \return the JSON Telegram's reply
*/
public function sendSticker(array $content) {
return $this->endpoint("sendSticker", $content);
}
/// Send a video
/**
* Use this method to send video files, Telegram clients support mp4 videos (other formats may be sent as Document). On success, the sent Message is returned. Bots can currently send video files of up to 50 MB in size, this limit may be changed in the future.<br/>Values inside $content:<br/>
* <table>
* <tr>
* <td><strong>Parameters</strong></td>
* <td><strong>Type</strong></td>
* <td><strong>Required</strong></td>
* <td><strong>Description</strong></td>
* </tr>
* <tr>
* <td>chat_id</td>
* <td>Integer</td>
* <td>Yes</td>
* <td>Unique identifier for the message recipient — User or GroupChat id</td>
* </tr>
* <tr>
* <td>video</td>
* <td><a href="#inputfile">InputFile</a> or String</td>
* <td>Yes</td>
* <td>Video to send. You can either pass a <em>file_id</em> as String to resend a video that is already on the Telegram servers, or upload a new video file using <strong>multipart/form-data</strong>.</td>
* </tr>
* <tr>
* <td>duration</td>
* <td>Integer</td>
* <td>Optional</td>
* <td>Duration of sent video in seconds</td>
* </tr>
* <tr>
* <td>caption</td>
* <td>String</td>
* <td>Optional</td>
* <td>Video caption (may also be used when resending videos by <em>file_id</em>).</td>
* </tr>
* <tr>
* <td>reply_to_message_id</td>
* <td>Integer</td>
* <td>Optional</td>
* <td>If the message is a reply, ID of the original message</td>
* </tr>
* <tr>
* <td>reply_markup</td>
* <td>ReplyKeyboardMarkup or ReplyKeyboardHide or ForceReply</td>
* <td>Optional</td>
* <td>Additional interface options. A JSON-serialized object for a custom reply keyboard, instructions to hide keyboard or to force a reply from the user.</td>
* </tr>
* </table>
* \param $content the request parameters as array
* \return the JSON Telegram's reply
*/
public function sendVideo(array $content) {
return $this->endpoint("sendVideo", $content);
}
/// Send a voice message
/**
* Use this method to send audio files, if you want Telegram clients to display the file as a playable voice message. For this to work, your audio must be in an .ogg file encoded with OPUS (other formats may be sent as Audio or Document). On success, the sent Message is returned. Bots can currently send voice messages of up to 50 MB in size, this limit may be changed in the future.<br/>Values inside $content:<br/>
* <table>
* <tr>
* <td><strong>Parameters</strong></td>
* <td><strong>Type</strong></td>
* <td><strong>Required</strong></td>
* <td><strong>Description</strong></td>
* </tr>
* <tr>
* <td>chat_id</td>
* <td>Integer</td>
* <td>Yes</td>
* <td>Unique identifier for the message recipient — User or GroupChat id</td>
* </tr>
* <tr>
* <td>voice</td>
* <td><a href="#inputfile">InputFile</a> or String</td>
* <td>Yes</td>
* <td>Audio file to send. You can either pass a <em>file_id</em> as String to resend an audio that is already on the Telegram servers, or upload a new audio file using <strong>multipart/form-data</strong>.</td>
* </tr>
* <tr>
* <td>duration</td>
* <td>Integer</td>
* <td>Optional</td>
* <td>Duration of sent audio in seconds</td>
* </tr>
* <tr>
* <td>reply_to_message_id</td>
* <td>Integer</td>
* <td>Optional</td>
* <td>If the message is a reply, ID of the original message</td>
* </tr>
* <tr>
* <td>reply_markup</td>
* <td>ReplyKeyboardMarkup</a> or ReplyKeyboardHide or ForceReply</td>
* <td>Optional</td>
* <td>Additional interface options. A JSON-serialized object for a custom reply keyboard, instructions to hide keyboard or to force a reply from the user.</td>
* </tr>
* </table>
* \param $content the request parameters as array
* \return the JSON Telegram's reply
*/
public function sendVoice(array $content) {
return $this->endpoint("sendVoice", $content);
}
/// Send a location
/**
* Use this method to send point on the map. On success, the sent Message is returned.<br/>Values inside $content:<br/>
* <table>
* <tr>
* <td><strong>Parameters</strong></td>
* <td><strong>Type</strong></td>
* <td><strong>Required</strong></td>
* <td><strong>Description</strong></td>
* </tr>
* <tr>
* <td>chat_id</td>
* <td>Integer</td>
* <td>Yes</td>
* <td>Unique identifier for the message recipient — User or GroupChat id</td>
* </tr>
* <tr>
* <td>latitude</td>
* <td>Float number</td>
* <td>Yes</td>
* <td>Latitude of location</td>
* </tr>
* <tr>
* <td>longitude</td>
* <td>Float number</td>
* <td>Yes</td>
* <td>Longitude of location</td>
* </tr>
* <tr>
* <td>reply_to_message_id</td>
* <td>Integer</td>
* <td>Optional</td>
* <td>If the message is a reply, ID of the original message</td>
* </tr>
* <tr>
* <td>reply_markup</td>
* <td>ReplyKeyboardMarkup or ReplyKeyboardHide or ForceReply</td>
* <td>Optional</td>
* <td>Additional interface options. A JSON-serialized object for a custom reply keyboard, instructions to hide keyboard or to force a reply from the user.</td>
* </tr>
* </table>
* \param $content the request parameters as array
* \return the JSON Telegram's reply
*/
public function sendLocation(array $content) {
return $this->endpoint("sendLocation", $content);
}
/// Send a chat action
/**
* Use this method when you need to tell the user that something is happening on the bot's side. The status is set for 5 seconds or less (when a message arrives from your bot, Telegram clients clear its typing status).
* Example: The ImageBot needs some time to process a request and upload the image. Instead of sending a text message along the lines of “Retrieving image, please wait…”, the bot may use sendChatAction with action = upload_photo. The user will see a “sending photo” status for the bot.
* We only recommend using this method when a response from the bot will take a noticeable amount of time to arrive.<br/>Values inside $content:<br/>
* <table>
* <tr>
* <td><strong>Parameters</strong></td>
* <td><strong>Type</strong></td>
* <td><strong>Required</strong></td>
* <td><strong>Description</strong></td>
* </tr>
* <tr>
* <td>chat_id</td>
* <td>Integer</td>
* <td>Yes</td>
* <td>Unique identifier for the message recipient — User or GroupChat id</td>
* </tr>
* <tr>
* <td>action</td>
* <td>String</td>
* <td>Yes</td>
* <td>Type of action to broadcast. Choose one, depending on what the user is about to receive: <em>typing</em> for text messages, <em>upload_photo</em> for photos, <em>record_video</em> or <em>upload_video</em> for videos, <em>record_audio</em> or <em>upload_audio</em> for audio files, <em>upload_document</em> for general files, <em>find_location</em> for location data.</td>
* </tr>
* </table>
* \param $content the request parameters as array
* \return the JSON Telegram's reply
*/
public function sendChatAction(array $content) {
return $this->endpoint("sendChatAction", $content);
}
/// Get a list of profile pictures for a user
/**
* Use this method to get a list of profile pictures for a user. Returns a UserProfilePhotos object.<br/>Values inside $content:<br/>
* <table>
* <tr>
* <td><strong>Parameters</strong></td>
* <td><strong>Type</strong></td>
* <td><strong>Required</strong></td>
* <td><strong>Description</strong></td>
* </tr>
* <tr>
* <td>user_id</td>
* <td>Integer</td>
* <td>Yes</td>
* <td>Unique identifier of the target user</td>
* </tr>
* <tr>
* <td>offset</td>
* <td>Integer</td>
* <td>Optional</td>
* <td>Sequential number of the first photo to be returned. By default, all photos are returned.</td>
* </tr>
* <tr>
* <td>limit</td>
* <td>Integer</td>
* <td>Optional</td>
* <td>Limits the number of photos to be retrieved. Values between 1—100 are accepted. Defaults to 100.</td>
* </tr>
* </table>
* \param $content the request parameters as array
* \return the JSON Telegram's reply
*/
public function getUserProfilePhotos(array $content) {
return $this->endpoint("getUserProfilePhotos", $content);
}
/// Use this method to get basic info about a file and prepare it for downloading
/**
* Use this method to get basic info about a file and prepare it for downloading. For the moment, bots can download files of up to 20MB in size. On success, a File object is returned. The file can then be downloaded via the link https://api.telegram.org/file/bot<token>/<file_path>, where <file_path> is taken from the response. It is guaranteed that the link will be valid for at least 1 hour. When the link expires, a new one can be requested by calling getFile again.
* \param $file_id String File identifier to get info about
* \return the JSON Telegram's reply
*/
public function getFile($file_id) {
$content = array('file_id' => $file_id);
return $this->endpoint("getFile", $content);
}
/// Use this method to download a file
/**
* Use this method to to download a file from the Telegram servers.
* \param $telegram_file_path String File path on Telegram servers
* \param $local_file_path String File path where save the file
*/
public function downloadFile($telegram_file_path, $local_file_path) {
$file_url = "https://api.telegram.org/file/bot" . $this->bot_id . "/" . $telegram_file_path;
$in = fopen($file_url, "rb");
$out = fopen($local_file_path, "wb");
while ($chunk = fread($in, 8192)) {
fwrite($out, $chunk, 8192);
}
fclose($in);
fclose($out);
}
/// Set a WebHook for the bot
/**
* Use this method to specify a url and receive incoming updates via an outgoing webhook. Whenever there is an update for the bot, we will send an HTTPS POST request to the specified url, containing a JSON-serialized Update. In case of an unsuccessful request, we will give up after a reasonable amount of attempts.
* If you'd like to make sure that the Webhook request comes from Telegram, we recommend using a secret path in the URL, e.g. https://www.example.com/<token>. Since nobody else knows your bot‘s token, you can be pretty sure it’s us.
* \param $url String HTTPS url to send updates to. Use an empty string to remove webhook integration
* \param $certificate InputFile Upload your public key certificate so that the root certificate in use can be checked
* \return the JSON Telegram's reply
*/
public function setWebhook($url, $certificate = "") {
if ($certificate == "") {
$content = array('url' => $url);
} else {
$content = array('url' => $url, 'certificate' => $certificate);
}
return $this->endpoint("setWebhook", $content);
}
/// Get the data of the current message
/** Get the POST request of a user in a Webhook or the message actually processed in a getUpdates() enviroment.
* \return the JSON users's message
*/
public function getData() {
if (empty($this->data)) {
$rawData = file_get_contents("php://input");
return json_decode($rawData, true);
} else {
return $this->data;
}
}
/// Set the data currently used
public function setData(array $data) {
$this->data = $data;
}
/// Get the text of the current message
/**
* \return the String users's text
*/
public function Text() {
return $this->data["message"] ["text"];
}
/// Get the chat_id of the current message
/**
* \return the String users's chat_id
*/
public function ChatID() {
return $this->data["message"]["chat"]["id"];
}
/// Get the callback_query of the current update
/**
* \return the String callback_query
*/
public function Callback_Query() {
return $this->data["callback_query"];
}
/// Get the callback_query id of the current update
/**
* \return the String callback_query id
*/
public function Callback_ID() {
return $this->data["callback_query"]["id"];
}
/// Get the Get the data of the current callback
/**
* \return the String callback_data
*/
public function Callback_Data() {
return $this->data["callback_query"]["data"];
}
/// Get the Get the message of the current callback
/**
* \return the Message
*/
public function Callback_Message() {
return $this->data["callback_query"]["message"];
}
/// Get the Get the chati_id of the current callback
/**
* \return the String callback_query
*/
public function Callback_ChatID() {
return $this->data["callback_query"]["message"]["chat"]["id"];
}
/// Get the date of the current message
/**
* \return the String message's date
*/
public function Date() {
return $this->data["message"]["date"];
}
/// Get the first name of the user
public function FirstName() {
return $this->data["message"]["from"]["first_name"];
}
/// Get the last name of the user
public function LastName() {
return $this->data["message"]["from"]["last_name"];
}
/// Get the username of the user
public function Username() {
return $this->data["message"]["from"]["username"];
}
/// Get the location in the message
public function Location() {
return $this->data["message"]["location"];
}
/// Get the update_id of the message
public function UpdateID() {
return $this->data["update_id"];
}
/// Get the number of updates
public function UpdateCount() {
return count($this->updates["result"]);
}
/// Tell if a message is from a group or user chat
/**
*
* \return BOOLEAN true if the message is from a Group chat, false otherwise
*/
public function messageFromGroup() {
if ($this->data["message"]["chat"]["title"] == "") {
return false;
}
return true;
}
/// Set a custom keyboard
/** This object represents a custom keyboard with reply options
* \param $options Array of Array of String; Array of button rows, each represented by an Array of Strings
* \param $onetime Boolean Requests clients to hide the keyboard as soon as it's been used. Defaults to false.
* \param $resize Boolean Requests clients to resize the keyboard vertically for optimal fit (e.g., make the keyboard smaller if there are just two rows of buttons). Defaults to false, in which case the custom keyboard is always of the same height as the app's standard keyboard.
* \param $selective Boolean Use this parameter if you want to show the keyboard to specific users only. Targets: 1) users that are @mentioned in the text of the Message object; 2) if the bot's message is a reply (has reply_to_message_id), sender of the original message.
* \return the requested keyboard as Json
*/
public function buildKeyBoard(array $options, $onetime = false, $resize = false, $selective = true) {
$replyMarkup = array(
'keyboard' => $options,
'one_time_keyboard' => $onetime,
'resize_keyboard' => $resize,
'selective' => $selective
);
$encodedMarkup = json_encode($replyMarkup, true);
return $encodedMarkup;
}
/// Set an InlineKeyBoard
/** This object represents an inline keyboard that appears right next to the message it belongs to.
* \param $options Array of Array of InlineKeyboardButton; Array of button rows, each represented by an Array of InlineKeyboardButton
* \return the requested keyboard as Json
*/
public function buildInlineKeyBoard(array $options) {
$replyMarkup = array(
'inline_keyboard' => $options,
);
$encodedMarkup = json_encode($replyMarkup, true);
return $encodedMarkup;
}
/// Create an InlineKeyboardButton
/** This object represents one button of an inline keyboard. You must use exactly one of the optional fields.
* \param $text String; Array of button rows, each represented by an Array of Strings
* \param $url String Optional. HTTP url to be opened when button is pressed
* \param $callback_data String Optional. Data to be sent in a callback query to the bot when button is pressed
* \param $switch_inline_query String Optional. If set, pressing the button will prompt the user to select one of their chats, open that chat and insert the bot‘s username and the specified inline query in the input field. Can be empty, in which case just the bot’s username will be inserted.
* \return the requested button as Array
*/
public function buildInlineKeyboardButton($text, $url = "", $callback_data = "", $switch_inline_query = "") {
$replyMarkup = array(
'text' => $text
);
if ($url != "") {
$replyMarkup['url'] = $url;
} else if ($callback_data != "") {
$replyMarkup['callback_data'] = $callback_data;
} else if ($switch_inline_query != "") {
$replyMarkup['switch_inline_query'] = $switch_inline_query;
}
return $replyMarkup;
}
/// Create a KeyboardButton
/** This object represents one button of an inline keyboard. You must use exactly one of the optional fields.
* \param $text String; Array of button rows, each represented by an Array of Strings
* \param $request_contact Boolean Optional. If True, the user's phone number will be sent as a contact when the button is pressed. Available in private chats only
* \param $request_location Boolean Optional. If True, the user's current location will be sent when the button is pressed. Available in private chats only
* \return the requested button as Array
*/
public function buildKeyboardButton($text, $request_contact = false, $request_location = false) {
$replyMarkup = array(
'text' => $text,
'request_contact' => $request_contact,
'request_location' => $request_location
);
if ($url != "") {
$replyMarkup['url'] = $url;
} else if ($callback_data != "") {
$replyMarkup['callback_data'] = $callback_data;
} else if ($switch_inline_query != "") {
$replyMarkup['switch_inline_query'] = $switch_inline_query;
}
return $replyMarkup;
}
/// Hide a custom keyboard
/** Upon receiving a message with this object, Telegram clients will hide the current custom keyboard and display the default letter-keyboard. By default, custom keyboards are displayed until a new keyboard is sent by a bot. An exception is made for one-time keyboards that are hidden immediately after the user presses a button.
* \param $selective Boolean Use this parameter if you want to show the keyboard to specific users only. Targets: 1) users that are @mentioned in the text of the Message object; 2) if the bot's message is a reply (has reply_to_message_id), sender of the original message.
* \return the requested keyboard hide as Array
*/
public function buildKeyBoardHide($selective = true) {
$replyMarkup = array(
'hide_keyboard' => true,
'selective' => $selective
);
$encodedMarkup = json_encode($replyMarkup, true);
return $encodedMarkup;
}
/// Display a reply interface to the user
/* * Upon receiving a message with this object, Telegram clients will display a reply interface to the user (act as if the user has selected the bot‘s message and tapped ’Reply'). This can be extremely useful if you want to create user-friendly step-by-step interfaces without having to sacrifice privacy mode.
* \param $selective Boolean Use this parameter if you want to show the keyboard to specific users only. Targets: 1) users that are @mentioned in the text of the Message object; 2) if the bot's message is a reply (has reply_to_message_id), sender of the original message.
* \return the requested force reply as Array
*/
public function buildForceReply($selective = true) {
$replyMarkup = array(
'force_reply' => true,
'selective' => $selective
);
$encodedMarkup = json_encode($replyMarkup, true);
return $encodedMarkup;
}
/// Receive incoming messages using polling
/** Use this method to receive incoming updates using long polling.
* \param $offset Integer Identifier of the first update to be returned. Must be greater by one than the highest among the identifiers of previously received updates. By default, updates starting with the earliest unconfirmed update are returned. An update is considered confirmed as soon as getUpdates is called with an offset higher than its update_id.
* \param $limit Integer Limits the number of updates to be retrieved. Values between 1—100 are accepted. Defaults to 100
* \param $timeout Integer Timeout in seconds for long polling. Defaults to 0, i.e. usual short polling
* \param $update Boolean If true updates the pending message list to the last update received. Default to true.
* \return the updates as Array
*/
public function getUpdates($offset = 0, $limit = 100, $timeout = 0, $update = true) {
$content = array('offset' => $offset, 'limit' => $limit, 'timeout' => $timeout);
$this->updates = $this->endpoint("getUpdates", $content);
if ($update) {
if(count($this->updates["result"]) >= 1) { //for CLI working.
$last_element_id = $this->updates["result"][count($this->updates["result"]) - 1]["update_id"] + 1;
$content = array('offset' => $last_element_id, 'limit' => "1", 'timeout' => $timeout);
$this->endpoint("getUpdates", $content);
}
}
return $this->updates;
}
/// Serve an update
/** Use this method to use the bultin function like Text() or Username() on a specific update.
* \param $update Integer The index of the update in the updates array.
*/
public function serveUpdate($update) {
$this->data = $this->updates["result"][$update];
}
private function sendAPIRequest($url, array $content, $post = true) {
if (isset($content['chat_id'])) {
$url = $url . "?chat_id=" . $content['chat_id'];
unset($content['chat_id']);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if ($post) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
}
// Helper for Uploading file using CURL
if (!function_exists('curl_file_create')) {
function curl_file_create($filename, $mimetype = '', $postname = '') {
return "@$filename;filename="
. ($postname ? : basename($filename))
. ($mimetype ? ";type=$mimetype" : '');
}
}
?>