forked from wrxck/telegram-bot-lua
-
Notifications
You must be signed in to change notification settings - Fork 1
/
telegram-bot.lua
1695 lines (1528 loc) · 53.1 KB
/
telegram-bot.lua
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
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[
_ _ _ _ _
| | | | | | | | | |
| |_ ___| | ___ __ _ _ __ __ _ _ __ ___ ______| |__ ___ | |_ ______| |_ _ __ _
| __/ _ \ |/ _ \/ _` | '__/ _` | '_ ` _ \______| '_ \ / _ \| __|______| | | | |/ _` |
| || __/ | __/ (_| | | | (_| | | | | | | | |_) | (_) | |_ | | |_| | (_| |
\__\___|_|\___|\__, |_| \__,_|_| |_| |_| |_.__/ \___/ \__| |_|\__,_|\__,_|
__/ |
|___/
Version 1.9-0
Copyright (c) 2017 Matthew Hesketh
See LICENSE for details
]]
local api = {}
local log = require('log')
local json = require('json')
local fiber = require('fiber')
local errors = require('errors')
local https = require('http.client').new()
local multipart = require('telegram-bot.multipart')
function api.configure(token)
if not token or type(token) ~= 'string' then
return false, 'Please specify your bot API token you received from @BotFather!'
end
api.token = token
local info, err = api.get_me()
if not info then
return false, err
end
api.info = info.result
log.info('Telegram bot authorized: @%s', api.info.username)
return api
end
function api.request(endpoint, parameters, file, timeout)
assert(endpoint, 'You must specify an endpoint to make this request to!')
parameters = setmetatable(parameters or {}, {__tostring = json.encode})
for k, v in pairs(parameters) do
parameters[k] = tostring(v)
end
log.verbose('%s: %s', endpoint, parameters)
if file and next(file) ~= nil then
local file_type, file_name = next(file)
local file_res = io.open(file_name, 'r')
if file_res then
parameters[file_type] = {
filename = file_name,
data = file_res:read('*a')
}
file_res:close()
else
parameters[file_type] = file_name
end
end
parameters = next(parameters) == nil and { '' } or parameters
local body, boundary = multipart.encode(parameters)
local resp = https:post(
'https://api.telegram.org/bot' .. api.token .. endpoint,
body,
{
['headers'] = {
['Content-Type'] = 'multipart/form-data; boundary=' .. boundary,
},
verify_peer = true,
timeout = timeout,
keepalive_idle = 30,
keepalive_interval = 30,
}
)
local ok, jdat = pcall(json.decode, resp.body)
if not ok then
return false, string.format(
'Request %s failed [%s]: %s',
endpoint, resp.status, resp.reason or 'No reason'
)
end
if not jdat.ok then
log.error('%s failed: %s', endpoint, jdat.description)
return false, string.format(
'Request %s failed [%s]: %s',
endpoint, jdat.error_code, jdat.description
)
end
return jdat
end
function api.get_me()
return api.request('/getMe', nil, nil, 30)
end
function api.get_updates(timeout, offset, limit, allowed_updates) -- https://core.telegram.org/bots/api#getupdates
allowed_updates = type(allowed_updates) == 'table' and json.encode(allowed_updates) or allowed_updates
return api.request(
'/getUpdates',
{
['timeout'] = timeout,
['offset'] = offset,
['limit'] = limit,
['allowed_updates'] = allowed_updates
}
)
end
function api.send_message(message, text, parse_mode, disable_web_page_preview, disable_notification, reply_to_message_id, reply_markup) -- https://core.telegram.org/bots/api#sendmessage
reply_markup = type(reply_markup) == 'table' and json.encode(reply_markup) or reply_markup
message = (type(message) == 'table' and message.chat and message.chat.id) and message.chat.id or message
parse_mode = (type(parse_mode) == 'boolean' and parse_mode == true) and 'markdown' or parse_mode
return api.request(
'/sendMessage',
{
['chat_id'] = message,
['text'] = text,
['parse_mode'] = parse_mode,
['disable_web_page_preview'] = disable_web_page_preview,
['disable_notification'] = disable_notification,
['reply_to_message_id'] = reply_to_message_id,
['reply_markup'] = reply_markup
}
)
end
function api.send_reply(message, text, parse_mode, disable_web_page_preview, reply_markup, disable_notification) -- A variant of api.send_message(), optimised for sending a message as a reply.
if type(message) ~= 'table' or not message.chat or not message.chat.id or not message.message_id then
return false
end
reply_markup = type(reply_markup) == 'table' and json.encode(reply_markup) or reply_markup
parse_mode = (type(parse_mode) == 'boolean' and parse_mode == true) and 'markdown' or parse_mode
return api.request(
'/sendMessage',
{
['chat_id'] = message.chat.id,
['text'] = text,
['parse_mode'] = parse_mode,
['disable_web_page_preview'] = disable_web_page_preview,
['disable_notification'] = disable_notification,
['reply_to_message_id'] = message.message_id,
['reply_markup'] = reply_markup
}
)
end
function api.forward_message(chat_id, from_chat_id, disable_notification, message_id) -- https://core.telegram.org/bots/api#forwardmessage
return api.request(
'/forwardMessage',
{
['chat_id'] = chat_id,
['from_chat_id'] = from_chat_id,
['disable_notification'] = disable_notification,
['message_id'] = message_id
}
)
end
function api.send_photo(chat_id, photo, caption, disable_notification, reply_to_message_id, reply_markup) -- https://core.telegram.org/bots/api#sendphoto
reply_markup = type(reply_markup) == 'table' and json.encode(reply_markup) or reply_markup
return api.request(
'/sendPhoto',
{
['chat_id'] = chat_id,
['caption'] = caption,
['disable_notification'] = disable_notification,
['reply_to_message_id'] = reply_to_message_id,
['reply_markup'] = reply_markup
},
{
['photo'] = photo
}
)
end
function api.send_audio(chat_id, audio, caption, duration, performer, title, disable_notification, reply_to_message_id, reply_markup) -- https://core.telegram.org/bots/api#sendaudio
reply_markup = type(reply_markup) == 'table' and json.encode(reply_markup) or reply_markup
return api.request(
'/sendAudio',
{
['chat_id'] = chat_id,
['caption'] = caption,
['duration'] = duration,
['performer'] = performer,
['title'] = title,
['disable_notification'] = disable_notification,
['reply_to_message_id'] = reply_to_message_id,
['reply_markup'] = reply_markup
},
{ ['audio'] = audio }
)
end
function api.send_document(chat_id, document, caption, disable_notification, reply_to_message_id, reply_markup) -- https://core.telegram.org/bots/api#senddocument
reply_markup = type(reply_markup) == 'table' and json.encode(reply_markup) or reply_markup
return api.request(
'/sendDocument',
{
['chat_id'] = chat_id,
['caption'] = caption,
['disable_notification'] = disable_notification,
['reply_to_message_id'] = reply_to_message_id,
['reply_markup'] = reply_markup
},
{ ['document'] = document }
)
end
function api.send_sticker(chat_id, sticker, disable_notification, reply_to_message_id, reply_markup) -- https://core.telegram.org/bots/api#sendsticker
reply_markup = type(reply_markup) == 'table' and json.encode(reply_markup) or reply_markup
return api.request(
'/sendSticker',
{
['chat_id'] = chat_id,
['disable_notification'] = disable_notification,
['reply_to_message_id'] = reply_to_message_id,
['reply_markup'] = reply_markup
},
{ ['sticker'] = sticker }
)
end
function api.send_video(chat_id, video, duration, width, height, caption, supports_streaming, disable_notification, reply_to_message_id, reply_markup) -- https://core.telegram.org/bots/api#sendvideo
reply_markup = type(reply_markup) == 'table' and json.encode(reply_markup) or reply_markup
return api.request(
'/sendVideo',
{
['chat_id'] = chat_id,
['duration'] = duration,
['width'] = width,
['height'] = height,
['caption'] = caption,
['supports_streaming'] = supports_streaming,
['disable_notification'] = disable_notification,
['reply_to_message_id'] = reply_to_message_id,
['reply_markup'] = reply_markup
},
{ ['video'] = video }
)
end
function api.send_animation(chat_id, animation, duration, width, height, thumb, caption, parse_mode, disable_notification, reply_to_message_id, reply_markup) -- https://core.telegram.org/bots/api#sendanimation
reply_markup = type(reply_markup) == 'table' and json.encode(reply_markup) or reply_markup
return api.request(
'/sendAnimation',
{
['chat_id'] = chat_id,
['duration'] = duration,
['width'] = width,
['height'] = height,
['caption'] = caption,
['parse_mode'] = parse_mode,
['disable_notification'] = disable_notification,
['reply_to_message_id'] = reply_to_message_id,
['reply_markup'] = reply_markup
}, {
['animation'] = animation,
['thumb'] = thumb
}
)
end
function api.send_voice(chat_id, voice, caption, duration, disable_notification, reply_to_message_id, reply_markup) -- https://core.telegram.org/bots/api#sendvoice
reply_markup = type(reply_markup) == 'table' and json.encode(reply_markup) or reply_markup
return api.request(
'/sendVoice',
{
['chat_id'] = chat_id,
['caption'] = caption,
['duration'] = duration,
['disable_notification'] = disable_notification,
['reply_to_message_id'] = reply_to_message_id,
['reply_markup'] = reply_markup
},
{ ['voice'] = voice }
)
end
function api.send_video_note(chat_id, video_note, duration, length, disable_notification, reply_to_message_id, reply_markup) -- https://core.telegram.org/bots/api#sendvideonote
reply_markup = type(reply_markup) == 'table' and json.encode(reply_markup) or reply_markup
return api.request(
'/sendVideoNote',
{
['chat_id'] = chat_id,
['duration'] = duration,
['length'] = length,
['disable_notification'] = disable_notification,
['reply_to_message_id'] = reply_to_message_id,
['reply_markup'] = reply_markup
},
{ ['video_note'] = video_note }
)
end
function api.send_media_group(chat_id, media, disable_notification, reply_to_message_id) -- https://core.telegram.org/bots/api#sendmediagroup
return api.request(
'/sendMediaGroup',
{
['chat_id'] = chat_id,
['media'] = media,
['disable_notification'] = disable_notification,
['reply_to_message_id'] = reply_to_message_id
}
)
end
function api.send_location(chat_id, latitude, longitude, live_period, disable_notification, reply_to_message_id, reply_markup) -- https://core.telegram.org/bots/api#sendlocation
reply_markup = type(reply_markup) == 'table' and json.encode(reply_markup) or reply_markup
return api.request(
'/sendLocation',
{
['chat_id'] = chat_id,
['latitude'] = latitude,
['longitude'] = longitude,
['live_period'] = live_period,
['disable_notification'] = disable_notification,
['reply_to_message_id'] = reply_to_message_id,
['reply_markup'] = reply_markup
}
)
end
function api.edit_message_live_location(chat_id, message_id, inline_message_id, latitude, longitude, reply_markup) -- https://core.telegram.org/bots/api#editmessagelivelocation
reply_markup = type(reply_markup) == 'table' and json.encode(reply_markup) or reply_markup
return api.request(
'/editMessageLiveLocation',
{
['chat_id'] = chat_id,
['message_id'] = message_id,
['inline_message_id'] = inline_message_id,
['latitude'] = latitude,
['longitude'] = longitude,
['reply_markup'] = reply_markup
}
)
end
function api.stop_message_live_location(chat_id, message_id, inline_message_id, reply_markup) -- https://core.telegram.org/bots/api#stopmessagelivelocation
reply_markup = type(reply_markup) == 'table' and json.encode(reply_markup) or reply_markup
return api.request(
'/stopMessageLiveLocation',
{
['chat_id'] = chat_id,
['message_id'] = message_id,
['inline_message_id'] = inline_message_id,
['reply_markup'] = reply_markup
}
)
end
function api.send_venue(chat_id, latitude, longitude, title, address, foursquare_id, foursquare_type, disable_notification, reply_to_message_id, reply_markup) -- https://core.telegram.org/bots/api#sendvenue
reply_markup = type(reply_markup) == 'table' and json.encode(reply_markup) or reply_markup
return api.request(
'/sendVenue',
{
['chat_id'] = chat_id,
['latitude'] = latitude,
['longitude'] = longitude,
['title'] = title,
['address'] = address,
['foursquare_id'] = foursquare_id,
['foursquare_type'] = foursquare_type,
['disable_notification'] = disable_notification,
['reply_to_message_id'] = reply_to_message_id,
['reply_markup'] = reply_markup
}
)
end
function api.send_contact(chat_id, phone_number, first_name, last_name, disable_notification, reply_to_message_id, reply_markup) -- https://core.telegram.org/bots/api#sendcontact
reply_markup = type(reply_markup) == 'table' and json.encode(reply_markup) or reply_markup
return api.request(
'/sendContact',
{
['chat_id'] = chat_id,
['phone_number'] = phone_number,
['first_name'] = first_name,
['last_name'] = last_name,
['disable_notification'] = disable_notification,
['reply_to_message_id'] = reply_to_message_id,
['reply_markup'] = reply_markup
}
)
end
function api.send_chat_action(chat_id, action) -- https://core.telegram.org/bots/api#sendchataction
return api.request(
'/sendChatAction',
{
['chat_id'] = chat_id,
['action'] = action or 'typing' -- Fallback to `typing` as the default action.
}
)
end
function api.get_user_profile_photos(user_id, offset, limit) -- https://core.telegram.org/bots/api#getuserprofilephotos
return api.request(
'/getUserProfilePhotos',
{
['user_id'] = user_id,
['offset'] = offset,
['limit'] = limit
}
)
end
function api.get_file(file_id) -- https://core.telegram.org/bots/api#getfile
return api.request(
'/getFile',
{ ['file_id'] = file_id }
)
end
function api.ban_chat_member(chat_id, user_id, until_date) -- https://core.telegram.org/bots/api#kickchatmember
return api.request(
'/kickChatMember',
{
['chat_id'] = chat_id,
['user_id'] = user_id,
['until_date'] = until_date
}
)
end
function api.kick_chat_member(chat_id, user_id)
local success = api.request(
'/kickChatMember',
{
['chat_id'] = chat_id,
['user_id'] = user_id
}
)
if not success then
return success
end
return api.unban_chat_member(chat_id, user_id, token)
end
function api.unban_chat_member(chat_id, user_id) -- https://core.telegram.org/bots/api#unbanchatmember
local success
for i = 1, 3 do -- Repeat 3 times to ensure the user was unbanned (I've encountered issues before so
-- this is for precautionary measures.)
success = api.request(
'/unbanChatMember',
{
['chat_id'] = chat_id,
['user_id'] = user_id
}
)
end
return success
end
function api.restrict_chat_member(chat_id, user_id, until_date, can_send_messages, can_send_media_messages, can_send_other_messages, can_add_web_page_previews) -- https://core.telegram.org/bots/api#restrictchatmember
return api.request(
'/restrictChatMember',
{
['chat_id'] = chat_id,
['user_id'] = user_id,
['until_date'] = until_date,
['can_send_messages'] = can_send_messages,
['can_send_media_messages'] = can_send_media_messages,
['can_send_other_messages'] = can_send_other_messages,
['can_add_web_page_previews'] = can_add_web_page_previews
}
)
end
function api.promote_chat_member(chat_id, user_id, can_change_info, can_post_messages, can_edit_messages, can_delete_messages, can_invite_users, can_restrict_members, can_pin_messages, can_promote_members) -- https://core.telegram.org/bots/api#promotechatmember
return api.request(
'/promoteChatMember',
{
['chat_id'] = chat_id,
['user_id'] = user_id,
['can_change_info'] = can_change_info,
['can_post_messages'] = can_post_messages,
['can_edit_messages'] = can_edit_messages,
['can_delete_messages'] = can_delete_messages,
['can_invite_users'] = can_invite_users,
['can_restrict_members'] = can_restrict_members,
['can_pin_messages'] = can_pin_messages,
['can_promote_members'] = can_promote_members
}
)
end
function api.export_chat_invite_link(chat_id) -- https://core.telegram.org/bots/api#exportchatinvitelink
return api.request(
'/exportChatInviteLink',
{ ['chat_id'] = chat_id }
)
end
function api.set_chat_photo(chat_id, photo) -- https://core.telegram.org/bots/api#setchatphoto
return api.request(
'/setChatPhoto',
{ ['chat_id'] = chat_id },
{ ['photo'] = photo }
)
end
function api.delete_chat_photo(chat_id) -- https://core.telegram.org/bots/api#deletechatphoto
return api.request(
'/deleteChatPhoto',
{ ['chat_id'] = chat_id }
)
end
function api.set_chat_title(chat_id, title) -- https://core.telegram.org/bots/api#setchattitle
return api.request(
'/setChatTitle',
{
['chat_id'] = chat_id,
['title'] = title
}
)
end
function api.set_chat_description(chat_id, description) -- https://core.telegram.org/bots/api#setchatdescription
return api.request(
'/setChatDescription',
{
['chat_id'] = chat_id,
['description'] = description
}
)
end
function api.pin_chat_message(chat_id, message_id, disable_notification) -- https://core.telegram.org/bots/api#pinchatmessage
return api.request(
'/pinChatMessage',
{
['chat_id'] = chat_id,
['message_id'] = message_id,
['disable_notification'] = disable_notification
}
)
end
function api.unpin_chat_message(chat_id) -- https://core.telegram.org/bots/api#unpinchatmessage
return api.request(
'/unpinChatMessage',
{ ['chat_id'] = chat_id }
)
end
function api.leave_chat(chat_id) -- https://core.telegram.org/bots/api#leavechat
return api.request(
'/leaveChat',
{ ['chat_id'] = chat_id }
)
end
function api.get_chat_administrators(chat_id) -- https://core.telegram.org/bots/api#getchatadministrators
return api.request(
'/getChatAdministrators',
{ ['chat_id'] = chat_id }
)
end
function api.get_chat_members_count(chat_id) -- https://core.telegram.org/bots/api#getchatmemberscount
return api.request(
'/getChatMembersCount',
{ ['chat_id'] = chat_id }
)
end
function api.get_chat_member(chat_id, user_id) -- https://core.telegram.org/bots/api#getchatmember
return api.request(
'/getChatMember',
{
['chat_id'] = chat_id,
['user_id'] = user_id
}
)
end
function api.set_chat_sticker_set(chat_id, sticker_set_name) -- https://core.telegram.org/bots/api#setchatstickerset
return api.request(
'/setChatStickerSet',
{
['chat_id'] = chat_id,
['sticker_set_name'] = sticker_set_name
}
)
end
function api.delete_chat_sticker_set(chat_id) -- https://core.telegram.org/bots/api#deletechatstickerset
return api.request(
'/deleteChatStickerSet',
{ ['chat_id'] = chat_id }
)
end
function api.answer_callback_query(callback_query_id, text, show_alert, url, cache_time) -- https://core.telegram.org/bots/api#answercallbackquery
return api.request(
'/answerCallbackQuery',
{
['callback_query_id'] = callback_query_id,
['text'] = text,
['show_alert'] = show_alert,
['url'] = url,
['cache_time'] = cache_time
}
)
end
function api.edit_message_text(chat_id, message_id, text, parse_mode, disable_web_page_preview, reply_markup, inline_message_id) -- https://core.telegram.org/bots/api#editmessagetext
reply_markup = type(reply_markup) == 'table' and json.encode(reply_markup) or reply_markup
parse_mode = (type(parse_mode) == 'boolean' and parse_mode == true) and 'markdown' or parse_mode
local success = api.request(
'/editMessageText',
{
['chat_id'] = chat_id,
['message_id'] = message_id,
['inline_message_id'] = inline_message_id,
['text'] = text,
['parse_mode'] = parse_mode,
['disable_web_page_preview'] = disable_web_page_preview,
['reply_markup'] = reply_markup
}
)
if not success
then
return api.request(
'/editMessageText',
{
['chat_id'] = chat_id,
['message_id'] = inline_message_id,
['inline_message_id'] = message_id,
['text'] = text,
['parse_mode'] = parse_mode,
['disable_web_page_preview'] = disable_web_page_preview,
['reply_markup'] = reply_markup
}
)
end
return success
end
function api.edit_message_caption(chat_id, message_id, caption, reply_markup, inline_message_id) -- https://core.telegram.org/bots/api#editmessagecaption
reply_markup = type(reply_markup) == 'table' and json.encode(reply_markup) or reply_markup
local success = api.request(
'/editMessageCaption',
{
['chat_id'] = chat_id,
['message_id'] = message_id,
['inline_message_id'] = inline_message_id,
['caption'] = caption,
['reply_markup'] = reply_markup
}
)
if not success then
return api.request(
'/editMessageCaption',
{
['chat_id'] = chat_id,
['message_id'] = inline_message_id,
['inline_message_id'] = message_id,
['caption'] = caption,
['reply_markup'] = reply_markup
}
)
end
return success
end
function api.edit_message_media(chat_id, message_id, media, reply_markup, inline_message_id) -- https://core.telegram.org/bots/api#editmessagemedia
reply_markup = type(reply_markup) == 'table' and json.encode(reply_markup) or reply_markup
local success = api.request(
'/editMessageMedia',
{
['chat_id'] = chat_id,
['message_id'] = message_id,
['inline_message_id'] = inline_message_id,
['media'] = media,
['reply_markup'] = reply_markup
}
)
if not success then
return api.request(
'/editMessageMedia',
{
['chat_id'] = chat_id,
['message_id'] = inline_message_id,
['inline_message_id'] = message_id,
['media'] = media,
['reply_markup'] = reply_markup
}
)
end
return success
end
function api.edit_message_reply_markup(chat_id, message_id, inline_message_id, reply_markup) -- https://core.telegram.org/bots/api#editmessagereplymarkup
reply_markup = type(reply_markup) == 'table' and json.encode(reply_markup) or reply_markup
local success = api.request(
'/editMessageReplyMarkup',
{
['chat_id'] = chat_id,
['message_id'] = message_id,
['inline_message_id'] = inline_message_id,
['reply_markup'] = reply_markup
}
)
if not success then
return api.request(
'/editMessageReplyMarkup',
{
['chat_id'] = chat_id,
['message_id'] = inline_message_id,
['inline_message_id'] = message_id,
['reply_markup'] = reply_markup
}
)
end
return success
end
function api.delete_message(chat_id, message_id) -- https://core.telegram.org/bots/api#deletemessage
return api.request(
'/deleteMessage',
{
['chat_id'] = chat_id,
['message_id'] = message_id
}
)
end
function api.get_sticker_set(name) -- https://core.telegram.org/bots/api#getstickerset
return api.request(
'/getStickerSet',
{ ['name'] = name }
)
end
function api.upload_sticker_file(user_id, png_sticker) -- https://core.telegram.org/bots/api#uploadstickerfile
return api.request(
'/uploadStickerFile',
{ ['user_id'] = user_id },
{ ['png_sticker'] = png_sticker }
)
end
function api.create_new_sticker_set(user_id, name, title, png_sticker, emojis, contains_masks, mask_position) -- https://core.telegram.org/bots/api#createnewstickerset
mask_position = type(mask_position) == 'table' and json.encode(mask_position) or mask_position
return api.request(
'/createNewStickerSet',
{
['user_id'] = user_id,
['name'] = name,
['title'] = title,
['emojis'] = emojis,
['contains_masks'] = contains_masks,
['mask_position'] = mask_position
},
{ ['png_sticker'] = png_sticker }
)
end
function api.add_sticker_to_set(user_id, name, png_sticker, emojis, mask_position) -- https://core.telegram.org/bots/api#addstickertoset
mask_position = type(mask_position) == 'table' and json.encode(mask_position) or mask_position
return api.request(
'/addStickerToSet',
{
['user_id'] = user_id,
['name'] = name,
['emojis'] = emojis,
['mask_position'] = mask_position
},
{ ['png_sticker'] = png_sticker }
)
end
function api.set_sticker_position_in_set(sticker, position) -- https://core.telegram.org/bots/api#setstickerpositioninset
return api.request(
'/setStickerPositionInSet',
{
['sticker'] = sticker,
['position'] = position
}
)
end
function api.delete_sticker_from_set(sticker) -- https://core.telegram.org/bots/api#deletestickerfromset
return api.request(
'/deleteStickerFromSet',
{ ['sticker'] = sticker }
)
end
function api.answer_inline_query(inline_query_id, results, cache_time, is_personal, next_offset, switch_pm_text, switch_pm_parameter) -- https://core.telegram.org/bots/api#answerinlinequery
if results and type(results) == 'table' then
if results.id then
results = { results }
end
results = json.encode(results)
end
return api.request(
'/answerInlineQuery',
{
['inline_query_id'] = inline_query_id,
['results'] = results,
['switch_pm_text'] = switch_pm_text,
['switch_pm_parameter'] = switch_pm_parameter,
['cache_time'] = cache_time,
['is_personal'] = is_personal,
['next_offset'] = next_offset
}
)
end
function api.send_game(chat_id, game_short_name, disable_notification, reply_to_message_id, reply_markup) -- https://core.telegram.org/bots/api#sendgame
reply_markup = type(reply_markup) == 'table' and json.encode(reply_markup) or reply_markup
return api.request(
'/sendGame',
{
['chat_id'] = chat_id,
['game_short_name'] = game_short_name,
['disable_notification'] = disable_notification,
['reply_to_message_id'] = reply_to_message_id,
['reply_markup'] = reply_markup
}
)
end
function api.set_game_score(chat_id, user_id, message_id, score, force, disable_edit_message, inline_message_id) -- https://core.telegram.org/bots/api#setgamescore
return api.request(
'/setGameScore',
{
['user_id'] = user_id,
['score'] = score,
['force'] = force,
['disable_edit_message'] = disable_edit_message,
['chat_id'] = chat_id,
['message_id'] = message_id,
['inline_message_id'] = inline_message_id
}
)
end
function api.get_game_high_scores(chat_id, user_id, message_id, inline_message_id) -- https://core.telegram.org/bots/api#getgamehighscores
return api.request(
'/getGameHighScores',
{
['user_id'] = user_id,
['chat_id'] = chat_id,
['message_id'] = message_id,
['inline_message_id'] = inline_message_id
}
)
end
function api.get_chat(chat_id) -- https://core.telegram.org/bots/api#getchat
return api.request(
'/getChat',
{ ['chat_id'] = chat_id }
)
end
function api.send_invoice(chat_id, title, description, payload, provider_token, start_parameter, currency, prices, provider_data, photo_url, photo_size, photo_width, photo_height, need_name, need_phone_number, need_email, need_shipping_address, is_flexible, disable_notification, reply_to_message_id, reply_markup)
return api.request(
'/sendInvoice',
{
['chat_id'] = chat_id,
['title'] = title,
['description'] = description,
['payload'] = payload,
['provider_token'] = provider_token,
['start_parameter'] = start_parameter,
['currency'] = currency,
['prices'] = prices,
['provider_data'] = provider_data,
['photo_url'] = photo_url,
['photo_size'] = photo_size,
['photo_width'] = photo_width,
['photo_height'] = photo_height,
['need_name'] = need_name,
['need_phone_number'] = need_phone_number,
['need_email'] = need_email,
['need_shipping_address'] = need_shipping_address,
['is_flexible'] = is_flexible,
['disable_notification'] = disable_notification,
['reply_to_message_id'] = reply_to_message_id,
['reply_markup'] = reply_markup
}
)
end
function api.answer_shipping_query(shipping_query_id, ok, shipping_options, error_message)
return api.request(
'/answerShippingQuery',
{
['shipping_query_id'] = shipping_query_id,
['ok'] = ok,
['shipping_options'] = shipping_options,
['error_message'] = error_message
}
)
end
function api.answer_pre_checkout_query(pre_checkout_query_id, ok, error_message)
return api.request(
'/answerPreCheckoutQuery',
{
['pre_checkout_query_id'] = pre_checkout_query_id,
['ok'] = ok,
['error_message'] = error_message
}
)
end
function api.on_update(update) end
function api.on_message(message) end
function api.on_private_message(message) end
function api.on_group_message(message) end
function api.on_supergroup_message(message) end
function api.on_callback_query(callback_query) end
function api.on_inline_query(inline_query) end
function api.on_channel_post(channel_post) end
function api.on_edited_message(edited_message) end
function api.on_edited_private_message(edited_message) end
function api.on_edited_group_message(edited_message) end
function api.on_edited_supergroup_message(edited_message) end
function api.on_edited_channel_post(edited_channel_post) end
function api.on_chosen_inline_result(chosen_inline_result) end
function api.on_shipping_query(shipping_query) end
function api.on_pre_checkout_query(pre_checkout_query) end
function api.process_update(update)
if update then
api.on_update(update)
end
if update.message then
if update.message.chat.type == 'private' then
api.on_private_message(update.message)
elseif update.message.chat.type == 'group' then
api.on_group_message(update.message)
elseif update.message.chat.type == 'supergroup' then
api.on_supergroup_message(update.message)
end
return api.on_message(update.message)
elseif update.edited_message then
if update.edited_message.chat.type == 'private' then
api.on_edited_private_message(update.edited_message)
elseif update.edited_message.chat.type == 'group' then
api.on_edited_group_message(update.edited_message)
elseif update.edited_message.chat.type == 'supergroup' then
api.on_edited_supergroup_message(update.edited_message)
end
return api.on_edited_message(update.edited_message)
elseif update.callback_query then
return api.on_callback_query(update.callback_query)
elseif update.inline_query then
return api.on_inline_query(update.inline_query)
elseif update.channel_post then
return api.on_channel_post(update.channel_post)
elseif update.edited_channel_post then
return api.on_edited_channel_post(update.edited_channel_post)
elseif update.chosen_inline_result then
return api.on_chosen_inline_result(update.chosen_inline_result)
elseif update.shipping_query then
return api.on_shipping_query(update.shipping_query)
elseif update.pre_checkout_query then
return api.on_pre_checkout_query(update.pre_checkout_query)
end
return false
end
local function _run(limit, timeout, offset, allowed_updates)
limit = tonumber(limit) ~= nil and limit or 1
timeout = tonumber(timeout) ~= nil and timeout or 0
offset = tonumber(offset) ~= nil and offset or 0
log.info('Telegram bot running...')
while true do
local updates, err = errors.pcall('GetUpdatesError', api.get_updates,
timeout, offset, limit, allowed_updates
)
if updates and type(updates) == 'table' and updates.result then
for k, v in pairs(updates.result) do
local _, err = errors.pcall('TelegramHandlerError', api.process_update, v)
if err ~= nil then
log.error('-> %s', err)
end
offset = v.update_id + 1
end
else
log.error('-> %s', err)
end
end
return false
end
function api.run()
api._fiber = fiber.new(_run, 100, 10)
api._fiber:name('telegram-bot')
return