repo: https://github.com/signalwire/examples/tree/main/Voice/Screen%20Calls%20%26%20Record%20Voicemail%20in%20PHP - language:php - sdk:compatibility - product:voice In a telecommunication world as abundant as the one we are living in, it is simply not practical to answer every call that lands in your system. That is why we have created a simple solution to redirect calls based upon the callers input to a given question. This example will show how you can easily handle incoming calls, screen them through a prompt, and record a voicemail if you do not want to speak with the caller at the current time based upon their answer to the prompt. What do I need to run this code? -- You can find the full github repository [HERE](https://github.com/signalwire/guides/tree/main/Voice/Screen%20Calls%20%26%20Record%20Voicemail%20in%20PHP) This guide uses the PHP SignalWire SDK, for a guide on installation click [here](/compatibility-api/sdks) You will need a SignalWire phone number as well as your API Credentials (**API Token**, **Space URL**, **and Project ID**) which can all be found in an easily copyable format within the **API** tab of your SignalWire portal. For more information on where to find these, check out our [Navigating your SignalWire Space section](/guides/navigating-your-space)! How to Run the Application -- Use composer install to install dependencies, then run TO_NUMBER=+15558877444 php -S localhost:8080 -t public/, of course replacing TO_NUMBER with your own number you want to have calls screened to. You may need to use an SSH tunnel for testing this code if running on your local machine. – we recommend [ngrok](https://ngrok.com/). You can learn more about how to use ngrok with SignalWire [here](/guides/how-to-test-webhooks-with-ngrok). Step by Step Code Walkthrough -- We will start by defining our first route / which will handle the original inbound call. In this route, we will create a [Dial](/compatibility-api/cxml/voice/dial) verb using the action URL parameter to redirect to the /voicemail route when the **dialed call has ended**. We will then use the [Number](/compatibility-api/cxml/voice/number-noun) noun inside of Dial along with the url parameter which redirects to the /screen calls route **after** the dialed call is answered but **before** the dialed call is connected. In this example, we use an environment variable for the to number, but in practice, it would be best to replace this with the number you would like all calls forwarded to or a database that can locate the correct number necessary. php # when a call comes in to this route, we will dial the env variable To number $app->post(/, function (Request $request, Response $response, $args) { # instantiate voice response $laml = new VoiceResponse; # create a
If the dialed call is answered, we will next go to the /screen route. We will create a [Gather](/compatibility-api/cxml/voice/gather) with the action URL that will redirect to the /connect route when any key is pressed. We will play a short message for the callee that tells them the number of the caller and asks for input to decide whether to accept or send to voicemail. If no input is detected, we will hang up the call. When the dialed call ends, we will execute the /voicemail route as indicated in the action URL of the original dialed call. php # check if callee wants to accept call or send to voicemail $app->post(/screen, function (Request $request, Response $response, $args) { # instantiate voice response $laml = new VoiceResponse; # get request parameters so that we can access the From number of the caller $post = $request->getParsedBody(); # create a
If the callee presses any key, we will use [Say](/compatibility-api/cxml/voice/say) to play a short message alerting the callee that they will be connected now. php # play message to tell the callee that they are being connected to the original caller $app->post(/connect, function (Request $request, Response $response, $args) { # instantiate voice response $laml = new VoiceResponse; # use TTS to alert callee of impending connection $laml->say(Connecting you); # convert response to XML $xmlResponse = $response->withHeader(Content-type, application/xml); $xmlResponse->getBody()->write(strval($laml)); return $xmlResponse; });
When the dialed call ends, we will redirect to the /voicemail route. We will first check if the DialCallStatus is completed (i.e. answered and not in need of voicemail) and if it is, we will hang up the call. If the DialCallStatus is anything other than completed, we will use [Say](/compatibility-api/cxml/voice/say) to play a voicemail prompt and [Record](/compatibility-api/cxml/voice/record) to record a message. The action URL in Record will redirect to the /store route when the recording is complete. php # this route will record a voicemail message $app->post(/voicemail, function (Request $request, Response $response, $args) { # instantiate voice response $laml = new VoiceResponse; # get request parameters so we can check the dial call status $post = $request->getParsedBody(); # check if dial call status is completed # if status is not completed, record a voicemail if ($post[DialCallStatus] != completed) { # play a prompt for the caller $laml->say(Please leave a message after the beep. Press the pound key when done.); # use
Our final route is more of a placeholder that you can fill based on your business needs! In the /store route, you can use the HTTP request parameters to get the recordingUrl, From, To, or many other parameters. You can use this to store on a server, update your CRM, or even send an SMS with the recording URL. If you dont want to do anything with the recording, you can replace this function with the code to [Hangup](/compatibility-api/cxml/voice/hangup) when the recording is complete instead. php # in this route, you can save the recording, upload it somewhere, send it via sms, etc $app->post(/store, function (Request $request, Response $response, $args) { $post = $request->getParsedBody(); // do something with the recording // saveRecordingSomehow($post[RecordingUrl], $post[From], $post[To]); $response->getBody()->write(); return $response; }); Wrap Up - It is time to stop wasting your time with calls that you do not care about, just send them to voicemail. Through the usage of the SIgnalWire PHP SDK, we can easily build out a call flow that tests the user calling into you, and only allows those important calls to get through. Once our application was running, ngrok helped transform our local port into a webhook that could then be configured to our SignalWire number. Required Resources: - [Github Repo](https://github.com/signalwire/guides/tree/main/Voice/Screen%20Calls%20%26%20Record%20Voicemail%20in%20PHP) - [PHP SignalWire SDK](/compatibility-api/sdks) - [ngrok](https://ngrok.com/) Sign Up Here If you would like to test this example out, [create a SignalWire account and Space](https://m.signalwire.com/signups/new?s=1). Please feel free to reach out to us on our [Community Slack](http://signalwire.community/) or create a Support ticket if you need guidance!