-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathtelega-tdlib.el
3224 lines (2808 loc) · 115 KB
/
telega-tdlib.el
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
;;; telega-tdlib.el --- TDLib API interface -*- lexical-binding:t -*-
;; Copyright (C) 2019-2020 by Zajcev Evgeny.
;; Author: Zajcev Evgeny <[email protected]>
;; Created: Tue Sep 17 15:01:21 2019
;; Keywords:
;; telega 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 3 of the License, or
;; (at your option) any later version.
;; telega 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 telega. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Emacs lisp interface to TDLib API v1.7.0
;;; Code:
(require 'telega-core)
(require 'telega-server)
(declare-function telega-chat-get "telega-chat" (chat-id &optional offline-p))
(declare-function telega-chat--ensure "telega-chat" (chat))
(declare-function telega-chatbuf--message-thread-id "telega-chat")
(declare-function telega-stickerset--ensure "telega-sticker" (sset))
(declare-function telega-user-get "telega-user" (user-id))
(declare-function telega-file--ensure "telega-media" (file))
(defvar telega-version)
(defvar telega-app)
(defmacro with-telega-server-reply (reply post-form call-sexp &optional callback)
"Do sync or async call to telega-server, processing REPLY by POST-FORM.
CALL-SEXP and CALLBACK are passed directly to `telega-server--call'."
(declare (indent 2))
(let ((reply-sym (gensym "reply"))
(reply-var (car reply)))
`(let ((,reply-var (telega-server--call
,call-sexp
(when ,callback
(lambda (,reply-sym)
(let ((,reply-var ,reply-sym))
(funcall ,callback ,post-form)))))))
(if ,callback
,reply-var
,post-form))))
(defun telega--MessageSender (msg-sender)
"Convert user or chat to TDLib MessageSender."
(if (telega-user-p msg-sender)
(list :@type "messageSenderUser"
:user_id (plist-get msg-sender :id))
(cl-assert (telega-chat-p msg-sender))
(list :@type "messageSenderChat"
:chat_id (plist-get msg-sender :id))))
(defun telega--getOption (prop-kw &optional callback)
(declare (indent 1))
(telega-server--call
(list :@type "getOption"
:name (substring (symbol-name prop-kw) 1)) ; strip `:'
callback))
(defun telega--setOption (prop-kw val &optional sync-p)
"Set option, defined by keyword PROP-KW to VAL.
If SYNC-P is specified, then set option is sync manner."
(declare (indent 1))
(funcall (if sync-p #'telega-server--call #'telega-server--send)
(list :@type "setOption"
:name (substring (symbol-name prop-kw) 1) ; strip `:'
:value (list :@type (cond ((memq val '(t nil :false))
"optionValueBoolean")
((integerp val)
"optionValueInteger")
((stringp val)
"optionValueString")
(t (error "Unknown value type: %S"
(type-of val))))
:value (or val :false)))))
(defun telega--addProxy (proxy-spec &optional callback)
"Add PROXY-SPEC to the list of proxies."
(telega-server--call
`(:@type "addProxy" ,@proxy-spec)
(or callback #'ignore)))
(defun telega--enableProxy (proxy &optional callback)
(declare (indent 1))
(telega-server--call
(list :@type "enableProxy"
:proxy_id (plist-get proxy :id))
(or callback #'ignore)))
(defun telega--disableProxy (&optional callback)
(declare (indent 1))
(telega-server--call
(list :@type "disableProxy")
(or callback #'ignore)))
(defun telega--pingProxy (proxy &optional callback)
"Get time needed to receive a response from a Telegram server through a PROXY.
If PROXY is nil, then ping a Telegram server without a proxy."
(declare (indent 1))
(telega-server--call
(list :@type "pingProxy"
:proxy_id (or (plist-get proxy :id) 0))
callback))
(defun telega--searchEmojis (text &optional exact-match-p
language-codes callback)
"Search for emojis by TEXT keywords.
Non-nil EXACT-MATCH-P to return only emojis that exactly matches TEXT."
(with-telega-server-reply (reply)
(plist-get reply :emoji_keywords)
(list :@type "searchEmojis"
:text text
:exact_match (or exact-match-p :false)
:input_language_codes (apply #'vector language-codes))
callback))
(defun telega--getAnimatedEmoji (emoji &optional callback)
"Return an animated emoji corresponding to a given EMOJI."
(telega-server--call
(list :@type "getAnimatedEmoji"
:emoji emoji)
callback))
(defun telega--getCustomEmojiStickers (custom-emoji-ids &optional callback)
"Returns list of custom emoji stickers by their identifiers."
(declare (indent 1))
(with-telega-server-reply (reply)
(append (plist-get reply :stickers) nil)
(list :@type "getCustomEmojiStickers"
:custom_emoji_ids (apply #'vector custom-emoji-ids))
callback))
(defun telega--setChatTitle (chat title)
"Changes the CHAT title to TITLE."
(telega-server--send
(list :@type "setChatTitle"
:chat_id (plist-get chat :id)
:title title)))
(defun telega--toggleChatDefaultDisableNotification (chat disable-p)
(telega-server--send
(list :@type "toggleChatDefaultDisableNotification"
:chat_id (plist-get chat :id)
:default_disable_notification (if disable-p t :false))))
(defun telega--setChatDescription (chat descr)
"Set CHAT's description to DESCR."
(telega-server--send
(list :@type "setChatDescription"
:chat_id (plist-get chat :id)
:description (or descr ""))))
(defun telega--createNewBasicGroupChat (title users &optional callback)
"Create new basicgroup with TITLE and USERS.
Return (or call the CALLBACK with) newly created chat."
(with-telega-server-reply (reply)
(telega-chat-get (plist-get reply :id))
(list :@type "createNewBasicGroupChat"
:user_ids (cl-map #'vector (telega--tl-prop :id) users)
:title title)
callback))
(cl-defun telega--createNewSupergroupChat (title &key forum-p channel-p
description location callback)
"Create new supergroup with TITLE.
Specify non-nil CHANNEL-P to create new channel.
Specify LOCATION to create location-based supergroup.
Return (or call the CALLBACK with) newly created chat."
(declare (indent 1))
(with-telega-server-reply (reply)
(telega-chat-get (plist-get reply :id))
(nconc (list :@type "createNewSupergroupChat"
:title title
:is_forum (if forum-p t :false)
:is_channel (if channel-p t :false))
(when description
(list :description description))
(when location
(list :location location)))
callback))
(defun telega--createNewSecretChat (user)
"Create secret chat with USER.
Return newly created chat."
(telega-chat-get
(plist-get
(telega-server--call
(list :@type "createNewSecretChat"
:user_id (plist-get user :id))) :id)))
(defun telega--createBasicGroupChat (basic-group-id &optional force)
"Return an existing chat corresponding to a known basicgroup.
BASIC-GROUP-ID is the id of the basicgroup."
(telega-chat-get
(plist-get
(telega-server--call
(list :@type "createBasicGroupChat"
:basic_group_id basic-group-id
:force (if force t :false)))
:id)))
(defun telega--upgradeBasicGroupChatToSupergroupChat (chat &optional callback)
"Creates a new supergroup from an existing basic.
Requires creator privileges.
Deactivates the original basic group.
Return (or call the CALLBACK with) newly create chat."
(with-telega-server-reply (reply)
(telega-chat-get (plist-get reply :id))
(list :@type "upgradeBasicGroupChatToSupergroupChat"
:chat_id (plist-get chat :id))
callback))
(defun telega--createSupergroupChat (supergroup-id &optional force)
"Create chat for SUPERGROUP-ID."
(telega-chat-get
(plist-get
(telega-server--call
(list :@type "createSupergroupChat"
:supergroup_id supergroup-id
:force (if force t :false)))
:id)))
(defun telega--setSupergroupUsername (supergroup username)
"Change SUPERGROUP's username to USERNAME.
Requires owner right."
(telega-server--send
(list :@type "setSupergroupUsername"
:supergroup_id (plist-get supergroup :id)
:username username)))
(defun telega--toggleSupergroupSignMessages (supergroup sign-messages-p)
(telega-server--send
(list :@type "toggleSupergroupSignMessages"
:supergroup_id (plist-get supergroup :id)
:sign_messages (if sign-messages-p t :false))))
(defun telega--toggleSupergroupJoinByRequest (supergroup join-by-request-p)
(telega-server--send
(list :@type "toggleSupergroupJoinByRequest"
:supergroup_id (plist-get supergroup :id)
:join_by_request (if join-by-request-p t :false))))
(defun telega--toggleSupergroupIsAllHistoryAvailable (supergroup all-history-available-p)
(telega-server--send
(list :@type "toggleSupergroupIsAllHistoryAvailable"
:supergroup_id (plist-get supergroup :id)
:is_all_history_available (if all-history-available-p t :false))))
(defun telega--reportSupergroupSpam (supergroup msg &rest other-messages)
"Report some messages in a supergroup as spam.
all messages must have same user sender."
(telega-server--send
(list :@type "reportSupergroupSpam"
:supergroup_id (plist-get supergroup :id)
:message_ids (cl-map #'vector (telega--tl-prop :id)
(cons msg other-messages)))))
(defun telega--createSecretChat (secret-chat-id)
"Return existing secret chat with id equal to SECRET-CHAT-ID."
(telega-chat-get
(plist-get
(telega-server--call
(list :@type "createSecretChat"
:secret_chat_id secret-chat-id))
:id)))
(defun telega--closeSecretChat (secretchat)
"Close SECRETCHAT."
(telega-server--send
(list :@type "closeSecretChat"
:secret_chat_id (plist-get secretchat :id))))
(defun telega-chat-message-thread-id (chat &optional only-if-topic-p for-send-p)
"Return current message_thread_id for the CHAT.
If ONLY-IF-TOPIC-P is specified, then return thread id only if topic
is enabled."
(or (with-telega-chatbuf chat
(telega-chatbuf--message-thread-id only-if-topic-p for-send-p))
0))
(defun telega--sendChatAction (chat action)
"Send ACTION on CHAT."
(telega-server--send
(list :@type "sendChatAction"
:chat_id (plist-get chat :id)
:message_thread_id (telega-chat-message-thread-id chat)
:action action)))
(defun telega--getMessageEmbeddingCode (msg &optional for-album-p)
"Returns an HTML code for embedding the message MSG."
;; TDLib: Available only for messages in supergroups and channels
;; with a username
(plist-get
(telega-server--call
(list :@type "getMessageEmbeddingCode"
:chat_id (plist-get msg :chat_id)
:message_id (plist-get msg :id)
:for_album (if for-album-p t :false)))
:text))
(defun telega--getMessage (chat-id msg-id &optional callback)
"Get message by CHAT-ID and MSG-ID.
If CALLBACK is specified, then get message asynchronously.
If message is not found, then return `nil'."
(declare (indent 2))
(with-telega-server-reply (reply)
(unless (telega--tl-error-p reply)
reply)
(list :@type "getMessage"
:chat_id chat-id
:message_id msg-id)
callback))
(defun telega--getRepliedMessage (msg &optional callback)
"Returns information about a message that is replied by a given message.
Also returns the pinned message, the game message, and the invoice
message for messages of the types messagePinMessage, messageGameScore,
and messagePaymentSuccessful respectively."
(declare (indent 1))
(telega-server--call
(list :@type "getRepliedMessage"
:chat_id (plist-get msg :chat_id)
:message_id (plist-get msg :id))
callback))
(defun telega--getChatMessageByDate (chat-id date &optional callback)
"Returns the last message sent in a chat no later than the specified DATE.
DATE is a unix timestamp."
(declare (indent 2))
(with-telega-server-reply (reply)
(unless (telega--tl-error-p reply)
reply)
(list :@type "getChatMessageByDate"
:chat_id chat-id
:date date)
callback))
(defun telega--getMessages (chat-id message-ids &optional callback)
"Get messages by CHAT-ID and MESSAGE-IDS."
(declare (indent 2))
(with-telega-server-reply (reply)
;; Filter out nil messages
;; See https://github.com/tdlib/td/issues/1511
(delq nil (append (plist-get reply :messages) nil))
(list :@type "getMessages"
:chat_id chat-id
:message_ids (apply #'vector message-ids))
callback))
(defun telega--getMessageProperties (msg &optional callback)
"Return properties of a message."
(declare (indent 1))
(telega-server--call
(list :@type "getMessageProperties"
:chat_id (plist-get msg :chat_id)
:message_id (plist-get msg :id))
callback))
(cl-defun telega--getMessageLink (msg &key for-album-p for-thread-p
media-timestamp)
"Get https link for message MSG in a supergroup or a channel."
(declare (indent 1))
(plist-get
(telega-server--call
(list :@type "getMessageLink"
:chat_id (plist-get msg :chat_id)
:message_id (plist-get msg :id)
:media_timestamp (or media-timestamp 0)
:for_album (if for-album-p t :false)
:in_message_thread (if for-thread-p t :false)))
:link))
(defun telega--recognizeSpeech (msg)
"Recognize speech in a voice note message MSG."
(telega-server--send
(list :@type "recognizeSpeech"
:chat_id (plist-get msg :chat_id)
:message_id (plist-get msg :id))))
(defun telega--replacePrimaryChatInviteLink (chat)
"Generate a new primary invite link for a CHAT.
Available for basic groups, supergroups, and channels.
Return generated link as string."
(plist-get
(telega-server--call
(list :@type "replacePrimaryChatInviteLink"
:chat_id (plist-get chat :id)))
:invite_link))
(defun telega--checkChatInviteLink (invite-link &optional callback)
"Check invitation link INVITE-LINK."
(telega-server--call
(list :@type "checkChatInviteLink"
:invite_link invite-link)
callback))
(defun telega--joinChat (chat)
"Add current user as a new member to a CHAT."
(telega-server--send
(list :@type "joinChat" :chat_id (plist-get chat :id))))
(defun telega--joinChatByInviteLink (invite-link &optional callback)
"Return new chat by its INVITE-LINK.
Return nil if can't join the chat."
(with-telega-server-reply (reply)
(telega-chat-get (plist-get reply :id))
(list :@type "joinChatByInviteLink"
:invite_link invite-link)
callback))
(defun telega--leaveChat (chat)
"Remove current user from CHAT members."
(telega-server--send
(list :@type "leaveChat" :chat_id (plist-get chat :id))))
(defun telega--getChatPinnedMessage (chat &optional callback)
"Get newest pinned message for the CHAT, if any."
(telega-server--call
(list :@type "getChatPinnedMessage"
:chat_id (plist-get chat :id))
callback))
(defun telega--getChatEventLog (chat &optional query from-event-id
limit filters users callback)
"Return event log for the CHAT.
FILTERS are created with `telega-chatevent-log-filter'."
(with-telega-server-reply (reply)
(append (plist-get reply :events) nil)
(nconc (list :@type "getChatEventLog"
:chat_id (plist-get chat :id)
:from_event_id (or from-event-id 0)
:limit (or limit 100))
(when query
(list :query query))
(when filters
(list :filters filters))
(when users
(list :user_ids
(cl-map 'vector (telega--tl-prop :id) users))))
callback))
(defun telega--getChatAvailableMessageSenders (chat &optional callback)
"Return message senders available for the CHAT."
(with-telega-server-reply (reply)
(mapcar #'telega-msg-sender (plist-get reply :senders))
(list :@type "getChatAvailableMessageSenders"
:chat_id (plist-get chat :id))
callback))
(defun telega--setChatMessageSender (chat sender)
"For CHAT set default message SENDER."
(telega-server--send
(list :@type "setChatMessageSender"
:chat_id (plist-get chat :id)
:message_sender_id (telega--MessageSender sender))))
(defun telega--getPaymentForm (invoice)
"Return a payment form for an INVOICE.
INVOICE could be a message or a name from the `internalLinkTypeInvoice' link.
TDLib 1.8.4"
(telega-server--call
(list :@type "getPaymentForm"
:input_invoice
(cond ((telega-msg-p invoice)
(list :@type "inputInvoiceMessage"
:chat_id (plist-get invoice :chat_id)
:message_id (plist-get invoice :id)))
((stringp invoice)
(list :@type "inputInvoiceName"
:name invoice))
(t (error "telega: invalid invoice: %S" invoice))))))
(defun telega--sendPaymentForm (msg order-info-id shipping-id credentials)
(telega-server--call
(list :@type "sendPaymentForm"
:chat_id (plist-get msg :chat_id)
:message_id (plist-get msg :id)
:order_info_id (or order-info-id "")
:shipping_option_id (or shipping-id "")
:credentials credentials)))
(defun telega--getCreatedPublicChats (chat-type &optional callback)
"Return list of public chats created by the user.
CHAT-TYPE is one of `has-username' or `location-based'."
(with-telega-server-reply (reply)
(mapcar #'telega-chat-get (append (plist-get reply :chat_ids) nil))
(list :@type "getCreatedPublicChats"
:type (cl-ecase chat-type
(has-username
(list :@type "publicChatTypeHasUsername"))
(location-based
(list :@type "publicChatTypeIsLocationBased"))))
callback))
(defun telega--getInactiveSupergroupChats (&optional callback)
"Return a list of recently inactive supergroups and channels.
Can be used when user reaches limit on the number of joined
supergroups and channels and receives CHANNELS_TOO_MUCH error."
(with-telega-server-reply (reply)
(mapcar #'telega-chat-get (plist-get reply :chat_ids))
(list :@type "getInactiveSupergroupChats")
callback))
(defun telega--getSuitableDiscussionChats (&optional callback)
"Returns a list of chats suitable to be discussion group for a channel."
(with-telega-server-reply (reply)
(mapcar #'telega-chat-get (plist-get reply :chat_ids))
(list :@type "getSuitableDiscussionChats")
callback))
(defun telega--setMessageSenderBlockList (msg-sender block-list
&optional callback)
"Set Toggle block state of a CHAT."
(telega-server--call
(list :@type "setMessageSenderBlockList"
:sender_id (telega--MessageSender msg-sender)
:block_list (when block-list
(list :@type (symbol-name block-list))))
(or callback #'ignore)))
(defun telega--getBlockedMessageSenders (block-list &optional offset callback)
"Get list of chats blocked by me.
BLOCK-LIST is one of `blockListMain' or `blockListStories'.
First element is the list is total number of blocked message senders."
(declare (indent 2))
(with-telega-server-reply (reply)
(nconc (list block-list (plist-get reply :total_count))
(mapcar #'telega-msg-sender (plist-get reply :senders)))
(list :@type "getBlockedMessageSenders"
:block_list (list :@type (symbol-name block-list))
:offset (or offset 0)
:limit 100)
callback))
(defun telega--blockMessageSenderFromReplies
(msg delete-msg-p &optional delete-all-msg-p report-spam-p)
"Block an original sender of a message in the Replies chat."
(telega-server--send
(list :@type "blockMessageSenderFromReplies"
:message-id (plist-get msg :id)
:delete_message (if delete-msg-p t :false)
:delete_all_messages (if delete-all-msg-p t :false)
:report_spam (if report-spam-p t :false))))
(cl-defun telega--getStickers
(query &key chat (limit 100)
(tl-sticker-type '(:@type "stickerTypeRegular")) callback)
"Return installed stickers that correspond to a given QUERY.
QUERY could be an emoji or a keyword prefix.
LIMIT defaults to 20."
(declare (indent 1))
(with-telega-server-reply (reply)
(append (plist-get reply :stickers) nil)
(nconc (list :@type "getStickers"
:sticker_type tl-sticker-type
:query query
:limit limit)
(when chat
(list :chat_id (plist-get chat :id))))
callback))
(cl-defun telega--getAllStickerEmojis (query &key chat
(tl-sticker-type
'(:@type "stickerTypeRegular"))
only-main-p
callback)
"Return unique emoji that correspond to installed stickers."
(declare (indent 1))
(with-telega-server-reply (reply)
(append (plist-get reply :stickers) nil)
(nconc (list :@type "getAllStickerEmojis"
:query query
:sticker_type tl-sticker-type
:return_only_main_emoji (if only-main-p t :false))
(when chat
(list :chat_id (plist-get chat :id))))
callback))
(cl-defun telega--searchStickers (emoji &key (tl-sticker-type
'(:@type "stickerTypeRegular"))
limit callback)
"Search for the public stickers that correspond to a given EMOJI.
LIMIT defaults to 20."
(declare (indent 1))
(with-telega-server-reply (reply)
(append (plist-get reply :stickers) nil)
(list :@type "searchStickers"
:emojis emoji
:sticker_type tl-sticker-type
:limit (or limit 100))
callback))
(cl-defun telega--getInstalledStickerSets (&key (tl-sticker-type
'(:@type "stickerTypeRegular"))
callback)
"Return a list of installed sticker sets.
TL-STICKER-TYPE is a TL StickerType object, such as.
By default TL-STICKER-TYPE is `(:@type \"stickerTypeRegular\")'."
(declare (indent 1))
(cl-assert (member tl-sticker-type
'((:@type "stickerTypeRegular")
(:@type "stickerTypeCustomEmoji")))
"installed masks not yet supported")
(with-telega-server-reply (reply)
(append (plist-get reply :sets) nil)
(list :@type "getInstalledStickerSets"
:sticker_type tl-sticker-type)
callback))
(cl-defun telega--getTrendingStickerSets
(&key (offset 0) (limit 100) callback
(tl-sticker-type '(:@type "stickerTypeRegular")))
"Return a list of trending sticker sets."
(with-telega-server-reply (reply)
(append (plist-get reply :sets) nil)
(list :@type "getTrendingStickerSets"
:sticker_type tl-sticker-type
:offset offset
:limit limit)
callback))
(defun telega--getStickerSet (set-id &optional callback)
"Get information about a sticker set by its identifier SET-ID."
(declare (indent 1))
(with-telega-server-reply (reply)
(telega-stickerset--ensure reply)
(list :@type "getStickerSet"
:set_id set-id)
callback))
(defun telega--searchStickerSet (name &optional callback)
"Search for sticker set by NAME."
(declare (indent 1))
(with-telega-server-reply (reply)
(telega-stickerset--ensure reply)
(list :@type "searchStickerSet"
:name name)
callback))
(cl-defun telega--searchStickerSets (query &key (tl-sticker-type
'(:@type "stickerTypeRegular"))
callback)
"Searches for ordinary sticker sets by looking for specified QUERY."
(declare (indent 1))
(with-telega-server-reply (reply)
(append (plist-get reply :sets) nil)
(list :@type "searchStickerSets"
:sticker_type tl-sticker-type
:query query)
callback))
(defun telega--viewTrendingStickerSets (set-id &rest other-ids)
(telega-server--call
(list :@type "viewTrendingStickerSets"
:sticker_set_ids (apply 'vector set-id other-ids))))
(defun telega--getRecentStickers (&optional attached-p callback)
"Returns a list of recently used stickers.
Pass non-nil ATTACHED-P to return only stickers attached to photos/videos."
(declare (indent 1))
(with-telega-server-reply (reply)
(append (plist-get reply :stickers) nil)
(list :@type "getRecentStickers"
:is_attached (if attached-p t :false))
callback))
(defun telega--getFavoriteStickers (&optional callback)
"Return favorite stickers."
(declare (indent 0))
(with-telega-server-reply (reply)
(append (plist-get reply :stickers) nil)
(list :@type "getFavoriteStickers")
callback))
(defun telega--addFavoriteSticker (sticker-input-file &optional callback)
"Add STICKER-INPUT-FILE on top of favorite stickers."
(telega-server--call
(list :@type "addFavoriteSticker"
:sticker sticker-input-file)
callback))
(defun telega--removeFavoriteSticker (sticker-input-file &optional callback)
(telega-server--call
(list :@type "removeFavoriteSticker"
:sticker sticker-input-file)
callback))
(defun telega--getStickerEmojis (sticker-input-file &optional callback)
(telega-server--call
(list :@type "getStickerEmojis"
:sticker sticker-input-file)
callback))
(defun telega--changeStickerSet (stickerset install-p &optional archive-p)
"Install/Uninstall STICKERSET."
(telega-server--call
(list :@type "changeStickerSet"
:set_id (plist-get stickerset :id)
:is_installed (or install-p :false)
:is_archived (or archive-p :false))))
(defun telega--getAttachedStickerSets (file-id)
"Return sticker sets attached to the FILE-ID.
Photo and Video files have attached sticker sets."
(telega-server--call
(list :@type "getAttachedStickerSets"
:file_id file-id)))
(defun telega--searchInstalledStickerSets (query &optional masks-p limit)
"Searches for installed sticker sets by QUERY."
(telega-server--call
(list :@type "searchInstalledStickerSets"
:is_masks (or masks-p :false)
:query query
:limit (or limit 20))))
(defun telega--getSavedAnimations ()
"Return list of saved animations."
(let* ((reply (telega-server--call
(list :@type "getSavedAnimations")))
(anims (append (plist-get reply :animations) nil)))
;; Start downloading animations
(when telega-animation-download-saved
(mapc 'telega-animation--download anims))
anims))
(defun telega--addSavedAnimation (input-file)
"Manually adds a new animation to the list of saved animations."
(telega-server--send
(list :@type "addSavedAnimation"
:animation input-file)))
(defun telega--removeSavedAnimation (input-file)
"Removes an animation from the list of saved animations."
(telega-server--send
(list :@type "removeSavedAnimation"
:animation input-file)))
(defun telega--getRecentInlineBots (&optional callback)
"Return recently used inline bots."
(with-telega-server-reply (reply)
(mapcar #'telega-user-get (plist-get reply :user_ids))
(list :@type "getRecentInlineBots")
callback))
(defun telega--getLinkPreview (fmt-text link-preview-options &optional callback)
"Return a link preview by the text of a message.
Do not call this function too often. Returns a 404 error if the text
has no link preview."
(telega-server--call
(list :@type "getLinkPreview"
:text fmt-text
:link_preview_options link-preview-options)
callback))
(defun telega--getWebPageInstantView (url &optional partial)
"Return instant view for the URL.
Return nil if URL is not available for instant view."
(let ((reply (telega-server--call
(list :@type "getWebPageInstantView"
:url url
:force_full (or (not partial) :false)))))
;; NOTE: May result in 404 error, return nil in this case
(and reply
(eq (telega--tl-type reply) 'webPageInstantView)
reply)))
(defun telega--resendMessages (&rest messages)
"Resend MESSAGES."
(telega-server--send
(list :@type "resendMessages"
:chat_id (plist-get (car messages) :chat_id)
:message_ids (cl-map 'vector (telega--tl-prop :id) messages))))
(defun telega--deleteChatReplyMarkup (message)
"Deletes the default reply markup from a chat.
Must be called after a one-time keyboard or a ForceReply reply
markup has been used."
(telega-server--send
(list :@type "deleteChatReplyMarkup"
:chat_id (plist-get message :chat_id)
:message_id (plist-get message :id))))
(cl-defun telega--searchChatMembers (chat query &optional members-filter
&key limit as-member-p callback)
"Search CHAT members by QUERY.
MEMBERS-FILTER is TDLib's \"ChatMembersFilter\".
LIMIT by default is 200.
If AS-MEMBER-P is non-nil, then return \"chatMember\" structs instead
of message sender."
(declare (indent 3))
(with-telega-server-reply (reply)
(mapcar (if as-member-p #'identity #'telega-msg-sender)
(plist-get reply :members))
(nconc (list :@type "searchChatMembers"
:chat_id (plist-get chat :id)
:query query
:limit (or limit 200))
(when members-filter
(list :filter members-filter)))
callback))
(defun telega--getChatAdministrators (chat &optional callback)
"Return a list of administrators for the CHAT."
(declare (indent 1))
(with-telega-server-reply (reply)
(append (plist-get reply :administrators) nil)
(list :@type "getChatAdministrators"
:chat_id (plist-get chat :id))
callback))
(defun telega--clearAllDraftMessages (&optional exclude-secret-chats-p)
"Clear draft messages in all chats."
(telega-server--send
(list :@type "clearAllDraftMessages"
:exclude_secret_chats (if exclude-secret-chats-p t :false))))
(defun telega--getChatNotificationSettingsExceptions
(scope-type &optional compare-sound-p callback)
"Returns list of chats with non-default notification settings."
(declare (indent 2))
(with-telega-server-reply (reply)
(mapcar #'telega-chat-get (append (plist-get reply :chat_ids) nil))
(list :@type "getChatNotificationSettingsExceptions"
:scope (list :@type scope-type)
:compare_sound (if compare-sound-p t :false))
callback))
(defun telega--getScopeNotificationSettings (scope-type &optional callback)
"Return the notification settings for chats of a given type SCOPE-TYPE.
SCOPE-TYPE is one of:
\"notificationSettingsScopePrivateChats\",
\"notificationSettingsScopeGroupChats\" or
\"notificationSettingsScopeChannelChats\"."
(telega-server--call
(list :@type "getScopeNotificationSettings"
:scope (list :@type scope-type))
callback))
(defun telega--setScopeNotificationSettings (scope-type &rest settings)
"Change notification settings for chats of a given SCOPE-TYPE.
SCOPE-TYPE is the same as in `telega--getScopeNotificationSettings'.
SETTINGS is a plist with notification settings to set."
(declare (indent 1))
(let ((scope-settings (telega-chat-notification-scope scope-type))
(request (list :@type "scopeNotificationSettings")))
(telega--tl-dolist ((prop-name value) (append scope-settings settings))
(setq request (plist-put request prop-name (or value :false))))
(cl-assert (stringp scope-type))
(telega-server--send
(list :@type "setScopeNotificationSettings"
:scope (list :@type scope-type)
:notification_settings request))))
(defun telega--resetAllNotificationSettings ()
"Resets all notification settings to their default values.
By default, all chats are unmuted, the sound is set to
\"default\" and message previews are shown."
(telega-server--call
(list :@type "resetAllNotificationSettings")))
(defun telega--getSupergroupFullInfo (supergroup-id &optional callback)
"Refresh SUPERGROUP full info."
(declare (indent 1))
(telega-server--call
(list :@type "getSupergroupFullInfo"
:supergroup_id supergroup-id)
callback))
(defun telega--getSupergroupMembers (supergroup &optional filter offset limit callback)
"Get SUPERGROUP members.
Default FILTER is \"supergroupMembersFilterRecent\".
Default OFFSET is 0.
Default LIMIT is 200.
Return list of \"ChatMember\" objects."
(declare (indent 4))
(with-telega-server-reply (reply)
(append (plist-get reply :members) nil)
(list :@type "getSupergroupMembers"
:supergroup_id (plist-get supergroup :id)
:filter (list :@type (or filter "supergroupMembersFilterRecent"))
:offset (or offset 0)
:limit (or limit 200))
callback))
(defun telega--toggleSupergroupHasAggressiveAntiSpamEnabled (supergroup has-aggressive-antispam-p)
"Toggle whether aggressive anti-spam checks are enabled in the supergroup."
(telega-server--send
(list :@type "toggleSupergroupHasAggressiveAntiSpamEnabled"
:supergroup_id (plist-get supergroup :id)
:has_aggressive_anti_spam_enabled (if has-aggressive-antispam-p t :false))))
(defun telega--toggleSupergroupHasHiddenMembers (supergroup has-hidden-members-p)
"Toggles whether non-administrators can receive only administrators and bots."
(telega-server--send
(list :@type "toggleSupergroupHasHiddenMembers"
:supergroup_id (plist-get supergroup :id)
:has_hidden_members (if has-hidden-members-p t :false))))
(defun telega--setChatMemberStatus (chat msg-sender status &optional callback)
"Change the STATUS of a MSG-SENDER, needs appropriate privileges.
STATUS is a tl object."
(telega-server--send
(list :@type "setChatMemberStatus"
:chat_id (plist-get chat :id)
:member_id (telega--MessageSender msg-sender)
:status status)
callback))
(defun telega--banChatMember (chat msg-sender &optional revoke-messages-p
until-date callback)
"Ban MSG-SENDER in the CHAT."
(telega-server--send
(list :@type "banChatMember"
:chat_id (plist-get chat :id)
:member_id (telega--MessageSender msg-sender)
:revoke_messages (if revoke-messages-p t :false)
:banned_until_date (or until-date 0))
callback))
(defun telega--addChatMember (chat user &optional forward-limit)
"Add new member USER to the CHAT."
(telega-server--send
(list :@type "addChatMember"
:chat_id (plist-get chat :id)
:user_id (plist-get user :id)
:forward_limit (or forward-limit 100))))
(defun telega--addChatMembers (chat users)
"Add new members to the CHAT.
CHAT must be supergroup or channel."
(telega-server--send
(list :@type "addChatMembers"
:chat_id (plist-get chat :id)
:user_ids (cl-map #'vector (telega--tl-prop :id) users))))
(defun telega--canTransferOwnership (&optional callback)
(telega-server--call
(list :@type "canTransferOwnership")
callback))
(defun telega--transferChatOwnership (chat to-user password &optional callback)
"Transfer ownership of the CHAT supergroup TO-USER."
(declare (indent 3))
(telega-server--call
(list :@type "transferChatOwnership"
:chat_id (plist-get chat :id)
:user_id (plist-get to-user :id)
:password password)
(or callback 'ignore)))
(defun telega--getActiveSessions (&optional callback)
"Get and return list of active sessions."
(with-telega-server-reply (reply)
(append (plist-get reply :sessions) nil)
(list :@type "getActiveSessions")
callback))
(defun telega--terminateSession (session-id)
"Terminate a session of the current user by SESSION-ID."
(telega-server--send