When receiving a voice call, SMS mesasge, or Fax, your TwiML <Response>
should only consist of TwiML verbs for the same communication type:
- Voice call responses should include Programmable Voice TwiML verbs.
- SMS message responses should include Programmable Messaging TwiML verbs.
Notice: Mixing TwiML verbs of different types is not supported in the same <Response>
. If Twilio receives a mixed TwiML response, this will result in an error.
If your use case requires multiple communication types, your HTTP Response can include API requests in addition to a TwiML<Response>
. This type of mixed-communication HTTP response is not supported in TwiML Bins, but can be hosted elsewhere - including Twilio Studio and Functions.
Multiple Communications Responses in Twilio Studio
Twilio Studio is a flowchart-style visual interface to design, deploy, and scale customer communications. This option is recommended for Twilio users who aren't familiar with code, don't have their own web hosting, or just want to get up and running as quickly as possible. For more information, please see the Twilio Studio landing page.
Here is an example for responding to calls and also sending a message with Studio:
- Login to your project at www.twilio.com/console.
-
Click Studio from the left-side navigation bar.
NOTE: If Studio is not visible, you may first need to click All Products & Services. - Click the + sign icon to create a new flow.
- Enter the desired name, and then click Add Flow.
- From the Widget Library on the right, drag and drop the following widgets into the flow:
- Send Message
- Connect Call To
- Click and drag the Trigger widget’s Incoming Call lead to connect it to the Send Message widget.
- Click the Send Message widget, and then enter the necessary information:
- MESSAGE BODY: Enter the desired message body text
- MESSAGING & CHAT CONFIG > SEND MESSAGE TO: Enter the destination phone number to receive your message.
- Click and drag the Send Message widget's Sent lead to connect it to the Connect Call To widget.
- Click the Connect Call To widget, and then enter the destination phone number for forwarding your calls in the CONNECT CALL TO field.
- Click Save, and then click Publish.
- Configure this Studio Flow on your Twilio number by following the steps here: Responding to Voice Calls with a Studio Flow.
Multiple Communications Responses in Twilio Functions (Beta)
Functions are Twilio-hosted Node.js webhooks for responding to Twilio's HTTP requests with coded instructions. This option is recommended for Twilio users who don't have their own web hosting, and want a versatile solution that can be as simple or complex as needed. For more information, please see the Twilio Functions landing page.
Here is some example code for responding to calls and also sending a message with Functions:
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(FORWARDING_NUMBER);
callback(null, twiml);
});
}
When configured to handle incoming calls, this function will respond by sending an outbound "Hello world" SMS message from the sender FROM_NUMBER
to the recipient TO_NUMBER
, and then forward the incoming call to FORWARDING_NUMBER
.
To make this code work for you, copy and paste it into a text editor, and then make the following changes:
- Line 4 update with the desired SMS destination
- Line 5 update with a valid sender number
- Line 6 update with the desired message text
- Line 9 update with the desired call forwarding number
Once your code is updated, you'll need to save it to Functions, and then activate the Function on a phone number:
- Login to your project at www.twilio.com/console.
-
Click Runtime from the left-side navigation bar.
NOTE: If Runtime is not visible, you may first need to click All Products & Services. - Click Functions, and then select Create a Function, or the red plus + sign button.
- Select the Blank template, and then click Create.
- Add a Path and update the CODE field, and then click Save.
- Path: This is up to you - we select using something that gives an idea of what the Function will do like “sms_forwardCalls”.
- CODE: In this field, copy and paste your updated function code.
- Configure this Function on your Twilio number by following the steps here: Responding to Voice Calls with a Function.