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

杀马特。学长 韩版系。学妹 提交于 2021-01-29 05:50:36

问题


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?


回答1:


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());


来源:https://stackoverflow.com/questions/53902705/twilio-send-a-voice-message-if-user-doesnt-answer-in-an-outbound-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!