From b8a124dc0fd9103cbafb34781a44c6d84d84c389 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Sun, 7 Aug 2016 09:10:36 +0300 Subject: [PATCH] Working on our bot --- app/Http/Controllers/BotController.php | 48 +++++++++++++------- app/Ninja/Intents/BaseIntent.php | 12 +++-- resources/lang/en/texts.php | 1 + resources/views/bots/skype/message.blade.php | 4 ++ 4 files changed, 45 insertions(+), 20 deletions(-) create mode 100644 resources/views/bots/skype/message.blade.php diff --git a/app/Http/Controllers/BotController.php b/app/Http/Controllers/BotController.php index faefe94ce5ee..64ba4e36dde8 100644 --- a/app/Http/Controllers/BotController.php +++ b/app/Http/Controllers/BotController.php @@ -2,20 +2,11 @@ namespace App\Http\Controllers; -use App\Ninja\Repositories\InvoiceRepository; +use Exception; use App\Ninja\Intents\BaseIntent; class BotController extends Controller { - protected $invoiceRepo; - - public function __construct(InvoiceRepository $invoiceRepo) - { - //parent::__construct(); - - $this->invoiceRepo = $invoiceRepo; - } - public function handleMessage($platform) { $message = 'invoice acme client for 2 tickets'; @@ -24,6 +15,35 @@ class BotController extends Controller //$message = view('bots.skype.message', ['message' => 'testing'])->render(); //return $this->sendResponse($to, $message); + $message = '{ + "type": "message/image", + "text": "Test message", + "attachments": [ + { + "contentType": "application/pdf", + "contentUrl": "http://www.orimi.com/pdf-test.pdf", + "thumbnailUrl": "http://www.orimi.com/pdf-test.pdf", + "filename": "test.pdf" + } + ] +}'; + return $this->sendResponse($to, $message); + + try { + $data = $this->parseMessage($message); + $intent = BaseIntent::createIntent($data); + $message = $intent->process(); + } catch (Exception $exception) { + $message = view('bots.skype.message', [ + 'message' => $exception->getMessage() + ])->render(); + } + + $this->sendResponse($to, $message); + } + + private function parseMessage($message) + { $appId = env('MSBOT_LUIS_APP_ID'); $subKey = env('MSBOT_LUIS_SUBSCRIPTION_KEY'); $message = rawurlencode($message); @@ -34,13 +54,7 @@ class BotController extends Controller var_dump($data->compositeEntities); - if ($intent = BaseIntent::createIntent($data)) { - $message = $intent->process(); - } else { - $message = view('bots.skype.message', ['message' => trans('texts.intent_not_found')])->render(); - } - - $this->sendResponse($to, $message); + return $data; } private function sendResponse($to, $message) diff --git a/app/Ninja/Intents/BaseIntent.php b/app/Ninja/Intents/BaseIntent.php index f400eef9a2c8..95362ec36474 100644 --- a/app/Ninja/Intents/BaseIntent.php +++ b/app/Ninja/Intents/BaseIntent.php @@ -1,5 +1,6 @@ intents)) { - return false; + throw new Exception(trans('texts.intent_not_found')); } $intent = $data->intents[0]; $intentType = $intent->intent; - + $className = "App\\Ninja\\Intents\\{$intentType}Intent"; - return new $className($data); + + if ( ! class_exists($className)) { + throw new Exception(trans('texts.intent_not_supported')); + } + + return (new $className($data)); } public function process() diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index fd040797c096..f928a063108d 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -2053,6 +2053,7 @@ $LANG = array( 'payment_type_on_file' => ':type on file', 'invoice_for_client' => 'Invoice :invoice for :client', 'intent_not_found' => 'Sorry, I\'m not sure what you\'re asking.', + 'intent_not_supported' => 'Sorry, I\'m not able to do that.', 'client_not_found' => 'We weren\'t able to find the client', ); diff --git a/resources/views/bots/skype/message.blade.php b/resources/views/bots/skype/message.blade.php new file mode 100644 index 000000000000..d33830c20871 --- /dev/null +++ b/resources/views/bots/skype/message.blade.php @@ -0,0 +1,4 @@ +{ + "type": "message/text", + "text": "{!! $message !!}" +}