Search code examples
twiliotwilio-apitwilio-twiml

how to send twiml to an in-progress call


Im using twiml for voice responses. I have a scenario where User might ask for a task that takes a little longer and ask user to wait/hold on for a sec, complete the DB calls and send another speech.e.g.

1) [User]: Book me an appointment tomorrow.

2) [Webhook]:

a) Let me check the schedule. (send this twiml to this in-progress call)

b) (Continue collecting data from DB)

c) I have booked your appointment. (now, send this twiml)

I have looked into Twilio's API EXPLORER but have not find any API to send twiml to an in-progress call. I have looked into twiml verbs but can't seem to figure out this.


Solution

  • When you initiate an outgoing call Twilio returns a call SID . Using the call SID, you can modify a call with an update, passing an URL where new TwiML is there to be executed.

    You have not mentioned your language but here is how you do it with Node.js and Twilio's library.

    
    const accountSid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
    const authToken = 'your_auth_token';
    const client = require('twilio')(accountSid, authToken);
    
    client.calls('CAe1644a7eed5088b159577c5802d8be38')
          .update({method: 'POST', url: 'http://demo.twilio.com/docs/voice.xml'})
          .then(call => console.log(call.to));
    
    

    You can see that

    • CAe1644a7eed5088b159577c5802d8be38 is the call SID to identify the call
    • http://demo.twilio.com/docs/voice.xml is the URL where Twilio will find new TwiML

    For more information, check these docs:
    (https://www.twilio.com/docs/voice/tutorials/how-to-modify-calls-in-progress)