· 5 years ago · Dec 21, 2020, 03:22 AM
1--[[
2
3lua-bot-api.lua - A Lua library to the Telegram Bot API
4(https://core.telegram.org/bots/api)
5
6Copyright (C) 2016 @cosmonawt
7
8This program is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2 of the License, or
11(at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License along
19with this program; if not, write to the Free Software Foundation, Inc.,
2051 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
22]]
23
24-- Import Libraries
25local https = require("ssl.https")
26local ltn12 = require("ltn12")
27local encode = require("multipart.multipart-post").encode
28local JSON = require("JSON")
29
30local M = {} -- Main Bot Framework
31local E = {} -- Extension Framework
32local C = {} -- Configure Constructor
33
34-- JSON Error handlers
35function JSON:onDecodeError(message, text, location, etc)
36 if text then
37 if location then
38 message = string.format("%s at char %d of: %s", message, location, text)
39 else
40 message = string.format("%s: %s", message, text)
41 end
42 end
43 --print((os.date("%x %X")), "Error while decoding JSON:\n", message)
44 local datefile = os.date("%d-%m-%Y.txt")
45 print((os.date("%x %X")), "Error: decode JSON, logged in ".. datefile)
46 local log = io.open("errors/" .. datefile,"a+") -- open log
47 log:write((os.date("%x %X")), "Error while decoding JSON:\n", message .. "\n") -- write in log
48 log:close()
49
50end
51
52function JSON:onDecodeOfHTMLError(message, text, _nil, etc)
53 if text then
54 if location then
55 message = string.format("%s at char %d of: %s", message, location, text)
56 else
57 message = string.format("%s: %s", message, text)
58 end
59 end
60 --print((os.date("%x %X")), "Error while decoding JSON [HTML]:\n", message)
61 local datefile = os.date("%d-%m-%Y.txt")
62 print((os.date("%x %X")), "Error: decode JSON [HTML], logged in ".. datefile)
63 local log = io.open("errors/" .. datefile,"a+") -- open log
64 log:write((os.date("%x %X")), "Error while decoding JSON [HTML]:\n", message .. "\n") -- write in log
65 log:close()
66end
67
68function JSON:onDecodeOfNilError(message, _nil, _nil, etc)
69 if text then
70 if location then
71 message = string.format("%s at char %d of: %s", message, location, text)
72 else
73 message = string.format("%s: %s", message, text)
74 end
75 end
76 print((os.date("%x %X")), "Error while decoding JSON [nil]:\n", message)
77end
78
79function JSON:onEncodeError(message, etc)
80 print((os.date("%x %X")), "Error while encoding JSON:\n", message)
81end
82
83-- configure and initialize bot
84local function configure(token)
85
86 if (token == "") then
87 token = nil
88 end
89
90 M.token = assert(token, "No token specified!")
91 local bot_info = M.getMe()
92 if (bot_info) then
93 M.id = bot_info.result.id
94 M.username = bot_info.result.username
95 M.first_name = bot_info.result.first_name
96 end
97 return M, E
98end
99
100C.configure = configure
101
102local function makeRequest(method, request_body)
103
104 local response = {}
105 local body, boundary = encode(request_body)
106
107 local success, code, headers, status = https.request{
108 url = "https://api.telegram.org/bot" .. M.token .. "/" .. method,
109 method = "POST",
110 headers = {
111 ["Content-Type"] = "multipart/form-data; boundary=" .. boundary,
112 ["Content-Length"] = string.len(body),
113 },
114 source = ltn12.source.string(body),
115 sink = ltn12.sink.table(response),
116 }
117
118 local r = {
119 success = success or "false",
120 code = code or "0",
121 headers = table.concat(headers or {"no headers"}),
122 status = status or "0",
123 body = table.concat(response or {"no response"}),
124 }
125 return r
126end
127
128-- Helper functions
129
130local function downloadFile(file_id, download_path)
131
132 if not file_id then return nil, "file_id not specified" end
133 if not download_path then return nil, "download_path not specified" end
134
135 local response = {}
136
137 local file_info = getFile(file_id)
138 local download_file_path = download_path or "downloads/" .. file_info.result.file_path
139
140 local download_file = io.open(download_file_path, "w")
141
142 if not download_file then return nil, "download_file could not be created"
143 else
144 local success, code, headers, status = https.request{
145 url = "https://api.telegram.org/file/bot" .. M.token .. "/" .. file_info.result.file_path,
146 --source = ltn12.source.string(body),
147 sink = ltn12.sink.file(download_file),
148 }
149
150 local r = {
151 success = true,
152 download_path = download_file_path,
153 file = file_info.result
154 }
155 return r
156 end
157end
158
159M.downloadFile = downloadFile
160
161local function generateReplyKeyboardMarkup(keyboard, resize_keyboard, one_time_keyboard, selective)
162
163 if not keyboard then return nil, "keyboard not specified" end
164 if #keyboard < 1 then return nil, "keyboard is empty" end
165
166 local response = {}
167
168 response.keyboard = keyboard
169 response.resize_keyboard = resize_keyboard
170 response.one_time_keyboard = one_time_keyboard
171 response.selective = selective
172
173
174 local responseString = JSON:encode(response)
175 return responseString
176end
177
178M.generateReplyKeyboardMarkup = generateReplyKeyboardMarkup
179
180
181local function generateReplyKeyboardHide(hide_keyboard, selective)
182
183 local response = {}
184
185 response.hide_keyboard = true
186 response.selective = selective
187
188 local responseString = JSON:encode(response)
189 return responseString
190end
191
192M.generateReplyKeyboardHide = generateReplyKeyboardHide
193
194
195local function generateForceReply(force_reply, selective)
196
197 local response = {}
198
199 response.force_reply = true
200 response.selective = selective
201
202 local responseString = JSON:encode(response)
203 return responseString
204end
205
206M.generateForceReply = generateForceReply
207
208-- Bot API 1.0
209
210local function getUpdates(offset, limit, timeout, allowed_updates)
211
212 local request_body = {}
213
214 request_body.offset = offset
215 request_body.limit = limit
216 request_body.timeout = timeout or 0
217 request_body.allowed_updates = allowed_updates or nil
218
219 local response = makeRequest("getUpdates", request_body)
220
221 if (response.success == 1) then
222 return JSON:decode(response.body)
223 else
224 return nil, "Request Error"
225 end
226end
227
228M.getUpdates = getUpdates
229
230
231local function getMe()
232 local request_body = {""}
233
234 local response = makeRequest("getMe",request_body)
235
236 if (response.success == 1) then
237 return JSON:decode(response.body)
238 else
239 return nil, "Request Error"
240 end
241end
242
243M.getMe = getMe
244
245
246local function sendMessage(chat_id, text, parse_mode, disable_web_page_preview, disable_notification, reply_to_message_id, reply_markup)
247
248 if not chat_id then return nil, "chat_id not specified" end
249 if not text then return nil, "text not specified" end
250
251 local allowed_parse_mode = {
252 ["Markdown"] = true,
253 ["HTML"] = true
254 }
255
256 if (not allowed_parse_mode[parse_mode]) then parse_mode = "" end
257
258 local request_body = {}
259
260 request_body.chat_id = chat_id
261 request_body.text = tostring(text)
262 request_body.parse_mode = parse_mode
263 request_body.disable_web_page_preview = tostring(disable_web_page_preview)
264 request_body.disable_notification = tostring(disable_notification)
265 request_body.reply_to_message_id = tonumber(reply_to_message_id)
266 request_body.reply_markup = reply_markup or ""
267
268 local response = makeRequest("sendMessage",request_body)
269
270 if (response.success == 1) then
271 return JSON:decode(response.body)
272 else
273 return nil, "Request Error"
274 end
275end
276
277M.sendMessage = sendMessage
278
279local function forwardMessage(chat_id, from_chat_id, disable_notification, message_id)
280
281 if not chat_id then return nil, "chat_id not specified" end
282 if not from_chat_id then return nil, "from_chat_id not specified" end
283 if not message_id then return nil, "message_id not specified" end
284
285 local request_body = {""}
286
287 request_body.chat_id = chat_id
288 request_body.from_chat_id = from_chat_id
289 request_body.disable_notification = tostring(disable_notification)
290 request_body.message_id = tonumber(message_id)
291
292 local response = makeRequest("forwardMessage",request_body)
293
294 if (response.success == 1) then
295 return JSON:decode(response.body)
296 else
297 return nil, "Request Error"
298 end
299end
300
301M.forwardMessage = forwardMessage
302
303
304local function sendPhoto(chat_id, photo, caption, disable_notification, reply_to_message_id, reply_markup)
305
306 if not chat_id then return nil, "chat_id not specified" end
307 if not photo then return nil, "photo not specified" end
308
309 local request_body = {""}
310 local file_id = ""
311 local photo_data = {}
312
313 if not(string.find(photo, "%.")) then
314 file_id = photo
315 else
316 file_id = nil
317 local photo_file = io.open(photo, "r")
318
319 photo_data.filename = photo
320 photo_data.data = photo_file:read("*a")
321 photo_data.content_type = "image"
322
323 photo_file:close()
324 end
325
326 request_body.chat_id = chat_id
327 request_body.photo = file_id or photo_data
328 request_body.caption = caption
329 request_body.disable_notification = tostring(disable_notification)
330 request_body.reply_to_message_id = tonumber(reply_to_message_id)
331 request_body.reply_markup = reply_markup
332
333 local response = makeRequest("sendPhoto",request_body)
334
335 if (response.success == 1) then
336 return JSON:decode(response.body)
337 else
338 return nil, "Request Error"
339 end
340end
341
342M.sendPhoto = sendPhoto
343
344
345local function sendAudio(chat_id, audio, caption, duration, performer, title, disable_notification, reply_to_message_id, reply_markup)
346
347 if not chat_id then return nil, "chat_id not specified" end
348 if not audio then return nil, "audio not specified" end
349
350 local request_body = {}
351 local file_id = ""
352 local audio_data = {}
353
354 if not(string.find(audio, "%.mp3")) then
355 file_id = audio
356 else
357 file_id = nil
358 local audio_file = io.open(audio, "r")
359
360 audio_data.filename = audio
361 audio_data.data = audio_file:read("*a")
362 audio_data.content_type = "audio/mpeg"
363
364 audio_file:close()
365 end
366
367 request_body.chat_id = chat_id
368 request_body.audio = file_id or audio_data
369 request_body.duration = duration
370 request_body.caption = caption
371 request_body.performer = performer
372 request_body.title = title
373 request_body.disable_notification = tostring(disable_notification)
374 request_body.reply_to_message_id = tonumber(reply_to_message_id)
375 request_body.reply_markup = reply_markup
376
377 local response = makeRequest("sendAudio",request_body)
378
379 if (response.success == 1) then
380 return JSON:decode(response.body)
381 else
382 return nil, "Request Error"
383 end
384end
385
386M.sendAudio = sendAudio
387
388
389local function sendDocument(chat_id, document, caption, disable_notification, reply_to_message_id, reply_markup)
390
391 if not chat_id then return nil, "chat_id not specified" end
392 if not document then return nil, "document not specified" end
393
394 local request_body = {}
395 local file_id = ""
396 local document_data = {}
397
398 if not(string.find(document, "%.")) then
399 file_id = document
400 else
401 file_id = nil
402 local document_file = io.open(document, "r")
403
404 document_data.filename = document
405 document_data.data = document_file:read("*a")
406
407 document_file:close()
408 end
409
410 request_body.chat_id = chat_id
411 request_body.document = file_id or document_data
412 request_body.caption = caption
413 request_body.disable_notification = tostring(disable_notification)
414 request_body.reply_to_message_id = tonumber(reply_to_message_id)
415 request_body.reply_markup = reply_markup
416
417 local response = makeRequest("sendDocument",request_body)
418
419 if (response.success == 1) then
420 return JSON:decode(response.body)
421 else
422 return nil, "Request Error"
423 end
424end
425
426M.sendDocument = sendDocument
427
428
429local function sendSticker(chat_id, sticker, disable_notification, reply_to_message_id, reply_markup)
430
431 if not chat_id then return nil, "chat_id not specified" end
432 if not sticker then return nil, "sticker not specified" end
433
434 local request_body = {}
435 local file_id = ""
436 local sticker_data = {}
437
438 if not(string.find(sticker, "%.webp")) then
439 file_id = sticker
440 else
441 file_id = nil
442 local sticker_file = io.open(sticker, "r")
443
444 sticker_data.filename = sticker
445 sticker_data.data = sticker_file:read("*a")
446 sticker_data.content_type = "image/webp"
447
448 sticker_file:close()
449 end
450
451 request_body.chat_id = chat_id
452 request_body.sticker = file_id or sticker_data
453 request_body.disable_notification = tostring(disable_notification)
454 request_body.reply_to_message_id = tonumber(reply_to_message_id)
455 request_body.reply_markup = reply_markup
456
457 local response = makeRequest("sendSticker",request_body)
458
459 if (response.success == 1) then
460 return JSON:decode(response.body)
461 else
462 return nil, "Request Error"
463 end
464end
465
466M.sendSticker = sendSticker
467
468
469local function sendVideo(chat_id, video, duration, caption, disable_notification, reply_to_message_id, reply_markup)
470
471 if not chat_id then return nil, "chat_id not specified" end
472 if not video then return nil, "video not specified" end
473
474 local request_body = {}
475 local file_id = ""
476 local video_data = {}
477
478 if not(string.find(video, "%.")) then
479 file_id = video
480 else
481 file_id = nil
482 local video_file = io.open(video, "r")
483
484 video_data.filename = video
485 video_data.data = video_file:read("*a")
486 video_data.content_type = "video"
487
488 video_file:close()
489 end
490
491 request_body.chat_id = chat_id
492 request_body.video = file_id or video_data
493 request_body.duration = duration
494 request_body.caption = caption
495 request_body.disable_notification = tostring(disable_notification)
496 request_body.reply_to_message_id = tonumber(reply_to_message_id)
497 request_body.reply_markup = reply_markup
498
499 local response = makeRequest("sendVideo",request_body)
500
501 if (response.success == 1) then
502 return JSON:decode(response.body)
503 else
504 return nil, "Request Error"
505 end
506end
507
508M.sendVideo = sendVideo
509
510
511local function sendVoice(chat_id, voice, caption, duration, disable_notification, reply_to_message_id, reply_markup)
512
513 if not chat_id then return nil, "chat_id not specified" end
514 if not voice then return nil, "voice not specified" end
515
516 local request_body = {}
517 local file_id = ""
518 local voice_data = {}
519
520 if not(string.find(voice, "%.ogg")) then
521 file_id = voice
522 else
523 file_id = nil
524 local voice_file = io.open(voice, "r")
525
526 voice_data.filename = voice
527 voice_data.data = voice_file:read("*a")
528 voice_data.content_type = "audio/ogg"
529
530 voice_file:close()
531 end
532
533 request_body.chat_id = chat_id
534 request_body.voice = file_id or voice_data
535 request_body.duration = duration
536 request_body.caption = caption
537 request_body.disable_notification = tostring(disable_notification)
538 request_body.reply_to_message_id = tonumber(reply_to_message_id)
539 request_body.reply_markup = reply_markup
540
541 local response = makeRequest("sendVoice",request_body)
542
543 if (response.success == 1) then
544 return JSON:decode(response.body)
545 else
546 return nil, "Request Error"
547 end
548end
549
550M.sendVoice = sendVoice
551
552local function sendLocation(chat_id, latitude, longitude, disable_notification, reply_to_message_id, reply_markup)
553
554 if not chat_id then return nil, "chat_id not specified" end
555 if not latitude then return nil, "latitude not specified" end
556 if not longitude then return nil, "longitude not specified" end
557
558 local request_body = {}
559
560 request_body.chat_id = chat_id
561 request_body.latitude = tonumber(latitude)
562 request_body.longitude = tonumber(longitude)
563 request_body.disable_notification = tostring(disable_notification)
564 request_body.reply_to_message_id = tonumber(reply_to_message_id)
565 request_body.reply_markup = reply_markup
566
567 local response = makeRequest("sendLocation",request_body)
568
569 if (response.success == 1) then
570 return JSON:decode(response.body)
571 else
572 return nil, "Request Error"
573 end
574end
575
576M.sendLocation = sendLocation
577
578local function sendChatAction(chat_id, action)
579
580 if not chat_id then return nil, "chat_id not specified" end
581 if not action then return nil, "action not specified" end
582
583 local request_body = {}
584
585 local allowedAction = {
586 ["typing"] = true,
587 ["upload_photo"] = true,
588 ["record_video"] = true,
589 ["upload_video"] = true,
590 ["record_audio"] = true,
591 ["upload_audio"] = true,
592 ["upload_document"] = true,
593 ["find_location"] = true,
594 }
595
596 if (not allowedAction[action]) then action = "typing" end
597
598 request_body.chat_id = chat_id
599 request_body.action = action
600
601 local response = makeRequest("sendChatAction",request_body)
602
603 if (response.success == 1) then
604 return JSON:decode(response.body)
605 else
606 return nil, "Request Error"
607 end
608end
609
610M.sendChatAction = sendChatAction
611
612local function getUserProfilePhotos(user_id, offset, limit)
613
614 if not user_id then return nil, "user_id not specified" end
615
616 local request_body = {}
617
618 request_body.user_id = tonumber(user_id)
619 request_body.offset = offset
620 request_body.limit = limit
621
622 local response = makeRequest("getUserProfilePhotos",request_body)
623
624 if (response.success == 1) then
625 return JSON:decode(response.body)
626 else
627 return nil, "Request Error"
628 end
629end
630
631M.getUserProfilePhotos = getUserProfilePhotos
632
633local function getFile(file_id)
634
635 if not file_id then return nil, "file_id not specified" end
636
637 local request_body = {}
638
639 request_body.file_id = file_id
640
641 local response = makeRequest("getFile",request_body)
642
643 if (response.success == 1) then
644 return JSON:decode(response.body)
645 else
646 return nil, "Request Error"
647 end
648end
649
650M.getFile = getFile
651
652local function answerInlineQuery(inline_query_id, results, cache_time, is_personal, next_offset, switch_pm_text, switch_pm_parameter)
653
654 if not inline_query_id then return nil, "inline_query_id not specified" end
655 if not results then return nil, "results not specified" end
656
657 local request_body = {}
658
659 request_body.inline_query_id = tostring(inline_query_id)
660 request_body.results = JSON:encode(results)
661 request_body.cache_time = tonumber(cache_time)
662 request_body.is_personal = tostring(is_personal)
663 request_body.next_offset = tostring(next_offset)
664 request_body.switch_pm_text = tostring(switch_pm_text)
665 request_body.switch_pm_parameter = tostring(switch_pm_text)
666
667 local response = makeRequest("answerInlineQuery",request_body)
668
669 if (response.success == 1) then
670 return JSON:decode(response.body)
671 else
672 return nil, "Request Error"
673 end
674end
675
676M.answerInlineQuery = answerInlineQuery
677
678-- Bot API 2.0
679
680local function sendVenue(chat_id, latitude, longitude, title, adress, foursquare_id, disable_notification, reply_to_message_id, reply_markup)
681
682 if not chat_id then return nil, "chat_id not specified" end
683 if not latitude then return nil, "latitude not specified" end
684 if not longitude then return nil, "longitude not specified" end
685 if not title then return nil, "title not specified" end
686 if not adress then return nil, "adress not specified" end
687
688 local request_body = {}
689
690 request_body.chat_id = chat_id
691 request_body.latitude = tonumber(latitude)
692 request_body.longitude = tonumber(longitude)
693 request_body.title = title
694 request_body.adress = adress
695 request_body.foursquare_id = foursquare_id
696 request_body.disable_notification = tostring(disable_notification)
697 request_body.reply_to_message_id = tonumber(reply_to_message_id)
698 request_body.reply_markup = reply_markup
699
700 local response = makeRequest("sendVenue",request_body)
701
702 if (response.success == 1) then
703 return JSON:decode(response.body)
704 else
705 return nil, "Request Error"
706 end
707end
708
709M.sendVenue = sendVenue
710
711local function sendContact(chat_id, phone_number, first_name, last_name, disable_notification, reply_to_message_id, reply_markup)
712
713 if not chat_id then return nil, "chat_id not specified" end
714 if not phone_number then return nil, "phone_number not specified" end
715 if not first_name then return nil, "first_name not specified" end
716
717 request_body.chat_id = chat_id
718 request_body.phone_number = tostring(phone_number)
719 request_body.first_name = tostring(first_name)
720 request_body.last_name = tostring(last_name)
721 request_body.disable_notification = tostring(disable_notification)
722 request_body.reply_to_message_id = tonumber(reply_to_message_id)
723 request_body.reply_markup = reply_markup
724
725 local response = makeRequest("sendContact",request_body)
726
727 if (response.success == 1) then
728 return JSON:decode(response.body)
729 else
730 return nil, "Request Error"
731 end
732end
733
734M.sendContact = sendContact
735
736local function kickChatMember(chat_id, user_id)
737 if not chat_id then return nil, "chat_id not specified" end
738 if not user_id then return nil, "user_id not specified" end
739
740 local request_body = {}
741
742 request_body.chat_id = chat_id
743 request_body.user_id = tonumber(user_id)
744
745 local response = makeRequest("kickChatMember",request_body)
746
747 if (response.success == 1) then
748 return JSON:decode(response.body)
749 else
750 return nil, "Request Error"
751 end
752end
753
754M.kickChatMember = kickChatMember
755
756local function unbanChatMember(chat_id, user_id)
757 if not chat_id then return nil, "chat_id not specified" end
758 if not user_id then return nil, "user_id not specified" end
759
760 local request_body = {}
761
762 request_body.chat_id = chat_id
763 request_body.user_id = tonumber(user_id)
764
765 local response = makeRequest("unbanChatMember",request_body)
766
767 if (response.success == 1) then
768 return JSON:decode(response.body)
769 else
770 return nil, "Request Error"
771 end
772end
773
774M.unbanChatMember = unbanChatMember
775
776local function answerCallbackQuery(callback_query_id, text, show_alert, cache_time)
777
778 if not callback_query_id then return nil, "callback_query_id not specified" end
779
780 local request_body = {}
781
782 request_body.callback_query_id = tostring(callback_query_id)
783 request_body.text = tostring(text)
784 request_body.show_alert = tostring(show_alert)
785 request_body.cache_time = tostring(cache_time)
786
787 local response = makeRequest("answerCallbackQuery",request_body)
788
789 if (response.success == 1) then
790 return JSON:decode(response.body)
791 else
792 return nil, "Request Error"
793 end
794end
795
796M.answerCallbackQuery = answerCallbackQuery
797
798local function editMessageText(chat_id, message_id, inline_message_id, text, parse_mode, disable_web_page_preview, reply_markup)
799
800 if not chat_id and not inline_message_id then return nil, "chat_id not specified" end
801 if not message_id and not inline_message_id then return nil, "message_id not specified" end
802 if not inline_message_id and not (chat_id and message_id) then return nil, "inline_message_id not specified" end
803 if not text then return nil, "text not specified" end
804
805 local request_body = {}
806
807 request_body.chat_id = chat_id
808 request_body.message_id = tonumber(message_id)
809 request_body.inline_message_id = tostring(inline_message_id)
810 request_body.text = tostring(text)
811 request_body.parse_mode = tostring(parse_mode)
812 request_body.disable_web_page_preview = disable_web_page_preview
813 request_body.reply_markup = reply_markup
814
815 local response = makeRequest("editMessageText",request_body)
816
817 if (response.success == 1) then
818 return JSON:decode(response.body)
819 else
820 return nil, "Request Error"
821 end
822end
823
824M.editMessageText = editMessageText
825
826local function editMessageCaption(chat_id, message_id, inline_message_id, caption, reply_markup)
827
828 if not chat_id and not inline_message_id then return nil, "chat_id not specified" end
829 if not message_id and not inline_message_id then return nil, "message_id not specified" end
830 if not inline_message_id and not (chat_id and message_id) then return nil, "inline_message_id not specified" end
831 if not caption then return nil, "caption not specified" end
832
833 local request_body = {}
834
835 request_body.chat_id = chat_id
836 request_body.message_id = tonumber(message_id)
837 request_body.inline_message_id = tostring(inline_message_id)
838 request_body.caption = tostring(caption)
839 request_body.reply_markup = reply_markup
840
841 local response = makeRequest("editMessageCaption",request_body)
842
843 if (response.success == 1) then
844 return JSON:decode(response.body)
845 else
846 return nil, "Request Error"
847 end
848end
849
850M.editMessageCaption = editMessageCaption
851
852local function editMessageReplyMarkup(chat_id, message_id, inline_message_id, reply_markup)
853
854 if not chat_id and not inline_message_id then return nil, "chat_id not specified" end
855 if not message_id and not inline_message_id then return nil, "message_id not specified" end
856 if not inline_message_id and not (chat_id and message_id) then return nil, "inline_message_id not specified" end
857
858 local request_body = {}
859
860 request_body.chat_id = chat_id
861 request_body.message_id = tonumber(message_id)
862 request_body.inline_message_id = tostring(inline_message_id)
863 request_body.reply_markup = reply_markup
864
865 local response = makeRequest("editMessageReplyMarkup",request_body)
866
867 if (response.success == 1) then
868 return JSON:decode(response.body)
869 else
870 return nil, "Request Error"
871 end
872end
873
874M.editMessageReplyMarkup = editMessageReplyMarkup
875
876-- Bot API 2.1
877
878local function getChat(chat_id)
879
880 if not chat_id then return nil, "chat_id not specified" end
881
882 local request_body = {}
883 request_body.chat_id = chat_id
884
885 local response = makeRequest("getChat", request_body)
886
887 if (response.success == 1) then
888 return JSON:decode(response.body)
889 else
890 return nil, "Request Error"
891 end
892end
893
894M.getChat = getChat
895
896local function leaveChat(chat_id)
897
898 if not chat_id then return nil, "chat_id not specified" end
899
900 local request_body = {}
901 request_body.chat_id = chat_id
902
903 local response = makeRequest("leaveChat", request_body)
904
905 if (response.success == 1) then
906 return JSON:decode(response.body)
907 else
908 return nil, "Request Error"
909 end
910end
911
912M.leaveChat = leaveChat
913
914local function getChatAdministrators(chat_id)
915
916 if not chat_id then return nil, "chat_id not specified" end
917
918 local request_body = {}
919 request_body.chat_id = chat_id
920
921 local response = makeRequest("getChatAdministrators", request_body)
922
923 if (response.success == 1) then
924 return JSON:decode(response.body)
925 else
926 return nil, "Request Error"
927 end
928end
929
930M.getChatAdministrators = getChatAdministrators
931
932local function getChatMembersCount(chat_id)
933
934 if not chat_id then return nil, "chat_id not specified" end
935
936 local request_body = {}
937 request_body.chat_id = chat_id
938
939 local response = makeRequest("getChatMembersCount", request_body)
940
941 if (response.success == 1) then
942 return JSON:decode(response.body)
943 else
944 return nil, "Request Error"
945 end
946end
947
948M.getChatMembersCount = getChatMembersCount
949
950local function getChatMember(chat_id, user_id)
951
952 if not chat_id then return nil, "chat_id not specified" end
953 if not user_id then return nil, "user_id not specified" end
954
955
956 local request_body = {}
957 request_body.chat_id = chat_id
958 request_body.user_id = user_id
959
960 local response = makeRequest("getChatMember", request_body)
961
962 if (response.success == 1) then
963 return JSON:decode(response.body)
964 else
965 return nil, "Request Error"
966 end
967end
968
969M.getChatMember = getChatMember
970
971-- Extension Framework
972
973local function onUpdateReceive(update) end
974E.onUpdateReceive = onUpdateReceive
975
976local function onTextReceive(message) end
977E.onMessageReceive = onMessageReceive
978
979local function onPhotoReceive(message) end
980E.onPhotoReceive = onPhotoReceive
981
982local function onAudioReceive(message) end
983E.onAudioReceive = onAudioReceive
984
985local function onDocumentReceive(message) end
986E.onDocumentReceive = onDocumentReceive
987
988local function onStickerReceive(message) end
989E.onStickerReceive = onStickerReceive
990
991local function onVideoReceive(message) end
992E.onVideoReceive = onVideoReceive
993
994local function onVoiceReceive(message) end
995E.onVoiceReceive = onVoiceReceive
996
997local function onContactReceive(message) end
998E.onContactReceive = onContactReceive
999
1000local function onLocationReceive(message) end
1001E.onLocationReceive = onLocationReceive
1002
1003local function onLeftChatParticipant(message) end
1004E.onLeftChatParticipant = onLeftChatParticipant
1005
1006local function onNewChatParticipant(message) end
1007E.onNewChatParticipant = onNewChatParticipant
1008
1009local function onNewChatTitle(message) end
1010E.onNewChatTitle = onNewChatTitle
1011
1012local function onNewChatPhoto(message) end
1013E.onNewChatPhoto = onNewChatPhoto
1014
1015local function onDeleteChatPhoto(message) end
1016E.onDeleteChatPhoto = onDeleteChatPhoto
1017
1018local function onGroupChatCreated(message) end
1019E.onGroupChatCreated = onGroupChatCreated
1020
1021local function onSupergroupChatCreated(message) end
1022E.onsuperGroupChatCreated = onsuperGroupChatCreated
1023
1024local function onChannelChatCreated(message) end
1025E.onChannelChatCreated = onChannelChatCreated
1026
1027local function onMigrateToChatId(message) end
1028E.onMigrateToChatId = onMigrateToChatId
1029
1030local function onMigrateFromChatId(message) end
1031E.onMigrateFromChatId = onMigrateFromChatId
1032
1033local function onEditedMessageReceive(message) end
1034E.onEditedMessageReceive = onEditedMessageReceive
1035
1036local function onInlineQueryReceive(inlineQuery) end
1037E.onInlineQueryReceive = onInlineQueryReceive
1038
1039local function onChosenInlineQueryReceive(chosenInlineQuery) end
1040E.onChosenInlineQueryReceive = onChosenInlineQueryReceive
1041
1042local function onCallbackQueryReceive(CallbackQuery) end
1043E.onCallbackQueryReceive = onCallbackQueryReceive
1044
1045local function onChannelPost(post) end
1046E.onChannelPost = onChannelPost
1047
1048local function onChannelEditPost(post) end
1049E.onChannelEditPost = onChannelEditPost
1050
1051local function onUnknownTypeReceive(unknownType)
1052 print("new unknownType!")
1053end
1054E.onUnknownTypeReceive = onUnknownTypeReceive
1055
1056local function parseUpdateCallbacks(update)
1057 if (update) then
1058 E.onUpdateReceive(update)
1059 end
1060 if (update.message) then
1061 if (update.message.text) then
1062 E.onTextReceive(update.message)
1063 elseif (update.message.photo) then
1064 E.onPhotoReceive(update.message)
1065 elseif (update.message.audio) then
1066 E.onAudioReceive(update.message)
1067 elseif (update.message.document) then
1068 E.onDocumentReceive(update.message)
1069 elseif (update.message.sticker) then
1070 E.onStickerReceive(update.message)
1071 elseif (update.message.video) then
1072 E.onVideoReceive(update.message)
1073 elseif (update.message.voice) then
1074 E.onVoiceReceive(update.message)
1075 elseif (update.message.contact) then
1076 E.onContactReceive(update.message)
1077 elseif (update.message.location) then
1078 E.onLocationReceive(update.message)
1079 elseif (update.message.left_chat_participant) then
1080 E.onLeftChatParticipant(update.message)
1081 elseif (update.message.new_chat_participant) then
1082 E.onNewChatParticipant(update.message)
1083 elseif (update.message.new_chat_photo) then
1084 E.onNewChatPhoto(update.message)
1085 elseif (update.message.delete_chat_photo) then
1086 E.onDeleteChatPhoto(update.message)
1087 elseif (update.message.group_chat_created) then
1088 E.onGroupChatCreated(update.message)
1089 elseif (update.message.supergroup_chat_created) then
1090 E.onSupergroupChatCreated(update.message)
1091 elseif (update.message.channel_chat_created) then
1092 E.onChannelChatCreated(update.message)
1093 elseif (update.message.migrate_to_chat_id) then
1094 E.onMigrateToChatId(update.message)
1095 elseif (update.message.migrate_from_chat_id) then
1096 E.onMigrateFromChatId(update.message)
1097 else
1098 E.onUnknownTypeReceive(update)
1099 end
1100 elseif (update.edited_message) then
1101 E.onEditedMessageReceive(update.edited_message)
1102 elseif (update.inline_query) then
1103 E.onInlineQueryReceive(update.inline_query)
1104 elseif (update.chosen_inline_result) then
1105 E.onChosenInlineQueryReceive(update.chosen_inline_result)
1106 elseif (update.callback_query) then
1107 E.onCallbackQueryReceive(update.callback_query)
1108 elseif (update.channel_post) then
1109 E.onChannelPost(update.channel_post)
1110 elseif (update.edited_channel_post) then
1111 E.onChannelEditPost(update.edited_channel_post)
1112 else
1113 E.onUnknownTypeReceive(update)
1114 end
1115end
1116
1117local function run(limit, timeout)
1118 if limit == nil then limit = 1 end
1119 if timeout == nil then timeout = 0 end
1120 local offset = 0
1121 while true do
1122 local updates = M.getUpdates(offset, limit, timeout)
1123 if(updates) then
1124 if (updates.result) then
1125 for key, update in pairs(updates.result) do
1126 parseUpdateCallbacks(update)
1127 offset = update.update_id + 1
1128 end
1129 end
1130 end
1131 end
1132end
1133
1134E.run = run
1135
1136return C
1137