1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 20:22:42 +01:00

Working on the bot

This commit is contained in:
Hillel Coren 2016-08-10 17:04:17 +03:00
parent 815c3991a6
commit 0a7381294e
16 changed files with 78 additions and 157 deletions

View File

@ -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;
}

View File

@ -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'],

View File

@ -0,0 +1,15 @@
<?php namespace App\Libraries\Skype;
class CarouselCard
{
public function __construct()
{
$this->contentType = 'application/vnd.microsoft.card.carousel';
$this->attachments = [];
}
public function addAttachment($attachment)
{
$this->attachments[] = $attachment;
}
}

View File

@ -1,5 +1,7 @@
<?php namespace App\Libraries\Skype;
use stdClass;
class HeroCard
{
public function __construct()
@ -19,8 +21,13 @@ class HeroCard
$this->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);
}
}

View File

@ -1,5 +1,6 @@
<?php namespace App\Models;
use Laracasts\Presenter\PresentableTrait;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
@ -7,12 +8,18 @@ use Illuminate\Database\Eloquent\SoftDeletes;
*/
class Product extends EntityModel
{
use PresentableTrait;
use SoftDeletes;
/**
* @var array
*/
protected $dates = ['deleted_at'];
/**
* @var string
*/
protected $presenter = 'App\Ninja\Presenters\ProductPresenter';
/**
* @var array
*/

View File

@ -203,7 +203,9 @@ class BaseIntent
if (is_string($content)) {
$response->setText($content);
} else {
if ( ! is_array($content)) {
if ($content instanceof \Illuminate\Database\Eloquent\Collection) {
// do nothing
} elseif ( ! is_array($content)) {
$content = [$content];
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -49,6 +49,6 @@ class UpdateInvoiceIntent extends InvoiceIntent
->present()
->skypeBot;
return $this->createResponse('message/card.receipt', $response);
return $this->createResponse(SKYPE_CARD_RECEIPT, $response);
}
}

View File

@ -5,7 +5,6 @@ use Laracasts\Presenter\Presenter;
class EntityPresenter extends Presenter
{
/**
* @return string
*/

View File

@ -0,0 +1,20 @@
<?php namespace App\Ninja\Presenters;
use App\Libraries\Skype\HeroCard;
class ProductPresenter extends EntityPresenter
{
public function skypeBot($account)
{
$product = $this->entity;
$card = new HeroCard();
$card->setTitle($product->product_key);
$card->setSubitle($account->formatMoney($product->cost));
$card->setText($product->notes);
return $card;
}
}

View File

@ -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',
);

View File

@ -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
}
}
]
}

View File

@ -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"
}
]
}
}
]
}

View File

@ -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
]
}

View File

@ -1,4 +0,0 @@
{
"type": "message/text",
"text": "{!! addslashes($message) !!}"
}