2024-06-05 15:12:48 +02:00
|
|
|
import { db, getWorkerUtils } from "@link-stack/bridge-common";
|
|
|
|
|
import * as signalApi from "@link-stack/signal-api";
|
|
|
|
|
const { Configuration, MessagesApi } = signalApi;
|
|
|
|
|
|
|
|
|
|
const fetchSignalMessagesTask = async (): Promise<void> => {
|
|
|
|
|
const worker = await getWorkerUtils();
|
|
|
|
|
const rows = await db.selectFrom("SignalBot").selectAll().execute();
|
|
|
|
|
const config = new Configuration({
|
|
|
|
|
basePath: process.env.BRIDGE_SIGNAL_URL,
|
|
|
|
|
});
|
|
|
|
|
const messagesClient = new MessagesApi(config);
|
|
|
|
|
|
|
|
|
|
for (const row of rows) {
|
|
|
|
|
const { id, phoneNumber: number } = row;
|
|
|
|
|
const messages = await messagesClient.v1ReceiveNumberGet({ number });
|
|
|
|
|
|
|
|
|
|
for (const msg of messages) {
|
2024-07-31 08:55:19 +02:00
|
|
|
console.log(msg);
|
2024-06-05 15:12:48 +02:00
|
|
|
const { envelope } = msg as any;
|
2024-06-28 12:28:11 +02:00
|
|
|
const { source, sourceUuid, dataMessage } = envelope;
|
|
|
|
|
const message = dataMessage?.message;
|
2024-07-31 08:55:19 +02:00
|
|
|
const rawTimestamp = dataMessage?.timestamp;
|
|
|
|
|
const timestamp = new Date(rawTimestamp);
|
|
|
|
|
const messageId = `${sourceUuid}-${rawTimestamp}`;
|
2024-07-18 11:08:01 +02:00
|
|
|
const attachment = undefined;
|
|
|
|
|
const mimeType = undefined;
|
|
|
|
|
const filename = undefined;
|
2024-06-05 15:12:48 +02:00
|
|
|
if (source !== number && message) {
|
|
|
|
|
await worker.addJob("signal/receive-signal-message", {
|
|
|
|
|
token: id,
|
2024-07-18 11:08:01 +02:00
|
|
|
to: number,
|
|
|
|
|
from: source,
|
|
|
|
|
messageId,
|
2024-06-05 15:12:48 +02:00
|
|
|
message,
|
2024-07-18 11:08:01 +02:00
|
|
|
sentAt: timestamp.toISOString(),
|
|
|
|
|
attachment,
|
|
|
|
|
filename,
|
|
|
|
|
mimeType,
|
2024-06-05 15:12:48 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default fetchSignalMessagesTask;
|