-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8ce1b90
Showing
12 changed files
with
526 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
], | ||
], | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
Oops, something went wrong.