Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //errors reporting On-----------------------------------------
- error_reporting(E_ALL);
- ini_set('display_errors', 1);
- //------------------------------------------------------------
- //Autoload Classes--------------------------------------------
- function my_autoload ($pClassName)
- {
- require_once("Class/" . $pClassName . ".class.php");
- }
- spl_autoload_register("my_autoload");
- //Load Services
- require_once ('Service/Settings.php'); //Настройки
- require_once ('Service/Database.php'); //БД
- //Load external libraries
- include('Html_dom/simple_html_dom.php');
- require_once ('../vendor/autoload.php');
- use ApiAi\Client;
- use ApiAi\Model\Query;
- use ApiAi\Method\QueryApi;
- //--------------------------------------------------------------
- if (!isset($_REQUEST)) {
- return;
- }
- //Get VK notification and decode it
- $data = json_decode(file_get_contents('php://input'));
- //Check "type" of notification
- switch ($data->type)
- {
- //If it is server adress confirmation------------------------
- case 'confirmation':
- //confirm adress
- echo VkBot\Settings\ConfirmationToken;
- break;
- //---------------------------------------------------------
- //If it is a new message-------------------------------------
- case 'message_new':
- //Send "ok" to Callback API server
- echo('ok');
- //Create new User (geting info from users.get)
- $user = new User (json_decode(file_get_contents("https://api.vk.com/method/users.get?user_ids={$data->object->user_id}&v=5.0")), $database);
- //Check if user is already in the database and add them to the DB if no
- if (!$user->Exist())
- {
- $user->Add();
- }
- //Trying to parse natural language with api.ai
- $client = new Client(VkBot\Settings\ApiAiToken);
- $queryApi = new QueryApi($client);
- $meaning = $queryApi->extractMeaning($data->object->body, [
- 'sessionId' => $data->object->id,
- 'lang' => 'ru',
- ]);
- $response = new Query($meaning);
- //Responding to the User's message
- //Array of arguments
- $arguments = array (
- "database" => $database, //DB instance
- "user" => $user, //current user
- "parameters" => $response -> getResult() -> getParameters() //parameters from response
- );
- //Execute operation
- $operation = Operation::initial($response -> getResult() -> getMetadata() -> getIntentName(), $arguments) -> Execute($response -> getResult() -> getFulfillment() -> getSpeech());
- break;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement