@satorijs/adapter-lark 3.11.9 → 3.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/bot.d.ts +5 -5
- package/lib/http.d.ts +6 -3
- package/lib/index.cjs +293 -223
- package/lib/types/acs.d.ts +4 -1
- package/lib/types/apaas.d.ts +152 -0
- package/lib/types/application.d.ts +20 -4
- package/lib/types/approval.d.ts +0 -13
- package/lib/types/attendance.d.ts +11 -5
- package/lib/types/authen.d.ts +8 -2
- package/lib/types/bitable.d.ts +25 -4
- package/lib/types/board.d.ts +17 -0
- package/lib/types/calendar.d.ts +16 -5
- package/lib/types/contact.d.ts +55 -12
- package/lib/types/corehr.d.ts +754 -142
- package/lib/types/drive.d.ts +20 -3
- package/lib/types/ehr.d.ts +11 -2
- package/lib/types/helpdesk.d.ts +15 -6
- package/lib/types/hire.d.ts +13 -13
- package/lib/types/human_authentication.d.ts +1 -1
- package/lib/types/im.d.ts +76 -20
- package/lib/types/index.d.ts +712 -115
- package/lib/types/mail.d.ts +106 -16
- package/lib/types/search.d.ts +4 -1
- package/lib/ws.d.ts +30 -0
- package/package.json +6 -3
- package/src/bot.ts +28 -15
- package/src/http.ts +10 -4
- package/src/types/acs.ts +4 -1
- package/src/types/apaas.ts +185 -0
- package/src/types/application.ts +20 -4
- package/src/types/approval.ts +0 -15
- package/src/types/attendance.ts +11 -5
- package/src/types/authen.ts +8 -2
- package/src/types/bitable.ts +25 -4
- package/src/types/board.ts +22 -0
- package/src/types/calendar.ts +16 -5
- package/src/types/contact.ts +55 -12
- package/src/types/corehr.ts +796 -142
- package/src/types/drive.ts +20 -3
- package/src/types/ehr.ts +11 -2
- package/src/types/helpdesk.ts +15 -6
- package/src/types/hire.ts +17 -17
- package/src/types/human_authentication.ts +1 -1
- package/src/types/im.ts +76 -20
- package/src/types/index.ts +738 -115
- package/src/types/mail.ts +106 -16
- package/src/types/search.ts +4 -1
- package/src/ws.ts +183 -0
- package/lib/types/api.d.ts +0 -28510
- package/lib/types/internal.d.ts +0 -21
- package/lib/types/message/content.d.ts +0 -433
- package/lib/types/message/index.d.ts +0 -95
package/src/types/mail.ts
CHANGED
|
@@ -458,7 +458,13 @@ export namespace Mail {
|
|
|
458
458
|
name?: string
|
|
459
459
|
/** The mail group's description */
|
|
460
460
|
description?: string
|
|
461
|
-
/**
|
|
461
|
+
/**
|
|
462
|
+
* Who can send mail to this mail group. Possible values are:
|
|
463
|
+
* - ANYONE: Any Internet user can send mail to this mail group
|
|
464
|
+
* - ALL_INTERNAL_USERS: Anyone in the team can send mail to this mail group
|
|
465
|
+
* - ALL_GROUP_MEMBERS: Any group member can send mail to this mail group
|
|
466
|
+
* - CUSTOM_MEMBERS: Only custom members can send mail to this mail group, define in mailgroup.permission_members resoure
|
|
467
|
+
*/
|
|
462
468
|
who_can_send_mail?: 'ANYONE' | 'ALL_INTERNAL_USERS' | 'ALL_GROUP_MEMBERS' | 'CUSTOM_MEMBERS'
|
|
463
469
|
}
|
|
464
470
|
|
|
@@ -477,7 +483,13 @@ export namespace Mail {
|
|
|
477
483
|
include_external_member?: boolean
|
|
478
484
|
/** Value is true if all company members are in this mail group */
|
|
479
485
|
include_all_company_member?: boolean
|
|
480
|
-
/**
|
|
486
|
+
/**
|
|
487
|
+
* Who can send mail to this mail group. Possible values are:
|
|
488
|
+
* - ANYONE: Any Internet user can send mail to this mail group
|
|
489
|
+
* - ALL_INTERNAL_USERS: Anyone in the team can send mail to this mail group
|
|
490
|
+
* - ALL_GROUP_MEMBERS: Any group member can send mail to this mail group
|
|
491
|
+
* - CUSTOM_MEMBERS: Only custom members can send mail to this mail group, define in mailgroup.permission_members resoure
|
|
492
|
+
*/
|
|
481
493
|
who_can_send_mail?: 'ANYONE' | 'ALL_INTERNAL_USERS' | 'ALL_GROUP_MEMBERS' | 'CUSTOM_MEMBERS'
|
|
482
494
|
}
|
|
483
495
|
|
|
@@ -488,7 +500,13 @@ export namespace Mail {
|
|
|
488
500
|
name?: string
|
|
489
501
|
/** The mail group's description */
|
|
490
502
|
description?: string
|
|
491
|
-
/**
|
|
503
|
+
/**
|
|
504
|
+
* Who can send mail to this mail group. Possible values are:
|
|
505
|
+
* - ANYONE: Any Internet user can send mail to this mail group
|
|
506
|
+
* - ALL_INTERNAL_USERS: Anyone in the team can send mail to this mail group
|
|
507
|
+
* - ALL_GROUP_MEMBERS: Any group member can send mail to this mail group
|
|
508
|
+
* - CUSTOM_MEMBERS: Only custom members can send mail to this mail group, define in mailgroup.permission_members resoure
|
|
509
|
+
*/
|
|
492
510
|
who_can_send_mail?: 'ANYONE' | 'ALL_INTERNAL_USERS' | 'ALL_GROUP_MEMBERS' | 'CUSTOM_MEMBERS'
|
|
493
511
|
}
|
|
494
512
|
|
|
@@ -507,7 +525,13 @@ export namespace Mail {
|
|
|
507
525
|
include_external_member?: boolean
|
|
508
526
|
/** Value is true if all company members are in this mail group */
|
|
509
527
|
include_all_company_member?: boolean
|
|
510
|
-
/**
|
|
528
|
+
/**
|
|
529
|
+
* Who can send mail to this mail group. Possible values are:
|
|
530
|
+
* - ANYONE: Any Internet user can send mail to this mail group
|
|
531
|
+
* - ALL_INTERNAL_USERS: Anyone in the team can send mail to this mail group
|
|
532
|
+
* - ALL_GROUP_MEMBERS: Any group member can send mail to this mail group
|
|
533
|
+
* - CUSTOM_MEMBERS: Only custom members can send mail to this mail group, define in mailgroup.permission_members resoure
|
|
534
|
+
*/
|
|
511
535
|
who_can_send_mail?: 'ANYONE' | 'ALL_INTERNAL_USERS' | 'ALL_GROUP_MEMBERS' | 'CUSTOM_MEMBERS'
|
|
512
536
|
}
|
|
513
537
|
|
|
@@ -518,7 +542,13 @@ export namespace Mail {
|
|
|
518
542
|
name?: string
|
|
519
543
|
/** The mail group's description */
|
|
520
544
|
description?: string
|
|
521
|
-
/**
|
|
545
|
+
/**
|
|
546
|
+
* Who can send mail to this mail group. Possible values are:
|
|
547
|
+
* - ANYONE: Any Internet user can send mail to this mail group
|
|
548
|
+
* - ALL_INTERNAL_USERS: Anyone in the team can send mail to this mail group
|
|
549
|
+
* - ALL_GROUP_MEMBERS: Any group member can send mail to this mail group
|
|
550
|
+
* - CUSTOM_MEMBERS: Only custom members can send mail to this mail group, define in mailgroup.permission_members resoure
|
|
551
|
+
*/
|
|
522
552
|
who_can_send_mail?: 'ANYONE' | 'ALL_INTERNAL_USERS' | 'ALL_GROUP_MEMBERS' | 'CUSTOM_MEMBERS'
|
|
523
553
|
}
|
|
524
554
|
|
|
@@ -537,7 +567,13 @@ export namespace Mail {
|
|
|
537
567
|
include_external_member?: boolean
|
|
538
568
|
/** Value is true if all company members are in this mail group */
|
|
539
569
|
include_all_company_member?: boolean
|
|
540
|
-
/**
|
|
570
|
+
/**
|
|
571
|
+
* Who can send mail to this mail group. Possible values are:
|
|
572
|
+
* - ANYONE: Any Internet user can send mail to this mail group
|
|
573
|
+
* - ALL_INTERNAL_USERS: Anyone in the team can send mail to this mail group
|
|
574
|
+
* - ALL_GROUP_MEMBERS: Any group member can send mail to this mail group
|
|
575
|
+
* - CUSTOM_MEMBERS: Only custom members can send mail to this mail group, define in mailgroup.permission_members resoure
|
|
576
|
+
*/
|
|
541
577
|
who_can_send_mail?: 'ANYONE' | 'ALL_INTERNAL_USERS' | 'ALL_GROUP_MEMBERS' | 'CUSTOM_MEMBERS'
|
|
542
578
|
}
|
|
543
579
|
|
|
@@ -556,7 +592,13 @@ export namespace Mail {
|
|
|
556
592
|
include_external_member?: boolean
|
|
557
593
|
/** Value is true if all company members are in this mail group */
|
|
558
594
|
include_all_company_member?: boolean
|
|
559
|
-
/**
|
|
595
|
+
/**
|
|
596
|
+
* Who can send mail to this mail group. Possible values are:
|
|
597
|
+
* - ANYONE: Any Internet user can send mail to this mail group
|
|
598
|
+
* - ALL_INTERNAL_USERS: Anyone in the team can send mail to this mail group
|
|
599
|
+
* - ALL_GROUP_MEMBERS: Any group member can send mail to this mail group
|
|
600
|
+
* - CUSTOM_MEMBERS: Only custom members can send mail to this mail group, define in mailgroup.permission_members resoure
|
|
601
|
+
*/
|
|
560
602
|
who_can_send_mail?: 'ANYONE' | 'ALL_INTERNAL_USERS' | 'ALL_GROUP_MEMBERS' | 'CUSTOM_MEMBERS'
|
|
561
603
|
}
|
|
562
604
|
|
|
@@ -653,7 +695,16 @@ export namespace Mail {
|
|
|
653
695
|
user_id?: string
|
|
654
696
|
/** The member's department id. Value is valid when type is DEPARTMENT */
|
|
655
697
|
department_id?: string
|
|
656
|
-
/**
|
|
698
|
+
/**
|
|
699
|
+
* The type of member. Possible values are:
|
|
700
|
+
* - USER: internal user in the team
|
|
701
|
+
* - DEPARTMENT: member is a department
|
|
702
|
+
* - COMPANY: member is the company
|
|
703
|
+
* - EXTERNAL_USER: internet user outside the organization
|
|
704
|
+
* - MAIL_GROUP: member is another mail group
|
|
705
|
+
* - PUBLIC_MAILBOX: member is a public mailbox
|
|
706
|
+
* - OTHER_MEMBER: other internal member
|
|
707
|
+
*/
|
|
657
708
|
type?: 'USER' | 'DEPARTMENT' | 'COMPANY' | 'EXTERNAL_USER' | 'MAIL_GROUP' | 'PUBLIC_MAILBOX' | 'OTHER_MEMBER'
|
|
658
709
|
}
|
|
659
710
|
|
|
@@ -673,7 +724,16 @@ export namespace Mail {
|
|
|
673
724
|
user_id?: string
|
|
674
725
|
/** The member's department id. Value is valid when type is DEPARTMENT */
|
|
675
726
|
department_id?: string
|
|
676
|
-
/**
|
|
727
|
+
/**
|
|
728
|
+
* The type of member. Possible values are:
|
|
729
|
+
* - USER: internal user in the team
|
|
730
|
+
* - DEPARTMENT: member is a department
|
|
731
|
+
* - COMPANY: member is the company
|
|
732
|
+
* - EXTERNAL_USER: internet user outside the organization
|
|
733
|
+
* - MAIL_GROUP: member is another mail group
|
|
734
|
+
* - PUBLIC_MAILBOX: member is a public mailbox
|
|
735
|
+
* - OTHER_MEMBER: other internal member
|
|
736
|
+
*/
|
|
677
737
|
type?: 'USER' | 'DEPARTMENT' | 'COMPANY' | 'EXTERNAL_USER' | 'MAIL_GROUP' | 'PUBLIC_MAILBOX' | 'OTHER_MEMBER'
|
|
678
738
|
}
|
|
679
739
|
|
|
@@ -693,7 +753,16 @@ export namespace Mail {
|
|
|
693
753
|
user_id?: string
|
|
694
754
|
/** The member's department id. Value is valid when type is DEPARTMENT */
|
|
695
755
|
department_id?: string
|
|
696
|
-
/**
|
|
756
|
+
/**
|
|
757
|
+
* The type of member. Possible values are:
|
|
758
|
+
* - USER: internal user in the team
|
|
759
|
+
* - DEPARTMENT: member is a department
|
|
760
|
+
* - COMPANY: member is the company
|
|
761
|
+
* - EXTERNAL_USER: internet user outside the organization
|
|
762
|
+
* - MAIL_GROUP: member is another mail group
|
|
763
|
+
* - PUBLIC_MAILBOX: member is a public mailbox
|
|
764
|
+
* - OTHER_MEMBER: other internal member
|
|
765
|
+
*/
|
|
697
766
|
type?: 'USER' | 'DEPARTMENT' | 'COMPANY' | 'EXTERNAL_USER' | 'MAIL_GROUP' | 'PUBLIC_MAILBOX' | 'OTHER_MEMBER'
|
|
698
767
|
}
|
|
699
768
|
|
|
@@ -803,7 +872,11 @@ export namespace Mail {
|
|
|
803
872
|
department_id?: string
|
|
804
873
|
/** The member's email address. Value is valid when type is MAIL_GROUP/PUBLIC_MAILBOX */
|
|
805
874
|
email?: string
|
|
806
|
-
/**
|
|
875
|
+
/**
|
|
876
|
+
* The type of member. Possible values are:
|
|
877
|
+
* - USER: internal user in the team
|
|
878
|
+
* - DEPARTMENT: member is a department
|
|
879
|
+
*/
|
|
807
880
|
type?: 'USER' | 'DEPARTMENT' | 'MAIL_GROUP' | 'PUBLIC_MAILBOX'
|
|
808
881
|
}
|
|
809
882
|
|
|
@@ -823,7 +896,11 @@ export namespace Mail {
|
|
|
823
896
|
department_id?: string
|
|
824
897
|
/** The member's email address. Value is valid when type is MAIL_GROUP/PUBLIC_MAILBOX */
|
|
825
898
|
email?: string
|
|
826
|
-
/**
|
|
899
|
+
/**
|
|
900
|
+
* The type of member. Possible values are:
|
|
901
|
+
* - USER: internal user in the team
|
|
902
|
+
* - DEPARTMENT: member is a department
|
|
903
|
+
*/
|
|
827
904
|
type?: 'USER' | 'DEPARTMENT' | 'MAIL_GROUP' | 'PUBLIC_MAILBOX'
|
|
828
905
|
}
|
|
829
906
|
|
|
@@ -843,7 +920,11 @@ export namespace Mail {
|
|
|
843
920
|
department_id?: string
|
|
844
921
|
/** The member's email address. Value is valid when type is MAIL_GROUP/PUBLIC_MAILBOX */
|
|
845
922
|
email?: string
|
|
846
|
-
/**
|
|
923
|
+
/**
|
|
924
|
+
* The type of member. Possible values are:
|
|
925
|
+
* - USER: internal user in the team
|
|
926
|
+
* - DEPARTMENT: member is a department
|
|
927
|
+
*/
|
|
847
928
|
type?: 'USER' | 'DEPARTMENT' | 'MAIL_GROUP' | 'PUBLIC_MAILBOX'
|
|
848
929
|
}
|
|
849
930
|
|
|
@@ -1029,7 +1110,10 @@ export namespace Mail {
|
|
|
1029
1110
|
export interface CreateRequest {
|
|
1030
1111
|
/** The member's user id. Value is valid when type is USER */
|
|
1031
1112
|
user_id?: string
|
|
1032
|
-
/**
|
|
1113
|
+
/**
|
|
1114
|
+
* The type of member. Possible values are:
|
|
1115
|
+
* - USER: internal user in the team
|
|
1116
|
+
*/
|
|
1033
1117
|
type?: 'USER'
|
|
1034
1118
|
}
|
|
1035
1119
|
|
|
@@ -1043,7 +1127,10 @@ export namespace Mail {
|
|
|
1043
1127
|
member_id?: string
|
|
1044
1128
|
/** The member's user id. Value is valid when type is USER */
|
|
1045
1129
|
user_id?: string
|
|
1046
|
-
/**
|
|
1130
|
+
/**
|
|
1131
|
+
* The type of member. Possible values are:
|
|
1132
|
+
* - USER: internal user in the team
|
|
1133
|
+
*/
|
|
1047
1134
|
type?: 'USER'
|
|
1048
1135
|
}
|
|
1049
1136
|
|
|
@@ -1057,7 +1144,10 @@ export namespace Mail {
|
|
|
1057
1144
|
member_id?: string
|
|
1058
1145
|
/** The member's user id. Value is valid when type is USER */
|
|
1059
1146
|
user_id?: string
|
|
1060
|
-
/**
|
|
1147
|
+
/**
|
|
1148
|
+
* The type of member. Possible values are:
|
|
1149
|
+
* - USER: internal user in the team
|
|
1150
|
+
*/
|
|
1061
1151
|
type?: 'USER'
|
|
1062
1152
|
}
|
|
1063
1153
|
|
package/src/types/search.ts
CHANGED
|
@@ -193,7 +193,10 @@ export namespace Search {
|
|
|
193
193
|
}
|
|
194
194
|
|
|
195
195
|
export interface ListQuery extends Pagination {
|
|
196
|
-
/**
|
|
196
|
+
/**
|
|
197
|
+
* 回包数据格式,0-全量数据;1-摘要数据。
|
|
198
|
+
* **注**:摘要数据仅包含"id","name","state"。
|
|
199
|
+
*/
|
|
197
200
|
view?: ListQueryView
|
|
198
201
|
}
|
|
199
202
|
|
package/src/ws.ts
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { Adapter, Context, Schema, Universal } from '@satorijs/core'
|
|
2
|
+
import { LarkBot } from './bot'
|
|
3
|
+
import { adaptSession, EventPayload } from './utils'
|
|
4
|
+
import pb from 'protobufjs/light'
|
|
5
|
+
|
|
6
|
+
enum FrameType {
|
|
7
|
+
control = 0,
|
|
8
|
+
data = 1,
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
enum MessageType {
|
|
12
|
+
event = 'event',
|
|
13
|
+
card = 'card',
|
|
14
|
+
ping = 'ping',
|
|
15
|
+
pong = 'pong',
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface FrameSegment {
|
|
19
|
+
message_id: string
|
|
20
|
+
sum: number
|
|
21
|
+
seq: number
|
|
22
|
+
data: Uint8Array
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export class WsClient<C extends Context = Context> extends Adapter.WsClient<C, LarkBot<C, LarkBot.BaseConfig & WsClient.Options>> {
|
|
26
|
+
_deviceId: string
|
|
27
|
+
_serviceId: number
|
|
28
|
+
_pingInterval: number = 90000
|
|
29
|
+
_ping: NodeJS.Timeout
|
|
30
|
+
_cache: Record<string, FrameSegment[]> = {}
|
|
31
|
+
_frame: pb.Type
|
|
32
|
+
|
|
33
|
+
constructor(ctx: C, bot: LarkBot<C, LarkBot.BaseConfig & WsClient.Options>) {
|
|
34
|
+
super(ctx, bot)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async prepare() {
|
|
38
|
+
this._frame = pb.Root.fromJSON({
|
|
39
|
+
nested: {
|
|
40
|
+
Header: {
|
|
41
|
+
fields: {
|
|
42
|
+
key: { rule: 'required', type: 'string', id: 1 },
|
|
43
|
+
value: { rule: 'required', type: 'string', id: 2 },
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
Frame: {
|
|
47
|
+
fields: {
|
|
48
|
+
SeqID: { rule: 'required', type: 'uint64', id: 1 },
|
|
49
|
+
LogID: { rule: 'required', type: 'uint64', id: 2 },
|
|
50
|
+
service: { rule: 'required', type: 'int32', id: 3 },
|
|
51
|
+
method: { rule: 'required', type: 'int32', id: 4 },
|
|
52
|
+
headers: { rule: 'repeated', type: 'Header', id: 5 },
|
|
53
|
+
payloadEncoding: { type: 'string', id: 6 },
|
|
54
|
+
payloadType: { type: 'string', id: 7 },
|
|
55
|
+
payload: { type: 'bytes', id: 8 },
|
|
56
|
+
LogIDNew: { type: 'string', id: 9 },
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
}).lookupType('Frame')
|
|
61
|
+
|
|
62
|
+
const baseUrl = this.bot.config.baseURL ?? new URL(this.bot.config.endpoint).origin
|
|
63
|
+
const { code, data: { URL: url, ClientConfig: config }, msg } = await this.bot.http.post(`${baseUrl}/callback/ws/endpoint`, {
|
|
64
|
+
AppID: this.bot.config.appId,
|
|
65
|
+
AppSecret: this.bot.config.appSecret,
|
|
66
|
+
})
|
|
67
|
+
if (code !== 0) throw new Error(`failed to get gateway url: ${code} ${msg}`)
|
|
68
|
+
|
|
69
|
+
const urlObj = new URL(url)
|
|
70
|
+
this._deviceId = urlObj.searchParams.get('device_id')
|
|
71
|
+
this._serviceId = +urlObj.searchParams.get('service_id')
|
|
72
|
+
this._pingInterval = config.PingInterval * 1000
|
|
73
|
+
|
|
74
|
+
return this.bot.ctx.http.ws(url)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
ping() {
|
|
78
|
+
if (!this.socket || this.bot.status !== Universal.Status.ONLINE) {
|
|
79
|
+
clearTimeout(this._ping)
|
|
80
|
+
return
|
|
81
|
+
}
|
|
82
|
+
const frame = {
|
|
83
|
+
headers: [{
|
|
84
|
+
key: 'type',
|
|
85
|
+
value: MessageType.ping,
|
|
86
|
+
}],
|
|
87
|
+
service: this._serviceId,
|
|
88
|
+
method: FrameType.control,
|
|
89
|
+
SeqID: 0,
|
|
90
|
+
LogID: 0,
|
|
91
|
+
}
|
|
92
|
+
this.send(frame)
|
|
93
|
+
this._ping = setTimeout(() => this.ping(), this._pingInterval)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
async accept() {
|
|
97
|
+
await this.bot.initialize()
|
|
98
|
+
|
|
99
|
+
this.socket.addEventListener('message', async ({ data }) => {
|
|
100
|
+
const frame: {
|
|
101
|
+
headers: { key: string; value: string }[]
|
|
102
|
+
method: FrameType
|
|
103
|
+
payload?: Uint8Array
|
|
104
|
+
} = this._frame.decode(new Uint8Array(data as any)) as any
|
|
105
|
+
const headers: {
|
|
106
|
+
message_id: string
|
|
107
|
+
type: MessageType
|
|
108
|
+
sum: string
|
|
109
|
+
seq: string
|
|
110
|
+
} = (frame.headers ?? []).reduce((acc, cur) => {
|
|
111
|
+
acc[cur.key] = cur.value
|
|
112
|
+
return acc
|
|
113
|
+
}, {} as any)
|
|
114
|
+
|
|
115
|
+
if (frame.method === FrameType.control && headers.type === MessageType.pong) {
|
|
116
|
+
this.bot.logger.debug('pong')
|
|
117
|
+
return
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (frame.method === FrameType.data) {
|
|
121
|
+
if (headers.type !== MessageType.event) return
|
|
122
|
+
|
|
123
|
+
const data = this.retrieve({
|
|
124
|
+
message_id: headers.message_id,
|
|
125
|
+
sum: +headers.sum,
|
|
126
|
+
seq: +headers.seq,
|
|
127
|
+
data: frame.payload,
|
|
128
|
+
})
|
|
129
|
+
if (!data) return
|
|
130
|
+
|
|
131
|
+
const body: EventPayload = JSON.parse(Buffer.from(data).toString('utf8'))
|
|
132
|
+
if (!body.header) return
|
|
133
|
+
this.bot.logger.info('received event: %o', body)
|
|
134
|
+
body.type = body.header.event_type
|
|
135
|
+
const session = await adaptSession(this.bot, body)
|
|
136
|
+
this.bot.dispatch(session)
|
|
137
|
+
|
|
138
|
+
this.send({
|
|
139
|
+
...frame,
|
|
140
|
+
headers: [...frame.headers, { key: 'biz_rt', value: '0' }],
|
|
141
|
+
payload: Buffer.from(JSON.stringify({ code: 200 })),
|
|
142
|
+
})
|
|
143
|
+
}
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
this.socket.addEventListener('close', (e) => {
|
|
147
|
+
clearTimeout(this._ping)
|
|
148
|
+
this.bot.offline()
|
|
149
|
+
})
|
|
150
|
+
this.ping()
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
send(frame: any) {
|
|
154
|
+
const encoded = this._frame.encode(frame).finish()
|
|
155
|
+
this.socket.send(encoded as any)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
retrieve(seg: FrameSegment): Uint8Array | undefined {
|
|
159
|
+
const { message_id, sum } = seg
|
|
160
|
+
if (sum === 1) return seg.data
|
|
161
|
+
if (!this._cache[message_id]) this._cache[message_id] = []
|
|
162
|
+
|
|
163
|
+
this._cache[message_id].push(seg)
|
|
164
|
+
if (this._cache[message_id].length === sum) {
|
|
165
|
+
const data = Buffer.concat(this._cache[message_id].sort((a, b) => a.seq - b.seq).map((item) => item.data))
|
|
166
|
+
delete this._cache[message_id]
|
|
167
|
+
return data
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export namespace WsClient {
|
|
173
|
+
export interface Options extends Adapter.WsClientConfig {
|
|
174
|
+
protocol: 'ws'
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export const Options: Schema<Options> = Schema.intersect([
|
|
178
|
+
Schema.object({
|
|
179
|
+
protocol: Schema.const('ws').required(),
|
|
180
|
+
}),
|
|
181
|
+
Adapter.WsClientConfig,
|
|
182
|
+
])
|
|
183
|
+
}
|