PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, November 18, 2022

[FIXED] How to record a voicemail if a number is not picked up in Twilio?

 November 18, 2022     php, twilio, twilio-api, twilio-php     No comments   

Issue

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.


Solution

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.



Answered By - philnash
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing