Search code examples
node.jstwiliotwilio-apitwilio-twiml

Add AMD to Twilio dial.number?


I have this code running on my server. It is in the endpoint on my server that is specified in my TwiML app->Voice Configuration->Request URL. It works correctly, and the call is successful.

My question is, can I add automatic voice detection to it?

const VoiceResponse = require("twilio").twiml.VoiceResponse;

[.....]

try {
    const response = new VoiceResponse();
    const dial = response.dial({
        callerId: twilioPhoneNumber
    });
    dial.number(toNumber);
    console.log('TwiML: ', response.toString());

    // Render the response as XML in reply to the webhook request
    res.type('text/xml');
    res.send(response.toString());
} catch (error) {
    console.log('/make-call endpoint: ', error);
    res.status(400);
}

Solution

  • Per their docs, it's a parameter on the create call api machineDetection = "Enabled"

    // Download the helper library from https://www.twilio.com/docs/node/install
    const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
    
    // Find your Account SID and Auth Token at twilio.com/console
    // and set the environment variables. See http://twil.io/secure
    const accountSid = process.env.TWILIO_ACCOUNT_SID;
    const authToken = process.env.TWILIO_AUTH_TOKEN;
    const client = twilio(accountSid, authToken);
    
    async function createCall() {
      const call = await client.calls.create({
        from: "+18180000000",
        machineDetection: "Enable",
        to: "+1562300000",
        url: "https://handler.twilio.com/twiml/EH8ccdbd7f0b8fe34357da8ce87ebe5a16",
      });
    
      console.log(call.sid);
    }
    
    createCall();