Skip to content

Commit

Permalink
Response mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
maxbeckers committed Aug 7, 2017
1 parent db75590 commit 965a5b8
Show file tree
Hide file tree
Showing 41 changed files with 1,018 additions and 6 deletions.
78 changes: 76 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,76 @@
# Amazon alexa php library
This library is a helper for google actions with php.
# Google actions php library
This library is a helper for google actions with php.

## Install via composer
Require the package with composer:
```
composer require maxbeckers/google-actions-php
```

## Usage
Handle the request:
- map request data to request object
- validate request
- handle request data
- create response
- send response

### Map request data to request object
Map needed request headers and request body to `Request`.
```php
use MaxBeckers\GoogleActions\Request\Request;
...
$requestBody = file_get_contents('php://input');
$googleRequest = Request::fromGoogleRequest($requestBody);
```
### Validate request
The `RequestValidator` will handle the google request validation.
```php
use MaxBeckers\GoogleActions\Validation\RequestValidator;
...
$validator = new RequestValidator();
$validator->validate($googleRequest);
```
### Register request handlers
For different requests it's helpful to create different RequestHandlers.
```php
use MaxBeckers\GoogleActions\RequestHandler\RequestHandlerRegistry;
...
$requestHandlerRegistry = new RequestHandlerRegistry();
$requestHandlerRegistry->addHandler($myRequestHandler);
```
### Use registry to handle request
```php
use MaxBeckers\GoogleActions\RequestHandler\RequestHandlerRegistry;
...
$requestHandler = $requestHandlerRegistry->getSupportingHandler($googleRequest);
$response = $requestHandler->handleRequest($googleRequest);
```
### Render response
```php
header('Content-Type: application/json');
echo json_encode($response);
exit();
```
### Create a new request handler
The new request handler must extend `AbstractRequestHandler`.
First implement the abstract `supportsRequest`-method.
```php
public function supportsRequest(Request $request): bool
{
return true; // check request data
}
```
Then implement the `handleRequest`-method. For simple responses there is a `ResponseHelper`.
```php
use MaxBeckers\GoogleActions\Helper\ResponseHelper;
...
/** @var ResponseHelper */
private $responseHelper;
...
public function handleRequest(Request $request): Response
{
// todo set needed response data
return $responseHelper->respond('Success :)');
}
```
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"keywords": [
"google",
"actions",
"home",
"php"
],
"license": "MIT",
Expand Down
36 changes: 36 additions & 0 deletions examples/Handlers/SimpleRequestHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use MaxBeckers\GoogleActions\Helper\ResponseHelper;
use MaxBeckers\GoogleActions\Request\Request;
use MaxBeckers\GoogleActions\RequestHandler\AbstractRequestHandler;
use MaxBeckers\GoogleActions\Response\Response;

/**
* Just a simple example request handler.
*
* @author Maximilian Beckers <beckers.maximilian@gmail.com>
*/
class SimpleRequestHandler extends AbstractRequestHandler
{
/**
* @var ResponseHelper
*/
private $responseHelper;

/**
* {@inheritdoc}
*/
public function supportsRequest(Request $request): bool
{
// support all requests, should not be done.
return true;
}

/**
* {@inheritdoc}
*/
public function handleRequest(Request $request): Response
{
return $this->responseHelper->respond('Success :)');
}
}
18 changes: 14 additions & 4 deletions examples/simple-request.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
<?php

use MaxBeckers\GoogleActions\Request\Request;
use MaxBeckers\GoogleActions\Response\Response;
use MaxBeckers\GoogleActions\RequestHandler\RequestHandlerRegistry;
use MaxBeckers\GoogleActions\Validation\RequestValidator;

require '../vendor/autoload.php';
require 'Handlers/SimpleRequestHandler.php';

$requestBody = file_get_contents('php://input');
if ($requestBody) {
$alexaRequest = Request::fromGoogleRequest($requestBody);
$googleRequest = Request::fromGoogleRequest($requestBody);

// todo generate response
$response = new Response();
// Request validation
$validator = new RequestValidator();
$validator->validate($googleRequest);

// add handlers to registry
$mySimpleRequestHandler = new SimpleRequestHandler();
$requestHandlerRegistry = new RequestHandlerRegistry();
$requestHandlerRegistry->addHandler($mySimpleRequestHandler);

$response = $requestHandlerRegistry->getSupportingHandler($googleRequest)->handleRequest($googleRequest);
// render response
header('Content-Type: application/json');
echo json_encode($response);
Expand Down
10 changes: 10 additions & 0 deletions src/Exception/InvalidRequestException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace MaxBeckers\GoogleActions\Exception;

/**
* @author Maximilian Beckers <beckers.maximilian@gmail.com>
*/
class InvalidRequestException extends \Exception
{
}
10 changes: 10 additions & 0 deletions src/Exception/MissingRequestHandlerException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace MaxBeckers\GoogleActions\Exception;

/**
* @author Maximilian Beckers <beckers.maximilian@gmail.com>
*/
class MissingRequestHandlerException extends \Exception
{
}
60 changes: 60 additions & 0 deletions src/Helper/ResponseHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace MaxBeckers\GoogleActions\Helper;

use MaxBeckers\GoogleActions\Response\ExpectedIntent;
use MaxBeckers\GoogleActions\Response\InputPrompt;
use MaxBeckers\GoogleActions\Response\Item;
use MaxBeckers\GoogleActions\Response\Response;
use MaxBeckers\GoogleActions\Response\ExpectedInput;
use MaxBeckers\GoogleActions\Response\RichResponse;
use MaxBeckers\GoogleActions\Response\SimpleResponse;

/**
* @author Maximilian Beckers <beckers.maximilian@gmail.com>
*/
class ResponseHelper
{

/**
* @var Response
*/
public $response;

/**
* ResponseHelper constructor creates a new response object.
*/
public function __construct()
{
$this->response = new Response();
}

/**
* @param string $text
*
* @return Response
*/
public function respond(string $text): Response
{
$simpleResponse = new SimpleResponse();
$simpleResponse->textToSpeech = $text;
$simpleResponse->displayText = $text;
$item = new Item();
$item->simpleResponse = $simpleResponse;
$richInitialPrompt = new RichResponse();
$richInitialPrompt->items[] = $item;
$inputPrompt = new InputPrompt();
$inputPrompt->richInitialPrompt = $richInitialPrompt;

$intent = new ExpectedIntent();
$intent->intent = ExpectedIntent::TYPE_TEXT;

$expectedInput = new ExpectedInput();
$expectedInput->inputPrompt = $inputPrompt;
$expectedInput->possibleIntents[] = $intent;

$this->response->expectedInputs[] = $expectedInput;

return $this->response;
}
}
30 changes: 30 additions & 0 deletions src/RequestHandler/AbstractRequestHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace MaxBeckers\GoogleActions\RequestHandler;

use MaxBeckers\GoogleActions\Request\Request;
use MaxBeckers\GoogleActions\Response\Response;

/**
* @author Maximilian Beckers <beckers.maximilian@gmail.com>
*/
abstract class AbstractRequestHandler
{
/**
* Check if the request handler can handle given request.
*
* @param Request $request
*
* @return bool
*/
public abstract function supportsRequest(Request $request): bool;

/**
* Handle the given request end return a response object.
*
* @param Request $request
*
* @return Response
*/
public abstract function handleRequest(Request $request): Response;
}
43 changes: 43 additions & 0 deletions src/RequestHandler/RequestHandlerRegistry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace MaxBeckers\GoogleActions\RequestHandler;

use MaxBeckers\GoogleActions\Exception\MissingRequestHandlerException;
use MaxBeckers\GoogleActions\Request\Request;

/**
* @author Maximilian Beckers <beckers.maximilian@gmail.com>
*/
class RequestHandlerRegistry
{
/**
* @var AbstractRequestHandler[]
*/
private $requestHandlers = [];

/**
* @param Request $request
*
* @throws MissingRequestHandlerException
*
* @return AbstractRequestHandler
*/
public function getSupportingHandler(Request $request): AbstractRequestHandler
{
foreach ($this->requestHandlers as $requestHandler) {
if ($requestHandler->supportsRequest($request)) {
return $requestHandler;
}
}

throw new MissingRequestHandlerException();
}

/**
* @param AbstractRequestHandler $handler
*/
public function addHandler(AbstractRequestHandler $handler)
{
$this->requestHandlers[] = $handler;
}
}
31 changes: 31 additions & 0 deletions src/Response/Action.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace MaxBeckers\GoogleActions\Response;

/**
* @author Maximilian Beckers <beckers.maximilian@gmail.com>
*/
class Action
{
const TYPE_UNKNOWN = 'UNKNOWN';
const TYPE_VIEW_DETAILS = 'VIEW_DETAILS';
const TYPE_MODIFY = 'MODIFY';
const TYPE_CANCEL = 'CANCEL';
const TYPE_RETURN = 'RETURN';
const TYPE_EXCHANGE = 'EXCHANGE';
const TYPE_EMAIL = 'EMAIL';
const TYPE_CALL = 'CALL';
const TYPE_REORDER = 'REORDER';
const TYPE_REVIEW = 'REVIEW';
const TYPE_CUSTOMER_SERVICE = 'CUSTOMER_SERVICE';

/**
* @var string|null
*/
public $type;

/**
* @var Button|null
*/
public $button;
}
34 changes: 34 additions & 0 deletions src/Response/BasicCard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace MaxBeckers\GoogleActions\Response;

/**
* @author Maximilian Beckers <beckers.maximilian@gmail.com>
*/
class BasicCard
{
/**
* @var string|null
*/
public $title;

/**
* @var string|null
*/
public $subtitle;

/**
* @var string|null
*/
public $formattedText;

/**
* @var Image|null
*/
public $image;

/**
* @var Button[]
*/
public $buttons = [];
}
Loading

0 comments on commit 965a5b8

Please sign in to comment.