Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.18 KB | None | 0 0
  1. <?php
  2.  
  3.     //errors reporting On-----------------------------------------
  4.       error_reporting(E_ALL);
  5.       ini_set('display_errors', 1);
  6.     //------------------------------------------------------------
  7.  
  8.     //Autoload Classes--------------------------------------------
  9.     function my_autoload ($pClassName)
  10.       {
  11.         require_once("Class/" . $pClassName . ".class.php");
  12.       }
  13.  
  14.     spl_autoload_register("my_autoload");
  15.  
  16.     //Load Services
  17.     require_once ('Service/Settings.php'); //Настройки
  18.     require_once ('Service/Database.php'); //БД
  19.    
  20.     //Load external libraries
  21.     include('Html_dom/simple_html_dom.php');
  22.     require_once ('../vendor/autoload.php');
  23.     use ApiAi\Client;
  24.     use ApiAi\Model\Query;
  25.     use ApiAi\Method\QueryApi;
  26.     //--------------------------------------------------------------
  27.  
  28.  
  29.  
  30.     if (!isset($_REQUEST)) {
  31.       return;
  32.     }
  33.  
  34.  
  35.  
  36.     //Get VK notification and decode it
  37.     $data = json_decode(file_get_contents('php://input'));
  38.  
  39.     //Check "type" of notification
  40.     switch ($data->type)
  41.     {
  42.         //If it is server adress confirmation------------------------
  43.         case 'confirmation':
  44.           //confirm adress
  45.           echo VkBot\Settings\ConfirmationToken;
  46.           break;
  47.           //---------------------------------------------------------
  48.  
  49.  
  50.  
  51.         //If it is a new message-------------------------------------
  52.         case 'message_new':
  53.    
  54.           //Send "ok" to Callback API server
  55.           echo('ok');
  56.  
  57.           //Create new User (geting info from users.get)
  58.           $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);
  59.    
  60.           //Check if user is already in the database and add them to the DB if no
  61.           if (!$user->Exist())
  62.             {
  63.               $user->Add();
  64.             }
  65.  
  66.    
  67.           //Trying to parse natural language with api.ai
  68.           $client = new Client(VkBot\Settings\ApiAiToken);
  69.           $queryApi = new QueryApi($client);
  70.  
  71.           $meaning = $queryApi->extractMeaning($data->object->body, [
  72.                                                'sessionId' => $data->object->id,
  73.                                                'lang' => 'ru',
  74.                                                                     ]);
  75.           $response = new Query($meaning);
  76.        
  77.    
  78.           //Responding to the User's message
  79.  
  80.           //Array of arguments
  81.           $arguments = array (
  82.                         "database" => $database,                                                              //DB instance
  83.                         "user" => $user,                                                                      //current user
  84.                         "parameters" => $response -> getResult() -> getParameters()                          //parameters from response
  85.                         );
  86.  
  87.  
  88.           //Execute operation
  89.           $operation = Operation::initial($response -> getResult() -> getMetadata() -> getIntentName(), $arguments) -> Execute($response -> getResult() -> getFulfillment() -> getSpeech());
  90.  
  91.         break;
  92.     }
  93. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement