-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.lua
2655 lines (2266 loc) · 89.2 KB
/
utils.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
--[[
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
]]--
-- Vector example form is like this: {[0] = v} or {v1, v2, v3, [0] = v}
-- If false or true crashed your telegram-cli, try to change true to 1 and false to 0
-- Main table
local M = {}
-- It's do nothing but suppress "lua: attempt to call a nil value" warning
function dl_cb(arg, data)
end
-- @chat_id = user, group, channel, and broadcast
-- @group_id = normal group
-- @channel_id = channel and broadcast
local function getChatId(chat_id)
local chat = {}
local chat_id = tostring(chat_id)
if chat_id:match('^-100') then
local channel_id = chat_id:gsub('-100', '')
chat = {ID = channel_id, type = 'channel'}
else
local group_id = chat_id:gsub('-', '')
chat = {ID = group_id, type = 'group'}
end
return chat
end
local function getInputFile(file)
local input = tostring(file)
if input:match('/') then
infile = {ID = "InputFileLocal", path_ = file}
elseif input:match('^%d+$') then
infile = {ID = "InputFileId", id_ = file}
else
infile = {ID = "InputFilePersistentId", persistent_id_ = file}
end
return infile
end
-- User can send bold, italic, and monospace text uses HTML or Markdown format.
local function getParseMode(parse_mode)
if parse_mode then
local mode = parse_mode:lower()
if mode == 'markdown' or mode == 'md' then
P = {ID = "TextParseModeMarkdown"}
elseif mode == 'html' then
P = {ID = "TextParseModeHTML"}
end
end
return P
end
-- Send SendMessage request
local function sendRequest(request_id, chat_id, reply_to_message_id, disable_notification, from_background, reply_markup, input_message_content, callback, extra)
tdcli_function ({
ID = request_id,
chat_id_ = chat_id,
reply_to_message_id_ = reply_to_message_id,
disable_notification_ = disable_notification,
from_background_ = from_background,
reply_markup_ = reply_markup,
input_message_content_ = input_message_content,
}, callback or dl_cb, extra)
end
-- Returns current authorization state, offline request
local function getAuthState(dl_cb, cmd)
tdcli_function ({
ID = "GetAuthState",
}, cb or dl_cb, cmd)
end
M.getAuthState = getAuthState
-- Sets user's phone number and sends authentication code to the user.
-- Works only when authGetState returns authStateWaitPhoneNumber.
-- If phone number is not recognized or another error has happened, returns an error. Otherwise returns authStateWaitCode
-- @phone_number User's phone number in any reasonable format
-- @allow_flash_call Pass True, if code can be sent via flash call to the specified phone number
-- @is_current_phone_number Pass true, if the phone number is used on the current device. Ignored if allow_flash_call is False
local function setAuthPhoneNumber(phone_number, allow_flash_call, is_current_phone_number, cb, cmd)
tdcli_function ({
ID = "SetAuthPhoneNumber",
phone_number_ = phone_number,
allow_flash_call_ = allow_flash_call,
is_current_phone_number_ = is_current_phone_number
}, cb or dl_cb, cmd)
end
M.setAuthPhoneNumber = setAuthPhoneNumber
-- Resends authentication code to the user.
-- Works only when authGetState returns authStateWaitCode and next_code_type of result is not null.
-- Returns authStateWaitCode on success
local function resendAuthCode(dl_cb, cmd)
tdcli_function ({
ID = "ResendAuthCode",
}, cb or dl_cb, cmd)
end
M.resendAuthCode = resendAuthCode
-- Checks authentication code.
-- Works only when authGetState returns authStateWaitCode.
-- Returns authStateWaitPassword or authStateOk on success
-- @code Verification code from SMS, Telegram message, voice call or flash call
-- @first_name User first name, if user is yet not registered, 1-255 characters
-- @last_name Optional user last name, if user is yet not registered, 0-255 characters
local function checkAuthCode(code, first_name, last_name, cb, cmd)
tdcli_function ({
ID = "CheckAuthCode",
code_ = code,
first_name_ = first_name,
last_name_ = last_name
}, cb or dl_cb, cmd)
end
M.checkAuthCode = checkAuthCode
-- Checks password for correctness.
-- Works only when authGetState returns authStateWaitPassword.
-- Returns authStateOk on success
-- @password Password to check
local function checkAuthPassword(password, cb, cmd)
tdcli_function ({
ID = "CheckAuthPassword",
password_ = password
}, cb or dl_cb, cmd)
end
M.checkAuthPassword = checkAuthPassword
-- Requests to send password recovery code to email.
-- Works only when authGetState returns authStateWaitPassword.
-- Returns authStateWaitPassword on success
local function requestAuthPasswordRecovery(dl_cb, cmd)
tdcli_function ({
ID = "RequestAuthPasswordRecovery",
}, cb or dl_cb, cmd)
end
M.requestAuthPasswordRecovery = requestAuthPasswordRecovery
-- Recovers password with recovery code sent to email.
-- Works only when authGetState returns authStateWaitPassword.
-- Returns authStateOk on success
-- @recovery_code Recovery code to check
local function recoverAuthPassword(recovery_code, cb, cmd)
tdcli_function ({
ID = "RecoverAuthPassword",
recovery_code_ = recovery_code
}, cb or dl_cb, cmd)
end
M.recoverAuthPassword = recoverAuthPassword
-- Logs out user.
-- If force == false, begins to perform soft log out, returns authStateLoggingOut after completion.
-- If force == true then succeeds almost immediately without cleaning anything at the server, but returns error with code 401 and description "Unauthorized"
-- @force If true, just delete all local data. Session will remain in list of active sessions
local function resetAuth(force, cb, cmd)
tdcli_function ({
ID = "ResetAuth",
force_ = force or nil
}, cb or dl_cb, cmd)
end
M.resetAuth = resetAuth
-- Check bot's authentication token to log in as a bot.
-- Works only when authGetState returns authStateWaitPhoneNumber.
-- Can be used instead of setAuthPhoneNumber and checkAuthCode to log in.
-- Returns authStateOk on success
-- @token Bot token
local function checkAuthBotToken(token, cb, cmd)
tdcli_function ({
ID = "CheckAuthBotToken",
token_ = token
}, cb or dl_cb, cmd)
end
M.checkAuthBotToken = checkAuthBotToken
-- Returns current state of two-step verification
local function getPasswordState(dl_cb, cmd)
tdcli_function ({
ID = "GetPasswordState",
}, cb or dl_cb, cmd)
end
M.getPasswordState = getPasswordState
-- Changes user password.
-- If new recovery email is specified, then error EMAIL_UNCONFIRMED is returned and password change will not be applied until email confirmation.
-- Application should call getPasswordState from time to time to check if email is already confirmed
-- @old_password Old user password
-- @new_password New user password, may be empty to remove the password
-- @new_hint New password hint, can be empty
-- @set_recovery_email Pass True, if recovery email should be changed
-- @new_recovery_email New recovery email, may be empty
local function setPassword(old_password, new_password, new_hint, set_recovery_email, new_recovery_email, cb, cmd)
tdcli_function ({
ID = "SetPassword",
old_password_ = old_password,
new_password_ = new_password,
new_hint_ = new_hint,
set_recovery_email_ = set_recovery_email,
new_recovery_email_ = new_recovery_email
}, cb or dl_cb, cmd)
end
M.setPassword = setPassword
-- Returns set up recovery email.
-- This method can be used to verify a password provided by the user
-- @password Current user password
local function getRecoveryEmail(password, cb, cmd)
tdcli_function ({
ID = "GetRecoveryEmail",
password_ = password
}, cb or dl_cb, cmd)
end
M.getRecoveryEmail = getRecoveryEmail
-- Changes user recovery email.
-- If new recovery email is specified, then error EMAIL_UNCONFIRMED is returned and email will not be changed until email confirmation.
-- Application should call getPasswordState from time to time to check if email is already confirmed.
-- If new_recovery_email coincides with the current set up email succeeds immediately and aborts all other requests waiting for email confirmation
-- @password Current user password
-- @new_recovery_email New recovery email
local function setRecoveryEmail(password, new_recovery_email, cb, cmd)
tdcli_function ({
ID = "SetRecoveryEmail",
password_ = password,
new_recovery_email_ = new_recovery_email
}, cb or dl_cb, cmd)
end
M.setRecoveryEmail = setRecoveryEmail
-- Requests to send password recovery code to email
local function requestPasswordRecovery(dl_cb, cmd)
tdcli_function ({
ID = "RequestPasswordRecovery",
}, cb or dl_cb, cmd)
end
M.requestPasswordRecovery = requestPasswordRecovery
-- Recovers password with recovery code sent to email
-- @recovery_code Recovery code to check
local function recoverPassword(recovery_code, cb, cmd)
tdcli_function ({
ID = "RecoverPassword",
recovery_code_ = tostring(recovery_code)
}, cb or dl_cb, cmd)
end
M.recoverPassword = recoverPassword
-- Returns current logged in user
local function getMe(cb, cmd)
tdcli_function ({
ID = "GetMe",
}, cb or dl_cb, cmd)
end
M.getMe = getMe
-- Returns information about a user by its identifier, offline request if current user is not a bot
-- @user_id User identifier
local function getUser(user_id, cb, cmd)
tdcli_function ({
ID = "GetUser",
user_id_ = user_id
}, cb or dl_cb, cmd)
end
M.getUser = getUser
-- Returns full information about a user by its identifier
-- @user_id User identifier
local function getUserFull(user_id, cb, cmd)
tdcli_function ({
ID = "GetUserFull",
user_id_ = user_id
}, cb or dl_cb, cmd)
end
M.getUserFull = getUserFull
-- Returns information about a group by its identifier, offline request if current user is not a bot
-- @group_id Group identifier
local function getGroup(group_id, cb, cmd)
tdcli_function ({
ID = "GetGroup",
group_id_ = getChatId(group_id).ID
}, cb or dl_cb, cmd)
end
M.getGroup = getGroup
-- Returns full information about a group by its identifier
-- @group_id Group identifier
local function getGroupFull(group_id, cb, cmd)
tdcli_function ({
ID = "GetGroupFull",
group_id_ = getChatId(group_id).ID
}, cb or dl_cb, cmd)
end
M.getGroupFull = getGroupFull
-- Returns information about a channel by its identifier, offline request if current user is not a bot
-- @channel_id Channel identifier
local function getChannel(channel_id, cb, cmd)
tdcli_function ({
ID = "GetChannel",
channel_id_ = getChatId(channel_id).ID
}, cb or dl_cb, cmd)
end
M.getChannel = getChannel
-- Returns full information about a channel by its identifier, cached for at most 1 minute
-- @channel_id Channel identifier
local function getChannelFull(channel_id, cb, cmd)
tdcli_function ({
ID = "GetChannelFull",
channel_id_ = getChatId(channel_id).ID
}, cb or dl_cb, cmd)
end
M.getChannelFull = getChannelFull
-- Returns information about a secret chat by its identifier, offline request
-- @secret_chat_id Secret chat identifier
local function getSecretChat(secret_chat_id, cb, cmd)
tdcli_function ({
ID = "GetSecretChat",
secret_chat_id_ = secret_chat_id
}, cb or dl_cb, cmd)
end
M.getSecretChat = getSecretChat
-- Returns information about a chat by its identifier, offline request if current user is not a bot
-- @chat_id Chat identifier
local function getChat(chat_id, cb, cmd)
tdcli_function ({
ID = "GetChat",
chat_id_ = chat_id
}, cb or dl_cb, cmd)
end
M.getChat = getChat
-- Returns information about a message
-- @chat_id Identifier of the chat, message belongs to
-- @message_id Identifier of the message to get
local function getMessage(chat_id, message_id, cb, cmd)
tdcli_function ({
ID = "GetMessage",
chat_id_ = chat_id,
message_id_ = message_id
}, cb or dl_cb, cmd)
end
M.getMessage = getMessage
-- Returns information about messages.
-- If message is not found, returns null on the corresponding position of the result
-- @chat_id Identifier of the chat, messages belongs to
-- @message_ids Identifiers of the messages to get
local function getMessages(chat_id, message_ids, cb, cmd)
tdcli_function ({
ID = "GetMessages",
chat_id_ = chat_id,
message_ids_ = message_ids -- vector
}, cb or dl_cb, cmd)
end
M.getMessages = getMessages
-- Returns information about a file, offline request
-- @file_id Identifier of the file to get
local function getFile(file_id, cb, cmd)
tdcli_function ({
ID = "GetFile",
file_id_ = file_id
}, cb or dl_cb, cmd)
end
M.getFile = getFile
-- Returns information about a file by its persistent id, offline request
-- @persistent_file_id Persistent identifier of the file to get
local function getFilePersistent(persistent_file_id, cb, cmd)
tdcli_function ({
ID = "GetFilePersistent",
persistent_file_id_ = persistent_file_id
}, cb or dl_cb, cmd)
end
M.getFilePersistent = getFilePersistent
-- Returns list of chats in the right order, chats are sorted by (order, chat_id) in decreasing order.
-- For example, to get list of chats from the beginning, the offset_order should be equal 2^63 - 1
-- @offset_order Chat order to return chats from
-- @offset_chat_id Chat identifier to return chats from
-- @limit Maximum number of chats to be returned
local function getChats(offset_order, offset_chat_id, limit, cb, cmd)
if not limit or limit > 20 then
limit = 20
end
tdcli_function ({
ID = "GetChats",
offset_order_ = offset_order or 9223372036854775807,
offset_chat_id_ = offset_chat_id or 0,
limit_ = limit
}, cb or dl_cb, cmd)
end
M.getChats = getChats
-- Searches public chat by its username.
-- Currently only private and channel chats can be public.
-- Returns chat if found, otherwise some error is returned
-- @username Username to be resolved
local function resolve_username(username, cb, cmd)
tdcli_function ({
ID = "SearchPublicChat",
username_ = username
}, cb or dl_cb, cmd)
end
M.resolve_username= resolve_username
-- Searches public chats by prefix of their username.
-- Currently only private and channel (including supergroup) chats can be public.
-- Returns meaningful number of results.
-- Returns nothing if length of the searched username prefix is less than 5.
-- Excludes private chats with contacts from the results
-- @username_prefix Prefix of the username to search
local function searchPublicChats(username_prefix, cb, cmd)
tdcli_function ({
ID = "SearchPublicChats",
username_prefix_ = username_prefix
}, cb or dl_cb, cmd)
end
M.searchPublicChats = searchPublicChats
-- Searches for specified query in the title and username of known chats, offline request.
-- Returns chats in the order of them in the chat list
-- @query Query to search for, if query is empty, returns up to 20 recently found chats
-- @limit Maximum number of chats to be returned
local function searchChats(query, limit, cb, cmd)
if not limit or limit > 20 then
limit = 20
end
tdcli_function ({
ID = "SearchChats",
query_ = query,
limit_ = limit
}, cb or dl_cb, cmd)
end
M.searchChats = searchChats
-- Adds chat to the list of recently found chats.
-- The chat is added to the beginning of the list.
-- If the chat is already in the list, at first it is removed from the list
-- @chat_id Identifier of the chat to add
local function addRecentlyFoundChat(chat_id, cb, cmd)
tdcli_function ({
ID = "AddRecentlyFoundChat",
chat_id_ = chat_id
}, cb or dl_cb, cmd)
end
M.addRecentlyFoundChat = addRecentlyFoundChat
-- Deletes chat from the list of recently found chats
-- @chat_id Identifier of the chat to delete
local function deleteRecentlyFoundChat(chat_id, cb, cmd)
tdcli_function ({
ID = "DeleteRecentlyFoundChat",
chat_id_ = chat_id
}, cb or dl_cb, cmd)
end
M.deleteRecentlyFoundChat = deleteRecentlyFoundChat
-- Clears list of recently found chats
local function deleteRecentlyFoundChats(dl_cb, cmd)
tdcli_function ({
ID = "DeleteRecentlyFoundChats",
}, cb or dl_cb, cmd)
end
M.deleteRecentlyFoundChats = deleteRecentlyFoundChats
-- Returns list of common chats with an other given user.
-- Chats are sorted by their type and creation date
-- @user_id User identifier
-- @offset_chat_id Chat identifier to return chats from, use 0 for the first request
-- @limit Maximum number of chats to be returned, up to 100
local function getCommonChats(user_id, offset_chat_id, limit, cb, cmd)
if not limit or limit > 100 then
limit = 100
end
tdcli_function ({
ID = "GetCommonChats",
user_id_ = user_id,
offset_chat_id_ = offset_chat_id,
limit_ = limit
}, cb or dl_cb, cmd)
end
M.getCommonChats = getCommonChats
-- Returns messages in a chat.
-- Automatically calls openChat.
-- Returns result in reverse chronological order, i.e. in order of decreasing message.message_id
-- @chat_id Chat identifier
-- @from_message_id Identifier of the message near which we need a history, you can use 0 to get results from the beginning, i.e. from oldest to newest
-- @offset Specify 0 to get results exactly from from_message_id or negative offset to get specified message and some newer messages
-- @limit Maximum number of messages to be returned, should be positive and can't be greater than 100.
-- If offset is negative, limit must be greater than -offset.
-- There may be less than limit messages returned even the end of the history is not reached
local function getChatHistory(chat_id, from_message_id, offset, limit, cb, cmd)
if not limit or limit > 100 then
limit = 100
end
tdcli_function ({
ID = "GetChatHistory",
chat_id_ = chat_id,
from_message_id_ = from_message_id,
offset_ = offset or 0,
limit_ = limit
}, cb or dl_cb, cmd)
end
M.getChatHistory = getChatHistory
-- Deletes all messages in the chat.
-- Can't be used for channel chats
-- @chat_id Chat identifier
-- @remove_from_chat_list Pass true, if chat should be removed from the chat list
local function deleteChatHistory(chat_id, remove_from_chat_list, cb, cmd)
tdcli_function ({
ID = "DeleteChatHistory",
chat_id_ = chat_id,
remove_from_chat_list_ = remove_from_chat_list
}, cb or dl_cb, cmd)
end
M.deleteChatHistory = deleteChatHistory
-- Searches for messages with given words in the chat.
-- Returns result in reverse chronological order, i. e. in order of decreasimg message_id.
-- Doesn't work in secret chats
-- @chat_id Chat identifier to search in
-- @query Query to search for
-- @from_message_id Identifier of the message from which we need a history, you can use 0 to get results from beginning
-- @limit Maximum number of messages to be returned, can't be greater than 100
-- @filter Filter for content of searched messages
-- filter = Empty|Animation|Audio|Document|Photo|Video|Voice|PhotoAndVideo|Url|ChatPhoto
local function searchChatMessages(chat_id, query, from_message_id, limit, filter, cb, cmd)
if not limit or limit > 100 then
limit = 100
end
tdcli_function ({
ID = "SearchChatMessages",
chat_id_ = chat_id,
query_ = query,
from_message_id_ = from_message_id,
limit_ = limit,
filter_ = {
ID = 'SearchMessagesFilter' .. filter
},
}, cb or dl_cb, cmd)
end
M.searchChatMessages = searchChatMessages
-- Searches for messages in all chats except secret chats. Returns result in reverse chronological order, i. e. in order of decreasing (date, chat_id, message_id)
-- @query Query to search for
-- @offset_date Date of the message to search from, you can use 0 or any date in the future to get results from the beginning
-- @offset_chat_id Chat identifier of the last found message or 0 for the first request
-- @offset_message_id Message identifier of the last found message or 0 for the first request
-- @limit Maximum number of messages to be returned, can't be greater than 100
local function searchMessages(query, offset_date, offset_chat_id, offset_message_id, limit, cb, cmd)
if not limit or limit > 100 then
limit = 100
end
tdcli_function ({
ID = "SearchMessages",
query_ = query,
offset_date_ = offset_date,
offset_chat_id_ = offset_chat_id,
offset_message_id_ = offset_message_id,
limit_ = limit
}, cb or dl_cb, cmd)
end
M.searchMessages = searchMessages
-- Invites bot to a chat (if it is not in the chat) and send /start to it.
-- Bot can't be invited to a private chat other than chat with the bot.
-- Bots can't be invited to broadcast channel chats and secret chats.
-- Returns sent message.
-- UpdateChatTopMessage will not be sent, so returned message should be used to update chat top message
-- @bot_user_id Identifier of the bot
-- @chat_id Identifier of the chat
-- @parameter Hidden parameter sent to bot for deep linking (https://api.telegram.org/bots#deep-linking)
-- parameter=start|startgroup or custom as defined by bot creator
local function sendBotStartMessage(bot_user_id, chat_id, parameter, cb, cmd)
tdcli_function ({
ID = "SendBotStartMessage",
bot_user_id_ = bot_user_id,
chat_id_ = chat_id,
parameter_ = parameter
}, cb or dl_cb, cmd)
end
M.sendBotStartMessage = sendBotStartMessage
-- Sends result of the inline query as a message.
-- Returns sent message.
-- UpdateChatTopMessage will not be sent, so returned message should be used to update chat top message.
-- Always clears chat draft message
-- @chat_id Chat to send message
-- @reply_to_message_id Identifier of a message to reply to or 0
-- @disable_notification Pass true, to disable notification about the message, doesn't works in secret chats
-- @from_background Pass true, if the message is sent from background
-- @query_id Identifier of the inline query
-- @result_id Identifier of the inline result
local function sendInlineQueryResultMessage(chat_id, reply_to_message_id, disable_notification, from_background, query_id, result_id, cb, cmd)
tdcli_function ({
ID = "SendInlineQueryResultMessage",
chat_id_ = chat_id,
reply_to_message_id_ = reply_to_message_id,
disable_notification_ = disable_notification,
from_background_ = from_background,
query_id_ = query_id,
result_id_ = result_id
}, cb or dl_cb, cmd)
end
M.sendInlineQueryResultMessage = sendInlineQueryResultMessage
-- Forwards previously sent messages.
-- Returns forwarded messages in the same order as message identifiers passed in message_ids.
-- If message can't be forwarded, null will be returned instead of the message.
-- UpdateChatTopMessage will not be sent, so returned messages should be used to update chat top message
-- @chat_id Identifier of a chat to forward messages
-- @from_chat_id Identifier of a chat to forward from
-- @message_ids Identifiers of messages to forward
-- @disable_notification Pass true, to disable notification about the message, doesn't works if messages are forwarded to secret chat
-- @from_background Pass true, if the message is sent from background
local function forwardMessages(chat_id, from_chat_id, message_ids, disable_notification, cb, cmd)
tdcli_function ({
ID = "ForwardMessages",
chat_id_ = chat_id,
from_chat_id_ = from_chat_id,
message_ids_ = message_ids, -- vector
disable_notification_ = disable_notification,
from_background_ = 1
}, cb or dl_cb, cmd)
end
M.forwardMessages = forwardMessages
-- Changes current ttl setting in a secret chat and sends corresponding message
-- @chat_id Chat identifier
-- @ttl New value of ttl in seconds
local function sendChatSetTtlMessage(chat_id, ttl, cb, cmd)
tdcli_function ({
ID = "SendChatSetTtlMessage",
chat_id_ = chat_id,
ttl_ = ttl
}, cb or dl_cb, cmd)
end
M.sendChatSetTtlMessage = sendChatSetTtlMessage
-- Deletes messages.
-- UpdateDeleteMessages will not be sent for messages deleted through that function
-- @chat_id Chat identifier
-- @message_ids Identifiers of messages to delete
local function deleteMessages(chat_id, message_ids, cb, cmd)
tdcli_function ({
ID = "DeleteMessages",
chat_id_ = chat_id,
message_ids_ = message_ids -- vector
}, cb or dl_cb, cmd)
end
M.deleteMessages = deleteMessages
-- Deletes all messages in the chat sent by the specified user.
-- Works only in supergroup channel chats, needs appropriate privileges
-- @chat_id Chat identifier
-- @user_id User identifier
local function deleteMessagesFromUser(chat_id, user_id, cb, cmd)
tdcli_function ({
ID = "DeleteMessagesFromUser",
chat_id_ = chat_id,
user_id_ = user_id
}, cb or dl_cb, cmd)
end
M.deleteMessagesFromUser = deleteMessagesFromUser
-- Edits text of text or game message.
-- Non-bots can edit message in a limited period of time.
-- Returns edited message after edit is complete server side
-- @chat_id Chat the message belongs to
-- @message_id Identifier of the message
-- @reply_markup Bots only. New message reply markup
-- @input_message_content New text content of the message. Should be of type InputMessageText
local function editMessageText(chat_id, message_id, reply_markup, text, disable_web_page_preview, parse_mode, cb, cmd)
local TextParseMode = getParseMode(parse_mode)
tdcli_function ({
ID = "EditMessageText",
chat_id_ = chat_id,
message_id_ = message_id,
reply_markup_ = reply_markup, -- reply_markup:ReplyMarkup
input_message_content_ = {
ID = "InputMessageText",
text_ = text,
disable_web_page_preview_ = disable_web_page_preview,
clear_draft_ = 0,
entities_ = {},
parse_mode_ = TextParseMode,
},
}, cb or dl_cb, cmd)
end
M.editMessageText = editMessageText
-- Edits message content caption.
-- Non-bots can edit message in a limited period of time.
-- Returns edited message after edit is complete server side
-- @chat_id Chat the message belongs to
-- @message_id Identifier of the message
-- @reply_markup Bots only. New message reply markup
-- @caption New message content caption, 0-200 characters
local function editMessageCaption(chat_id, message_id, reply_markup, caption, cb, cmd)
tdcli_function ({
ID = "EditMessageCaption",
chat_id_ = chat_id,
message_id_ = message_id,
reply_markup_ = reply_markup, -- reply_markup:ReplyMarkup
caption_ = caption
}, cb or dl_cb, cmd)
end
M.editMessageCaption = editMessageCaption
-- Bots only.
-- Edits message reply markup.
-- Returns edited message after edit is complete server side
-- @chat_id Chat the message belongs to
-- @message_id Identifier of the message
-- @reply_markup New message reply markup
local function editMessageReplyMarkup(inline_message_id, reply_markup, caption, cb, cmd)
tdcli_function ({
ID = "EditInlineMessageCaption",
inline_message_id_ = inline_message_id,
reply_markup_ = reply_markup, -- reply_markup:ReplyMarkup
caption_ = caption
}, cb or dl_cb, cmd)
end
M.editMessageReplyMarkup = editMessageReplyMarkup
-- Bots only.
-- Edits text of an inline text or game message sent via bot
-- @inline_message_id Inline message identifier
-- @reply_markup New message reply markup
-- @input_message_content New text content of the message. Should be of type InputMessageText
local function editInlineMessageText(inline_message_id, reply_markup, text, disable_web_page_preview, cb, cmd)
tdcli_function ({
ID = "EditInlineMessageText",
inline_message_id_ = inline_message_id,
reply_markup_ = reply_markup, -- reply_markup:ReplyMarkup
input_message_content_ = {
ID = "InputMessageText",
text_ = text,
disable_web_page_preview_ = disable_web_page_preview,
clear_draft_ = 0,
entities_ = {}
},
}, cb or dl_cb, cmd)
end
M.editInlineMessageText = editInlineMessageText
-- Bots only.
-- Edits caption of an inline message content sent via bot
-- @inline_message_id Inline message identifier
-- @reply_markup New message reply markup
-- @caption New message content caption, 0-200 characters
local function editInlineMessageCaption(inline_message_id, reply_markup, caption, cb, cmd)
tdcli_function ({
ID = "EditInlineMessageCaption",
inline_message_id_ = inline_message_id,
reply_markup_ = reply_markup, -- reply_markup:ReplyMarkup
caption_ = caption
}, cb or dl_cb, cmd)
end
M.editInlineMessageCaption = editInlineMessageCaption
-- Bots only.
-- Edits reply markup of an inline message sent via bot
-- @inline_message_id Inline message identifier
-- @reply_markup New message reply markup
local function editInlineMessageReplyMarkup(inline_message_id, reply_markup, cb, cmd)
tdcli_function ({
ID = "EditInlineMessageReplyMarkup",
inline_message_id_ = inline_message_id,
reply_markup_ = reply_markup -- reply_markup:ReplyMarkup
}, cb or dl_cb, cmd)
end
M.editInlineMessageReplyMarkup = editInlineMessageReplyMarkup
-- Sends inline query to a bot and returns its results.
-- Unavailable for bots
-- @bot_user_id Identifier of the bot send query to
-- @chat_id Identifier of the chat, where the query is sent
-- @user_location User location, only if needed
-- @query Text of the query
-- @offset Offset of the first entry to return
local function getInlineQueryResults(bot_user_id, chat_id, latitude, longitude, query, offset, cb, cmd)
tdcli_function ({
ID = "GetInlineQueryResults",
bot_user_id_ = bot_user_id,
chat_id_ = chat_id,
user_location_ = {
ID = "Location",
latitude_ = latitude,
longitude_ = longitude
},
query_ = query,
offset_ = offset
}, cb or dl_cb, cmd)
end
M.getInlineQueryResults = getInlineQueryResults
-- Bots only.
-- Sets result of the inline query
-- @inline_query_id Identifier of the inline query
-- @is_personal Does result of the query can be cached only for specified user
-- @results Results of the query
-- @cache_time Allowed time to cache results of the query in seconds
-- @next_offset Offset for the next inline query, pass empty string if there is no more results
-- @switch_pm_text If non-empty, this text should be shown on the button, which opens private chat with the bot and sends bot start message with parameter switch_pm_parameter
-- @switch_pm_parameter Parameter for the bot start message
local function answerInlineQuery(inline_query_id, is_personal, cache_time, next_offset, switch_pm_text, switch_pm_parameter, cb, cmd)
tdcli_function ({
ID = "AnswerInlineQuery",
inline_query_id_ = inline_query_id,
is_personal_ = is_personal,
results_ = results, --vector<InputInlineQueryResult>,
cache_time_ = cache_time,
next_offset_ = next_offset,
switch_pm_text_ = switch_pm_text,
switch_pm_parameter_ = switch_pm_parameter
}, cb or dl_cb, cmd)
end
M.answerInlineQuery = answerInlineQuery
-- Sends callback query to a bot and returns answer to it.
-- Unavailable for bots
-- @chat_id Identifier of the chat with a message
-- @message_id Identifier of the message, from which the query is originated
-- @payload Query payload
-- @text Text of the answer
-- @show_alert If true, an alert should be shown to the user instead of a toast
-- @url URL to be open
local function getCallbackQueryAnswer(chat_id, message_id, text, show_alert, url, cb, cmd)
tdcli_function ({
ID = "GetCallbackQueryAnswer",
chat_id_ = chat_id,
message_id_ = message_id,
payload_ = {
ID = "CallbackQueryAnswer",
text_ = text,
show_alert_ = show_alert,
url_ = url
},
}, cb or dl_cb, cmd)
end
M.getCallbackQueryAnswer = getCallbackQueryAnswer
-- Bots only.
-- Sets result of the callback query
-- @callback_query_id Identifier of the callback query
-- @text Text of the answer
-- @show_alert If true, an alert should be shown to the user instead of a toast
-- @url Url to be opened
-- @cache_time Allowed time to cache result of the query in seconds
local function answerCallbackQuery(callback_query_id, text, show_alert, url, cache_time, cb, cmd)
tdcli_function ({
ID = "AnswerCallbackQuery",
callback_query_id_ = callback_query_id,
text_ = text,
show_alert_ = show_alert,
url_ = url,
cache_time_ = cache_time
}, cb or dl_cb, cmd)
end
M.answerCallbackQuery = answerCallbackQuery
-- Bots only.
-- Updates game score of the specified user in the game
-- @chat_id Chat a message with the game belongs to
-- @message_id Identifier of the message
-- @edit_message True, if message should be edited
-- @user_id User identifier
-- @score New score
-- @force Pass True to update the score even if it decreases. If score is 0, user will be deleted from the high scores table
local function setGameScore(chat_id, message_id, edit_message, user_id, score, force, cb, cmd)
tdcli_function ({
ID = "SetGameScore",
chat_id_ = chat_id,
message_id_ = message_id,
edit_message_ = edit_message,
user_id_ = user_id,
score_ = score,
force_ = force
}, cb or dl_cb, cmd)
end
M.setGameScore = setGameScore
-- Bots only.
-- Updates game score of the specified user in the game
-- @inline_message_id Inline message identifier
-- @edit_message True, if message should be edited
-- @user_id User identifier
-- @score New score
-- @force Pass True to update the score even if it decreases. If score is 0, user will be deleted from the high scores table
local function setInlineGameScore(inline_message_id, edit_message, user_id, score, force, cb, cmd)
tdcli_function ({
ID = "SetInlineGameScore",
inline_message_id_ = inline_message_id,
edit_message_ = edit_message,
user_id_ = user_id,
score_ = score,
force_ = force
}, cb or dl_cb, cmd)
end
M.setInlineGameScore = setInlineGameScore
-- Bots only.
-- Returns game high scores and some part of the score table around of the specified user in the game
-- @chat_id Chat a message with the game belongs to