Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
akalsey committed Jun 22, 2015
0 parents commit ea432ef
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 0 deletions.
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2015 Tropo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
14 changes: 14 additions & 0 deletions audio/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Options None
Options +FollowSymLinks

# Set a handler to prevent scripts from being executed.
SetHandler Security_Do_Not_Remove
<Files *>
# Override the handler again if we're run later in the evaluation list.
SetHandler Drupal_Security_Do_Not_Remove
</Files>

# If we know how to do it safely, disable the PHP engine entirely.
<IfModule mod_php5.c>
php_flag engine off
</IfModule>
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"require": {
"slim/slim": "~2.6",
"slim/extras": "*",
"rmccue/requests": ">=1.0"
}
}
95 changes: 95 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
error_reporting(E_ALL | E_STRICT);
require 'vendor/autoload.php';
$app = new \Slim\Slim(array(
'debug' => true,
'log.level' => \Slim\Log::INFO,
'log.enabled' => true,
'log.writer' => new Slim\Extras\Log\DateTimeFileWriter(
array(
'path' => __DIR__ . '/logs',
'name_format' => 'y-m-d'
)
)
));

$conf = json_decode(file_get_contents('config.json'), true);
$conf['endpoint'] = array_key_exists('endpoint', $conf) ? $conf['loglevel'] : 'https://api.voicebase.com/services';

// Allow the log level to be set from the config
if (array_key_exists('loglevel', $conf)) {
$app->log->setLevel(constant('\Slim\Log::' . $conf['loglevel']));
unset($conf['loglevel']); // no need to pass this to the app config
}

$app->config($conf);


$app->get('/test', function() use($app) {
$dir = getcwd();
print '<p>Your Tropo upload URL is <code>' . $app->request()->getUrl() . $app->request()->getScriptName() . '/recording</code></p>';
print '<p>Your files will be stored in <code>' . $dir . '/audio</code></p>';
});

$app->post('/recording/:id', function($id) use($app) {
$dir = getcwd();
move_uploaded_file($_FILES['filename']['tmp_name'], "$dir/audio/$id.wav");
$app->log->debug("SAVE $id / $dir/audio/$id.wav");

$params = array(
"version" => "1.1",
"apikey" => $app->config('apikey'),
"password" => $app->config('password'),
"action" => "uploadMedia",
"mediaURL" => $app->request()->getUrl() . $app->request()->getScriptName() . "/audio/$id.wav",
"machineReadyCallBack" => $app->request()->getUrl() . $app->request()->getScriptName() . "/transcription",
"speakerChannelFlag" => 'true',
"speakerNames" => 'speaker-1,speaker-2',
"externalID" => $id
);
$response = Requests::post("{$app->config('endpoint')}", array(), $params);
$app->log->debug("UPLOAD $id / " . $response->body);
if ('SUCCESS' != json_decode($response->body)->requestStatus) {
$app->log->error("UPLOAD $id / " . json_decode($response->body)->statusMessage);
}
});

$app->post('/transcription', function() use($app) {
$req = $app->request();
$state = $req->params('state');
$id = $req->params('externalId');
$app->log->debug("CALLBACK $id / " . json_encode($req->params()));

if ($state != 'MACHINEREADY') {
$app->log->error("TRANSCRIBE $id / Error in callback: $state. " . json_encode($req->params()));
} else {
$params = array(
"version" => "1.1",
"apikey" => $app->config('apikey'),
"password" => $app->config('password'),
"action" => "getTranscript",
"format" => "TXT",
"externalId" => $id
);
$qs = '';
foreach ($params as $k => $v) {
$k = urlencode($k);
$v = urlencode($v);
$qs .= "$k=$v&";
}
$app->log->debug("REQ TRANSCRIPT $id / {$app->config('endpoint')}?$qs");

$response = Requests::get("{$app->config('endpoint')}?$qs", array(), $params);
$app->log->debug("TRANSCRIPT $id / " . $response->body);

$transcript = json_decode($response->body)->transcript;
$dir = getcwd();

$file = fopen("$dir/audio/$id.txt","w");
fwrite($file,$transcript);
fclose($file);
$app->log->info("transcribed $id / $transcript");
}
});

$app->run();
32 changes: 32 additions & 0 deletions readme.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Tropo transcription through Voicebase

Copyright (c) 2015 Tropo. Released under MIT license. See LICENSE file for
details.

How to integrate Tropo with VoiceBase's audio indexing and transcription API.

VoiceBase provides an API that can transcribe in multiple languages, provides fast, accurate transcription for longer-form text, and can classify and analyze the resulting transcription. At the basic level, you can simply get a transcription back. The audio file and transcription are then stored in your Voicebase account for searching and detailed analytics.

A Tropo recording file gets sent to a URL of your choice. To send this to Voicebase, you'll need a small application that receives the Tropo upload and then creates the Voicebase API call to send the recording for transcription. The application then waits for the transcription to be completed and does something with it, perhaps emailing it, storing in a database, or sending a text message.

The sample application uses the Slim Framework and will run on any PHP web server. It receives the Tropo recording and saves it to disk. It then asks for a basic machine transcription from Voicebase, and once Voicebase has finished transcribing the file, places the transcription in a text file with a filename that matches the audio file name.

## Installing

Rename `sample.config.json` to `config.json` and edit to add your Voicebase API key and password. Copy `config.json`. `.htaccess` and `index.php` to your web server. On your web server, use [Composer](https://getcomposer.org/) to install the dependancies [Slim Framework](http://www.slimframework.com/) and [Requests](https://github.com/rmccue/Requests). Create a directory called `audio` in the same location as index.php and make sure it is writable by the web server.

## Your Tropo application

In your Tropo application, use one of the recording methods to make a recording and set the `recordURI` to `http://your-server.com/path/to/application/recording/{uniqueid}`, replacing _your-server.com_ with the hostname of your server, _path/to/application_ with the directory where you installed the application, and {uniqueid} with an ID that will be unique per call.

Generating a unique ID can be as simple as using the timestamp and caller's phone number, like so:

<?php
$id = $currentCall->callerID . '-' . date('Y-m-d-His');

record('Leave your message',
array(
'recordURI' => 'http://your-server.com/path/to/application/recording/' . $id
)
);
?>
4 changes: 4 additions & 0 deletions sample.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"apikey": "your-voicebase-api-key",
"password": "your-voicebase-password"
}

0 comments on commit ea432ef

Please sign in to comment.