link-stack/apps/bridge-worker/lib/common.ts

71 lines
2 KiB
TypeScript
Raw Permalink Normal View History

2023-02-13 12:41:30 +00:00
/* eslint-disable camelcase */
2024-04-21 16:59:50 +02:00
// import { SavedVoiceProvider } from "@digiresilience/bridge-db";
2023-02-13 12:41:30 +00:00
import Twilio from "twilio";
import { CallInstance } from "twilio/lib/rest/api/v2010/account/call";
2024-05-14 15:31:44 +02:00
import { Zammad, getOrCreateUser } from "./zammad.js";
2023-02-13 12:41:30 +00:00
2024-04-21 16:59:50 +02:00
type SavedVoiceProvider = any;
2023-02-13 12:41:30 +00:00
export const twilioClientFor = (
2024-04-21 09:44:30 +02:00
provider: SavedVoiceProvider,
2023-02-13 12:41:30 +00:00
): Twilio.Twilio => {
const { accountSid, apiKeySid, apiKeySecret } = provider.credentials;
if (!accountSid || !apiKeySid || !apiKeySecret)
throw new Error(
2024-04-21 09:44:30 +02:00
`twilio provider ${provider.name} does not have credentials`,
2023-02-13 12:41:30 +00:00
);
return Twilio(apiKeySid, apiKeySecret, {
accountSid,
});
};
export const createZammadTicket = async (
call: CallInstance,
2024-04-21 09:44:30 +02:00
mp3: Buffer,
2023-02-13 12:41:30 +00:00
): Promise<void> => {
const title = `Call from ${call.fromFormatted} at ${call.startTime}`;
const body = `<ul>
<li>Caller: ${call.fromFormatted}</li>
<li>Service Number: ${call.toFormatted}</li>
<li>Call Duration: ${call.duration} seconds</li>
<li>Start Time: ${call.startTime}</li>
<li>End Time: ${call.endTime}</li>
</ul>
<p>See the attached recording.</p>`;
const filename = `${call.sid}-${call.startTime}.mp3`;
const zammad = Zammad(
{
token: "EviH_WL0p6YUlCoIER7noAZEAPsYA_fVU4FZCKdpq525Vmzzvl8d7dNuP_8d-Amb",
},
2024-04-21 09:44:30 +02:00
"https://demo.digiresilience.org",
2023-02-13 12:41:30 +00:00
);
try {
const customer = await getOrCreateUser(zammad, call.fromFormatted);
await zammad.ticket.create({
title,
group: "Finances",
note: "This ticket was created automaticaly from a recorded phone call.",
customer_id: customer.id,
article: {
body,
subject: title,
content_type: "text/html",
type: "note",
attachments: [
{
filename,
data: mp3.toString("base64"),
"mime-type": "audio/mpeg",
},
],
},
});
} catch (error: any) {
if (error.isBoom) {
2025-01-22 17:50:38 +01:00
console.error(error.output);
2023-02-13 12:41:30 +00:00
throw new Error("Failed to create zamamd ticket");
}
}
};