Combining Voice, SMS, and Fax TwiML in the Same Response

When receiving a voice call, SMS mesasge, or Fax, your TwiML <Response> should only consist of TwiML verbs for the same communication type:

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:

  1. Login to your project at www.twilio.com/console.
  2. Click Studio from the left-side navigation bar.
    NOTE: If Studio is not visible, you may first need to click All Products & Services.
  3. Click the + sign icon Icon_03-New.png to create a new flow.
  4. Enter the desired name, and then click Add Flow.
  5. From the Widget Library on the right, drag and drop the following widgets into the flow:
    • Send Message
    • Connect Call To 
  6. Click and drag the Trigger widget’s Incoming Call lead to connect it to the Send Message widget.
  7. 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.
  8. Click and drag the Send Message widget's Sent lead to connect it to the Connect Call To widget.
  9. Click the Connect Call To widget, and then enter the destination phone number for forwarding your calls in the CONNECT CALL TO field.
  10. Click Save, and then click Publish.
  11. 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:

  1. Login to your project at www.twilio.com/console.
  2. Click Runtime from the left-side navigation bar.
    NOTE: If Runtime is not visible, you may first need to click All Products & Services.
  3. Click Functions, and then select Create a Function, or the red plus + sign button.
  4. Select the Blank template, and then click Create.
  5. 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.
  6. Configure this Function on your Twilio number by following the steps here: Responding to Voice Calls with a Function.

Related Topics

Have more questions? Submit a request
Powered by Zendesk