Working on the bot

This commit is contained in:
Hillel Coren 2016-08-08 17:45:37 +03:00
parent f8fda5f241
commit 472ba842eb
9 changed files with 93 additions and 13 deletions

View File

@ -11,7 +11,7 @@ class BotController extends Controller
public function handleMessage($platform)
{
$to = '29:1C-OsU7OWBEDOYJhQUsDkYHmycOwOq9QOg5FVTwRX9ts';
$message = 'invoice acme client for 2 tickets';
$message = 'add 8 tickets';
//$message = view('bots.skype.message', ['message' => $message])->render();
//return $this->sendResponse($to, $message);
@ -93,6 +93,7 @@ class BotController extends Controller
];
$data = '{ eTag: "*", data: "' . addslashes(json_encode($data)) . '" }';
//$data = '{ eTag: "*", data: "" }';
var_dump($data);
$response = CurlUtils::post($url, $data, $headers);

View File

@ -44,6 +44,11 @@ class EntityRequest extends Request {
return $this->entity;
}
public function setEntity($entity)
{
$this->entity = $entity;
}
public function authorize()
{
if ($this->entity()) {

View File

@ -353,7 +353,7 @@ if (!defined('CONTACT_EMAIL')) {
define('ENTITY_CONTACT', 'contact');
define('ENTITY_INVOICE', 'invoice');
define('ENTITY_DOCUMENT', 'document');
define('ENTITY_INVOICE_ITEMS', 'invoice_items');
define('ENTITY_INVOICE_ITEM', 'invoice_item');
define('ENTITY_INVITATION', 'invitation');
define('ENTITY_RECURRING_INVOICE', 'recurring_invoice');
define('ENTITY_PAYMENT', 'payment');
@ -627,7 +627,7 @@ if (!defined('CONTACT_EMAIL')) {
define('EMAIL_MARKUP_URL', env('EMAIL_MARKUP_URL', 'https://developers.google.com/gmail/markup'));
define('OFX_HOME_URL', env('OFX_HOME_URL', 'http://www.ofxhome.com/index.php/home/directory/all'));
define('GOOGLE_ANALYITCS_URL', env('GOOGLE_ANALYITCS_URL', 'https://www.google-analytics.com/collect'));
define('MSBOT_LOGIN_URL', 'https://login.microsoftonline.com/common/oauth2/v2.0/token');
define('MSBOT_LUIS_URL', 'https://api.projectoxford.ai/luis/v1/application');
define('SKYPE_API_URL', 'https://apis.skype.com/v3');

View File

@ -202,9 +202,10 @@ class EntityModel extends Eloquent
* @param $entityType
* @return bool|string
*/
public static function validate($data, $entityType, $action = 'create')
public static function validate($data, $entityType, $entity = false)
{
// Use the API request if it exists
$action = $entity ? 'update' : 'create';
$requestClass = sprintf('App\\Http\\Requests\\%s%sAPIRequest', ucwords($action), ucwords($entityType));
if ( ! class_exists($requestClass)) {
$requestClass = sprintf('App\\Http\\Requests\\%s%sRequest', ucwords($action), ucwords($entityType));
@ -212,6 +213,7 @@ class EntityModel extends Eloquent
$request = new $requestClass();
$request->setUserResolver(function() { return Auth::user(); });
$request->setEntity($entity);
$request->replace($data);
if ( ! $request->authorize()) {

View File

@ -38,15 +38,25 @@ class BaseIntent
// do nothing by default
}
public function addState($entities)
public function setState($entityType, $entities)
{
var_dump($this->state);
if (isset($this->state->current)) {
$this->state->previous = $this->state->current;
$state = $this->state;
if (isset($state->current->$entityType)) {
if ( ! isset($state->previous)) {
$state->previous = new stdClass;
}
$state->previous->$entityType = $state->current->$entityType;
}
if ( ! isset($state->current)) {
$state->current = new stdClass;
}
$this->state->current = $entities;
if ($entities) {
$state->current->$entityType = $entities;
}
}
public function getState()
@ -54,6 +64,22 @@ class BaseIntent
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()
{
$clientRepo = app('App\Ninja\Repositories\ClientRepository');

View File

@ -31,8 +31,13 @@ class CreateInvoiceIntent extends BaseIntent
}
$invoice = $invoiceRepo->save($data);
$invoiceItemIds = array_map(function($item) {
return $item['public_id'];
}, $invoice->invoice_items->toArray());
$this->addState([$invoice->entityKey()]);
$this->setState(ENTITY_CLIENT, [$client->public_id]);
$this->setState(ENTITY_INVOICE, [$invoice->public_id]);
$this->setState(ENTITY_INVOICE_ITEM, $invoiceItemIds);
return view('bots.skype.invoice', [
'invoice' => $invoice

View File

@ -0,0 +1,41 @@
<?php namespace App\Ninja\Intents;
use App\Models\EntityModel;
use App\Models\Invoice;
class CreateInvoiceItemsIntent extends BaseIntent
{
public function process()
{
$invoiceRepo = app('App\Ninja\Repositories\InvoiceRepository');
$invoiceId = $this->getCurrentState(ENTITY_INVOICE, true);
$invoice = Invoice::scope($invoiceId)->first();
$invoiceItems = $this->parseInvoiceItems();
$data = [
'invoice_items' => $invoiceItems
];
$valid = EntityModel::validate($data, ENTITY_INVOICE, $invoice);
if ($valid !== true) {
return view('bots.skype.message', [
'message' => $valid
])->render();
}
$invoice = $invoiceRepo->save($data, $invoice);
$invoiceItems = array_slice($invoice->invoice_items->toArray(), count($invoiceItems) * -1);
$invoiceItemIds = array_map(function($item) {
return $item['public_id'];
}, $invoiceItems);
$this->setState(ENTITY_INVOICE_ITEM, [$invoiceItemId]);
return view('bots.skype.invoice', [
'invoice' => $invoice
])->render();
}
}

View File

@ -141,7 +141,7 @@ class ClientRepository extends BaseRepository
$max = 0;
$clientId = 0;
$clients = Client::scope()->get(['id', 'name']);
$clients = Client::scope()->get(['id', 'name', 'public_id']);
foreach ($clients as $client) {
if ( ! $client->name) {
@ -157,7 +157,7 @@ class ClientRepository extends BaseRepository
}
}
$contacts = Contact::scope()->get(['client_id', 'first_name', 'last_name']);
$contacts = Contact::scope()->get(['client_id', 'first_name', 'last_name', 'public_id']);
foreach ($contacts as $contact) {
if ( ! $contact->getFullName()) {

View File

@ -40,7 +40,7 @@ class InvoiceTransformer extends EntityTransformer
public function includeInvoiceItems(Invoice $invoice)
{
$transformer = new InvoiceItemTransformer($this->account, $this->serializer);
return $this->includeCollection($invoice->invoice_items, $transformer, ENTITY_INVOICE_ITEMS);
return $this->includeCollection($invoice->invoice_items, $transformer, ENTITY_INVOICE_ITEM);
}
public function includeInvitations(Invoice $invoice)