Skip to content

Instantly share code, notes, and snippets.

@jmadden
Last active September 1, 2022 22:28
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jmadden/a886deb7607756a6a85d53da70445d0e to your computer and use it in GitHub Desktop.
Save jmadden/a886deb7607756a6a85d53da70445d0e to your computer and use it in GitHub Desktop.
TwiML Incoming Calls - Forward call to numbers in order. Fire off VMail and send SMS on no answer
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<!-- Forward to first number and if no answer, action URL points to second number to try -->
<Dial timeout="15" action="forward_to_second_number.xml">
<Number url="whisper_instructions.xml">+15554441212</Number>
</Dial>
</Response>
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<!-- Forward to second number and if no answer, action URL points to voice mail. -->
<Dial timeout="15" action="vmail.xml">
<Number url="whisper_instructions.xml">+15554449898</Number>
</Dial>
</Response>
<?xml version="1.0" encoding="UTF-8"?>
<!-- Use this when you need a value for the action URL, but you don't need it to actually do anything." -->
<Response />
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Say>
You have reached the voice mail of <your name here>.
Please leave a message at the beep.
Press the star key when finished.
</Say>
<Sms to="+14442228989"> <!-- Your mobile number goes here -->
You missed a call from {{From}}.
</Sms>
<Record
<!-- response.xml is used to prevent Twilio from requesting the current document URL. -->
action="response.xml"
method="GET"
maxLength="20"
finishOnKey="*"
/>
<Say>I did not receive a recording</Say>
</Response>
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<!-- response.xml is used in the action URL below so the gather doesn't go into an endless loop. -->
<Gather timeout="5" numDigits="1" action="response.xml">
<Say>
You have an incoming call.
Press 1 to accept.
</Say>
</Gather>
</Response>
@jmadden
Copy link
Author

jmadden commented Sep 25, 2017

How this works:

  1. You will build all of the files above as TwiML Bins on your Twilio account here: https://www.twilio.com/console/runtime/twiml-bins.
  2. The TwiML Bin you set up with the code from forward_to_first_number.xml will be the TwiML Bin set on your Twilio phone number for when "A Call Comes In".
  3. A call comes into forward_to_first_number.xml and if there is no answer, or there is an answer, but person being called hangs up without pressing a button, the call will go to your second number at forward_to_second_number.xml.
  4. If no one answers at the second number, or answers but doesn't press a button, the call will go to vmail.xml.
  5. vmail.xml will fire off an sms letting you know you missed a call regardless of whether or not the caller left a message.

@adamseoul
Copy link

Thanks,this ends up with the caller going to voicemail after the second number hangs up. Any way to prevent that?

@StratusGrid-StephenColeman

@adamseoul. There's no reason vmail.xml has to be the action in foward_to_second_number.xml. You can chain as few or as many of these together as necessary for your structure, assuming the caller doesn't hang up too quickly.

@bookingninja
Copy link

bookingninja commented May 12, 2022

Thanks, I've been looking forever to build a sequence like this. But I keep getting an error once the flow passes the first bin.

msg | "An attempt to retrieve content from https://handler.twilio.com/twiml/whisper_instructions.xml returned the HTTP status code 404"
sourceComponent | "12000"
ErrorCode | "11200"
httpResponse | "404"
url | "https://handler.twilio.com/twiml/whisper_instructions.xml"
LogLevel | "ERROR"

Any ideas on how to fix this? (I get the same error for the vmail bin as well)

UPDATE: I found the issue. Not exactly sure why (sorry, just learning), but I had to use absolute URLs for all my actions. Once I switched those out it worked like a charm. Maybe because I'm redirecting to this twiML from a Studio flow? Anyhow - thanks for this!

@bookingninja
Copy link

bookingninja commented May 16, 2022

The call flow works well but I found another issue I can't seem to solve.

If the original caller doesn't hang up after being connected to one of the forwarded calls, the flow will proceed to the next action in the sequence. I know, we'd assume that once both parties say goodbye, they'd each hang up. Nonetheless, if the original caller does not hang up, even after talking to someone, they will eventually land on the voicemail. Do you have any ideas about how to add a hangup verb to this sequence without compromising the overall flow you made?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment