Signal API updates
This commit is contained in:
parent
83653ef23b
commit
c729a46a0c
25 changed files with 501 additions and 279 deletions
32
apps/bridge-worker/tasks/signal/fetch-signal-messages.ts
Normal file
32
apps/bridge-worker/tasks/signal/fetch-signal-messages.ts
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
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) {
|
||||
const { envelope } = msg as any;
|
||||
const { source } = envelope;
|
||||
const message = envelope?.dataMessage?.message;
|
||||
if (source !== number && message) {
|
||||
await worker.addJob("signal/receive-signal-message", {
|
||||
token: id,
|
||||
sender: source,
|
||||
message,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default fetchSignalMessagesTask;
|
||||
|
|
@ -1,11 +1,33 @@
|
|||
// import { db, getWorkerUtils } from "@link-stack/bridge-common";
|
||||
import { db, getWorkerUtils } from "@link-stack/bridge-common";
|
||||
|
||||
interface ReceiveSignalMessageTaskOptions {
|
||||
message: any;
|
||||
token: string;
|
||||
sender: string;
|
||||
message: string;
|
||||
}
|
||||
|
||||
const receiveSignalMessageTask = async ({
|
||||
token,
|
||||
sender,
|
||||
message,
|
||||
}: ReceiveSignalMessageTaskOptions): Promise<void> => {};
|
||||
}: ReceiveSignalMessageTaskOptions): Promise<void> => {
|
||||
console.log({ token, sender, message });
|
||||
const worker = await getWorkerUtils();
|
||||
const row = await db
|
||||
.selectFrom("SignalBot")
|
||||
.selectAll()
|
||||
.where("id", "=", token)
|
||||
.executeTakeFirstOrThrow();
|
||||
|
||||
console.log(row);
|
||||
|
||||
const backendId = row.id;
|
||||
const payload = {
|
||||
message,
|
||||
recipient: sender,
|
||||
};
|
||||
|
||||
await worker.addJob("common/notify-webhooks", { backendId, payload });
|
||||
};
|
||||
|
||||
export default receiveSignalMessageTask;
|
||||
|
|
|
|||
|
|
@ -1,11 +1,38 @@
|
|||
// import { db, getWorkerUtils } from "@link-stack/bridge-common";
|
||||
import { db } from "@link-stack/bridge-common";
|
||||
import * as signalApi from "@link-stack/signal-api";
|
||||
const { Configuration, MessagesApi } = signalApi;
|
||||
|
||||
interface SendSignalMessageTaskOptions {
|
||||
token: string;
|
||||
recipient: string;
|
||||
message: any;
|
||||
}
|
||||
|
||||
const sendSignalMessageTask = async ({
|
||||
message,
|
||||
}: SendSignalMessageTaskOptions): Promise<void> => {};
|
||||
recipient,
|
||||
token,
|
||||
}: SendSignalMessageTaskOptions): Promise<void> => {
|
||||
const bot = await db
|
||||
.selectFrom("SignalBot")
|
||||
.selectAll()
|
||||
.where("token", "=", token)
|
||||
.executeTakeFirstOrThrow();
|
||||
|
||||
const { phoneNumber: number } = bot;
|
||||
const config = new Configuration({
|
||||
basePath: process.env.BRIDGE_SIGNAL_URL,
|
||||
});
|
||||
const messagesClient = new MessagesApi(config);
|
||||
const response = await messagesClient.v2SendPost({
|
||||
data: {
|
||||
number,
|
||||
recipients: [recipient],
|
||||
message,
|
||||
},
|
||||
});
|
||||
|
||||
console.log({ response });
|
||||
};
|
||||
|
||||
export default sendSignalMessageTask;
|
||||
|
|
|
|||
|
|
@ -61,7 +61,7 @@ const notifyWebhooks = async (
|
|||
return;
|
||||
}
|
||||
|
||||
webhooks.forEach(({ id }) => {
|
||||
webhooks.forEach(({ id }: any) => {
|
||||
const payload = formatPayload(messageInfo);
|
||||
// logger.debug(
|
||||
// { payload },
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue