twilio-samples / speech-assistant-openai-realtime-api-node

MIT License
164 stars 94 forks source link

outgoing call #4

Closed kjjd84 closed 1 month ago

kjjd84 commented 1 month ago

can we get a tutorial on how to do outgoing calls with this as well?

thank you very much for this btw

brainvine commented 1 month ago

Add this:


fastify.post('/make-call', async (request, reply) => {
    try {
        console.log('Initiating outbound call...');
        const call = await client.calls.create({
            url: `https://your-ngrok-url.ngrok-free.app/incoming-call`,
            to: '+31612345678',
            from: twilioPhoneNumber,
            statusCallback: `https://${request.headers.host}/call-status`,
            statusCallbackEvent: ['initiated', 'ringing', 'answered', 'completed'],
            statusCallbackMethod: 'POST'
        });

        console.log('Outbound call initiated:', call.sid);
        console.log('Full call details:', JSON.stringify(call, null, 2));
        reply.send({ message: 'Outbound call initiated', callSid: call.sid });
    } catch (error) {
        console.error('Error making outbound call:', error);
        reply.status(500).send({ error: 'Failed to initiate outbound call', details: error.message });
    }
});

fastify.post('/call-status', async (request, reply) => {
    console.log('Call status update received');
    console.log('Headers:', request.headers);
    console.log('Body:', request.body);
    reply.send({ received: true });
});

Trigger in terminal:

curl -X POST http://localhost:5050/make-call

kjjd84 commented 1 month ago

Add this:


fastify.post('/make-call', async (request, reply) => {
    try {
        console.log('Initiating outbound call...');
        const call = await client.calls.create({
            url: `https://your-ngrok-url.ngrok-free.app/incoming-call`,
            to: '+31612345678',
            from: twilioPhoneNumber,
            statusCallback: `https://${request.headers.host}/call-status`,
            statusCallbackEvent: ['initiated', 'ringing', 'answered', 'completed'],
            statusCallbackMethod: 'POST'
        });

        console.log('Outbound call initiated:', call.sid);
        console.log('Full call details:', JSON.stringify(call, null, 2));
        reply.send({ message: 'Outbound call initiated', callSid: call.sid });
    } catch (error) {
        console.error('Error making outbound call:', error);
        reply.status(500).send({ error: 'Failed to initiate outbound call', details: error.message });
    }
});

fastify.post('/call-status', async (request, reply) => {
    console.log('Call status update received');
    console.log('Headers:', request.headers);
    console.log('Body:', request.body);
    reply.send({ received: true });
});

Trigger in terminal:

curl -X POST http://localhost:5050/make-call

ended up making it work with a script like this:

client.calls
  .create({
    to: to,
    from: from,
    twiml: `
      <Response>
        <Connect>
          <Stream url="wss://${process.env.NGROK_URL}/media-stream" />
        </Connect>
      </Response>
    `,
  })
  .then((call) => console.log(call.sid))
  .catch((err) => console.error(err));