4

I am using Twilio in a PHP project, currently I am able to make calls and send SMS using its API as given below:

        $client = new \Services_Twilio($AccountSid, $AuthToken);
        try {
            // Initiate a new outbound call
            $call = $client->account->calls->create(
                "<From Number>",
                $input['phone'],
                array("url" => "http://demo.twilio.com/welcome/voice/")
            );
            //echo "Started call: " . $call->sid;
            \Session::flash("success","Calling to ". $input['phone'] ."");
        }

but now client wants to send voice messages if the call is not picked up.

halfer
  • 19,824
  • 17
  • 99
  • 186
Amrinder Singh
  • 5,300
  • 12
  • 46
  • 88
  • What do you mean "client wants to send voice messages if the call is not picked up"? Do you mean that you want to play a pre-recorded message in case the call goes to answering machine? – Alex Baban Jun 13 '17 at 20:43
  • No, i want to make the caller able to leave his voice message if someone not picked his phone. – Amrinder Singh Jun 14 '17 at 02:39
  • @AlexBaban, okay let me explain, look if you are trying to call someone, but he is not picking your call, then you want to leave him a voice message, it's a common thing which all mobile companies provide, the same thing i want to add in my project using Twilio, does it make sense? – Amrinder Singh Jun 14 '17 at 02:59
  • Just to clarify, you want it so that when someone dials your Twilio number and there is no-one to answer the Twilio number the caller should be able to leave a message? – philnash Jun 14 '17 at 07:18
  • @philnash exacty – Amrinder Singh Jun 14 '17 at 07:50
  • @AmrinderSingh: looks like you received an excellent, personalised response to your problem below, from the makers of the product you are using. How did you get on with that, and is it worth a comment reply and/or an acceptance? – halfer Sep 02 '17 at 11:24
  • 1
    @halfer I apologize. – Amrinder Singh Feb 07 '18 at 04:55

2 Answers2

11

Twilio developer evangelist here.

Here's how it all works. When someone calls your Twilio number, Twilio will make an HTTP request, a webhook, to a URL you set in the number admin in your Twilio console for your phone number.

That URL needs to respond with some TwiML, which is just some XML markup to tell Twilio what to do with the call.

It sounds like, in your case, you want to dial your own number and after an amount of time take a message instead of continuing to ring. You will want two endpoints for this. The first one should do the dialling and the second is where we will redirect once the call is redirected for voicemail.

So, the first endpoint TwiML should look a bit like this, using <Dial> to forward the call:

<Response>
  <Dial timeout="30" action="/voicemail.php">
    <Number>YOUR_PHONE_NUMBER</Number>
  </Dial>
</Response>

We use the timeout attribute to set how long you want the phone to ring for. You can set that between 5 and 600 seconds. The action attribute is the endpoint we direct the call to once the timeout finishes. That endpoint will then read the caller a message to tell them to leave a message using <Say> for text to speech, then <Record> the message.

<Response>
  <Say voice="alice">Your call could not be answered at the moment. Please leave a message.</Say>
  <Record action="/hangup.php"/>
</Response>

I've added one extra action to the <Record> tag which just hangs up the call. That would look like this:

<Response>
  <Hangup/>
</Response>

There are other attributes you can use with <Record>. Most importantly, the recordingStatusCallback attribute takes a URL on which your application will be notified when there is a new recording.

For a bit more in depth reading about this, check out the guide on recording phone calls in PHP.

Let me know if this helps.

philnash
  • 70,667
  • 10
  • 60
  • 88
  • 2
    I am sorry for responding so late, I don't know how did I miss it, Yes it helped me a lot, I would like to thank you for such a great info. – Amrinder Singh Feb 07 '18 at 04:56
  • No worries! I'm just glad it helped! – philnash Feb 07 '18 at 19:15
  • I had trouble implementing this... was able to finally get it to work when I combined TwiML syntax of `` and `` responses. See [this answer](https://stackoverflow.com/questions/33921659/how-can-i-handle-reaching-voicemail-using-twilios-dial-verb) – openwonk Apr 07 '18 at 07:30
  • I am a bit confused about `YOUR_PHONE_NUMBER` why you again route the call to another phone number. As the client called on the Twilio number IF we want to answer or receive voicemail from the Twilio number, how the same logic will be implemented. @philnash – NomanJaved Jan 13 '21 at 06:50
  • @NomanJaved I clarified with the OP what they wanted, and it was an incoming voicemail system if they didn't answer their phone. The `` part was forwarding incoming voice calls that were made to their Twilio number onto their personal phone number, then after the timeout the call is directed on to leave a message. If you have a question about your own application, please start a new question as it is hard to answer new questions in the comments. – philnash Jan 13 '21 at 09:07
  • @philnash I just don't want to call on my personal number after twilio number called. Just want to know how to remove `YOUR_PHONE_NUMBER` from the flow and application still works. Because when someone calls on my twilio number and no one answer him then he must be able to send voicemail. – NomanJaved Jan 13 '21 at 15:15
  • If you remove the `` then the application won't dial anyone. Perhaps you can ask a new question and give the full context of your application. Then I might be able to help. – philnash Jan 13 '21 at 22:51
  • @philnash on your request, I had posted a separate question. Please review https://stackoverflow.com/questions/65714735/how-to-receive-and-send-voicemail-from-on-twilio-number – NomanJaved Jan 14 '21 at 07:12
4

1) Create an mp3 as a greeting. Store it somewhere on the web and save the URL. I saved mine as a media file on a wordpress website I manage. It will use the Twilio default greeting if you don't create your own. 2) I use two twilio tools to route to voicemail. Twiml and Twimlets. Create a Twiml bin by going to the main owner dashboard and choosing "runtime" and then "Twimls". The code below can be used in Twiml. Change the Message URL, email, timeout length and Phone numbers. I call two people at once and if neither pickup then it goes to voicemail. A audio file is sent to the email you provide. The twimlet inside the code works - "http://twimlets.com/voicemail" with different parameters.

Code for TWIML:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Dial timeout="30" action="http://twimlets.com/voicemail?Email=test@test.com&amp;Message=http://test.com/wp-content/uploads/2019/01/voicemail1.mp3">
    <Number>703-555-1212</Number>
    <Number>540-555-1212</Number>
  </Dial>
</Response>
Greg Sadetsky
  • 4,863
  • 1
  • 38
  • 48