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:
parent
91ecfc0ea8
commit
d374843e50
@ -11,17 +11,19 @@ class BotController extends Controller
|
|||||||
public function handleMessage($platform)
|
public function handleMessage($platform)
|
||||||
{
|
{
|
||||||
$to = '29:1C-OsU7OWBEDOYJhQUsDkYHmycOwOq9QOg5FVTwRX9ts';
|
$to = '29:1C-OsU7OWBEDOYJhQUsDkYHmycOwOq9QOg5FVTwRX9ts';
|
||||||
//$message = 'create a new invoice for Jenifer Altenwerth ';
|
//$message = 'create a new invoice for Mr. Gino ';
|
||||||
$message = 'add 2 items';
|
$message = 'add 1 item';
|
||||||
|
//$message = 'set the cost to 20';
|
||||||
|
//$message = 'send the invoice';
|
||||||
|
|
||||||
//$message = view('bots.skype.message', ['message' => $message])->render();
|
//$message = view('bots.skype.message', ['message' => $message])->render();
|
||||||
//return $this->sendResponse($to, $message);
|
//return $this->sendResponse($to, $message);
|
||||||
|
|
||||||
|
echo "Message: $message <p>";
|
||||||
$token = $this->authenticate();
|
$token = $this->authenticate();
|
||||||
|
|
||||||
//try {
|
//try {
|
||||||
$state = $this->loadState($token);
|
$state = $this->loadState($token);
|
||||||
var_dump($state);
|
|
||||||
|
|
||||||
$data = $this->parseMessage($message);
|
$data = $this->parseMessage($message);
|
||||||
|
|
||||||
$intent = BaseIntent::createIntent($state, $data);
|
$intent = BaseIntent::createIntent($state, $data);
|
||||||
@ -78,8 +80,6 @@ class BotController extends Controller
|
|||||||
$data = file_get_contents($url);
|
$data = file_get_contents($url);
|
||||||
$data = json_decode($data);
|
$data = json_decode($data);
|
||||||
|
|
||||||
var_dump($data);
|
|
||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -111,6 +111,7 @@ class BotController extends Controller
|
|||||||
|
|
||||||
$response = CurlUtils::post($url, $message, $headers);
|
$response = CurlUtils::post($url, $message, $headers);
|
||||||
|
|
||||||
|
var_dump($message);
|
||||||
var_dump($response);
|
var_dump($response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,8 +11,22 @@ class BaseIntent
|
|||||||
|
|
||||||
public function __construct($state, $data)
|
public function __construct($state, $data)
|
||||||
{
|
{
|
||||||
$this->state = $state ?: new stdClass;
|
//if (true) {
|
||||||
|
if ( ! $state) {
|
||||||
|
$state = new stdClass;
|
||||||
|
foreach (['current', 'previous'] as $reference) {
|
||||||
|
$state->$reference = new stdClass;
|
||||||
|
$state->$reference->entityType = false;
|
||||||
|
foreach ([ENTITY_INVOICE, ENTITY_CLIENT, ENTITY_INVOICE_ITEM] as $entityType) {
|
||||||
|
$state->$reference->$entityType = [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->state = $state;
|
||||||
$this->data = $data;
|
$this->data = $data;
|
||||||
|
|
||||||
|
var_dump($state);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function createIntent($state, $data)
|
public static function createIntent($state, $data)
|
||||||
@ -21,10 +35,11 @@ class BaseIntent
|
|||||||
throw new Exception(trans('texts.intent_not_found'));
|
throw new Exception(trans('texts.intent_not_found'));
|
||||||
}
|
}
|
||||||
|
|
||||||
$intent = $data->intents[0];
|
$intent = $data->intents[0]->intent;
|
||||||
$intentType = $intent->intent;
|
$entityType = false;
|
||||||
|
|
||||||
$className = "App\\Ninja\\Intents\\{$intentType}Intent";
|
echo "Intent: $intent<p>";
|
||||||
|
$className = "App\\Ninja\\Intents\\{$intent}Intent";
|
||||||
|
|
||||||
if ( ! class_exists($className)) {
|
if ( ! class_exists($className)) {
|
||||||
throw new Exception(trans('texts.intent_not_supported'));
|
throw new Exception(trans('texts.intent_not_supported'));
|
||||||
@ -33,53 +48,64 @@ class BaseIntent
|
|||||||
return (new $className($state, $data));
|
return (new $className($state, $data));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function process()
|
public function process()
|
||||||
{
|
{
|
||||||
// do nothing by default
|
// do nothing by default
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setState($entityType, $entities)
|
public function setEntities($entityType, $entities)
|
||||||
|
{
|
||||||
|
if ( ! is_array($entities)) {
|
||||||
|
$entities = [$entities];
|
||||||
|
}
|
||||||
|
|
||||||
|
$state = $this->state;
|
||||||
|
|
||||||
|
$state->previous->$entityType = $state->current->$entityType;
|
||||||
|
$state->current->$entityType = $entities;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setEntityType($entityType)
|
||||||
{
|
{
|
||||||
$state = $this->state;
|
$state = $this->state;
|
||||||
|
|
||||||
if (isset($state->current->$entityType)) {
|
if ($state->current->entityType == $entityType) {
|
||||||
if ( ! isset($state->previous)) {
|
return;
|
||||||
$state->previous = new stdClass;
|
|
||||||
}
|
|
||||||
|
|
||||||
$state->previous->$entityType = $state->current->$entityType;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ! isset($state->current)) {
|
$state->previous->entityType = $state->current->entityType;
|
||||||
$state->current = new stdClass;
|
$state->current->entityType = $entityType;
|
||||||
}
|
|
||||||
|
|
||||||
if ($entities) {
|
|
||||||
$state->current->$entityType = $entities;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function entities($entityType)
|
||||||
|
{
|
||||||
|
return $this->state->current->$entityType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function entity($entityType)
|
||||||
|
{
|
||||||
|
$entities = $this->state->current->$entityType;
|
||||||
|
|
||||||
|
return count($entities) ? $entities[0] : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function previousEntities($entityType)
|
||||||
|
{
|
||||||
|
return $this->state->previous->$entityType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function entityType()
|
||||||
|
{
|
||||||
|
return $this->state->current->entityType;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public function getState()
|
public function getState()
|
||||||
{
|
{
|
||||||
return $this->state;
|
return $this->state;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getCurrentState($entityType = false, $first = false)
|
|
||||||
{
|
|
||||||
$current = $this->state->current;
|
|
||||||
$value = $entityType ? $current->$entityType : $current;
|
|
||||||
|
|
||||||
if ($value) {
|
|
||||||
if ($first && count($value)) {
|
|
||||||
return $value[0];
|
|
||||||
} else {
|
|
||||||
return $value;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function parseClient()
|
protected function parseClient()
|
||||||
{
|
{
|
||||||
$clientRepo = app('App\Ninja\Repositories\ClientRepository');
|
$clientRepo = app('App\Ninja\Repositories\ClientRepository');
|
||||||
@ -95,35 +121,61 @@ class BaseIntent
|
|||||||
return $client;
|
return $client;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function parseInvoiceItems()
|
protected function parseFields()
|
||||||
{
|
{
|
||||||
$productRepo = app('App\Ninja\Repositories\ProductRepository');
|
$data = [];
|
||||||
|
|
||||||
$invoiceItems = [];
|
foreach ($this->data->compositeEntities as $compositeEntity) {
|
||||||
|
if ($compositeEntity->parentType != 'FieldValuePair') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if ( ! isset($this->data->compositeEntities) || ! count($this->data->compositeEntities)) {
|
$field = false;
|
||||||
return [];
|
$value = false;
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($this->data->compositeEntities as $entity) {
|
foreach ($compositeEntity->children as $child) {
|
||||||
if ($entity->parentType == 'InvoiceItem') {
|
if ($child->type == 'Field') {
|
||||||
$product = false;
|
$field = $child->value;;
|
||||||
$qty = 1;
|
} elseif ($child->type == 'Value') {
|
||||||
foreach ($entity->children as $child) {
|
$value = $child->value;
|
||||||
if ($child->type == 'Product') {
|
|
||||||
$product = $productRepo->findPhonetically($child->value);
|
|
||||||
} else {
|
|
||||||
$qty = $child->value;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$item = $product->toArray();
|
if ($field && $value) {
|
||||||
$item['qty'] = $qty;
|
$field = $this->processField($field);
|
||||||
$invoiceItems[] = $item;
|
$value = $this->processValue($value);
|
||||||
|
|
||||||
|
$data[$field] = $value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $invoiceItems;
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function processField($field)
|
||||||
|
{
|
||||||
|
$field = str_replace(' ', '_', $field);
|
||||||
|
|
||||||
|
if (strpos($field, 'date') !== false) {
|
||||||
|
$field .= '_sql';
|
||||||
|
}
|
||||||
|
|
||||||
|
return $field;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function processValue($value)
|
||||||
|
{
|
||||||
|
// look for LUIS pre-built entity matches
|
||||||
|
foreach ($this->data->entities as $entity) {
|
||||||
|
if ($entity->entity === $value) {
|
||||||
|
if ($entity->type == 'builtin.datetime.date') {
|
||||||
|
$value = $entity->resolution->date;
|
||||||
|
$value = str_replace('XXXX', date('Y'), $value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,12 @@
|
|||||||
<?php namespace App\Ninja\Intents;
|
<?php namespace App\Ninja\Intents;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
use App\Models\EntityModel;
|
use App\Models\EntityModel;
|
||||||
|
|
||||||
class CreateInvoiceIntent extends BaseIntent
|
class CreateInvoiceIntent extends InvoiceIntent
|
||||||
{
|
{
|
||||||
public function process()
|
public function process()
|
||||||
{
|
{
|
||||||
$invoiceRepo = app('App\Ninja\Repositories\InvoiceRepository');
|
|
||||||
|
|
||||||
$client = $this->parseClient();
|
$client = $this->parseClient();
|
||||||
$invoiceItems = $this->parseInvoiceItems();
|
$invoiceItems = $this->parseInvoiceItems();
|
||||||
|
|
||||||
@ -22,22 +21,22 @@ class CreateInvoiceIntent extends BaseIntent
|
|||||||
'invoice_items' => $invoiceItems,
|
'invoice_items' => $invoiceItems,
|
||||||
];
|
];
|
||||||
|
|
||||||
|
var_dump($data);
|
||||||
$valid = EntityModel::validate($data, ENTITY_INVOICE);
|
$valid = EntityModel::validate($data, ENTITY_INVOICE);
|
||||||
|
|
||||||
if ($valid !== true) {
|
if ($valid !== true) {
|
||||||
return view('bots.skype.message', [
|
throw new Exception($valid);
|
||||||
'message' => $valid
|
|
||||||
])->render();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$invoice = $invoiceRepo->save($data);
|
$invoice = $this->invoiceRepo->save($data);
|
||||||
$invoiceItemIds = array_map(function($item) {
|
$invoiceItemIds = array_map(function($item) {
|
||||||
return $item['public_id'];
|
return $item['public_id'];
|
||||||
}, $invoice->invoice_items->toArray());
|
}, $invoice->invoice_items->toArray());
|
||||||
|
|
||||||
$this->setState(ENTITY_CLIENT, [$client->public_id]);
|
$this->setEntityType(ENTITY_INVOICE);
|
||||||
$this->setState(ENTITY_INVOICE, [$invoice->public_id]);
|
$this->setEntities(ENTITY_CLIENT, $client->public_id);
|
||||||
$this->setState(ENTITY_INVOICE_ITEM, $invoiceItemIds);
|
$this->setEntities(ENTITY_INVOICE, $invoice->public_id);
|
||||||
|
$this->setEntities(ENTITY_INVOICE_ITEM, $invoiceItemIds);
|
||||||
|
|
||||||
return view('bots.skype.invoice', [
|
return view('bots.skype.invoice', [
|
||||||
'invoice' => $invoice
|
'invoice' => $invoice
|
||||||
|
@ -1,41 +1,39 @@
|
|||||||
<?php namespace App\Ninja\Intents;
|
<?php namespace App\Ninja\Intents;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
use App\Models\EntityModel;
|
use App\Models\EntityModel;
|
||||||
use App\Models\Invoice;
|
use App\Models\Invoice;
|
||||||
|
|
||||||
class CreateInvoiceItemsIntent extends BaseIntent
|
class CreateInvoiceItemsIntent extends InvoiceIntent
|
||||||
{
|
{
|
||||||
public function process()
|
public function process()
|
||||||
{
|
{
|
||||||
$invoiceRepo = app('App\Ninja\Repositories\InvoiceRepository');
|
$invoice = $this->invoice();
|
||||||
|
|
||||||
$invoiceId = $this->getCurrentState(ENTITY_INVOICE, true);
|
|
||||||
$invoice = Invoice::scope($invoiceId)->first();
|
|
||||||
|
|
||||||
$invoiceItems = $this->parseInvoiceItems();
|
$invoiceItems = $this->parseInvoiceItems();
|
||||||
|
|
||||||
$data = [
|
$data = [
|
||||||
'invoice_items' => $invoiceItems
|
'public_id' => $invoice->public_id,
|
||||||
|
'invoice_items' => array_merge($invoice->invoice_items->toArray(), $invoiceItems),
|
||||||
];
|
];
|
||||||
|
|
||||||
|
var_dump($data);
|
||||||
$valid = EntityModel::validate($data, ENTITY_INVOICE, $invoice);
|
$valid = EntityModel::validate($data, ENTITY_INVOICE, $invoice);
|
||||||
|
|
||||||
if ($valid !== true) {
|
if ($valid !== true) {
|
||||||
return view('bots.skype.message', [
|
throw new Exception($valid);
|
||||||
'message' => $valid
|
|
||||||
])->render();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$invoice = $invoiceRepo->save($data, $invoice);
|
$invoice = $this->invoiceRepo->save($data, $invoice);
|
||||||
|
|
||||||
$invoiceItems = array_slice($invoice->invoice_items->toArray(), count($invoiceItems) * -1);
|
$invoiceItems = array_slice($invoice->invoice_items->toArray(), count($invoiceItems) * -1);
|
||||||
$invoiceItemIds = array_map(function($item) {
|
$invoiceItemIds = array_map(function($item) {
|
||||||
return $item['public_id'];
|
return $item['public_id'];
|
||||||
}, $invoiceItems);
|
}, $invoiceItems);
|
||||||
|
|
||||||
$this->setState(ENTITY_INVOICE_ITEM, $invoiceItemIds);
|
$this->setEntities(ENTITY_INVOICE_ITEM, $invoiceItemIds);
|
||||||
|
|
||||||
return view('bots.skype.invoice', [
|
return view('bots.skype.invoice', [
|
||||||
'invoice' => $invoice
|
'invoice' => $invoice->load('invoice_items')
|
||||||
])->render();
|
])->render();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
20
app/Ninja/Intents/DownloadInvoiceIntent.php
Normal file
20
app/Ninja/Intents/DownloadInvoiceIntent.php
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
<?php namespace App\Ninja\Intents;
|
||||||
|
|
||||||
|
use App\Models\EntityModel;
|
||||||
|
use App\Models\Invoice;
|
||||||
|
use Auth;
|
||||||
|
|
||||||
|
class DownloadInvoiceIntent extends InvoiceIntent
|
||||||
|
{
|
||||||
|
public function process()
|
||||||
|
{
|
||||||
|
$invoice = $this->invoice();
|
||||||
|
|
||||||
|
$message = trans('texts.' . $invoice->getEntityType()) . ' ' . $invoice->invoice_number;
|
||||||
|
$message = link_to('/download/' . $invoice->invitations[0]->invitation_key, $message);
|
||||||
|
|
||||||
|
return view('bots.skype.message', [
|
||||||
|
'message' => $message
|
||||||
|
])->render();
|
||||||
|
}
|
||||||
|
}
|
27
app/Ninja/Intents/EmailInvoiceIntent.php
Normal file
27
app/Ninja/Intents/EmailInvoiceIntent.php
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
<?php namespace App\Ninja\Intents;
|
||||||
|
|
||||||
|
use Auth;
|
||||||
|
use Exception;
|
||||||
|
use App\Models\EntityModel;
|
||||||
|
use App\Models\Invoice;
|
||||||
|
|
||||||
|
class EmailInvoiceIntent extends InvoiceIntent
|
||||||
|
{
|
||||||
|
public function process()
|
||||||
|
{
|
||||||
|
$invoice = $this->invoice();
|
||||||
|
|
||||||
|
if ( ! Auth::user()->can('edit', $invoice)) {
|
||||||
|
throw new Exception(trans('texts.not_allowed'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$contactMailer = app('App\Ninja\Mailers\ContactMailer');
|
||||||
|
$contactMailer->sendInvoice($invoice);
|
||||||
|
|
||||||
|
$message = trans('texts.bot_emailed_' . $invoice->getEntityType());
|
||||||
|
|
||||||
|
return view('bots.skype.message', [
|
||||||
|
'message' => $message
|
||||||
|
])->render();
|
||||||
|
}
|
||||||
|
}
|
68
app/Ninja/Intents/InvoiceIntent.php
Normal file
68
app/Ninja/Intents/InvoiceIntent.php
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
<?php namespace App\Ninja\Intents;
|
||||||
|
|
||||||
|
use Auth;
|
||||||
|
use Exception;
|
||||||
|
use App\Models\Invoice;
|
||||||
|
|
||||||
|
class InvoiceIntent extends BaseIntent
|
||||||
|
{
|
||||||
|
public function __construct($state, $data)
|
||||||
|
{
|
||||||
|
$this->invoiceRepo = app('App\Ninja\Repositories\InvoiceRepository');
|
||||||
|
|
||||||
|
parent::__construct($state, $data);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function invoice()
|
||||||
|
{
|
||||||
|
$invoiceId = $this->entity(ENTITY_INVOICE);
|
||||||
|
|
||||||
|
if ( ! $invoiceId) {
|
||||||
|
throw new Exception(trans('texts.intent_not_supported'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$invoice = Invoice::scope($invoiceId)->first();
|
||||||
|
|
||||||
|
if ( ! $invoice) {
|
||||||
|
throw new Exception(trans('texts.intent_not_supported'));
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( ! Auth::user()->can('view', $invoice)) {
|
||||||
|
throw new Exception(trans('texts.not_allowed'));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $invoice;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function parseInvoiceItems()
|
||||||
|
{
|
||||||
|
$productRepo = app('App\Ninja\Repositories\ProductRepository');
|
||||||
|
|
||||||
|
$invoiceItems = [];
|
||||||
|
|
||||||
|
if ( ! isset($this->data->compositeEntities) || ! count($this->data->compositeEntities)) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($this->data->compositeEntities as $entity) {
|
||||||
|
if ($entity->parentType == 'InvoiceItem') {
|
||||||
|
$product = false;
|
||||||
|
$qty = 1;
|
||||||
|
foreach ($entity->children as $child) {
|
||||||
|
if ($child->type == 'Product') {
|
||||||
|
$product = $productRepo->findPhonetically($child->value);
|
||||||
|
} else {
|
||||||
|
$qty = $child->value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$item = $product->toArray();
|
||||||
|
$item['qty'] = $qty;
|
||||||
|
$invoiceItems[] = $item;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $invoiceItems;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
21
app/Ninja/Intents/UpdateInvoiceIntent.php
Normal file
21
app/Ninja/Intents/UpdateInvoiceIntent.php
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
<?php namespace App\Ninja\Intents;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use App\Models\EntityModel;
|
||||||
|
use App\Models\Invoice;
|
||||||
|
|
||||||
|
class UpdateInvoiceIntent extends InvoiceIntent
|
||||||
|
{
|
||||||
|
public function process()
|
||||||
|
{
|
||||||
|
$invoice = $this->invoice();
|
||||||
|
|
||||||
|
$data = $this->parseFields();
|
||||||
|
|
||||||
|
dd($data);
|
||||||
|
|
||||||
|
return view('bots.skype.invoice', [
|
||||||
|
'invoice' => $invoice
|
||||||
|
])->render();
|
||||||
|
}
|
||||||
|
}
|
@ -2056,6 +2056,7 @@ $LANG = array(
|
|||||||
'intent_not_supported' => 'Sorry, I\'m not able to do that.',
|
'intent_not_supported' => 'Sorry, I\'m not able to do that.',
|
||||||
'client_not_found' => 'We weren\'t able to find the client',
|
'client_not_found' => 'We weren\'t able to find the client',
|
||||||
'not_allowed' => 'Sorry, you don\'t have the needed permissions',
|
'not_allowed' => 'Sorry, you don\'t have the needed permissions',
|
||||||
|
'bot_emailed_invoice' => 'Your invoice has been emailed',
|
||||||
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
{
|
{
|
||||||
"type": "message/text",
|
"type": "message/text",
|
||||||
"text": "{!! $message !!}"
|
"text": "{!! addslashes($message) !!}"
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user