Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Alexa Code for handling AudioPlayer Events
/*
DATABASE STRUCTURE
CREATE TABLE `alexa_events` (
`id` bigint(33) NOT NULL,
`user_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`item_id` int(33) NOT NULL,
`offsetInMilliseconds` int(33) NOT NULL,
`status` varchar(25) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
*/
$event_name = $_GET['event_name'];
// STARTED
if( $event_name == "AudioPlayer.PlaybackStarted" )
{
update_alexa_event( $user_id, 'CURRENT', $offset );
$action = new stdclass;
$action->type = "AudioPlayer.Play";
}
// FINISHED
if( $event_name == "AudioPlayer.PlaybackFinished" )
{
// CLEAN OUT ITEM
clear_alexa_event( $user_id );
$action = new stdclass;
$action->type = "AudioPlayer.Stop";
}
// STOPPED
if( $event_name == "AudioPlayer.PlaybackStopped" )
{
update_alexa_event( $user_id, 'CURRENT', $offset, 2 );
$action = new stdclass;
$action->type = "AudioPlayer.Stop";
}
// NEARLY FINISHED
if( $event_name == "AudioPlayer.PlaybackNearlyFinished" )
{
// GET LAST ITEM ACCESSED FROM USER
$alexa_event = get_alexa_event( $user_id );
if( $alexa_event )
{
// CHECK FOR A NEXT ITEM
$item = get_sibling_item( $item_id );
// QUEUE NEXT ITEM
if( $item )
{
$outputSpeech = new stdclass;
$outputSpeech->type = 'SSML';
if( $item->audio_in_s3 )
{
$action = new stdclass;
$action->type = "AudioPlayer.Play";
$action->playBehavior = "ENQUEUE";
$action->audioItem = new stdclass;
$action->audioItem->stream = new stdclass;
$action->audioItem->stream->token = $item->slug;
$action->audioItem->stream->url = "https://bibletalk.tv/" . $item->slug . ".alexa.mp3";
$action->audioItem->stream->offsetInMilliseconds = 0;
}
else
{
$speech = "This lesson does not have audio yet.";
if( $item->timestamp > time() )
{
$speech .= " It will be recorded on " . date('l', $item->timestamp) . " the ". date('jS', $item->timestamp) . " at " . date('g:ia', $item->timestamp);
}
$outputSpeech->ssml = "<speak>" . $speech . "</speak>";
$response->outputSpeech = $outputSpeech;
$response->reprompt->outputSpeech = $outputSpeech;
}
}
}
}
// FAILED
if( $event_name == "AudioPlayer.PlaybackFailed" )
{
update_alexa_event( $user_id, 'CURRENT', 'CURRENT', 0 );
}
$response->response->directives = array( $action );
header('Content-Type: application/json;charset=UTF-8');
echo json_encode($response);
die;
/* EXTRA FUNCTIONS
function update_alexa_event( $user_id, $item_id, $offset = 0, $status = 1 )
{
if( !$user_id ) return false;
global $db;
// ATTS
$user_id = addslashes(trim($user_id));
// IF WE HAVE USER, UPDATE
$user = $db->get_row("SELECT * FROM alexa_events WHERE user_id = '$user_id' LIMIT 1");
if( $user )
{
if( $item_id == "CURRENT" ) $item_id = $user->item_id;
if( $offset == "CURRENT" ) $offset = $user->offsetInMilliseconds;
if( $status == "CURRENT" ) $status = $user->status;
// SANITIZE
$item_id = (int) $item_id;
$offset = (int) $offset;
$status = (int) $status;
$db->query("UPDATE alexa_events SET item_id = $item_id, offsetInMilliseconds = $offset, status = $status WHERE user_id = '$user_id' LIMIT 1");
return true;
}
else
{
// SANITIZE
$item_id = (int) $item_id;
$offset = (int) $offset;
$status = (int) $status;
// ELSE INSERT
$db->query("INSERT INTO alexa_events (user_id, item_id, offsetInMilliseconds, status) VALUES ('$user_id', $item_id, $offset, $status) ");
return true;
}
}
function get_alexa_event( $user_id )
{
if( !$user_id ) return false;
global $db;
$user_id = addslashes(trim($user_id));
return $db->get_row("SELECT * FROM alexa_events WHERE user_id = '$user_id' LIMIT 1");
}
function clear_alexa_event( $user_id )
{
global $db;
$user_id = addslashes(trim($user_id));
$db->query("DELETE FROM alexa_events WHERE user_id = '$user_id' LIMIT 1");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment