fierce-zoo-69908
07/15/2025, 3:42 PMfierce-zoo-69908
07/15/2025, 3:43 PMimport { FastifyPluginAsync } from 'fastify';
import { createDatabaseClient } from '@pf-workflo/core';
import { inboundMappings } from '@pf-workflo/core/src/services/db/schema/telephony';
import { eq } from 'drizzle-orm';
import { WorkflowType } from '@pf-workflo/core/dist/types/index';
import { createWorkflowEngine } from '../engine';
const webhookSecret = process.env.LIVEKIT_WEBHOOK_SECRET; // Set in env
export const livekitWebhookRoutes: FastifyPluginAsync = async (fastify) => {
const workflowEngine = createWorkflowEngine({ signingKey: process.env.INNGEST_SIGNING_KEY || '', eventKey: process.env.INNGEST_EVENT_KEY || '' });
<http://fastify.post|fastify.post>('/api/livekit-webhook', async (request, reply) => {
const sig = request.headers['lk-signature'] as string;
// Verify signature logic here
const event = request.body as any;
if (event.event === 'participant_joined' && event.participant.attributes?.sip) {
const dialedNumber = event.participant.attributes.sip.dialedNumber;
const caller = event.participant.attributes.sip.phoneNumber;
const sipIdentity = event.participant.identity;
const { db } = await createDatabaseClient();
const mapping = await db.select().from(inboundMappings).where(eq(inboundMappings.phoneNumber, dialedNumber)).limit(1);
if (mapping.length > 0) {
const { tenantId, workflowId } = mapping[0];
await workflowEngine.startWorkflow(tenantId, workflowId, {
isInbound: true,
roomName: event.room,
sipIdentity,
callerPhoneNumber: caller,
dialedNumber
}, WorkflowType.SERVER);
}
}
reply.status(200).send({});
});
};
and then inside startWorkflow
this.config.roomName = inputs.roomName as string;
agentMetadata.participantIdentity = inputs.sipIdentity as string;
agentMetadata.isInbound = true;
await this.createExplicitDispatch(agentMetadata);
return { status: ExecutionStatus.PENDING, output: {
roomName: this.config.roomName,
agentName: agentName ?? this.config.agentName,
participantToken: await this.createTokenWithAgentDispatch(participantIdentity, agentMetadata),
participantIdentity,
phoneNumber,
executionId: this.context.executionId
}, resumeExecutionId: resumeExecutionId, triggeredOutputPorts: [], creditsUsed: 0 };fierce-zoo-69908
07/15/2025, 3:45 PM