Scenario: inbound call is coming. I want to dispat...
# server-api
f
Scenario: inbound call is coming. I want to dispatch an agent based on phone number with a context specific metadata. Can I use server APIs to add a participant agent based on some custom logic from webhook? https://docs.livekit.io/home/server/webhooks/
let's say something like this:
Copy code
import { 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
Copy code
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 };
workers in my case don't have context and nuances of each specific workflow. Alternative solution I am considering is having an livekit worker, who will call my server to get the context before creating an agent, but this is not ideal , as I am losing control from server side.