简体   繁体   中英

Twilio send a voice message if user doesn't answer, in an outbound call

I am new to using twilio. I am using twilio to make calls from browser to phone. In the browser side I am using twiml Device to connect to the call.

Twilio.Device.connect({ phoneNumber: phoneNumber, userId: id });

In the nodejs server side I am using this code.

import twilio from 'twilio';

const VoiceResponse = twilio.twiml.VoiceResponse; 

let phoneNumber = req.body.phoneNumber;
let callerId = user.phoneNumber;
let twiml = new VoiceResponse();

let dial = twiml.dial({ callerId: callerId });
dial.number(phoneNumber);

res.send(twiml.toString());

If the user in the other end hasn't answered the call, I need to send a recording by pressing a button as a voicemail to that user.

<button>Send Voicemail</button>

How can I achieve this?

This should be possible using a combination of Twilio's Answering Machine Detection service and the <Play> TwiML verb.

Here is a code sample of making an outbound call with answering machine detection.

const accountSid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
const authToken = 'your_auth_token';
const client = require('twilio')(accountSid, authToken);

client.calls
  .create({
     machineDetection: 'Enable',
     url: 'https://handler.twilio.com/twiml/EHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
     to: '+1562300000',
     from: '+18180000000'
   })
 .then(call => console.log(call.sid))
 .done();

With AMD enabled on your call, Twilio will post the result of the call to the webhook you specify. That webhook will receive an AnsweredBy parameter that will indicate events like machine_start or machine_end_beep .

The controller that receives the webhook should respond by using the <Play> TwiML verb to "press" the correct button. Here is a code sample of what that might look like (this code is untested):

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

app.post('/answering-machine-handler', function (req, res) {
  const response = new VoiceResponse();

  if (req.params.AnsweredBy === 'machine_start') {
    response.play({
        digits: 'wwww3'
    });
  } else {
    // Handle other cases here.
  }

  res.send(response);
})

console.log(response.toString());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM