feat: add more functionality
This commit is contained in:
+33
-71
@@ -7,9 +7,22 @@ import {
|
||||
serializeMessage,
|
||||
} from "./serializer";
|
||||
|
||||
const WS_URL = "wss://wa-bridge.rul.sh/ws?bridge=1";
|
||||
|
||||
let socket: WebSocket | undefined;
|
||||
let consts: Record<string, unknown> = {
|
||||
DEVICE_ID: String(Date.now()),
|
||||
DEVICE_NAME: "",
|
||||
};
|
||||
|
||||
function init() {
|
||||
try {
|
||||
const _consts = localStorage.getItem("wa-bridge-consts");
|
||||
if (!_consts) {
|
||||
localStorage.setItem("wa-bridge-consts", JSON.stringify(consts));
|
||||
} else {
|
||||
consts = JSON.parse(_consts);
|
||||
}
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
function send(message: unknown) {
|
||||
if (socket?.readyState !== WebSocket.OPEN) {
|
||||
@@ -46,6 +59,8 @@ async function handleRequest(request: BridgeRequest) {
|
||||
return {
|
||||
authenticated: WPP.conn.isAuthenticated(),
|
||||
ready: WPP.isReady,
|
||||
deviceId: consts.DEVICE_ID,
|
||||
deviceName: consts.DEVICE_NAME,
|
||||
};
|
||||
|
||||
case "listChats": {
|
||||
@@ -73,7 +88,9 @@ async function handleRequest(request: BridgeRequest) {
|
||||
case "listGroups": {
|
||||
const groups = await WPP.group.getAllGroups();
|
||||
|
||||
return groups.filter(Boolean).map(serializeGroupMetadata);
|
||||
return groups
|
||||
.filter((i) => i.groupMetadata != null)
|
||||
.map((i) => serializeGroupMetadata(i.groupMetadata));
|
||||
}
|
||||
|
||||
case "getMessages": {
|
||||
@@ -86,13 +103,6 @@ async function handleRequest(request: BridgeRequest) {
|
||||
return messages.filter(Boolean).map(serializeMessage);
|
||||
}
|
||||
|
||||
case "searchMessages":
|
||||
return searchMessages(
|
||||
request.params.query,
|
||||
request.params.chatId,
|
||||
request.params.limit ?? 100,
|
||||
);
|
||||
|
||||
case "sendText": {
|
||||
const result = await WPP.chat.sendTextMessage(
|
||||
request.params.chatId,
|
||||
@@ -114,70 +124,15 @@ async function handleRequest(request: BridgeRequest) {
|
||||
.then(WPP.util.blobToBase64);
|
||||
return media;
|
||||
|
||||
case "exec":
|
||||
const fn = new Function(request.params);
|
||||
return fn();
|
||||
|
||||
default:
|
||||
throw new Error(`Unknown method: ${(request as any).method}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* WA-JS doesn't need a separate "global search" API for our
|
||||
* first implementation. Search the messages loaded from chats.
|
||||
*
|
||||
* Later we can optimize this using WhatsApp's internal message
|
||||
* store/index if necessary.
|
||||
*/
|
||||
async function searchMessages(query: string, chatId?: string, limit = 100) {
|
||||
const normalized = query.toLowerCase();
|
||||
|
||||
let chats;
|
||||
|
||||
if (chatId) {
|
||||
const chat = WPP.chat.get(chatId);
|
||||
chats = chat ? [chat] : [];
|
||||
} else {
|
||||
chats = await WPP.chat.list();
|
||||
}
|
||||
|
||||
const results: any[] = [];
|
||||
|
||||
for (const chat of chats) {
|
||||
if (results.length >= limit) {
|
||||
break;
|
||||
}
|
||||
|
||||
const id = chat?.id?._serialized ?? chat?.id ?? chat?.wid?._serialized;
|
||||
|
||||
if (!id) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
const messages = await WPP.chat.getMessages(id, {
|
||||
count: 100,
|
||||
});
|
||||
|
||||
for (const message of messages) {
|
||||
const body = String(message.body ?? message.caption ?? "");
|
||||
|
||||
if (body.toLowerCase().includes(normalized)) {
|
||||
results.push({
|
||||
chatId: id,
|
||||
message: serialize(message),
|
||||
});
|
||||
|
||||
if (results.length >= limit) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn("Failed searching chat", id, err);
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
function connect() {
|
||||
if (
|
||||
socket &&
|
||||
@@ -187,16 +142,18 @@ function connect() {
|
||||
return;
|
||||
}
|
||||
|
||||
socket = new WebSocket(WS_URL);
|
||||
socket = new WebSocket(__WS_URL__);
|
||||
|
||||
socket.onopen = () => {
|
||||
console.log("[WA Bridge] connected");
|
||||
console.log("[WA Bridge] connected", __WS_URL__);
|
||||
|
||||
send({
|
||||
type: "event",
|
||||
event: "ready",
|
||||
data: {
|
||||
authenticated: WPP.conn.isAuthenticated(),
|
||||
deviceId: consts.DEVICE_ID,
|
||||
deviceName: consts.DEVICE_NAME,
|
||||
},
|
||||
});
|
||||
};
|
||||
@@ -205,6 +162,10 @@ function connect() {
|
||||
try {
|
||||
const request = JSON.parse(event.data) as BridgeRequest;
|
||||
|
||||
if (request.type === "ping") {
|
||||
send({ type: "pong" });
|
||||
}
|
||||
|
||||
if (request.type !== "request") {
|
||||
return;
|
||||
}
|
||||
@@ -260,4 +221,5 @@ function start() {
|
||||
});
|
||||
}
|
||||
|
||||
init();
|
||||
start();
|
||||
|
||||
Reference in New Issue
Block a user