Random Facts Voice Call With PHP, Useless facts and AWS Lambda

Have you wanted to run a service without needing to create and maintain a server? Whether this to be a function triggered at set intervals or a specific action triggers this function?

In this tutorial, we will create a PHP application and host it on AWS Lambda, listening for a specific webhook URL to be triggered when someone calls a specific phone number. The application will then confirm the caller’s number and convert a random fact from text to speech for the caller to hear. The random facts get retrieved from an API called Random Useless Facts.

Prerequisites

Vonage API Account

To complete this tutorial, you will need a Vonage API account. If you don’t have one already, you can sign up today and start building with free credit. Once you have an account, you can find your API Key and API Secret at the top of the Vonage API Dashboard.

This tutorial also uses a virtual phone number. To purchase one, go to Numbers > Buy Numbers and search for one that meets your needs.

Getting Started

Install the Dependencies

First, you’ll need to create a directory for your project, then, in your Terminal, navigate to this directory and run the following command to initialise your Composer project:

Once you’ve finished with that step, run the command below to install the required third-party libraries. These libraries are:

  • the Slim PHP Framework,

Now that you’ve installed the third-party libraries, it’s time to make use of one of these. In your Terminal, run the following command to initialise a Bref project, be sure to accept all the defaults provided:

This will create two new files:

  • index.php will contain the code for the application

Write the Code

In your code editor, open the newly created index.php file. Remove the contents of this file as we're going to be rewriting everything.

The first thing to add to the file is the imports for the classes used from the third-party libraries we installed in the previous step. Copy the following into your index.php file:

We need to create the Slim application in our index.php file and an empty GET endpoint with the URI being /webhooks/answer. To do this, add the following to the file:

The /webhooks/answer endpoint will be configured in your Vonage dashboard later as a way for your virtual phone number to get instructions on what to do when a call is made.

Next, we need to create the functionality that will allow us to receive the data transmitted to the webhook, parse the from phone number, make a GET request to the random fact generator, create and return a call control object (NCCO) to the caller. The code below carries out all of this functionality described, so add this into your $app->get('/webhooks/answer' function:

Deploy the Code

To deploy the code to AWS Lambda, in your Terminal, run the following command:

When the deployment is successful, you’ll see an output similar to the example shown in the image below. The output may be slightly different depending on the values in your serverless.yml file, though. Make sure to keep note of the URL found in the endpoints: section.

Create an Application

We now need an application in with Vonage in order to enable our virtual phone number to know which endpoint it needs to request when a call is made. Create an application in your Dashboard under “Your Applications” and give your new application a name.

Add Voice capabilities to the application and configure the URLs using the Lambda URL you copied earlier in the previous step. For the Answer URL, use [paste lambda url]/webhooks/answer and for the Event URL [paste lambda url]/webhooks/event.

Now, click the Link button next to your recently purchased Vonage virtual number to link your new application to the phone number.

You’ve purchased a Vonage virtual number, created a Vonage Application, and written the code to handle the voice webhook events. It’s time to test your project in action!

Test It

To test your project once you’ve deployed it to AWS Lambda call your virtual number and hear the voice reading back your phone number followed by a random fact.

What Now?

You’ve now successfully created and deployed a PHP application to AWS Lambda which listens to a webhook waiting for your virtual number to receive a phone call.

Here are some links to other tutorials we’ve using serverless functions:

As always, if you have any questions, advice or ideas you’d like to share with the community, then please feel free to jump on our Community Slack workspace, or you can contact me directly on Twitter. I’d love to hear how you’ve gotten on with this tutorial and how your project works.

Developer content from the team at Vonage, including posts on our Java, Node.js, Python, DotNet, Ruby and Go SDKs

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store