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.
DT API Account
To complete this tutorial, you will need a DT 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 DT API Dashboard.
Prerequisites
To get the most from this example, you should look at the following repos to start:
Though the first post is needed, you will mostly be updating code in the second for this example.
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 thesentiment
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 theRDS
database created in the previous post, to include thesentiment
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.
Ready
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.