Adam Culp

Vonage Team Member

Adam is a developer and consultant who enjoys ultra-running, blogging/vlogging, and helping others tame technology to accomplish amazing things with an insatiable desire to mentor and help.

[Read more]
< Tutorial />

Multi-Channel Tone Analysis in PHP with Amazon Comprehend

Last updated on May 04, 2021

In this post, you'll update an AWS Lambda function introduced in a previous post where the results of a voice transcription get stored in RDS. The objective for this example is to use Amazon Comprehend to retrieve the tone analysis for an entire conversation, by channels, and then add the results to an RDS MySQL database instance.

See nexmo-community/voice-channels-aws-transcribe-php, and nexmo-community/aws-voice-transcription-rds-callback-php for an understanding of what you will be working with for this example.

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.

Screenshot of new Meetings API session in progress
Start developing in minutes with free credits on us. No credit card required!


Setup Instructions

With the nexmo-community/aws-voice-transcription-rds-callback-php code deployed, you will need to update the index.php file in the following ways:

  • Add an import statement for AWS Comprehend, to the top of the file with the other imports.
use Aws\Comprehend\ComprehendClient;
  • From line 54 to 62, update the $conn-insert() contents to include the sentiment field.
$conn->insert('transcriptions', [
    'conversation_uuid' => $conversation_uuid,
    'channel' => ($channel['channel_label'] == 'ch_0' ? 'caller' : 'recipient'),
    'start_time' => $startTime,
    'end_time' => $endTime,
    'content' => $item['alternatives'][0]['content'],
    'sentiment' => serialize(getSentiment($conversation['content'])->toArray()),
    'created' => $record_date,
    'modified' => $record_date
  • Add the following function to the end of the file, that enables sentiment to get populated for the database insertion.
function getSentiment(string $content) : Aws\Result {

    $comprehendClient = new ComprehendClient([
        'region' => $_ENV['AWS_REGION'],
        'version' => $_ENV['AWS_VERSION'],

    return $comprehendClient->detectSentiment([
        'LanguageCode' => 'en',
        'Text' => $content,
  • Update the conversations table in the RDS database created in the previous post, to include the sentiment content.
USE `voice_transcriptions`;

ALTER TABLE `conversations` ADD COLUMN `sentiment` TEXT AFTER `content`

Deploy to Lambda

With all the above updated successfully, you can now use Serverless to redeploy the app to AWS Lambda.

serverless deploy


Your app is now updated to include tone analysis with the conversation content, going forward.

Next Steps

If you have any questions or run into troubles, you can reach out to @VonageDev on Twitter or inquire in the Nexmo Community Slack team. Good luck.