-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
298 lines (294 loc) · 11.5 KB
/
bot.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
import lib from 'https://bash.ooo/lib.js';
import zhi from 'https://raw.githubusercontent.com/TxThinkingInc/zhi.js/refs/heads/master/lib.js';
import { v4, v7 } from 'uuid';
import path from 'node:path';
import os from 'node:os';
import { $ } from 'bun';
class Bot {
// chats: [{
// ChatUUID: "", // get from https://www.txthinking.com/zhi.html
// Key: "", // your Chat Key
// UserUUID: "", // get from https://www.txthinking.com/zhi.html
// Name: "", // bot name
// Avatar: "", // bot avatar, file path
// }]
static async init(BotToken, chats) {
var f = Bun.file(os.homedir() + "/.zhi.bot")
var j = {}
if (await f.exists()) {
j = JSON.parse(await f.text())
}
for (var i = 0; i < chats.length; i++) {
if (j[chats[i].ChatUUID] && j[chats[i].ChatUUID].Avatar == chats[i].Avatar) {
chats[i].AvatarUUID = j[chats[i].ChatUUID].AvatarUUID
continue
}
var b = await Bun.file(chats[i].Avatar).bytes()
var res = await fetch(`https://upload.zhi.shiliew.com/?ChatUUID=${chats[i].ChatUUID}&Kind=avatar&Token=${BotToken}`, {
method: "PUT",
body: await zhi.encrypt_file(chats[i].Key, chats[i].ChatUUID, chats[i].UserUUID, b),
})
if (res.status != 200) {
throw await res.text()
}
chats[i].AvatarUUID = await res.text()
}
var cache = {}
var cs = {}
chats.forEach(v => {
cache[v.ChatUUID] = {
Avatar: v.Avatar,
AvatarUUID: v.AvatarUUID,
}
cs[v.ChatUUID] = {
Key: v.Key,
UserUUID: v.UserUUID,
Name: v.Name,
AvatarUUID: v.AvatarUUID,
}
})
await Bun.write(os.homedir() + "/.zhi.bot", JSON.stringify(cache));
return new Bot(BotToken, cs);
}
constructor(token, chats) {
this.token = token
this.device = v4()
this.chats = chats
this.ws = null
}
connect() {
return new Promise((resolve, reject) => {
var ws = new WebSocket(`wss://api.txthinking.com/im/node/ws?Token=${this.token}&Device=${this.device}`);
ws.onopen = () => {
this.ws = ws
resolve();
}
ws.onerror = (error) => reject(error);
});
}
// f(e)
on_error(f) {
this.ws.addEventListener("error", e => {
f(e)
});
}
// f(reason)
on_close(f) {
this.ws.addEventListener("close", e => {
f(e.reason)
});
}
// f(message)
on_message(f) {
this.ws.addEventListener("message", async e => {
var m = JSON.parse(e.data)
if (!m.Payload) {
// a message sent successful
return
}
this.ws.send(JSON.stringify({ MessageUUID: m.MessageUUID }))
if (!this.chats[m.ChatUUID]) {
return
}
var s = await zhi.decrypt_payload(this.chats[m.ChatUUID].Key, m.ChatUUID, m.UserUUID, m.Payload)
var o = JSON.parse(s)
delete m.Payload
f({ ...o, ...m })
});
}
// you should call close if no need bot
close() {
this.ws.close()
}
async send_text(ChatUUID, text) {
this.ws.send(JSON.stringify({
MessageUUID: v7(),
ChatUUID: ChatUUID,
UserUUID: this.chats[ChatUUID].UserUUID,
Payload: await zhi.encrypt_payload(this.chats[ChatUUID].Key, ChatUUID, this.chats[ChatUUID].UserUUID, JSON.stringify({
Kind: "text",
Text: text,
CreatedAt: lib.now(),
Name: this.chats[ChatUUID].Name,
AvatarUUID: this.chats[ChatUUID].AvatarUUID,
})),
}))
}
async send_markdown(ChatUUID, text) {
this.ws.send(JSON.stringify({
MessageUUID: v7(),
ChatUUID: ChatUUID,
UserUUID: this.chats[ChatUUID].UserUUID,
Payload: await zhi.encrypt_payload(this.chats[ChatUUID].Key, ChatUUID, this.chats[ChatUUID].UserUUID, JSON.stringify({
Kind: "markdown",
Text: text,
CreatedAt: lib.now(),
Name: this.chats[ChatUUID].Name,
AvatarUUID: this.chats[ChatUUID].AvatarUUID,
})),
}))
}
// file path
// require: $ nami install ffmpeg
async send_image(ChatUUID, file) {
var FileName = path.basename(file)
var i = FileName.lastIndexOf(".");
if (i == -1) {
throw 'The file name is missing a valid extension';
}
if (!/^\w+$/.test(FileName.substring(i + 1))) {
throw 'The file name is missing a valid extension';
}
var s = await $`ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=p=0 ${file}`.text()
var l = s.trim().split(",")
var b = await Bun.file(file).bytes()
var res = await fetch(`https://upload.zhi.shiliew.com/?ChatUUID=${ChatUUID}&Kind=image&Token=${this.token}`, {
method: "PUT",
body: await zhi.encrypt_file(this.chats[ChatUUID].Key, ChatUUID, this.chats[ChatUUID].UserUUID, b),
})
if (res.status != 200) {
throw await res.text()
}
var FileUUID = await res.text()
this.ws.send(JSON.stringify({
MessageUUID: v7(),
ChatUUID: ChatUUID,
UserUUID: this.chats[ChatUUID].UserUUID,
Payload: await zhi.encrypt_payload(this.chats[ChatUUID].Key, ChatUUID, this.chats[ChatUUID].UserUUID, JSON.stringify({
Kind: "image",
Size: b.length,
Width: parseInt(l[0]),
Height: parseInt(l[1]),
FileName: FileName,
FileUUID: FileUUID,
CreatedAt: lib.now(),
Name: this.chats[ChatUUID].Name,
AvatarUUID: this.chats[ChatUUID].AvatarUUID,
})),
}))
}
// file path
// require: $ nami install ffmpeg
async send_video(ChatUUID, file) {
var FileName = path.basename(file)
var i = FileName.lastIndexOf(".");
if (i == -1) {
throw 'The file name is missing a valid extension';
}
if (!/^\w+$/.test(FileName.substring(i + 1))) {
throw 'The file name is missing a valid extension';
}
var s = await $`ffprobe -v error -show_entries format=duration -select_streams v:0 -show_entries stream=width,height -of default=noprint_wrappers=1:nokey=1 ${file}`.text()
var l = s.trim().split("\n")
var tf = `/tmp/${Date.now()}.jpeg`
await $`ffmpeg -i ${file} -ss 00:00:00 -vframes 1 -y ${tf} 2>/dev/null`
var b = await Bun.file(tf).bytes()
var res = await fetch(`https://upload.zhi.shiliew.com/?ChatUUID=${ChatUUID}&Kind=image&Token=${this.token}`, {
method: "PUT",
body: await zhi.encrypt_file(this.chats[ChatUUID].Key, ChatUUID, this.chats[ChatUUID].UserUUID, b),
})
if (res.status != 200) {
throw await res.text()
}
var ThumbnailUUID = await res.text()
var b = await Bun.file(file).bytes()
var res = await fetch(`https://upload.zhi.shiliew.com/?ChatUUID=${ChatUUID}&Kind=video&Token=${this.token}`, {
method: "PUT",
body: await zhi.encrypt_file(this.chats[ChatUUID].Key, ChatUUID, this.chats[ChatUUID].UserUUID, b),
})
if (res.status != 200) {
throw await res.text()
}
var FileUUID = await res.text()
this.ws.send(JSON.stringify({
MessageUUID: v7(),
ChatUUID: ChatUUID,
UserUUID: this.chats[ChatUUID].UserUUID,
Payload: await zhi.encrypt_payload(this.chats[ChatUUID].Key, ChatUUID, this.chats[ChatUUID].UserUUID, JSON.stringify({
Kind: "video",
Size: b.length,
Width: parseInt(l[0]),
Height: parseInt(l[1]),
Duration: parseInt(l[2]),
FileName: FileName,
ThumbnailUUID: ThumbnailUUID,
FileUUID: FileUUID,
CreatedAt: lib.now(),
Name: this.chats[ChatUUID].Name,
AvatarUUID: this.chats[ChatUUID].AvatarUUID,
})),
}))
}
// file path
async send_file(ChatUUID, file) {
var FileName = path.basename(file)
var i = FileName.lastIndexOf(".");
if (i == -1) {
throw 'The file name is missing a valid extension';
}
if (!/^\w+$/.test(FileName.substring(i + 1))) {
throw 'The file name is missing a valid extension';
}
var b = await Bun.file(file).bytes()
var res = await fetch(`https://upload.zhi.shiliew.com/?ChatUUID=${ChatUUID}&Kind=file&Token=${this.token}`, {
method: "PUT",
body: await zhi.encrypt_file(this.chats[ChatUUID].Key, ChatUUID, this.chats[ChatUUID].UserUUID, b),
})
if (res.status != 200) {
throw await res.text()
}
var FileUUID = await res.text()
this.ws.send(JSON.stringify({
MessageUUID: v7(),
ChatUUID: ChatUUID,
UserUUID: this.chats[ChatUUID].UserUUID,
Payload: await zhi.encrypt_payload(this.chats[ChatUUID].Key, ChatUUID, this.chats[ChatUUID].UserUUID, JSON.stringify({
Kind: "file",
Size: b.length,
FileName: FileName,
FileUUID: FileUUID,
CreatedAt: lib.now(),
Name: this.chats[ChatUUID].Name,
AvatarUUID: this.chats[ChatUUID].AvatarUUID,
})),
}))
}
// delete message from server db, and send a action to users to let them delete this message from their local db
async action_delete_message(ChatUUID, MessageUUID) {
var action = JSON.stringify({
Action: "DELETE",
MessageUUID: MessageUUID,
AvatarUUID: this.chats[ChatUUID].AvatarUUID,
})
this.ws.send(JSON.stringify({
MessageUUID: v7(),
ChatUUID: ChatUUID,
UserUUID: this.chats[ChatUUID].UserUUID,
Payload: `ACTION:${action}`,
}))
}
// kind is avatar/image/video/file, UUID is avatar UUID or file UUID
// return encrypted Uint8Array
async fetch_file(ChatUUID, kind, UUID) {
var res = await fetch(`https://zhi.shiliew.com/${ChatUUID}/${kind}/${UUID}?Token=${this.token}`)
if (res.status != 200) {
throw await res.text()
}
return new Uint8Array(await res.arrayBuffer())
}
// delete messages from server db, if specify MessageUUID then clear messages before/and MessageUUID, otherwise clear all
async clear_messages(ChatUUID, MessageUUID) {
var res = await fetch(`https://api.txthinking.com/im/http/BotClearMessages?ChatUUID=${ChatUUID}&MessageUUID=${MessageUUID ? MessageUUID : ''}&Token=${this.token}`)
if (res.status != 200) {
throw await res.text()
}
}
// remove a user
async remove_user(ChatUUID, UserUUID) {
var res = await fetch(`https://api.txthinking.com/im/http/BotRemoveUser?ChatUUID=${ChatUUID}&UserUUID=${UserUUID}&Token=${this.token}`)
if (res.status != 200) {
throw await res.text()
}
}
}
export default Bot