Signal API updates
This commit is contained in:
parent
83653ef23b
commit
c729a46a0c
25 changed files with 501 additions and 279 deletions
|
|
@ -6,6 +6,8 @@ const __filename = fileURLToPath(import.meta.url);
|
|||
const __dirname = path.dirname(__filename);
|
||||
|
||||
const startWorker = async () => {
|
||||
console.log("Starting worker...");
|
||||
console.log(process.env);
|
||||
await run({
|
||||
connectionString: process.env.DATABASE_URL,
|
||||
concurrency: 10,
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
"license": "AGPL-3.0-or-later",
|
||||
"scripts": {
|
||||
"build": "tsc -p tsconfig.json",
|
||||
"dev": "dotenv -- graphile-worker",
|
||||
"dev": "graphile-worker",
|
||||
"start": "node build/main/index.js"
|
||||
},
|
||||
"dependencies": {
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
/* eslint-disable camelcase */
|
||||
/*
|
||||
import { convert } from "html-to-text";
|
||||
import { URLSearchParams } from "url";
|
||||
import { withDb, AppDatabase } from "../../lib/db.js";
|
||||
|
|
@ -181,8 +182,10 @@ const sendToLabelStudio = async (tickets: FormattedZammadTicket[]) => {
|
|||
console.log(JSON.stringify(importResult, undefined, 2));
|
||||
}
|
||||
};
|
||||
*/
|
||||
|
||||
const importLabelStudioTask = async (): Promise<void> => {
|
||||
/*
|
||||
withDb(async (db: AppDatabase) => {
|
||||
const {
|
||||
leafcutter: { contributorName },
|
||||
|
|
@ -204,6 +207,7 @@ const importLabelStudioTask = async (): Promise<void> => {
|
|||
});
|
||||
}
|
||||
});
|
||||
*/
|
||||
};
|
||||
|
||||
export default importLabelStudioTask;
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
/* eslint-disable camelcase */
|
||||
/*
|
||||
import { URLSearchParams } from "url";
|
||||
import { withDb, AppDatabase } from "../../lib/db.js";
|
||||
// import { loadConfig } from "@digiresilience/bridge-config";
|
||||
|
|
@ -143,8 +144,9 @@ const sendToLeafcutter = async (tickets: LabelStudioTicket[]) => {
|
|||
});
|
||||
console.log({ result });
|
||||
};
|
||||
|
||||
*/
|
||||
const importLeafcutterTask = async (): Promise<void> => {
|
||||
/*
|
||||
withDb(async (db: AppDatabase) => {
|
||||
const {
|
||||
leafcutter: { contributorName },
|
||||
|
|
@ -169,6 +171,7 @@ const importLeafcutterTask = async (): Promise<void> => {
|
|||
minUpdatedTimestamp: newLastTimestamp,
|
||||
});
|
||||
});
|
||||
*/
|
||||
};
|
||||
|
||||
export default importLeafcutterTask;
|
||||
|
|
|
|||
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 },
|
||||
|
|
|
|||
|
|
@ -50,7 +50,7 @@ const notifyWebhooks = async (
|
|||
const webhooks = await db.webhooks.findAllByBackendId("voice", voiceLineId);
|
||||
if (webhooks && webhooks.length === 0) return;
|
||||
|
||||
webhooks.forEach(({ id }) => {
|
||||
webhooks.forEach(({ id }: any) => {
|
||||
const payload = formatPayload(call, recording);
|
||||
workerUtils.addJob(
|
||||
"notify-webhook",
|
||||
|
|
|
|||
|
|
@ -1,22 +1,7 @@
|
|||
{
|
||||
"extends": "@link-stack/typescript-config",
|
||||
"extends": "@link-stack/typescript-config/tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "build/main",
|
||||
"module": "esnext",
|
||||
"target": "esnext",
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true,
|
||||
"moduleResolution": "node"
|
||||
},
|
||||
"ts-node": {
|
||||
"esm": true,
|
||||
"experimentalSpecifierResolution": "node",
|
||||
"transpileOnly": true,
|
||||
"compilerOptions": {
|
||||
"module": "esNext",
|
||||
"target": "esNext",
|
||||
"moduleResolution": "node"
|
||||
}
|
||||
"outDir": "build/main"
|
||||
},
|
||||
"include": ["**/*.ts", "**/.*.ts"],
|
||||
"exclude": ["node_modules", "build"]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue