简体   繁体   English

使用Twilio功能在同一响应中发出语音和消息

[英]Voice and Message in same response using Twilio Functions

I am migrating from Tropo to Twilio. 我正在从Tropo迁移到Twilio。 I have a need to send an SMS to a monitored phone number while parsing a voice call (menu system). 我需要在解析语音呼叫时(菜单系统)将SMS发送到受监控的电话号码。

The flow should be: 流程应为:
1. Incoming Voice call 1.来电语音通话
2. Function sends SMS to one or more cell phones //alerts there is a voice call 2.功能将短信发送到一个或多个手机//提示有语音通话
3. Voice IVR system takes over and processes call 3.语音IVR系统接管并处理呼叫

Using the Twilio Runtime functions (twilio's hosted node.js) I seem to be unable to combine both of these needs into a single function as the callback looks for a single twiml object. 使用Twilio运行时函数(twilio的托管node.js),由于回调函数查找单个twiml对象,因此我似乎无法将这两种需求合并为一个函数。 Further I also seem to be unable to send an SMS from within a voice call. 此外,我似乎也无法通过语音呼叫发送SMS。

Is there example code anywhere, or is there solid documentation that might help me achieve my goal? 到处都有示例代码,或者有可靠的文档可以帮助我实现目标吗?

The following code works, but I have been advised by Twilio tech support not to use the SMS verb as its future viability is not guaranteed. 以下代码有效,但是Twilio技术支持建议我不要使用SMS动词,因为不能保证其将来的可行性。

exports.handler = function(context, event, callback) { exports.handler =函数(上下文,事件,回调){

let twiml = new Twilio.twiml.VoiceResponse(); 让twiml = new Twilio.twiml.VoiceResponse();

twiml.say("Hello World"); twiml.say(“ Hello World”); // respond to voice caller //回应语音来电者
twiml.sms({to:"+19735551212"},"Hello SMS!!"); twiml.sms({to:“ + 19735551212”},“ Hello SMS !!”); // send SMS // 发简讯

callback(null, twiml); callback(null,twiml);

} }

Twilio developer evangelist here. Twilio开发人员布道者在这里。

Rather than use the TwiML to do both the call routing and the message sending, you can send messages with the REST API and use the TwiML just for the voice routing. 您可以使用REST API发送消息并将TwiML仅用于语音路由,而不必使用TwiML进行呼叫路由和消息发送。

For example: 例如:

exports.handler = function(context, event, callback) {

    const client = context.getTwilioClient();
    client.messages.create({  // Send SMS
        to: "+19735551212",
        from: event.From,
        body: "Hello from SMS"
    }).then(() => {  // When request to send SMS is complete, deal with the caller
        let twiml = new Twilio.twiml.VoiceResponse();
        twiml.say("Hello World");   // respond to voice caller
        callback(null, twiml);
    })    
}

I got it working with a single iteration. 我通过一次迭代就可以使用它。 The example @philnash posted above was correct with one semantic exception. 上面发布的示例@philnash是正确的,但有一个语义异常。 The "from" parameter should be using "event.To" or "event.Called" as Twilio requires a valid Twilio number. 由于Twilio需要有效的Twilio编号,因此“ from”参数应使用“ event.To”或“ event.Called”。 "event.From" in my case was my cell phone and not a vaild Twilio number. 在我的情况下,“ event.From”是我的手机,而不是有效的Twilio号码。

exports.handler = function(context, event, callback) {
    const client = context.getTwilioClient();
    client.messages.create({  // Send SMS
        to: "+19735551212",
        from: event.Called,  // **must be a valid Twilio number **
        body: "Hello from SMS"
    }).then(function(message) {
        console.log(event.Called);
        console.log(message.sid);
        let twiml = new Twilio.twiml.VoiceResponse();
        twiml.say("How now brown cow ");
        callback(null, twiml);
    });  
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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