forked from nexmo-community/voice-aws-speechtotext-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
134 lines (105 loc) · 3.69 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
require('vendor/autoload.php');
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
use Nexmo\Client;
use Nexmo\Client\Credentials\Keypair;
use Aws\S3\S3Client;
use League\Flysystem\AwsS3v3\AwsS3Adapter;
use League\Flysystem\Filesystem;
use Aws\TranscribeService\TranscribeServiceClient;
Dotenv\Dotenv::create(__DIR__)->load();
$app = AppFactory::create();
/**
* Answer the call and prompt using NCCO flow
*/
$app->get('/webhooks/answer', function (Request $request, Response $response) {
$uri = $request->getUri();
$ncco = [
[
'action' => 'talk',
'text' => 'Please leave a message after the tone, then press #.'
],
[
'action' => 'record',
'eventUrl' => [
$uri->getScheme().'://'.$uri->getHost().'/webhooks/fetch'
],
'endOnSilence' => '3',
'endOnKey' => '#',
'beepOnStart' => true
],
[
'action' => 'talk',
'text' => 'Thank you for your message. Goodbye.'
],
[
'action' => 'notify',
'payload' => ['followup'=>true],
'eventUrl' => [
$uri->getScheme().'://'.$uri->getHost().'/webhooks/transcribe'
],
'eventMethod' => "POST"
],
];
$response->getBody()->write(json_encode($ncco));
return $response
->withHeader('Content-Type', 'application/json');
});
/**
* Event URL used by Nexmo for update POSTs
*/
$app->post('/webhooks/event', function (Request $request, Response $response) {
$params = $request->getParsedBody();
error_log($params['recording_url']);
return $response
->withStatus(204);
});
/**
* Fetch the voice recording from Nexmo, store on AWS S3
*/
$app->post('/webhooks/fetch', function (Request $request, Response $response) {
$params = json_decode($request->getBody(), true);
// Create Nexmo Client
$keypair = new Keypair(
file_get_contents($_ENV['NEXMO_APPLICATION_PRIVATE_KEY_PATH']),
$_ENV['NEXMO_APPLICATION_ID']
);
$nexmoClient = new Client($keypair);
$data = $nexmoClient->get($params['recording_url']);
// Create AWS S3 Client
$S3Client = new S3Client([
'region' => $_ENV['AWS_REGION'],
'version' => 'latest'
]);
$adapter = new AwsS3Adapter($S3Client, $_ENV['AWS_S3_BUCKET_NAME']);
$filesystem = new Filesystem($adapter);
$filesystem->put('/' . $_ENV['AWS_S3_RECORDING_FOLDER_NAME'] .'/'.$params['conversation_uuid'].'.mp3', $data->getBody());
return $response
->withStatus(204);
});
/**
* Request Amazon Transcription of the voice file
*/
$app->post('/webhooks/transcribe', function (Request $request, Response $response) {
$params = json_decode($request->getBody(), true);
// Create Amazon Transcribe Client
$awsTranscribeClient = new TranscribeServiceClient([
'region' => $_ENV['AWS_REGION'],
'version' => 'latest'
]);
$transcriptionResult = $awsTranscribeClient->startTranscriptionJob([
'LanguageCode' => 'en-US',
'Media' => [
'MediaFileUri' => 'https://' . $_ENV['AWS_S3_BUCKET_NAME'] . '.s3.amazonaws.com/' . $_ENV['AWS_S3_RECORDING_FOLDER_NAME'] . '/' . $params['conversation_uuid'] . '.mp3',
],
'MediaFormat' => 'mp3',
'TranscriptionJobName' => 'nexmo_voice_' . $params['conversation_uuid'],
]);
$response->getBody()->write(json_encode($transcriptionResult->toArray()));
return $response
->withHeader('Content-Type', 'application/json')
->withStatus(204);
});
$app->run();