Working on our bot

This commit is contained in:
Hillel Coren 2016-08-06 20:54:56 +03:00
parent 837fbe3763
commit f1a278e4ff
5 changed files with 132 additions and 34 deletions

File diff suppressed because one or more lines are too long

View File

@ -5,15 +5,22 @@ class BaseIntent
{
protected $parameters;
public function __construct($parameters)
public function __construct($data)
{
$this->parameters = $parameters;
$this->data = $data;
}
public static function createIntent($intentType, $parameters)
public static function createIntent($data)
{
if ( ! count($data->intents)) {
return false;
}
$intent = $data->intents[0];
$intentType = $intent->intent;
$className = "App\\Ninja\\Intents\\{$intentType}Intent";
return new $className($parameters);
return new $className($data);
}
public function process()

View File

@ -4,20 +4,15 @@ class CreateInvoiceIntent extends BaseIntent
{
public function process()
{
$clientRepo = app('App\Ninja\Repositories\ClientRepository');
$invoiceRepo = app('App\Ninja\Repositories\InvoiceRepository');
$client = false;
foreach ($this->parameters as $param) {
if ($param->type == 'client') {
$client = $clientRepo->findPhonetically($param->entity);
}
}
$client = $this->parseClient();
$invoiceItems = $this->parseInvoiceItems();
if ($client) {
$data = [
'client_id' => $client->id,
'invoice_items' => [],
'invoice_items' => $invoiceItems,
];
$invoice = $invoiceRepo->save($data);
@ -25,19 +20,53 @@ class CreateInvoiceIntent extends BaseIntent
return view('bots.skype.invoice', [
'invoice' => $invoice
])->render();
/*
if ($invoice->amount > 0) {
} else {
return view('bots.skype.card', [
'title' => 'Testing',
'subtitle' => $invoice->invoice_number,
return view('bots.skype.message', [
'message' => trans('texts.client_not_found')
])->render();
}
*/
}
private function parseClient()
{
$clientRepo = app('App\Ninja\Repositories\ClientRepository');
$client = false;
foreach ($this->data->entities as $param) {
if ($param->type == 'Client') {
$client = $clientRepo->findPhonetically($param->entity);
}
}
return $client;
}
private function parseInvoiceItems()
{
$productRepo = app('App\Ninja\Repositories\ProductRepository');
$invoiceItems = [];
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;
}
}

View File

@ -56,4 +56,32 @@ class ProductRepository extends BaseRepository
return $product;
}
public function findPhonetically($productName)
{
$productNameMeta = metaphone($productName);
$map = [];
$max = 0;
$productId = 0;
$products = Product::scope()->get(['product_key', 'notes', 'cost']);
foreach ($products as $product) {
if ( ! $product->product_key) {
continue;
}
$map[$product->id] = $product;
$similar = similar_text($productNameMeta, metaphone($product->product_key), $percent);
if ($percent > $max) {
$productId = $product->id;
$max = $percent;
}
}
return $map[$productId];
}
}

View File

@ -2052,6 +2052,8 @@ $LANG = array(
'gateway_config_error' => 'It may help to set new passwords or generate new API keys.',
'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.',
'client_not_found' => 'We weren\'t able to find the client',
);