Random Facts Voice Call With PHP, Uselessfacts and AWS Lambda
Published on April 13, 2021

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 the 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:

composer init

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,

  • Slim's PSR-7 which contains Response and Request interfaces we'll need,

  • GuzzleHttp to make API requests in retrieving random facts,

  • Bref to bring support for PHP on AWS Lambda,

  • Vonage's PHP client to correctly handle incoming webhook requests for a voice call.

composer require slim/slim:"4.*" slim/psr7 guzzlehttp/guzzle:"^7.0" bref/bref vonage/client

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:

vendor/bin/bref init

This will create two new files:

  • index.php will contain the code for the application

  • serverless.yml will contain the configuration requirements for deploying the application to AWS

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:

<!--?php

use GuzzleHttp\Client;
use Laminas\Diactoros\Response\JsonResponse;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
use Vonage\Voice\NCCO\Action\Talk;
use Vonage\Voice\NCCO\NCCO;
use Vonage\Voice\Webhook\Answer;
use Vonage\Voice\Webhook\Factory;

require __DIR__ . '/vendor/autoload.php';
</code-->

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: ```blok {"type":"codeBlock","props":{"lang":"php","code":"$app%20=%20AppFactory::create();%0A%0A$app-&gt;get('/webhooks/answer',%20function%20(Request%20$request,%20Response%20$response,%20array%20$args)%20%7B%0A%0A%7D);%0A%0A$app-&gt;run();%0A"}} ``` 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](https://uselessfacts.jsph.pl/random.json?language=en), 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: ```blok {"type":"codeBlock","props":{"lang":"php","code":"//%20Convert%20the%20contents%20of%20the%20%60$request%60%20sent%20in%20the%20%60GET%60%20request%20into%20a%20Voice%20Webhook%20Object.%0A/**%20@var%20Answer%20$call%20*/%0A$call%20=%20Factory::createFromRequest($request);%0A//%20Take%20the%20%60from%60%20phone%20number%20and%20add%20spaces%20so%20it%20can%20be%20read%20properly%20in%20the%20voice%20call%0A$fromSplitIntoCharacters%20=%20implode(%22%20%22,%20str_split($call-&gt;getFrom()));%0A%0A//%20Create%20a%20new%20GuzzleHttp%20client%20ready%20to%20make%20a%20%60GET%60%20request%0A$client%20=%20new%20Client();%0A//%20Make%20a%20%60GET%60%20request%20for%20a%20random%20useless%20fact%20in%20English%0A$response%20=%20$client-&gt;get('https://uselessfacts.jsph.pl/random.json?language=en');%0A//%20Convert%20the%20response%20JSON%20into%20a%20PHP%20Array%0A$responseArray%20=%20json_decode($response-&gt;getBody(),%20true);%0A%0A//%20Initialise%20the%20Call%20Control%20Object%20ready%20to%20take%20actions%20to%20return%20back%20to%20the%20caller%0A$ncco%20=%20new%20NCCO();%0A$ncco%0A%20%20%20%20//%20Create%20the%20first%20Talk%20Action%20thanking%20the%20caller%20and%20reading%20out%20their%20number%20back%20to%20them%0A%20%20%20%20-&gt;addAction(%0A%20%20%20%20%20%20%20%20new%20Talk('Thank%20you%20for%20calling%20from%20'%20.%20$fromSplitIntoCharacters)%0A%20%20%20%20)%0A%20%20%20%20//%20Create%20the%20second%20Talk%20Action%20reading%20the%20caller%20their%20random%20fact.%0A%20%20%20%20-&gt;addAction(%0A%20%20%20%20%20%20%20%20new%20Talk('Your%20fact%20is:%20'%20.%20$responseArray%5B'text'%5D)%0A%20%20%20%20);%0A%0A//%20Returns%20a%20Json%20Response%20of%20the%20NCCO%20containing%20the%20two%20Talk%20Actions.%0Areturn%20new%20JsonResponse($ncco);%0A"}} ``` ### Deploy the Code To deploy the code to AWS Lambda, in your Terminal, run the following command: ```blok {"type":"termynal","props":{"code":"%3Cspan%20data-ty=%22input%22%3Eserverless%20deploy%3C/span%3E"}} ``` 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. ![Output showing a serverless deployment](https://d226lax1qjow5r.cloudfront.net/blog/blogposts/random-fact-voice-call-with-php-uselessfacts-and-aws-lambda/serverless-deployment-success.png) ### 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]($%7BCUSTOMER_DASHBOARD_URL%7D/) 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: * [Build a Basic Video Call Platform with Netlify Functions](https://developer.vonage.com/en/blog/build-a-basic-video-call-platform-with-netlify-functions/) * [Build a Serverless Eurovision Voting System with Node.js and Vonage](https://developer.vonage.com/en/blog/build-a-serverless-eurovision-voting-system-with-node-js-and-vonage/) * [Multi-Channel Tone Analysis in PHP with Amazon Comprehend](https://developer.vonage.com/en/blog/multi-channel-tone-analysis-in-php-with-amazon-comprehend/) 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](/community/slack), or you can contact me directly on [Twitter](https://www.twitter.com/greg__holmes). I'd love to hear how you've gotten on with this tutorial and how your project works.

Greg HolmesVonage Alumni

Former Developer Educator @Vonage. Coming from a PHP background, but not restricted to one language. An avid gamer and a Raspberry pi enthusiast. Often found bouldering at indoor climbing venues.

Ready to start building?

Experience seamless connectivity, real-time messaging, and crystal-clear voice and video calls-all at your fingertips.

Subscribe to Our Developer Newsletter

Subscribe to our monthly newsletter to receive our latest updates on tutorials, releases, and events. No spam.