From 0a7381294ead2bb1414207e5093830a5bbe67eb6 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Wed, 10 Aug 2016 17:04:17 +0300 Subject: [PATCH] Working on the bot --- app/Http/Controllers/BotController.php | 6 +- app/Http/routes.php | 4 ++ app/Libraries/Skype/CarouselCard.php | 15 +++++ app/Libraries/Skype/HeroCard.php | 11 +++- app/Models/Product.php | 7 ++ app/Ninja/Intents/BaseIntent.php | 4 +- app/Ninja/Intents/CreateInvoiceIntent.php | 2 +- app/Ninja/Intents/ListProductsIntent.php | 18 ++++-- app/Ninja/Intents/UpdateInvoiceIntent.php | 2 +- app/Ninja/Presenters/EntityPresenter.php | 1 - app/Ninja/Presenters/ProductPresenter.php | 20 ++++++ resources/lang/en/texts.php | 2 + resources/views/bots/skype/card.blade.php | 43 ------------- resources/views/bots/skype/invoice.blade.php | 67 -------------------- resources/views/bots/skype/list.blade.php | 29 --------- resources/views/bots/skype/message.blade.php | 4 -- 16 files changed, 78 insertions(+), 157 deletions(-) create mode 100644 app/Libraries/Skype/CarouselCard.php create mode 100644 app/Ninja/Presenters/ProductPresenter.php delete mode 100644 resources/views/bots/skype/card.blade.php delete mode 100644 resources/views/bots/skype/invoice.blade.php delete mode 100644 resources/views/bots/skype/list.blade.php delete mode 100644 resources/views/bots/skype/message.blade.php diff --git a/app/Http/Controllers/BotController.php b/app/Http/Controllers/BotController.php index fbcde8259859..cb349a4f42fb 100644 --- a/app/Http/Controllers/BotController.php +++ b/app/Http/Controllers/BotController.php @@ -13,10 +13,10 @@ class BotController extends Controller { $to = '29:1C-OsU7OWBEDOYJhQUsDkYHmycOwOq9QOg5FVTwRX9ts'; //$message = 'new invoice for john for 2 items due tomorrow'; - //$message = 'create a new invoice for john smith for 2 tickets, set the invoice date to today, the due date to tomorrow, the deposit to 5 and the discount set to 10 percent'; + $message = 'invoice acme client for 3 months support, set due date to next thursday and the discount to 10 percent'; //$message = 'create a new invoice for john smith with a due date of September 7th'; //$message = 'create a new invoice for john'; - $message = 'add 2 tickets and set the due date to yesterday'; + //$message = 'add 2 tickets and set the due date to yesterday'; //$message = 'set the po number to 0004'; //$message = 'set the quantity to 20'; //$message = 'send the invoice'; @@ -80,7 +80,7 @@ class BotController extends Controller $url = sprintf('%s?id=%s&subscription-key=%s&q=%s', MSBOT_LUIS_URL, $appId, $subKey, $message); $data = file_get_contents($url); $data = json_decode($data); - + dd($data); return $data; } diff --git a/app/Http/routes.php b/app/Http/routes.php index 402d8dd4c33c..5e4a7752deb8 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -797,6 +797,10 @@ if (!defined('CONTACT_EMAIL')) { define('WEPAY_APP_FEE_MULTIPLIER', env('WEPAY_APP_FEE_MULTIPLIER', 0.002)); define('WEPAY_APP_FEE_FIXED', env('WEPAY_APP_FEE_MULTIPLIER', 0.00)); + define('SKYPE_CARD_RECEIPT', 'message/card.receipt'); + define('SKYPE_CARD_CAROUSEL', 'message/card.carousel'); + define('SKYPE_CARD_HERO', ''); + $creditCards = [ 1 => ['card' => 'images/credit_cards/Test-Visa-Icon.png', 'text' => 'Visa'], 2 => ['card' => 'images/credit_cards/Test-MasterCard-Icon.png', 'text' => 'Master Card'], diff --git a/app/Libraries/Skype/CarouselCard.php b/app/Libraries/Skype/CarouselCard.php new file mode 100644 index 000000000000..22d45fd95838 --- /dev/null +++ b/app/Libraries/Skype/CarouselCard.php @@ -0,0 +1,15 @@ +contentType = 'application/vnd.microsoft.card.carousel'; + $this->attachments = []; + } + + public function addAttachment($attachment) + { + $this->attachments[] = $attachment; + } +} diff --git a/app/Libraries/Skype/HeroCard.php b/app/Libraries/Skype/HeroCard.php index 3b3112c8a5be..468d17d10dcc 100644 --- a/app/Libraries/Skype/HeroCard.php +++ b/app/Libraries/Skype/HeroCard.php @@ -1,5 +1,7 @@ content->subtitle = $subtitle; } - public function addButton($button) + public function setText($text) { - $this->content->buttons[] = $button; + $this->content->text = $text; + } + + public function addButton($type, $title, $value) + { + $this->content->buttons[] = new ButtonCard($type, $title, $value); } } diff --git a/app/Models/Product.php b/app/Models/Product.php index ba22c58339e2..abb93f80b26d 100644 --- a/app/Models/Product.php +++ b/app/Models/Product.php @@ -1,5 +1,6 @@ setText($content); } else { - if ( ! is_array($content)) { + if ($content instanceof \Illuminate\Database\Eloquent\Collection) { + // do nothing + } elseif ( ! is_array($content)) { $content = [$content]; } diff --git a/app/Ninja/Intents/CreateInvoiceIntent.php b/app/Ninja/Intents/CreateInvoiceIntent.php index a9daa84b6b2d..3cb0abb8e18c 100644 --- a/app/Ninja/Intents/CreateInvoiceIntent.php +++ b/app/Ninja/Intents/CreateInvoiceIntent.php @@ -38,6 +38,6 @@ class CreateInvoiceIntent extends InvoiceIntent $this->setEntities(ENTITY_INVOICE, $invoice->public_id); $this->setEntities(ENTITY_INVOICE_ITEM, $invoiceItemIds); - return $this->createResponse('message/card.receipt', $invoice->present()->skypeBot); + return $this->createResponse(SKYPE_CARD_RECEIPT, $invoice->present()->skypeBot); } } diff --git a/app/Ninja/Intents/ListProductsIntent.php b/app/Ninja/Intents/ListProductsIntent.php index 93baf14fc10c..deb510b41f2d 100644 --- a/app/Ninja/Intents/ListProductsIntent.php +++ b/app/Ninja/Intents/ListProductsIntent.php @@ -9,11 +9,19 @@ class ListProductsIntent extends ProductIntent { public function process() { - $products = Product::scope()->get(); - - return view('bots.skype.list', [ - 'items' => $products - ])->render(); + $account = Auth::user()->account; + $products = Product::scope() + ->orderBy('product_key') + ->limit(10) + ->get() + ->transform(function($item, $key) use ($account) { + $card = $item->present()->skypeBot($account); + if ($this->entity(ENTITY_INVOICE)) { + $card->addButton('imBack', trans('texts.add_to_invoice'), trans('texts.add_to_invoice_command', ['product' => $item->product_key])); + } + return $card; + }); + return $this->createResponse(SKYPE_CARD_CAROUSEL, $products); } } diff --git a/app/Ninja/Intents/UpdateInvoiceIntent.php b/app/Ninja/Intents/UpdateInvoiceIntent.php index 72be9f6bc5e0..b271331a3b34 100644 --- a/app/Ninja/Intents/UpdateInvoiceIntent.php +++ b/app/Ninja/Intents/UpdateInvoiceIntent.php @@ -49,6 +49,6 @@ class UpdateInvoiceIntent extends InvoiceIntent ->present() ->skypeBot; - return $this->createResponse('message/card.receipt', $response); + return $this->createResponse(SKYPE_CARD_RECEIPT, $response); } } diff --git a/app/Ninja/Presenters/EntityPresenter.php b/app/Ninja/Presenters/EntityPresenter.php index 7a8255ccb70a..a37bf8b6c7b3 100644 --- a/app/Ninja/Presenters/EntityPresenter.php +++ b/app/Ninja/Presenters/EntityPresenter.php @@ -5,7 +5,6 @@ use Laracasts\Presenter\Presenter; class EntityPresenter extends Presenter { - /** * @return string */ diff --git a/app/Ninja/Presenters/ProductPresenter.php b/app/Ninja/Presenters/ProductPresenter.php new file mode 100644 index 000000000000..0e3acba47d96 --- /dev/null +++ b/app/Ninja/Presenters/ProductPresenter.php @@ -0,0 +1,20 @@ +entity; + + $card = new HeroCard(); + $card->setTitle($product->product_key); + $card->setSubitle($account->formatMoney($product->cost)); + $card->setText($product->notes); + + return $card; + } + +} diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index b1321471f82b..b90a9e5a650a 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -2057,6 +2057,8 @@ $LANG = array( 'client_not_found' => 'We weren\'t able to find the client', 'not_allowed' => 'Sorry, you don\'t have the needed permissions', 'bot_emailed_invoice' => 'Your invoice has been emailed', + 'add_to_invoice' => 'Add to invoice', + 'add_to_invoice_command' => 'Add 1 :product', ); diff --git a/resources/views/bots/skype/card.blade.php b/resources/views/bots/skype/card.blade.php deleted file mode 100644 index 93b138c61d47..000000000000 --- a/resources/views/bots/skype/card.blade.php +++ /dev/null @@ -1,43 +0,0 @@ -{ - "type":"message/card.carousel", - "attachments":[ - { - "contentType":"application/vnd.microsoft.card.hero", - "content":{ - "title":"{!! $title !!}" - @if ( ! empty($subtitle)) - , "subtitle":"{!! $subtitle !!}" - @endif - @if ( ! empty($text)) - , "text":"{!! $text !!}" - @endif - @if ( ! empty($images)) - , "images":[ - @foreach($images as $image) - @if ($images[0] != $image) - , - @endif - { - "image":"{{ $image }}" - } - @endforeach - ] - @endif - @if ( ! empty($buttons)) - , "buttons":[ - @foreach($buttons as $button) - @if ($buttons[0] != $button) - , - @endif - { - "type":"{{ $button['type'] }}", - "title":"{!! $button['title'] !!}", - "value":"{!! $button['value'] !!}" - } - @endforeach - ] - @endif - } - } - ] -} diff --git a/resources/views/bots/skype/invoice.blade.php b/resources/views/bots/skype/invoice.blade.php deleted file mode 100644 index d4ab26f20a96..000000000000 --- a/resources/views/bots/skype/invoice.blade.php +++ /dev/null @@ -1,67 +0,0 @@ -{ - "type":"message/card.receipt", - "attachments":[ - { - "contentType":"application/vnd.microsoft.card.receipt", - "content":{ - "title" : '{!! trans('texts.invoice_for_client', [ - 'invoice' => link_to($invoice->getRoute(), $invoice->invoice_number), - 'client' => link_to($invoice->client->getRoute(), $invoice->client->getDisplayName()) - ]) !!}', - "facts": [ - { - "key": "{{ trans('texts.email') }}:", - "value": "{!! addslashes(HTML::mailto($invoice->client->contacts[0]->email)->toHtml()) !!}" - } - @if ($invoice->due_date) - , { - "key": "{{ $invoice->present()->dueDateLabel }}:", - "value": "{{ $invoice->present()->due_date }}" - } - @endif - @if ($invoice->po_number) - , { - "key": "{{ trans('texts.po_number') }}:", - "value": "{{ $invoice->po_number }}" - } - @endif - @if ($invoice->discount) - , { - "key": "{{ trans('texts.discount') }}:", - "value": "{{ $invoice->present()->discount }}" - } - @endif - ], - "items":[ - @foreach ($invoice->invoice_items as $item) - @if ($invoice->invoice_items[0] != $item) - , - @endif - { - "title":"{{ $item->product_key }}", - "subtitle":"{{ $item->notes }}", - "price":"{{ $item->cost }}", - "quantity":"{{ $item->qty }}" - } - @endforeach - ], - @if (false) - "tax":"0.00", - @endif - "total":"{{ $invoice->present()->requestedAmount }}", - "buttons":[ - { - "type":"imBack", - "title":"{{ trans('texts.send_email') }}", - "value":"send_email" - }, - { - "type":"imBack", - "title":"{{ trans('texts.download_pdf') }}", - "value":"download_pdf" - } - ] - } - } - ] -} diff --git a/resources/views/bots/skype/list.blade.php b/resources/views/bots/skype/list.blade.php deleted file mode 100644 index 989e898a4a46..000000000000 --- a/resources/views/bots/skype/list.blade.php +++ /dev/null @@ -1,29 +0,0 @@ -{ - "type":"message/card.carousel", - "attachments":[ - @foreach ($items as $item) - @if ($items[0] != $item) - , - @endif - { - "contentType": "application/vnd.microsoft.card.hero", - "content": { - "title": "{{ $item['title'] }}", - "subtitle": "{{ $item['subtitle'] }}", - "buttons": [ - @foreach($item['buttons'] as $button) - @if ($items['buttons'][0] != $button) - , - @endif - { - "type": "{{ $button['type'] }}", - "title": "{{ $button['title'] }}", - "value": "https://en.wikipedia.org/wiki/{cardContent.Key}" - } - @endforeach - ] - } - } - @endforeach - ] -} diff --git a/resources/views/bots/skype/message.blade.php b/resources/views/bots/skype/message.blade.php deleted file mode 100644 index 831f98433577..000000000000 --- a/resources/views/bots/skype/message.blade.php +++ /dev/null @@ -1,4 +0,0 @@ -{ - "type": "message/text", - "text": "{!! addslashes($message) !!}" -}