hyperclaw 5.2.5 → 5.2.7
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/dist/audit-NPINyRh4.js +445 -0
- package/dist/chat-8E4H6nqx.js +325 -0
- package/dist/chat-B5Y8u68o.js +325 -0
- package/dist/connector-1_9a4Mhv.js +276 -0
- package/dist/connector-BeHsEhpz.js +164 -0
- package/dist/connector-DD5pq6wT.js +566 -0
- package/dist/connector-DJ63fLj9.js +555 -0
- package/dist/connector-Ic8H84de.js +204 -0
- package/dist/daemon-B4dlIs7x.js +7 -0
- package/dist/daemon-DRhU750_.js +7 -0
- package/dist/daemon-Dhy9qq9P.js +421 -0
- package/dist/daemon-bJ8IYnkd.js +421 -0
- package/dist/delivery-1vTBQ0a0.js +95 -0
- package/dist/delivery-BbOfKejh.js +4 -0
- package/dist/delivery-D-ud3VlK.js +4 -0
- package/dist/delivery-DcK118_L.js +95 -0
- package/dist/engine-BjzV25HS.js +7 -0
- package/dist/engine-DJSr69DF.js +327 -0
- package/dist/engine-DgGG7aYJ.js +7 -0
- package/dist/engine-w5V7Xp_6.js +327 -0
- package/dist/heartbeat-engine-0swQl6wg.js +89 -0
- package/dist/hub-BuUwiTxh.js +6 -0
- package/dist/hub-DIoASRn6.js +512 -0
- package/dist/hyperclawbot-CIvGq2IG.js +516 -0
- package/dist/hyperclawbot-PfthmAly.js +516 -0
- package/dist/inference-BSWFHqzs.js +2854 -0
- package/dist/inference-DQiqWbqu.js +8 -0
- package/dist/loader-Bpju2Xqs.js +6 -0
- package/dist/loader-DRfmh8hU.js +410 -0
- package/dist/logger-CnxILOPV.js +86 -0
- package/dist/mcp-loader-Btdmmj1A.js +93 -0
- package/dist/mcp-loader-D-uIqYwB.js +93 -0
- package/dist/memory-auto-CK5M1YV8.js +5 -0
- package/dist/memory-auto-Cs6XiIxb.js +306 -0
- package/dist/node-4_wJsNEN.js +226 -0
- package/dist/oauth-flow-CJ7dFXKT.js +148 -0
- package/dist/onboard-BFuPscDx.js +3865 -0
- package/dist/onboard-BbJROE0s.js +13 -0
- package/dist/onboard-C1RArB82.js +3865 -0
- package/dist/onboard-CQkUrkNk.js +13 -0
- package/dist/orchestrator-D9R2u9yL.js +6 -0
- package/dist/orchestrator-DMDgfB8j.js +189 -0
- package/dist/orchestrator-Wv1gLWH6.js +189 -0
- package/dist/orchestrator-gncKq3wS.js +6 -0
- package/dist/pairing-CNNtZ8JR.js +6 -0
- package/dist/pairing-fGaxBlgG.js +207 -0
- package/dist/pc-access-CaE4x3Vt.js +8 -0
- package/dist/pc-access-OIwXRyAD.js +858 -0
- package/dist/run-main.js +50 -44
- package/dist/runner-BccY5FGT.js +1274 -0
- package/dist/runner-CFvEFt23.js +1274 -0
- package/dist/server-BSCeWSlZ.js +1304 -0
- package/dist/server-CnZ-Y6L-.js +1305 -0
- package/dist/server-DIwR4tT3.js +4 -0
- package/dist/server-MpkM9aIZ.js +4 -0
- package/dist/skill-runtime-CCwGR7iX.js +5 -0
- package/dist/skill-runtime-DUlp_2uW.js +104 -0
- package/dist/skill-runtime-RB7_EOoi.js +5 -0
- package/dist/skill-runtime-vmBIhuVk.js +104 -0
- package/dist/src-3dXyf5GQ.js +458 -0
- package/dist/src-BRDu1tbl.js +63 -0
- package/dist/src-BTIUlese.js +458 -0
- package/dist/src-BVeLalMV.js +63 -0
- package/dist/sub-agent-tools-BgXug8yS.js +39 -0
- package/dist/sub-agent-tools-C1dWyUAR.js +39 -0
- package/dist/tts-elevenlabs-F_xjKQ-I.js +64 -0
- package/dist/vision-BR5Gdb2s.js +169 -0
- package/dist/vision-BZG7_ybG.js +167 -0
- package/dist/vision-tools-BZM7OifC.js +51 -0
- package/dist/vision-tools-COLYNbXb.js +5 -0
- package/dist/vision-tools-DuB1QtlE.js +51 -0
- package/dist/vision-tools-LvL8RMWR.js +5 -0
- package/dist/voice-transcription-D6dK7b9A.js +171 -0
- package/dist/voice-transcription-j1jpOpH2.js +170 -0
- package/dist/website-watch-tools-Cqp7RPvn.js +176 -0
- package/dist/website-watch-tools-UPSrnBk2.js +5 -0
- package/package.json +1 -1
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
const require_chunk = require('./chunk-jS-bbMI5.js');
|
|
2
|
+
const chalk = require_chunk.__toESM(require("chalk"));
|
|
3
|
+
const fs_extra = require_chunk.__toESM(require("fs-extra"));
|
|
4
|
+
const path = require_chunk.__toESM(require("path"));
|
|
5
|
+
const os = require_chunk.__toESM(require("os"));
|
|
6
|
+
const https = require_chunk.__toESM(require("https"));
|
|
7
|
+
const events = require_chunk.__toESM(require("events"));
|
|
8
|
+
|
|
9
|
+
//#region extensions/telegram/src/connector.ts
|
|
10
|
+
function tgDownloadFile(token, filePath) {
|
|
11
|
+
return new Promise((resolve, reject) => {
|
|
12
|
+
const req = https.default.request({
|
|
13
|
+
hostname: "api.telegram.org",
|
|
14
|
+
port: 443,
|
|
15
|
+
path: `/file/bot${token}/${filePath}`,
|
|
16
|
+
method: "GET"
|
|
17
|
+
}, (res) => {
|
|
18
|
+
const chunks = [];
|
|
19
|
+
res.on("data", (c) => chunks.push(c));
|
|
20
|
+
res.on("end", () => resolve(Buffer.concat(chunks)));
|
|
21
|
+
});
|
|
22
|
+
req.on("error", reject);
|
|
23
|
+
req.setTimeout(3e4, () => {
|
|
24
|
+
req.destroy();
|
|
25
|
+
reject(new Error("Download timeout"));
|
|
26
|
+
});
|
|
27
|
+
req.end();
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
function tgRequest(token, method, body) {
|
|
31
|
+
return new Promise((resolve, reject) => {
|
|
32
|
+
const payload = body ? JSON.stringify(body) : null;
|
|
33
|
+
const req = https.default.request({
|
|
34
|
+
hostname: "api.telegram.org",
|
|
35
|
+
port: 443,
|
|
36
|
+
path: `/bot${token}/${method}`,
|
|
37
|
+
method: payload ? "POST" : "GET",
|
|
38
|
+
headers: payload ? {
|
|
39
|
+
"Content-Type": "application/json",
|
|
40
|
+
"Content-Length": Buffer.byteLength(payload)
|
|
41
|
+
} : {}
|
|
42
|
+
}, (res) => {
|
|
43
|
+
let data = "";
|
|
44
|
+
res.on("data", (c) => data += c);
|
|
45
|
+
res.on("end", () => {
|
|
46
|
+
try {
|
|
47
|
+
const r = JSON.parse(data);
|
|
48
|
+
if (r.ok) resolve(r.result);
|
|
49
|
+
else reject(new Error(r.description || "Telegram API error"));
|
|
50
|
+
} catch {
|
|
51
|
+
reject(new Error("Invalid JSON"));
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
req.on("error", reject);
|
|
56
|
+
req.setTimeout(35e3, () => {
|
|
57
|
+
req.destroy();
|
|
58
|
+
reject(new Error("Timeout"));
|
|
59
|
+
});
|
|
60
|
+
if (payload) req.write(payload);
|
|
61
|
+
req.end();
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
const STATE_FILE = path.default.join(os.default.homedir(), ".hyperclaw", "telegram-state.json");
|
|
65
|
+
function sleep(ms) {
|
|
66
|
+
return new Promise((r) => setTimeout(r, ms));
|
|
67
|
+
}
|
|
68
|
+
function generateCode() {
|
|
69
|
+
const chars = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
|
|
70
|
+
return Array.from({ length: 6 }, () => chars[Math.floor(Math.random() * chars.length)]).join("");
|
|
71
|
+
}
|
|
72
|
+
function chunkText(text, max) {
|
|
73
|
+
if (text.length <= max) return [text];
|
|
74
|
+
const chunks = [];
|
|
75
|
+
let i = 0;
|
|
76
|
+
while (i < text.length) {
|
|
77
|
+
let end = Math.min(i + max, text.length);
|
|
78
|
+
if (end < text.length) {
|
|
79
|
+
const nl = text.lastIndexOf("\n", end);
|
|
80
|
+
if (nl > i) end = nl + 1;
|
|
81
|
+
}
|
|
82
|
+
chunks.push(text.slice(i, end));
|
|
83
|
+
i = end;
|
|
84
|
+
}
|
|
85
|
+
return chunks;
|
|
86
|
+
}
|
|
87
|
+
var TelegramConnector = class extends events.EventEmitter {
|
|
88
|
+
token;
|
|
89
|
+
config;
|
|
90
|
+
offset = 0;
|
|
91
|
+
running = false;
|
|
92
|
+
botInfo = null;
|
|
93
|
+
constructor(token, config) {
|
|
94
|
+
super();
|
|
95
|
+
this.token = token;
|
|
96
|
+
this.config = {
|
|
97
|
+
token,
|
|
98
|
+
dmPolicy: "allowlist",
|
|
99
|
+
allowFrom: [],
|
|
100
|
+
pendingPairings: {},
|
|
101
|
+
approvedPairings: [],
|
|
102
|
+
...config
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
async connect() {
|
|
106
|
+
this.botInfo = await tgRequest(this.token, "getMe");
|
|
107
|
+
console.log(chalk.default.green(` 🦅 Telegram: @${this.botInfo?.username} connected`));
|
|
108
|
+
await this.loadState();
|
|
109
|
+
this.running = true;
|
|
110
|
+
this.pollLoop();
|
|
111
|
+
this.emit("connected", this.botInfo);
|
|
112
|
+
}
|
|
113
|
+
async disconnect() {
|
|
114
|
+
this.running = false;
|
|
115
|
+
await this.saveState();
|
|
116
|
+
}
|
|
117
|
+
async pollLoop() {
|
|
118
|
+
while (this.running) try {
|
|
119
|
+
const updates = await tgRequest(this.token, "getUpdates", {
|
|
120
|
+
offset: this.offset,
|
|
121
|
+
timeout: 30,
|
|
122
|
+
allowed_updates: ["message"]
|
|
123
|
+
});
|
|
124
|
+
for (const u of updates) {
|
|
125
|
+
this.offset = u.update_id + 1;
|
|
126
|
+
await this.handleUpdate(u).catch((e) => console.log(chalk.default.yellow(` ⚠ ${e.message}`)));
|
|
127
|
+
}
|
|
128
|
+
} catch (e) {
|
|
129
|
+
if (this.running) {
|
|
130
|
+
console.log(chalk.default.yellow(` ⚠ Telegram poll: ${e.message}`));
|
|
131
|
+
await sleep(5e3);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
async handleUpdate(u) {
|
|
136
|
+
const msg = u.message || u.edited_message;
|
|
137
|
+
if (!msg) return;
|
|
138
|
+
const text = msg.text?.trim() || "";
|
|
139
|
+
if (!text && !msg.voice) return;
|
|
140
|
+
const userId = String(msg.from?.id || "");
|
|
141
|
+
const isDM = msg.chat.type === "private";
|
|
142
|
+
const isGroup = msg.chat.type === "group" || msg.chat.type === "supergroup";
|
|
143
|
+
if (isDM && !await this.checkDMPolicy(userId, msg.chat.id, text || "[voice note]")) return;
|
|
144
|
+
let finalText = text || "[voice note]";
|
|
145
|
+
if (isGroup) {
|
|
146
|
+
const groupAllowFrom = this.config.groupAllowFrom ?? [];
|
|
147
|
+
if (groupAllowFrom.length > 0 && !groupAllowFrom.includes(String(msg.chat.id))) return;
|
|
148
|
+
const activation = this.config.groupActivation ?? "mention";
|
|
149
|
+
if (activation === "mention") {
|
|
150
|
+
const botUsername = this.botInfo?.username ? `@${this.botInfo.username}`.toLowerCase() : "";
|
|
151
|
+
const mentioned = botUsername && text.toLowerCase().includes(botUsername);
|
|
152
|
+
const isReplyToBot = msg.reply_to_message?.from?.is_bot && msg.reply_to_message.from.id === this.botInfo?.id;
|
|
153
|
+
if (!mentioned && !isReplyToBot) return;
|
|
154
|
+
if (mentioned && botUsername) finalText = text.replace(new RegExp(botUsername, "gi"), "").trim() || finalText;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
let audioBuffer;
|
|
158
|
+
if (msg?.voice?.file_id) try {
|
|
159
|
+
const file = await tgRequest(this.token, "getFile", { file_id: msg.voice.file_id });
|
|
160
|
+
audioBuffer = await tgDownloadFile(this.token, file.file_path);
|
|
161
|
+
} catch (e) {
|
|
162
|
+
console.log(chalk.default.yellow(` ⚠ Telegram voice download failed: ${e.message}`));
|
|
163
|
+
}
|
|
164
|
+
this.emit("message", {
|
|
165
|
+
id: String(msg.message_id),
|
|
166
|
+
channelId: "telegram",
|
|
167
|
+
from: userId,
|
|
168
|
+
fromUsername: msg.from?.username,
|
|
169
|
+
chatId: msg.chat.id,
|
|
170
|
+
text: finalText,
|
|
171
|
+
audioBuffer,
|
|
172
|
+
timestamp: new Date(msg.date * 1e3).toISOString(),
|
|
173
|
+
isDM
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
async checkDMPolicy(userId, chatId, text) {
|
|
177
|
+
if (this.config.dmPolicy === "none") return false;
|
|
178
|
+
if (this.config.dmPolicy === "open") return true;
|
|
179
|
+
if (this.config.dmPolicy === "allowlist") {
|
|
180
|
+
if (this.config.allowFrom.includes(userId)) return true;
|
|
181
|
+
await this.sendMessage(chatId, `🦅 *HyperClaw*\n\nYou are not on the allowlist.`);
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
if (this.config.dmPolicy === "pairing") {
|
|
185
|
+
if (this.config.approvedPairings.includes(userId)) return true;
|
|
186
|
+
const upper = text.trim().toUpperCase();
|
|
187
|
+
if (this.config.pendingPairings[upper]) {
|
|
188
|
+
this.config.approvedPairings.push(userId);
|
|
189
|
+
delete this.config.pendingPairings[upper];
|
|
190
|
+
await this.saveState();
|
|
191
|
+
await this.sendMessage(chatId, `🦅 *Paired!* You can now send messages.`);
|
|
192
|
+
this.emit("pairing:approved", {
|
|
193
|
+
userId,
|
|
194
|
+
channelId: "telegram"
|
|
195
|
+
});
|
|
196
|
+
return true;
|
|
197
|
+
}
|
|
198
|
+
const code = generateCode();
|
|
199
|
+
this.config.pendingPairings[code] = userId;
|
|
200
|
+
await this.saveState();
|
|
201
|
+
await this.sendMessage(chatId, `🦅 *HyperClaw Pairing*\n\nSend the owner this code:\n\`${code}\`\n\nApprove with:\n\`hyperclaw pairing approve telegram ${code}\``);
|
|
202
|
+
console.log(chalk.default.cyan(` 🦅 Telegram pairing from ${userId} — code: ${code}`));
|
|
203
|
+
return false;
|
|
204
|
+
}
|
|
205
|
+
return false;
|
|
206
|
+
}
|
|
207
|
+
async sendMessage(chatId, text, opts = {}) {
|
|
208
|
+
const chunks = chunkText(text, 4096);
|
|
209
|
+
let last = null;
|
|
210
|
+
for (const chunk of chunks) last = await tgRequest(this.token, "sendMessage", {
|
|
211
|
+
chat_id: chatId,
|
|
212
|
+
text: chunk,
|
|
213
|
+
parse_mode: opts.parse_mode || "Markdown",
|
|
214
|
+
disable_web_page_preview: opts.disable_web_page_preview ?? true,
|
|
215
|
+
...opts.reply_to_message_id ? { reply_to_message_id: opts.reply_to_message_id } : {}
|
|
216
|
+
});
|
|
217
|
+
return last;
|
|
218
|
+
}
|
|
219
|
+
async sendTyping(chatId) {
|
|
220
|
+
await tgRequest(this.token, "sendChatAction", {
|
|
221
|
+
chat_id: chatId,
|
|
222
|
+
action: "typing"
|
|
223
|
+
}).catch(() => {});
|
|
224
|
+
}
|
|
225
|
+
approvePairing(code) {
|
|
226
|
+
const upper = code.toUpperCase();
|
|
227
|
+
if (!this.config.pendingPairings[upper]) return false;
|
|
228
|
+
const userId = this.config.pendingPairings[upper];
|
|
229
|
+
this.config.approvedPairings.push(userId);
|
|
230
|
+
delete this.config.pendingPairings[upper];
|
|
231
|
+
this.saveState();
|
|
232
|
+
return true;
|
|
233
|
+
}
|
|
234
|
+
addToAllowlist(userId) {
|
|
235
|
+
if (!this.config.allowFrom.includes(userId)) {
|
|
236
|
+
this.config.allowFrom.push(userId);
|
|
237
|
+
this.saveState();
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
listPendingPairings() {
|
|
241
|
+
return Object.entries(this.config.pendingPairings).map(([code, userId]) => ({
|
|
242
|
+
code,
|
|
243
|
+
userId
|
|
244
|
+
}));
|
|
245
|
+
}
|
|
246
|
+
handleWebhookPayload(body) {
|
|
247
|
+
try {
|
|
248
|
+
this.handleUpdate(JSON.parse(body));
|
|
249
|
+
} catch {}
|
|
250
|
+
}
|
|
251
|
+
async loadState() {
|
|
252
|
+
try {
|
|
253
|
+
const s = await fs_extra.default.readJson(STATE_FILE);
|
|
254
|
+
this.offset = s.offset || 0;
|
|
255
|
+
if (s.pendingPairings) this.config.pendingPairings = s.pendingPairings;
|
|
256
|
+
if (s.approvedPairings) this.config.approvedPairings = s.approvedPairings;
|
|
257
|
+
} catch {}
|
|
258
|
+
}
|
|
259
|
+
async saveState() {
|
|
260
|
+
await fs_extra.default.ensureDir(path.default.dirname(STATE_FILE));
|
|
261
|
+
await fs_extra.default.writeJson(STATE_FILE, {
|
|
262
|
+
offset: this.offset,
|
|
263
|
+
pendingPairings: this.config.pendingPairings,
|
|
264
|
+
approvedPairings: this.config.approvedPairings
|
|
265
|
+
}, { spaces: 2 });
|
|
266
|
+
}
|
|
267
|
+
isRunning() {
|
|
268
|
+
return this.running;
|
|
269
|
+
}
|
|
270
|
+
getBotInfo() {
|
|
271
|
+
return this.botInfo;
|
|
272
|
+
}
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
//#endregion
|
|
276
|
+
exports.TelegramConnector = TelegramConnector;
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
const require_chunk = require('./chunk-jS-bbMI5.js');
|
|
2
|
+
const chalk = require_chunk.__toESM(require("chalk"));
|
|
3
|
+
const fs_extra = require_chunk.__toESM(require("fs-extra"));
|
|
4
|
+
const path = require_chunk.__toESM(require("path"));
|
|
5
|
+
const os = require_chunk.__toESM(require("os"));
|
|
6
|
+
const events = require_chunk.__toESM(require("events"));
|
|
7
|
+
|
|
8
|
+
//#region extensions/whatsapp-baileys/src/connector.ts
|
|
9
|
+
const AUTH_DIR = path.default.join(os.default.homedir(), ".hyperclaw", "baileys-auth");
|
|
10
|
+
const STATE_FILE = path.default.join(os.default.homedir(), ".hyperclaw", "whatsapp-baileys-state.json");
|
|
11
|
+
var WhatsAppBaileysConnector = class extends events.EventEmitter {
|
|
12
|
+
config;
|
|
13
|
+
sock = null;
|
|
14
|
+
running = false;
|
|
15
|
+
qrCallback;
|
|
16
|
+
constructor(config = {}) {
|
|
17
|
+
super();
|
|
18
|
+
this.config = {
|
|
19
|
+
dmPolicy: "allowlist",
|
|
20
|
+
allowFrom: [],
|
|
21
|
+
approvedPairings: [],
|
|
22
|
+
pendingPairings: {},
|
|
23
|
+
...config
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
onQR(cb) {
|
|
27
|
+
this.qrCallback = cb;
|
|
28
|
+
}
|
|
29
|
+
async connect() {
|
|
30
|
+
try {
|
|
31
|
+
const makeWASocket$1 = require("@whiskeysockets/baileys");
|
|
32
|
+
const { useMultiFileAuthState: useMultiFileAuthState$1 } = require("@whiskeysockets/baileys");
|
|
33
|
+
} catch {
|
|
34
|
+
throw new Error("Baileys not installed. Run: npm install @whiskeysockets/baileys");
|
|
35
|
+
}
|
|
36
|
+
const makeWASocket = require("@whiskeysockets/baileys").default;
|
|
37
|
+
const { useMultiFileAuthState } = require("@whiskeysockets/baileys");
|
|
38
|
+
await fs_extra.default.ensureDir(AUTH_DIR);
|
|
39
|
+
const { state, saveCreds } = await useMultiFileAuthState(AUTH_DIR);
|
|
40
|
+
await this.loadState();
|
|
41
|
+
this.sock = makeWASocket({
|
|
42
|
+
auth: state,
|
|
43
|
+
printQRInTerminal: true,
|
|
44
|
+
generateHighQualityLinkPreview: false
|
|
45
|
+
});
|
|
46
|
+
this.sock.ev.on("creds.update", saveCreds);
|
|
47
|
+
this.sock.ev.on("connection.update", (upd) => {
|
|
48
|
+
if (upd.qr) this.qrCallback?.(upd.qr);
|
|
49
|
+
if (upd.connection === "open") {
|
|
50
|
+
console.log(chalk.default.green(" 🦅 WhatsApp Baileys: connected"));
|
|
51
|
+
this.emit("connected", {});
|
|
52
|
+
}
|
|
53
|
+
if (upd.connection === "close") this.emit("disconnected", upd);
|
|
54
|
+
});
|
|
55
|
+
this.sock.ev.on("messages.upsert", async ({ messages }) => {
|
|
56
|
+
for (const m of messages) {
|
|
57
|
+
if (m.key.fromMe) continue;
|
|
58
|
+
const msg = m.message;
|
|
59
|
+
if (!msg) continue;
|
|
60
|
+
let text = msg.conversation || msg.extendedTextMessage?.text || "";
|
|
61
|
+
const from = (m.key.remoteJid || "").replace(/@.*/, "");
|
|
62
|
+
if (msg.audioMessage || msg.pttMessage) {
|
|
63
|
+
let audioBuffer;
|
|
64
|
+
try {
|
|
65
|
+
const { downloadMediaMessage } = require("@whiskeysockets/baileys");
|
|
66
|
+
audioBuffer = await downloadMediaMessage(m, "buffer", {}, { reuploadRequest: this.sock?.updateMessageSent });
|
|
67
|
+
} catch {}
|
|
68
|
+
this.emit("message", {
|
|
69
|
+
id: m.key.id,
|
|
70
|
+
channelId: "whatsapp-baileys",
|
|
71
|
+
from,
|
|
72
|
+
chatId: m.key.remoteJid,
|
|
73
|
+
text: "[voice note]",
|
|
74
|
+
audioBuffer,
|
|
75
|
+
timestamp: new Date(parseInt(m.messageTimestamp || "0") * 1e3).toISOString(),
|
|
76
|
+
isDM: true
|
|
77
|
+
});
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
if (!text) continue;
|
|
81
|
+
const allowed = await this.checkDMPolicy(from, text);
|
|
82
|
+
if (!allowed) continue;
|
|
83
|
+
this.emit("message", {
|
|
84
|
+
id: m.key.id,
|
|
85
|
+
channelId: "whatsapp-baileys",
|
|
86
|
+
from,
|
|
87
|
+
chatId: m.key.remoteJid,
|
|
88
|
+
text,
|
|
89
|
+
timestamp: new Date(parseInt(m.messageTimestamp) * 1e3).toISOString(),
|
|
90
|
+
isDM: true
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
this.running = true;
|
|
95
|
+
}
|
|
96
|
+
async checkDMPolicy(from, text) {
|
|
97
|
+
if (this.config.dmPolicy === "none") return false;
|
|
98
|
+
if (this.config.dmPolicy === "open") return true;
|
|
99
|
+
if (this.config.dmPolicy === "allowlist") {
|
|
100
|
+
if (this.config.allowFrom.includes(from)) return true;
|
|
101
|
+
await this.sendMessage(from, "🦅 HyperClaw: Not on allowlist.").catch(() => {});
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
if (this.config.dmPolicy === "pairing") {
|
|
105
|
+
if (this.config.approvedPairings.includes(from)) return true;
|
|
106
|
+
const upper = text.trim().toUpperCase();
|
|
107
|
+
if (this.config.pendingPairings[upper]) {
|
|
108
|
+
this.config.approvedPairings.push(from);
|
|
109
|
+
delete this.config.pendingPairings[upper];
|
|
110
|
+
await this.saveState();
|
|
111
|
+
await this.sendMessage(from, "🦅 Paired!").catch(() => {});
|
|
112
|
+
this.emit("pairing:approved", {
|
|
113
|
+
userId: from,
|
|
114
|
+
channelId: "whatsapp-baileys"
|
|
115
|
+
});
|
|
116
|
+
return true;
|
|
117
|
+
}
|
|
118
|
+
const code = Array.from({ length: 6 }, () => "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"[Math.floor(Math.random() * 32)]).join("");
|
|
119
|
+
this.config.pendingPairings[code] = from;
|
|
120
|
+
await this.saveState();
|
|
121
|
+
await this.sendMessage(from, `🦅 Code: ${code}\nApprove: hyperclaw pairing approve whatsapp-baileys ${code}`).catch(() => {});
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
async sendMessage(to, text) {
|
|
127
|
+
if (!this.sock) return;
|
|
128
|
+
const jid = to.includes("@") ? to : `${to}@s.whatsapp.net`;
|
|
129
|
+
await this.sock.sendMessage(jid, { text: text.slice(0, 4096) });
|
|
130
|
+
}
|
|
131
|
+
disconnect() {
|
|
132
|
+
this.running = false;
|
|
133
|
+
this.sock?.end?.();
|
|
134
|
+
this.sock = null;
|
|
135
|
+
}
|
|
136
|
+
approvePairing(code) {
|
|
137
|
+
const upper = code.toUpperCase();
|
|
138
|
+
if (!this.config.pendingPairings[upper]) return false;
|
|
139
|
+
this.config.approvedPairings.push(this.config.pendingPairings[upper]);
|
|
140
|
+
delete this.config.pendingPairings[upper];
|
|
141
|
+
this.saveState();
|
|
142
|
+
return true;
|
|
143
|
+
}
|
|
144
|
+
async loadState() {
|
|
145
|
+
try {
|
|
146
|
+
const s = await fs_extra.default.readJson(STATE_FILE);
|
|
147
|
+
if (s.p) this.config.pendingPairings = s.p;
|
|
148
|
+
if (s.a) this.config.approvedPairings = s.a;
|
|
149
|
+
} catch {}
|
|
150
|
+
}
|
|
151
|
+
async saveState() {
|
|
152
|
+
await fs_extra.default.ensureDir(path.default.dirname(STATE_FILE));
|
|
153
|
+
await fs_extra.default.writeJson(STATE_FILE, {
|
|
154
|
+
p: this.config.pendingPairings,
|
|
155
|
+
a: this.config.approvedPairings
|
|
156
|
+
}, { spaces: 2 });
|
|
157
|
+
}
|
|
158
|
+
isRunning() {
|
|
159
|
+
return this.running;
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
//#endregion
|
|
164
|
+
exports.WhatsAppBaileysConnector = WhatsAppBaileysConnector;
|