问题
Using only a single TwiML bin, is it possible to mix voice and SMS verbs? I get errors when I try.
For example, a call comes into a Twilio number, TwiML would first send an SMS <Message to="+18005551212"> {{From}}: <Body>Hello World!</Body> </Message> then forward the call <Dial>+18005551212</Dial>. Another example: use mixed Voice/SMS to send an SMS for handling when Dial doesn't connect.
If it's not possible currently, is there a technical reason why TwiML voice & TwiML SMS couldn't be further developed to be interoperable?
回答1:
Twilio developer evangelist here.
Wanted to follow up with the other comments here. If you are just using a TwiML Bin to respond to an incoming call and you want to send SMS messages then the <Sms> verb is what you want.
However, it is deprecated. <Sms> is also not as up to date as the <Message> verb in messaging TwiML. It can't send messages over 160 characters or media messages or use messaging services.
The recommendation is to move on from a TwiML Bin and send the message using the REST API while returning TwiML to control the call. You could do this with a Twilio Function so you still wouldn't need to host anything. Here's a quick example:
exports.handler = function(context, event, callback) {
const twilioClient = context.getTwilioClient();
twilioClient.messages.create({
to: TO_NUMBER,
from: FROM_NUMBER
body: "Hello world"
}).then(function() {
const twiml = new Twilio.twiml.VoiceResponse();
twiml.dial(TO_NUMBER);
callback(null, twiml);
});
}
Let me know if that helps at all.
来源:https://stackoverflow.com/questions/50377549/can-a-twiml-bin-use-both-voice-sms-features