Skip to content

Commit

Permalink
Basic working functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
cjnewbs committed Aug 12, 2018
0 parents commit 8ce1b90
Show file tree
Hide file tree
Showing 12 changed files with 526 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Alexa Skill Framework

I built this to make it simple to create Amazon Alexa skills using PHP. It handles the request verification, intent routing, provides some methods to get intent/slot information from the request and methods for building the response object to AVS.

#### To install:
- Run `composer require newbury/alexa-framework`,
- Copy the contents of the contents of the `/example` directory to the application root,
- Set the webserver document root to `/pub`,
24 changes: 24 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "newbury/alexa-framework",
"description": "Simple framework for creating Amazon Alex skills using PHP.",
"type": "library",
"version": "0.1.0",
"license": [
"MIT"
],
"autoload": {
"psr-4": {
"Newbury\\AlexaFramework\\": "src/"
}
},
"authors": [
{
"name": "Craig Newbury",
"email": "[email protected]",
"role": "Developer"
}
],
"support": {
"email": "[email protected]"
}
}
9 changes: 9 additions & 0 deletions example/config/skills.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
return [
'amzn1.ask.skill.xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' => [
'routes' => [
/** Custom Intents */
'YourIntentName' => \YourVendor\YourPackage\YourIntentHandler::class
],
],
];
9 changes: 9 additions & 0 deletions example/pub/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
use Newbury\AlexaFramework\App;
try {
define('BASE_PATH', __DIR__ . '/../');
require_once '../vendor/autoload.php';
App::run(App::APP_MODE_PRODUCTION);
} catch (Exception $e) {
die($e->getMessage());
}
77 changes: 77 additions & 0 deletions src/App.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Newbury\AlexaFramework;

class App
{
const APP_MODE_DEVELOPER = 'DEVELOP';
const APP_MODE_PRODUCTION = 'PRODUCTION';

public static function run($mode)
{
/**
* Load Config files
*/
$skills = require_once BASE_PATH . 'config/skills.php';

/**
* Prepare for routing
*/
$http = new \Newbury\AlexaFramework\Http\Http($mode);
$appId = $http->request->getAppId();
$type = $http->request->getRequestType();

self::createCachePath();

if ($mode === self::APP_MODE_PRODUCTION) {
$proceed = $http->request->isRequestAuthentic($appId);
} else {
$proceed = true;
}

/**
* Determine controller
*/
if (isset($skills[$appId]) && $proceed) {
switch ($type) {
case 'LaunchRequest':
$controller = $skills[$appId]['routes']['LaunchRequest'] ??
\Newbury\AlexaFramework\Intent\DefaultIntent::class;
break;

case 'IntentRequest':
$intent = $http->request->getIntent();
$controller = $skills[$appId]['routes'][$intent] ??
\Newbury\AlexaFramework\Intent\DefaultIntent::class;
break;

case 'SessionEndedRequest':
$controller = $skills[$appId]['routes']['SessionEndedRequest'] ??
\Newbury\AlexaFramework\Intent\SessionEnd::class;
break;

default:
$controller = \Newbury\AlexaFramework\Intent\DefaultIntent::class;
}
} else {
$controller = \Newbury\AlexaFramework\Intent\InvalidApplication::class;
}

/**
* Load and execute controller
*/
/** @var $controller \Newbury\AlexaFramework\Intent\BaseIntent */
$controller = new $controller(
$http
);
$controller->execute();
}

private static function createCachePath()
{
$path = BASE_PATH . '/var/CertificateCache/';
if (!file_exists($path)) {
mkdir($path, 0777, true);
}
}
}
37 changes: 37 additions & 0 deletions src/Http/Http.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Newbury\AlexaFramework\Http;

class Http
{
protected $debug;
public $request;
public $response;

public function __construct($debug)
{
$this->debug = $debug;
$this->request = new \Newbury\AlexaFramework\Http\Request();
$this->response = new \Newbury\AlexaFramework\Http\Response();
}

public function __destruct()
{
if ($this->debug) {
$requestBody = $this->request->getRequestBody();
$response = $this->response->getResponse();

$sessionId = $this->request->getSessionId();

$requestId = $this->request->getRequestId();

$path = BASE_PATH . '/var/debug/' . $sessionId . '/' . $requestId;
if (!file_exists($path)) {
$success = mkdir($path, 0777, true);
}
is_null($requestBody) ?: file_put_contents($path . '/request', json_encode($requestBody));
is_null($response) ?: file_put_contents($path . '/response', json_encode($response));
is_null($response) ?: file_put_contents($path . '/rawResponse', ob_get_contents());
}
}
}
Loading

0 comments on commit 8ce1b90

Please sign in to comment.