/
message-event.ts
203 lines (169 loc) · 5.22 KB
/
message-event.ts
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
import { Context, ContextFactoryOptions, ContextDefaultState } from './context';
import { IMessageContextSendOptions, MessageContext } from './message';
import { getRandomId, pickProperties } from '../../utils/helpers';
import { UpdateSource, kSerializeData } from '../../utils/constants';
export interface IMessageEventShowSnackbar {
type: 'show_snackbar';
text: string;
}
export interface IMessageEventOpenLink {
type: 'open_link';
link: string;
}
export interface IMessageEventOpenApp {
type: 'open_app';
app_id: number;
owner_id?: number;
hash: string;
}
export type MessageEventAction =
IMessageEventShowSnackbar
| IMessageEventOpenLink
| IMessageEventOpenApp;
export type MessageEventContextType = 'message_event';
export type MessageEventContextSubType = 'message_event';
export interface IMessageEventContextPayload {
user_id: number;
conversation_message_id: number;
peer_id: number;
event_id: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
payload: any;
}
export type MessageEventContextOptions<S> =
ContextFactoryOptions<IMessageEventContextPayload, S>;
export class MessageEventContext<S = ContextDefaultState>
extends Context<
IMessageEventContextPayload,
S,
MessageEventContextType,
MessageEventContextSubType
> {
public constructor(options: MessageEventContextOptions<S>) {
super({
...options,
type: 'message_event',
subTypes: [
options.updateType as MessageEventContextSubType,
],
});
}
/**
* Returns the identifier user
*/
public get userId(): number {
return this.payload.user_id;
}
/**
* Returns the conversation message id
*/
public get conversationMessageId(): number {
return this.payload.conversation_message_id;
}
/**
* Returns the destination identifier
*/
public get peerId(): number {
return this.payload.peer_id;
}
/**
* Returns a random string. Active for a minute, after a minute becomes invalid
*/
public get eventId(): string {
return this.payload.event_id;
}
/**
* Returns the event payload
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public get eventPayload(): any {
return this.payload.payload;
}
/**
* Dispatches an event with an action that will occur when the callback button is pressed
*/
public answer(eventData: MessageEventAction): Promise<1> {
return this.api.messages.sendMessageEventAnswer({
event_id: this.eventId,
peer_id: this.peerId,
user_id: this.userId,
event_data: JSON.stringify(eventData),
});
}
/**
* Sends a message to the current dialog
*/
async send(
text: string | IMessageContextSendOptions,
params?: IMessageContextSendOptions,
): Promise<MessageContext<S>> {
const randomId = getRandomId();
const options = {
random_id: randomId,
...(
typeof text !== 'object'
? {
message: text,
...params,
}
: text
),
} as IMessageContextSendOptions;
if (this.$groupId !== undefined) {
options.peer_ids = this.peerId;
} else {
options.peer_id = this.peerId;
}
const rawDestination = await this.api.messages.send(options);
const destination = typeof rawDestination !== 'number'
? rawDestination[0] as {
peer_id : number;
message_id: number;
conversation_message_id: number;
error: number;
}
: {
peer_id: this.peer_id,
message_id: rawDestination,
conversation_message_id: 0,
};
const messageContext = new MessageContext<S>({
api: this.api,
upload: this.upload,
source: UpdateSource.WEBHOOK,
groupId: this.$groupId,
updateType: 'message_new',
state: this.state,
payload: {
client_info: this.clientInfo,
message: {
id: destination.message_id,
conversation_message_id: destination.conversation_message_id,
from_id: this.$groupId as unknown as number,
peer_id: destination.peer_id,
out: 1,
important: false,
random_id: randomId,
text: options.text,
date: Math.floor(Date.now() / 1000),
attachments: [],
},
},
});
// @ts-expect-error private method
messageContext.$filled = false;
return messageContext;
}
/**
* Returns the custom data
*/
public [kSerializeData](): object {
return pickProperties(this, [
'userId',
'conversationMessageId',
'peerId',
'eventId',
'eventPayload',
]);
}
}