dev-master
9999999-devA Chatbot platform exchanger
MIT
The Requires
The Development Requires
by Quentin de Longraye
Wallogit.com
2017 © Pedro Peláez
A Chatbot platform exchanger
This platform is not heavy tested and is a proof-of-concept. Any contributions are welcomed., (*2)
ChatbotPlatform is a PHP library allowing to build a multiple chatbot platform with multiple actions providers and multiple sources., (*3)
The current implementation allows basic Ajax interactions or Facebook Messenger discussions. It can be extended easily., (*4)
Install the library using Composer:, (*5)
composer require dldl/chatbot-platform
ChatbotPlatform may be used as a standalone project or integrated into existing applications using any framework or CMS., (*6)
For a basic standalone usage, create an index.php file with the following code:, (*7)
<?php
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Dotenv\Dotenv;
use dLdL\ChatbotPlatform\ChatbotPlatform;
use dLdL\ChatbotPlatform\Handler\FacebookMessageHandler;
use dLdL\ChatbotPlatform\Handler\AjaxMessageHandler;
use dLdL\ChatbotPlatform\Action\RepeatMessageAction;
use dLdL\ChatbotPlatform\Action\APIAIAction;
require __DIR__.'/vendor/autoload.php';
(new Dotenv())->load(__DIR__.'/.env'); // Requires a .env file at project root to load configuration (see an example on .env.dist file)
$request = Request::createFromGlobals();
$chatbotPlatform = new ChatbotPlatform([
new FacebookMessageHandler(), // Enables discussion through Facebook messenger (configuration required in .env file)
new AjaxMessageHandler(), // Enables discussion through basic HTTP requests
], [
[new APIAIAction(), 10], // Enables API.AI support (configuration required in .env file)
new RepeatMessageAction(), // Enables a bot repeating anything you said (useful for testing)
]);
$response = $chatbotPlatform->handleRequest($request);
$response->send();
As you can see, you may pass an action instance or an array with the action instance and the priority it should hold., (*8)
You can then start a server redirecting to this file, and send requests from Facebook or an
Ajax script., (*9)
For Facebook usage, please refer to Facebook developers documentation.
For Ajax support, you must send POST requests to your server with the following body:, (*10)
{
"message": "Hello world!",
"sender": "sender-id",
"recipient": "recipient-id",
"discussion_id": "12345"
}
Reply will be returned immediately. Asynchronous replies are also supported using tags. Enable the AsynchronousMessageAction and add
to your body a tags: ['ASYNC_GET'] or tags: ['ASYNC_SAVE'] to get or save a message. Messages are removed from the SQLite database
when read. This can be used with your own actions to append the required tag to the message when needed., (*11)
ChatbotPlatform can be easily extended. It provides by default some message handlers and action providers., (*12)
See ChatbotPlatform/Handler for available message handlers. See ChatbotPlatform/Action for available actions., (*13)
You may add your own message handlers by implementing the MessageHandlerInterface and providing them to the ChatbotPlatform
instance., (*14)
You should also add your own actions by implementing the MessageActionInterface to perform custom actions when a message
is received (e.g. generating a reply or delegating the task to any external API)., (*15)
A Chatbot platform exchanger
MIT