-
Notifications
You must be signed in to change notification settings - Fork 3
/
bot_types.js
1784 lines (1705 loc) · 69.5 KB
/
bot_types.js
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
const _util = require('./util')
const maskpoint_mirror_table = new Map([
['maskPointChin', 'chin'],
['maskPointEyes', 'eyes'],
['maskPointForehead', 'forehead'],
['maskPointMouth', 'mouth']
])
const maskpoint_mirror_table_reversed = new Map([
['chin', 'maskPointChin'],
['eyes', 'maskPointEyes'],
['forehead', 'maskPointForehead'],
['mouth', 'maskPointMouth']
])
const chat_member_status_mirror_table = new Map([
['chatMemberStatusAdministrator', 'administrator'],
['chatMemberStatusBanned', 'kicked'],
['chatMemberStatusCreator', 'creator'],
['chatMemberStatusLeft', 'left'],
['chatMemberStatusMember', 'member'],
['chatMemberStatusRestricted', 'restricted']
])
const chataction_mirror_table = new Map([
['typing', 'chatActionTyping'],
['upload_photo', 'chatActionUploadingPhoto'],
['record_video', 'chatActionRecordingVideo'],
['upload_video', 'chatActionUploadingVideo'],
['record_audio', 'chatActionRecordingVoiceNote'],
['upload_audio', 'chatActionRecordingVoiceNote'],
['upload_document', 'chatActionUploadingDocument'],
['find_location', 'chatActionChoosingLocation'],
['record_video_note', 'chatActionRecordingVideoNote'],
['upload_video_note', 'chatActionUploadingVideoNote'],
['find_contact', 'chatActionChoosingContact'],
['play_game', 'chatActionStartPlayingGame']
])
const passport_element_type_mirror_table = new Map([
['passportElementTypeAddress', 'address'],
['passportElementTypeBankStatement', 'bank_statement'],
['passportElementTypeDriverLicense', 'driver_license'],
['passportElementTypeEmailAddress', 'email'],
['passportElementTypeIdentityCard', 'identity_card'],
['passportElementTypeInternalPassport', 'internal_passport'],
['passportElementTypePassport', 'passport'],
['passportElementTypePassportRegistration', 'passport_registration'],
['passportElementTypePersonalDetails', 'personal_details'],
['passportElementTypePhoneNumber', 'phone_number'],
['passportElementTypeRentalAgreement', 'rental_agreement'],
['passportElementTypeTemporaryRegistration', 'temporary_registration'],
['passportElementTypeUtilityBill', 'utility_bill']
])
const passport_element_type_mirror_table_reversed = new Map([
['address', 'passportElementTypeAddress'],
['bank_statement', 'passportElementTypeBankStatement'],
['driver_license', 'passportElementTypeDriverLicense'],
['email', 'passportElementTypeEmailAddress'],
['identity_card', 'passportElementTypeIdentityCard'],
['internal_passport', 'passportElementTypeInternalPassport'],
['passport', 'passportElementTypePassport', ],
['passport_registration', 'passportElementTypePassportRegistration'],
['personal_details', 'passportElementTypePersonalDetails'],
['phone_number', 'passportElementTypePhoneNumber'],
['rental_agreement', 'passportElementTypeRentalAgreement'],
['temporary_registration', 'passportElementTypeTemporaryRegistration'],
['utility_bill', 'passportElementTypeUtilityBill']
])
class BotTypeConversion {
/**
* @param {TdClientActor.TdClientActor} TdClient
*/
constructor(TdClient) {
if (!TdClient) throw new Error('You have to pass a functional TdClient for this to work.')
this.client = TdClient
}
/**
* @param {TdTypes.user} user
* @returns {Promise<BotAPITypes.User & BotAPITypes$Extended.User>}
*/
async buildUser(user, out_full = false) {
/** @type {BotAPITypes.User & BotAPITypes$Extended.User} */
let bot_user = {
id: user.id,
first_name: user.first_name,
last_name: user.last_name,
username: user.username,
language_code: user.language_code,
restriction_reason: user.restriction_reason,
is_verified: user.is_verified,
is_support: user.is_support,
phone_number: user.phone_number,
is_bot: false,
last_seen: null,
type: null
}
if (user.profile_photo) {
bot_user.photo = {
small_file_id: user.profile_photo.small.remote.id,
big_file_id: user.profile_photo.big.remote.id
}
}
switch (user.status['@type']) {
/* -5: long time ago
* -4: last month
* -3: last week
* -2: recently
* -1: currently online
* 0 - max_int: last seen on timestamp
*/
case 'userStatusEmpty':
bot_user.last_seen = -5
break
case 'userStatusLastMonth':
bot_user.last_seen = -4
break
case 'userStatusLastWeek':
bot_user.last_seen = -3
break
case 'userStatusRecently':
bot_user.last_seen = -2
break
case 'userStatusOnline':
bot_user.last_seen = -1
break
case 'userStatusOffline':
bot_user.last_seen = user.status.was_online
break
}
switch (user.type['@type']) {
case 'userTypeBot':
bot_user.is_bot = true
bot_user.type = 'bot'
bot_user.bot_options = {
can_join_groups: user.type.can_join_groups,
can_read_all_group_messages: user.type.can_read_all_group_messages,
is_inline: user.type.is_inline,
inline_query_placeholder: user.type.inline_query_placeholder,
need_location: user.type.need_location
}
break
case 'userTypeDeleted':
bot_user.type = 'deleted'
break
case 'userTypeRegular':
bot_user.type = 'user'
break
case 'userTypeUnknown':
// No information on the user besides the user_id is available, yet this user has not been deleted. This object is extremely rare and must be handled like a deleted user. It is not possible to perform any actions on users of this type.
bot_user.type = 'deleted'
break
}
if (out_full) {
let full = await this.client.run('getUserFullInfo', {
user_id: user.id
})
bot_user.description = full.bio
bot_user.group_in_common_count = full.group_in_common_count
if (full.bot_info) {
bot_user.bot_options.description = full.bot_info.description
if (full.bot_info.commands) {
bot_user.bot_options.commands = []
for (let {
command,
description
} of full.bot_info.commands) {
bot_user.bot_options.commands.push({
command,
description
})
}
}
}
}
return bot_user
}
async buildChat(chat, out_full = false) {
let bot_chat = {
id: chat.id,
title: chat.title
}
if (chat.photo) {
bot_chat.photo = {
small_file_id: chat.photo.small.remote.id,
big_file_id: chat.photo.big.remote.id
}
}
if (chat.type['@type'] == 'chatTypeSupergroup') {
let additional = await this.client.run('getSupergroup', {
supergroup_id: chat.type.supergroup_id
})
if (chat.type.is_channel) {
bot_chat.type = 'channel'
bot_chat.sign_messages = additional.sign_messages
} else {
bot_chat.type = 'supergroup'
bot_chat.anyone_can_invite = additional.anyone_can_invite
}
bot_chat.username = additional.username
bot_chat.date = additional.date
bot_chat.status = await this.buildChatMember(additional.status)
bot_chat.member_count = additional.member_count // This is not reliable. Use another approach.
bot_chat.is_verified = additional.is_verified
bot_chat.restriction_reason = additional.restriction_reason
if (out_full) {
try {
let additional_full = await this.client.run('getSupergroupFullInfo', {
supergroup_id: chat.type.supergroup_id
})
// Issue: https://github.com/tdlib/td/issues/289#issuecomment-397820646
// Bot can't receive a message, which isn't accessible due to bots privacy settings even through getChatPinnedMessage. The only exception is for replied messages, which can be received through getRepliedMessage.
// So we will just try. If it failed, we ignore it.
// In the future, we will read the privacy settings from the bot's profile.
try {
let pin_msg_orig = await this.client.run('getChatPinnedMessage', {
chat_id: chat.id
})
bot_chat.pinned_message = await this.buildMessage(pin_msg_orig, 1)
} catch (e) {
// failed to get the pinned msg
}
if (additional_full.sticker_set_id && additional_full.sticker_set_id.toString() != '0') {
try {
let sticker_set = await this.client.run('getStickerSet', {
set_id: additional_full.sticker_set_id
})
bot_chat.sticker_set_name = sticker_set.name
bot_chat.sticker_set_id = additional_full.sticker_set_id
} catch (e) {
console.error('failed to get sticker set', additional_full.sticker_set_id)
}
}
bot_chat.description = additional_full.description
if (additional_full.member_count) bot_chat.member_count = additional_full.member_count
bot_chat.administrator_count = additional_full.administrator_count
bot_chat.restricted_count = additional_full.restricted_count
bot_chat.banned_count = additional_full.banned_count
bot_chat.can_get_members = additional_full.can_get_members
bot_chat.can_set_username = additional_full.can_set_username
bot_chat.can_set_sticker_set = additional_full.can_set_sticker_set
bot_chat.can_view_statistics = additional_full.can_view_statistics
bot_chat.is_all_history_available = additional_full.is_all_history_available
bot_chat.invite_link = additional_full.invite_link
if (!isNaN(additional_full.upgraded_from_basic_group_id))
bot_chat.migrate_from_chat_id = -additional_full.upgraded_from_basic_group_id
} catch (e) {
if (e.message !== 'CHANNEL_PRIVATE') console.error(e)
}
}
} else if (chat.type['@type'] == 'chatTypeBasicGroup') {
let additional = await this.client.run('getBasicGroup', {
basic_group_id: chat.type.basic_group_id
})
bot_chat.type = 'group'
bot_chat.status = additional.everyone_is_administrator ? { status: 'member' } : await this.buildChatMember(additional.status)
bot_chat.all_members_are_administrators = additional.everyone_is_administrator
bot_chat.is_active = additional.is_active
bot_chat.member_count = additional.member_count
// bot_chat.upgraded_to_supergroup_id = additional.upgraded_to_supergroup_id
if (!isNaN(bot_chat.upgraded_to_supergroup_id)) {
bot_chat.upgraded_to_supergroup_id = -Math.pow(10, 12) - additional.upgraded_to_supergroup_id
}
if (out_full) {
try {
let additional_full = await this.client.run('getBasicGroupFullInfo', {
basic_group_id: chat.type.basic_group_id
})
bot_chat.creator = additional_full.creator_user_id
bot_chat.members = additional_full.members
// members here? really?
} catch (e) {
console.error(e)
}
}
} else if (chat.type['@type'] == 'chatTypePrivate') {
let additional = await this.client.run('getUser', {
user_id: chat.type.user_id
})
bot_chat.type = 'private'
bot_chat = Object.assign(bot_chat, await this.buildUser(additional, out_full))
} else {
throw new Error('Unknown Chat Type.')
}
return bot_chat
}
async buildMessage(message, follow_replies_level = 1) {
let bot_message = {
message_id: _util.get_api_message_id(message.id),
date: message.date,
edit_date: message.edit_date,
is_channel_post: message.is_channel_post,
can_be_deleted_for_all_users: message.can_be_deleted_for_all_users,
}
let chat = await this.client.run('getChat', {
chat_id: message.chat_id
})
bot_message.chat = await this.buildChat(chat, false)
if (message.sender_user_id) {
let from = await this.client.run('getUser', {
user_id: message.sender_user_id
})
bot_message.from = await this.buildUser(from, false)
}
if (message.reply_to_message_id) {
bot_message.reply_to_message_id = _util.get_api_message_id(message.reply_to_message_id)
if (follow_replies_level > 0) {
try {
let reply_msg = await this.client.run('getRepliedMessage', {
chat_id: message.chat_id,
message_id: message.id
})
bot_message.reply_to_message = await this.buildMessage(reply_msg, follow_replies_level - 1)
} catch (e) {
// failed to get replied message. did it got deleted? lets ignore this.
}
}
}
if (message.media_group_id)
bot_message.media_group_id = message.media_group_id
if ('views' in message)
bot_message.views = message.views
if (message.via_bot_user_id) {
bot_message.via_bot_user_id = message.via_bot_user_id
try {
let via_bot = await this.client.run('getUser', {
user_id: message.via_bot_user_id
})
bot_message.via_bot = await this.buildUser(via_bot, false)
} catch (e) {
// ignore
}
}
bot_message.author_signature = message.author_signature
if (message.forward_info) {
bot_message.forwarded = true
bot_message.forward_date = message.forward_info.date
switch (message.forward_info.origin['@type']) {
case 'messageForwardOriginUser': {
let fwd_user = await this.client.run('getUser', {
user_id: message.forward_info.origin.sender_user_id
})
bot_message.forward_from = await this.buildUser(fwd_user, false)
break
}
case 'messageForwardOriginHiddenUser': {
bot_message.forward_sender_name = message.forward_info.sender_name
break
}
case 'messageForwardOriginChannel': {
let fwd_chat = await this.client.run('getChat', {
chat_id: message.forward_info.origin.chat_id
})
bot_message.forward_from_chat = await this.buildChat(fwd_chat, false)
bot_message.forward_from_message_id = _util.get_api_message_id(message.forward_info.origin.message_id)
bot_message.forward_signature = message.forward_info.origin.author_signature
break
}
}
}
switch (message.content['@type']) {
case 'messageText':
bot_message.text = message.content.text.text
bot_message.entities = await this.buildEntities(message.content.text.entities)
break
case 'messageAudio':
bot_message.audio = await this.buildAudio(message.content.audio)
break
case 'messageDocument':
bot_message.document = await this.buildDocument(message.content.document)
break
case 'messageGame':
bot_message.game = await this.buildGame(message.content.game)
break
case 'messageAnimation':
bot_message.animation = await this.buildAnimation(message.content.animation)
bot_message.document = bot_message.animation // Full compatible, original behavior
break
case 'messagePhoto':
bot_message.photo = await this.buildPhoto(message.content.photo)
break
case 'messageSticker':
bot_message.sticker = await this.buildSticker(message.content.sticker)
break
case 'messageVideo':
bot_message.video = await this.buildVideo(message.content.video)
break
case 'messageVoiceNote':
bot_message.voice = await this.buildVoice(message.content.voice_note)
bot_message.voice.is_listened = message.content.is_listened
break
case 'messageVideoNote':
bot_message.video_note = await this.buildVideoNote(message.content.video_note)
bot_message.video_note.is_viewed = message.content.is_viewed
break
case 'messageContact':
bot_message.contact = await this.buildContact(message.content.contact)
break
case 'messageLocation':
bot_message.location = await this.buildLocation(message.content.location)
bot_message.location.live_period = message.content.live_period
bot_message.location.expires_in = message.content.expires_in
break
case 'messageVenue':
bot_message.venue = await this.buildVenue(message.content.venue)
break
case 'messageChatAddMembers': {
let new_members = []
for (let uid of message.content.member_user_ids) {
let new_member = await this.client.run('getUser', {
user_id: uid
})
new_members.push(await this.buildUser(new_member, false))
}
bot_message.new_chat_members = new_members
bot_message.new_chat_member = new_members[0]
break
}
case 'messageChatJoinByLink':
bot_message.new_chat_members = [bot_message.from]
bot_message.new_chat_member = bot_message.from
break
case 'messageChatDeleteMember': {
let left_member = await this.client.run('getUser', {
user_id: message.content.user_id
})
bot_message.left_chat_member = await this.buildUser(left_member, false)
// NOTE: we didn't get this msg when the bot itself got kicked.
// Instead, we got this:
// {"@type":"updateSupergroup","supergroup":{"@type":"supergroup","id":***,"username":"","date":0,"status":{"@type":"chatMemberStatusBanned","banned_until_date":0},"member_count":0,"anyone_can_invite":false,"sign_messages":true,"is_channel":false,"is_verified":false,"restriction_reason":""}}
// ^ got banned
// {"@type":"updateSupergroup","supergroup":{"@type":"supergroup","id":***,"username":"","date":1529156811,"status":{"@type":"chatMemberStatusLeft"},"member_count":0,"anyone_can_invite":true,"sign_messages":true,"is_channel":false,"is_verified":false,"restriction_reason":""}}
// ^ got unbanned
break
}
case 'messageChatChangeTitle':
bot_message.new_chat_title = message.content.title
break
case 'messageChatChangePhoto':
bot_message.new_chat_photo = await this.buildPhoto(message.content.photo)
break
case 'messageChatDeletePhoto':
bot_message.delete_chat_photo = true
break
case 'messageBasicGroupChatCreate': {
let new_created_members = []
for (let uid of message.content.member_user_ids) {
let new_member = await this.client.run('getUser', {
user_id: uid
})
new_created_members.push(await this.buildUser(new_member, false))
}
bot_message.new_chat_members = new_created_members
bot_message.new_chat_member = new_created_members[0]
bot_message.group_chat_created = true
break
}
case 'messageSupergroupChatCreate':
if (bot_message.chat.type == 'channel') bot_message.channel_chat_created = true
else bot_message.supergroup_chat_created = true
break
case 'messageChatUpgradeTo':
bot_message.migrate_to_chat_id = -(message.content.supergroup_id + Math.pow(10, 13))
break
case 'messageChatUpgradeFrom':
bot_message.migrate_from_chat_id = message.content.basic_group_id
break
case 'messageInvoice':
bot_message.invoice = {
title: message.content.title,
description: message.content.description,
currency: message.content.currency,
total_amount: message.content.total_amount,
start_parameter: message.content.start_parameter,
is_test: message.content.is_test,
need_shipping_address: message.content.need_shipping_address,
receipt_message_id: _util.get_api_message_id(message.content.receipt_message_id)
}
if (message.content.photo) {
bot_message.invoice.photo = this.buildPhoto(message.content.photo)
}
break
case 'messagePaymentSuccessfulBot':
bot_message.successful_payment = {
invoice_message_id: _util.get_api_message_id(message.content.invoice_message_id),
currency: message.content.currency,
total_amount: message.content.total_amount,
invoice_payload: message.content.invoice_payload, // Base64?
shipping_option_id: message.content.shipping_option_id,
telegram_payment_charge_id: message.content.telegram_payment_charge_id,
provider_payment_charge_id: message.content.provider_payment_charge_id
}
if (message.content.order_info) {
bot_message.successful_payment.order_info = await this.buildOrderInfo(message.content.order_info)
}
break
case 'messageWebsiteConnected':
bot_message.connected_website = message.content.domain_name
break
case 'messageScreenshotTaken':
bot_message.screenshot_taken = true
break
case 'messagePassportDataReceived':
bot_message.passport_data = await this.buildPassportData(message.content)
break
case 'messagePoll':
bot_message.poll = await this.buildPoll(message.content.poll)
break
case 'messageUnsupported':
bot_message.unsupported = true
break
}
if (message.content.caption) {
if (message.content.caption.text) {
bot_message.caption = message.content.caption.text
bot_message.caption_entities = await this.buildEntities(message.content.caption.entities)
}
}
return bot_message
}
async buildEntities(entities) {
let _entities = []
for (let entity of entities) {
let _ent = {
offset: entity.offset,
length: entity.length
}
switch (entity.type['@type']) {
case 'textEntityTypeBold':
_ent.type = 'bold'
break
case 'textEntityTypeBotCommand':
_ent.type = 'bot_command'
break
case 'textEntityTypeCashtag':
_ent.type = 'cashtag'
break
case 'textEntityTypeCode':
_ent.type = 'code'
break
case 'textEntityTypeEmailAddress':
_ent.type = 'email'
break
case 'textEntityTypeHashtag':
_ent.type = 'hashtag'
break
case 'textEntityTypeItalic':
_ent.type = 'italic'
break
case 'textEntityTypeMention':
_ent.type = 'mention'
break
case 'textEntityTypeMentionName': {
_ent.type = 'text_mention'
let mention_user = await this.client.run('getUser', {
user_id: entity.type.user_id
})
_ent.user = await this.buildUser(mention_user, false)
break
}
case 'textEntityTypePhoneNumber':
_ent.type = 'phone'
break
case 'textEntityTypePre':
_ent.type = 'pre'
break
case 'textEntityTypePreCode':
_ent.type = 'pre'
_ent.language = entity.type.language
break
case 'textEntityTypeTextUrl':
_ent.type = 'text_link'
_ent.url = entity.type.url
break
case 'textEntityTypeUrl':
_ent.type = 'url'
break
}
_entities.push(_ent)
}
return _entities
}
async buildAnimation(animation) {
let _ani = {
file_id: animation.animation.remote.id,
file_name: animation.file_name,
mime_type: animation.mime_type,
file_size: animation.animation.size || animation.animation.expected_size,
width: animation.width,
height: animation.height,
duration: animation.duration
}
if (animation.thumbnail) {
_ani.thumb = await this.buildPhotoSize(animation.thumbnail)
}
return _ani
}
async buildAudio(audio) {
let _audio = {
file_id: audio.audio.remote.id,
duration: audio.duration,
performer: audio.performer,
title: audio.title,
mime_type: audio.mime_type,
}
if (audio.album_cover_thumbnail) {
_audio.thumb = await this.buildPhotoSize(audio.album_cover_thumbnail)
}
return _audio
}
async buildTdlibChatAction(action) {
if (chataction_mirror_table.has(action)) {
return {
'@type': chataction_mirror_table.get(action)
}
} else {
return {
'@type': 'chatActionCancel'
}
}
}
async buildChatMember(cm) {
let ret = {}
if (cm.user_id) {
let user = await this.client.run('getUser', {
user_id: cm.user_id
})
ret.user = await this.buildUser(user, false)
}
if (cm.joined_chat_date) ret.joined_chat_date = cm.joined_chat_date
const cmstat = cm.status ? cm.status : cm
ret.status = chat_member_status_mirror_table.get(cmstat['@type'])
if (cmstat['@type'] == 'chatMemberStatusAdministrator') {
ret.can_be_edited = cmstat.can_be_edited
ret.can_change_info = cmstat.can_change_info
ret.can_post_messages = cmstat.can_post_messages
ret.can_edit_messages = cmstat.can_edit_messages
ret.can_delete_messages = cmstat.can_delete_messages
ret.can_invite_users = cmstat.can_invite_users
ret.can_restrict_members = cmstat.can_restrict_members
ret.can_pin_messages = cmstat.can_pin_messages
ret.can_promote_members = cmstat.can_promote_members
} else if (cmstat['@type'] == 'chatMemberStatusRestricted') {
ret.is_member = cmstat.is_member
ret.until_date = cmstat.restricted_until_date
ret.can_send_messages = cmstat.can_send_messages
ret.can_send_media_messages = cmstat.can_send_media_messages
ret.can_send_other_messages = cmstat.can_send_other_messages
ret.can_add_web_page_previews = cmstat.can_add_web_page_previews
} else if (cmstat['@type'] == 'chatMemberStatusBanned') {
ret.until_date = cmstat.banned_until_date
} else if (cmstat['@type'] == 'chatMemberStatusCreator') {
ret.is_member = cmstat.is_member
}
if (cm.inviter_user_id) {
let inviter = await this.client.run('getUser', {
user_id: cm.inviter_user_id
})
ret.inviter = await this.buildUser(inviter, false)
}
return ret
}
async buildContact(contact) {
return {
phone_number: contact.phone_number,
first_name: contact.first_name,
last_name: contact.last_name,
user_id: contact.user_id,
vcard: contact.vcard
}
}
async buildDocument(document) {
let _doc = {
file_id: document.document.remote.id,
file_name: document.file_name,
mime_type: document.mime_type,
file_size: document.document.size || document.document.expected_size
}
if (document.thumbnail) {
_doc.thumb = await this.buildPhotoSize(document.thumbnail)
}
return _doc
}
async buildEncryptedCredentials(encrypted_credentials) {
return {
data: encrypted_credentials.data,
hash: encrypted_credentials.hash,
secret: encrypted_credentials.secret
}
}
async buildEncryptedPassportElement(encrypted_passport_element) {
let _element = {
type: passport_element_type_mirror_table.get(encrypted_passport_element.type['@type']),
hash: encrypted_passport_element.hash
}
if (encrypted_passport_element.data) {
_element.data = encrypted_passport_element.data
}
if (encrypted_passport_element.front_side) {
_element.front_side = await this.buildPassportFile(encrypted_passport_element.front_side)
}
if (encrypted_passport_element.reverse_side) {
_element.reverse_side = await this.buildPassportFile(encrypted_passport_element.reverse_side)
}
if (encrypted_passport_element.selfie) {
_element.selfie = await this.buildPassportFile(encrypted_passport_element.selfie)
}
if (Array.isArray(encrypted_passport_element.translation)) {
_element.translation = await Promise.all(encrypted_passport_element.translation.map(n => this.buildPassportFile(n)))
}
if (Array.isArray(encrypted_passport_element.files)) {
_element.files = await Promise.all(encrypted_passport_element.files.map(n => this.buildPassportFile(n)))
}
switch (_element.type) {
case 'email':
_element.email = encrypted_passport_element.value
break
case 'phone_number':
_element.phone_number = encrypted_passport_element.value
}
return _element
}
async buildFile(file) {
return {
file_id: file.remote.id,
file_size: file.size
}
}
async buildGame(game) {
let _game = {
id: game.id,
short_name: game.short_name,
title: game.title,
description: game.description,
text: game.text.text,
// entity
photo: await this.buildPhoto(game.photo),
}
if (game.animation) {
_game.animation = await this.buildAnimation(game.animation)
}
return _game
}
async buildGameHighScore(game_high_score) {
return {
position: game_high_score.position,
user: await this.buildUser(await this.client.run('getUser', { user_id: game_high_score.user_id }), false),
score: game_high_score.score
}
}
async buildGameHighScores(game_high_scores) {
let _buf = await Promise.all(game_high_scores.map(n => this.buildGameHighScore(n)))
return _buf
}
async buildInlineQuery(iq) {
let _iq = {
id: iq.id,
from: await this.buildUser(await this.client.run('getUser', { user_id: iq.sender_user_id }), false),
query: iq.query,
offset: iq.offset
}
if (iq.user_location) {
_iq.location = await this.buildLocation(iq.user_location)
}
return _iq
}
async buildTdlibInlineQueryResult(iqr) {
switch (iqr.type) {
case 'article':
return this.buildTdlibInlineQueryResultArticle(iqr)
case 'photo':
return this.buildTdlibInlineQueryResultPhoto(iqr)
case 'gif':
return this.buildTdlibInlineQueryResultAnimatedGif(iqr)
case 'mpeg4_gif':
return this.buildTdlibInlineQueryResultAnimatedMpeg4(iqr)
case 'video':
return this.buildTdlibInlineQueryResultVideo(iqr)
case 'audio':
return this.buildTdlibInlineQueryResultAudio(iqr)
case 'voice':
return this.buildTdlibInlineQueryResultVoiceNote(iqr)
case 'document':
return this.buildTdlibInlineQueryResultDocument(iqr)
case 'location':
return this.buildTdlibInlineQueryResultLocation(iqr)
case 'venue':
return this.buildTdlibInlineQueryResultVenue(iqr)
case 'contact':
return this.buildTdlibInlineQueryResultContact(iqr)
case 'game':
return this.buildTdlibInlineQueryResultGame(iqr)
default:
throw new Error(`Invalid inline query result type: ${iqr.type}`)
}
}
async buildTdlibInlineQueryResultAnimatedGif(gif) {
let _gif = {
'@type': 'inputInlineQueryResultAnimatedGif',
id: gif.id
}
if (gif.gif_url) {
_gif.gif_url = gif.gif_url
if (gif.thumb_url) _gif.thumbnail_url = gif.thumb_url
} else if (gif.gif_file_id) {
_gif.gif_url = gif.gif_file_id
}
if (gif.gif_width) {
_gif.gif_width = gif.gif_width
}
if (gif.gif_height) {
_gif.gif_height = gif.gif_height
}
if (gif.gif_duration) {
_gif.gif_duration = gif.gif_duration
}
if (gif.title) {
_gif.title = gif.title
}
if (gif.reply_markup) {
_gif.reply_markup = this.client._parseReplyMarkup(gif.reply_markup)
}
if (gif.input_message_content) {
_gif.input_message_content = await this.buildTdlibInlineInputMessageContent(gif.input_message_content)
} else {
_gif.input_message_content = {
'@type': 'inputMessageAnimation',
animation: null,
thumbnail: null,
}
if (gif.gif_duration) {
_gif.input_message_content.duration = gif.gif_duration
}
if (gif.gif_width) {
_gif.input_message_content.width = gif.gif_width
}
if (gif.gif_height) {
_gif.input_message_content.height = gif.gif_height
}
if (gif.caption) {
_gif.input_message_content.caption = await this.client._generateFormattedText(gif.caption, gif.parse_mode)
}
}
return _gif
}
async buildTdlibInlineQueryResultAnimatedMpeg4(mpeg4) {
let _mpeg4 = {
'@type': 'inputInlineQueryResultAnimatedMpeg4',
id: mpeg4.id
}
if (mpeg4.mpeg4_url) {
_mpeg4.mpeg4_url = mpeg4.mpeg4_url
if (mpeg4.thumb_url) _mpeg4.thumbnail_url = mpeg4.thumb_url
} else if (mpeg4.mpeg4_file_id) {
_mpeg4.mpeg4_url = mpeg4.mpeg4_file_id
}
if (mpeg4.mpeg4_width) {
_mpeg4.mpeg4_width = mpeg4.mpeg4_width
}
if (mpeg4.mpeg4_height) {
_mpeg4.mpeg4_height = mpeg4.mpeg4_height
}
if (mpeg4.mpeg4_duration) {
_mpeg4.mpeg4_duration = mpeg4.mpeg4_duration
}
if (mpeg4.title) {
_mpeg4.title = mpeg4.title
}
if (mpeg4.reply_markup) {
_mpeg4.reply_markup = this.client._parseReplyMarkup(mpeg4.reply_markup)
}
if (mpeg4.input_message_content) {
_mpeg4.input_message_content = await this.buildTdlibInlineInputMessageContent(mpeg4.input_message_content)
} else {
_mpeg4.input_message_content = {
'@type': 'inputMessageAnimation',
animation: null,
thumbnail: null,
}
if (mpeg4.mpeg4_duration) {
_mpeg4.input_message_content.duration = mpeg4.mpeg4_duration
}
if (mpeg4.mpeg4_width) {
_mpeg4.input_message_content.width = mpeg4.mpeg4_width
}
if (mpeg4.mpeg4_height) {
_mpeg4.input_message_content.height = mpeg4.mpeg4_height
}
if (mpeg4.caption) {
_mpeg4.input_message_content.caption = await this.client._generateFormattedText(mpeg4.caption, mpeg4.parse_mode)
}
}
return _mpeg4
}
async buildTdlibInlineQueryResultArticle(article) {
let _article = {
'@type': 'inputInlineQueryResultArticle',
id: article.id,
title: article.title,
hide_url: !!article.hide_url
}
if (article.url) {
_article.url = article.url
}
if (article.description) {
_article.description = article.description
}
if (article.thumb_url) {
_article.thumbnail_url = article.thumb_url
}
if (article.thumb_width) {
_article.thumbnail_width = article.thumb_width
}
if (article.thumb_height) {
_article.thumbnail_height = article.thumb_height
}
if (article.reply_markup) {
_article.reply_markup = this.client._parseReplyMarkup(article.reply_markup)
}
if (article.input_message_content) {
_article.input_message_content = await this.buildTdlibInlineInputMessageContent(article.input_message_content)
} else {
throw new Error('Input_message_content not exist')
}
return _article
}
async buildTdlibInlineQueryResultAudio(audio) {
let _audio = {
id: audio.id,
audio_url: audio.audio_url || audio.audio_file_id
}
if (audio.performer) {
_audio.performer = audio.performer
}
if (audio.audio_duration) {
_audio.audio_duration = audio.audio_duration
}
if (audio.title) {
_audio.title = audio.title
}
if (audio.reply_markup) {
_audio.reply_markup = this.client._parseReplyMarkup(audio.reply_markup)
}
if (audio.input_message_content) {
_audio.input_message_content = await this.buildTdlibInlineInputMessageContent(audio.input_message_content)
} else {
_audio.input_message_content = {
'@type': 'inputMessageAudio',
audio: null,
album_cover_thumbnail: null
}
if (audio.audio_duration) {
_audio.input_message_content.duration = audio.audio_duration
}
if (audio.title) {
_audio.input_message_content.title = audio.title
}