Skip to content

Instantly share code, notes, and snippets.

@mpociot
Created May 11, 2017 19:52
Show Gist options
  • Save mpociot/7ea1271cac1d9d377bc9e1e64bbd616e to your computer and use it in GitHub Desktop.
Save mpociot/7ea1271cac1d9d377bc9e1e64bbd616e to your computer and use it in GitHub Desktop.
This is an Amazon Alexa driver to work in combination with the BotMan chatbot library.

The driver requires the minicodemonkey/amazon-alexa-php PHP package.

composer require minicodemonkey/amazon-alexa-php
<?php
namespace App;
use Alexa\Response\Response as AlexaResponse;
use Illuminate\Support\Collection;
use Mpociot\BotMan\Answer;
use Mpociot\BotMan\Drivers\Driver;
use Mpociot\BotMan\Interfaces\UserInterface;
use Mpociot\BotMan\Message;
use Mpociot\BotMan\Question;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Mpociot\BotMan\Messages\Message as IncomingMessage;
class AlexaDriver extends Driver
{
const DRIVER_NAME = 'Alexa';
/**
* Determine if the request is for this driver.
*
* @return bool
*/
public function matchesRequest()
{
return $this->event->has('requestId') && $this->event->has('type');
}
/**
* Retrieve the chat message(s).
*
* @return array
*/
public function getMessages()
{
$intent = $this->event->get('intent');
$session = $this->payload->get('session');
$message = new Message($intent['name'], $session['user']['userId'], $session['sessionId']);
$message->addExtras('slots', $intent['slots']);
return [$message];
}
/**
* @return bool
*/
public function isBot()
{
return false;
}
/**
* @return bool
*/
public function isConfigured()
{
return true;
}
/**
* Retrieve User information.
* @param Message $matchingMessage
* @return UserInterface
*/
public function getUser(Message $matchingMessage)
{
// TODO: Implement getUser() method.
}
/**
* @param Message $message
* @return Answer
*/
public function getConversationAnswer(Message $message)
{
return Answer::create($message->getMessage())->setMessage($message);
}
/**
* @param string|Question $message
* @param Message $matchingMessage
* @param array $additionalParameters
* @return $this
*/
public function reply($message, $matchingMessage, $additionalParameters = [])
{
if ($message instanceof Question) {
$text = $message->getText();
} elseif ($message instanceof IncomingMessage) {
$text = $message->getMessage();
} else {
$text = $message;
}
$response = new AlexaResponse();
$response->respond($text);
Response::create(json_encode($response->render()))->send();
}
/**
* @param Request $request
* @return void
*/
public function buildPayload(Request $request)
{
$this->payload = new ParameterBag((array) json_decode($request->getContent(), true));
$this->event = Collection::make((array) $this->payload->get('request'));
}
/**
* Low-level method to perform driver specific API requests.
*
* @param string $endpoint
* @param array $parameters
* @param Message $matchingMessage
* @return void
*/
public function sendRequest($endpoint, array $parameters, Message $matchingMessage)
{
// TODO: Implement sendRequest() method.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment