From 9dd2ab43c22549282425dac5f90e19f77345d1c1 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Tue, 4 Apr 2017 12:54:38 +0300 Subject: [PATCH] Working on speech rec --- app/Constants.php | 3 + app/Http/Controllers/BotController.php | 10 +- app/Http/routes.php | 1 + app/Ninja/Intents/BaseIntent.php | 11 +- .../Intents/WebApp/CreateInvoiceIntent.php | 28 +++ resources/views/header.blade.php | 13 +- .../partials/speech_recognition.blade.php | 215 ++++++++++++++++++ 7 files changed, 272 insertions(+), 9 deletions(-) create mode 100644 app/Ninja/Intents/WebApp/CreateInvoiceIntent.php create mode 100644 resources/views/partials/speech_recognition.blade.php diff --git a/app/Constants.php b/app/Constants.php index 75634ad9da..c7d5ccd45a 100644 --- a/app/Constants.php +++ b/app/Constants.php @@ -325,6 +325,9 @@ if (! defined('APP_NAME')) { define('SKYPE_API_URL', 'https://apis.skype.com/v3'); define('MSBOT_STATE_URL', 'https://state.botframework.com/v3'); + define('BOT_PLATFORM_WEB_APP', 'WebApp'); + define('BOT_PLATFORM_SKYPE', 'Skype'); + define('BLANK_IMAGE', 'data:image/png;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs='); define('COUNT_FREE_DESIGNS', 4); diff --git a/app/Http/Controllers/BotController.php b/app/Http/Controllers/BotController.php index 02267924f2..365e48c075 100644 --- a/app/Http/Controllers/BotController.php +++ b/app/Http/Controllers/BotController.php @@ -80,7 +80,7 @@ class BotController extends Controller $user->account->loadLocalizationSettings(); $data = $this->parseMessage($text); - $intent = BaseIntent::createIntent($state, $data); + $intent = BaseIntent::createIntent($platform, $state, $data); $response = $intent->process(); $state = $intent->getState(); } @@ -97,6 +97,14 @@ class BotController extends Controller return RESULT_SUCCESS; } + public function handleCommand() + { + $data = $this->parseMessage(request()->command); + $intent = BaseIntent::createIntent(BOT_PLATFORM_WEB_APP, false, $data); + + return $intent->process(); + } + private function authenticate($input) { $token = isset($_SERVER['HTTP_AUTHORIZATION']) ? $_SERVER['HTTP_AUTHORIZATION'] : false; diff --git a/app/Http/routes.php b/app/Http/routes.php index 2a4258f8c4..614954aa3d 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -127,6 +127,7 @@ Route::group(['middleware' => 'auth:user'], function () { Route::get('check_invoice_number/{invoice_id?}', 'InvoiceController@checkInvoiceNumber'); Route::post('save_sidebar_state', 'UserController@saveSidebarState'); Route::post('contact_us', 'HomeController@contactUs'); + Route::post('handle_command', 'BotController@handleCommand'); Route::get('settings/user_details', 'AccountController@showUserDetails'); Route::post('settings/user_details', 'AccountController@saveUserDetails'); diff --git a/app/Ninja/Intents/BaseIntent.php b/app/Ninja/Intents/BaseIntent.php index 012a75d46e..766810c018 100644 --- a/app/Ninja/Intents/BaseIntent.php +++ b/app/Ninja/Intents/BaseIntent.php @@ -32,7 +32,7 @@ class BaseIntent //var_dump($state); } - public static function createIntent($state, $data) + public static function createIntent($platform, $state, $data) { if (! count($data->intents)) { throw new Exception(trans('texts.intent_not_found')); @@ -48,13 +48,18 @@ class BaseIntent } } - if (! $entityType) { + if ($state && ! $entityType) { $entityType = $state->current->entityType; } $entityType = ucwords(strtolower($entityType)); $intent = str_replace('Entity', $entityType, $intent); - $className = "App\\Ninja\\Intents\\{$intent}Intent"; + + if ($platform == BOT_PLATFORM_WEB_APP) { + $className = "App\\Ninja\\Intents\\WebApp\\{$intent}Intent"; + } else { + $className = "App\\Ninja\\Intents\\{$intent}Intent"; + } //echo "Intent: $intent

"; diff --git a/app/Ninja/Intents/WebApp/CreateInvoiceIntent.php b/app/Ninja/Intents/WebApp/CreateInvoiceIntent.php new file mode 100644 index 0000000000..d2962fd184 --- /dev/null +++ b/app/Ninja/Intents/WebApp/CreateInvoiceIntent.php @@ -0,0 +1,28 @@ +requestClient(); + $invoiceItems = $this->requestInvoiceItems(); + + if (! $client) { + throw new Exception(trans('texts.client_not_found')); + } + + $data = array_merge($this->requestFields(), [ + 'client_id' => $client->public_id, + 'invoice_items' => $invoiceItems, + ]); + + //var_dump($data); + dd($data); + } +} diff --git a/resources/views/header.blade.php b/resources/views/header.blade.php index df27b0ab4b..ff4ddbe8e2 100644 --- a/resources/views/header.blade.php +++ b/resources/views/header.blade.php @@ -309,12 +309,15 @@ -