Working on speech rec

This commit is contained in:
Hillel Coren 2017-04-05 22:20:11 +03:00
parent f8b5f179a8
commit 2581e529e4
19 changed files with 145 additions and 18 deletions

View File

@ -6,6 +6,7 @@ use App\Libraries\CurlUtils;
use App\Libraries\Skype\SkypeResponse; use App\Libraries\Skype\SkypeResponse;
use App\Models\SecurityCode; use App\Models\SecurityCode;
use App\Models\User; use App\Models\User;
use App\Models\Client;
use App\Ninja\Intents\BaseIntent; use App\Ninja\Intents\BaseIntent;
use App\Ninja\Mailers\UserMailer; use App\Ninja\Mailers\UserMailer;
use Auth; use Auth;
@ -100,9 +101,28 @@ class BotController extends Controller
public function handleCommand() public function handleCommand()
{ {
$data = $this->parseMessage(request()->command); $data = $this->parseMessage(request()->command);
//dd($data);
$intent = BaseIntent::createIntent(BOT_PLATFORM_WEB_APP, false, $data); // If they're viewing a client set it as the current state
$state = false;
$url = url()->previous();
preg_match('/clients\/(\d*)/', $url, $matches);
if (count($matches) >= 2) {
if ($client = Client::scope($matches[1])->first()) {
$state = BaseIntent::blankState();
$state->current->client = $client;
}
}
try {
$intent = BaseIntent::createIntent(BOT_PLATFORM_WEB_APP, $state, $data);
return $intent->process(); return $intent->process();
} catch (Exception $exception) {
$message = $exception->getMessage();
if (env('APP_DEBUG')) {
$message .= '<br/>' . request()->command . ' => ' . json_encode($data);
}
return redirect()->back()->withWarning($message);
}
} }
private function authenticate($input) private function authenticate($input)

View File

@ -13,4 +13,26 @@ class InvoiceStatus extends Eloquent
* @var bool * @var bool
*/ */
public $timestamps = false; public $timestamps = false;
public static function getIdFromAlias($status)
{
switch ($status) {
case 'draft':
return INVOICE_STATUS_DRAFT;
case 'sent':
return INVOICE_STATUS_SENT;
case 'viewed':
return INVOICE_STATUS_VIEWED;
case 'approved':
return INVOICE_STATUS_APPROVED;
case 'partial':
return INVOICE_STATUS_PARTIAL;
case 'overdue':
return INVOICE_STATUS_OVERDUE;
case 'unpaid':
return INVOICE_STATUS_UNPAID;
default:
return false;
}
}
} }

View File

@ -16,6 +16,17 @@ class BaseIntent
{ {
//if (true) { //if (true) {
if (! $state || is_string($state)) { if (! $state || is_string($state)) {
$state = static::blankState();
}
$this->state = $state;
$this->data = $data;
//var_dump($state);
}
public static function blankState()
{
$state = new stdClass(); $state = new stdClass();
foreach (['current', 'previous'] as $reference) { foreach (['current', 'previous'] as $reference) {
$state->$reference = new stdClass(); $state->$reference = new stdClass();
@ -24,12 +35,8 @@ class BaseIntent
$state->$reference->$entityType = []; $state->$reference->$entityType = [];
} }
} }
}
$this->state = $state; return $state;
$this->data = $data;
//var_dump($state);
} }
public static function createIntent($platform, $state, $data) public static function createIntent($platform, $state, $data)
@ -67,7 +74,7 @@ class BaseIntent
//echo "Intent: $intent<p>"; //echo "Intent: $intent<p>";
if (! class_exists($className)) { if (! class_exists($className)) {
throw new Exception($intent . ': ' . trans('texts.intent_not_supported')); throw new Exception($intent . '... ' . trans('texts.intent_not_supported'));
} }
return new $className($state, $data); return new $className($state, $data);
@ -84,6 +91,30 @@ class BaseIntent
return false; return false;
} }
protected function getFields($field)
{
$data = [];
foreach ($this->data->entities as $entity) {
if ($entity->type === $field) {
$data[] = $entity->entity;
}
}
return $data;
}
protected function loadStates($entityType)
{
$states = array_filter($this->getFields('State'), function($state) {
return in_array($state, [STATUS_ACTIVE, STATUS_ARCHIVED, STATUS_DELETED]);
});
if (count($states)) {
session(['entity_state_filter:' . $entityType => join(',', $states)]);
}
}
protected function hasField($field, $value = false) protected function hasField($field, $value = false)
{ {
$fieldValue = $this->getField($field); $fieldValue = $this->getField($field);
@ -158,10 +189,15 @@ class BaseIntent
foreach ($this->data->entities as $param) { foreach ($this->data->entities as $param) {
if ($param->type == 'Name') { if ($param->type == 'Name') {
$param->type = rtrim($param->type, ' \' s');
$client = $clientRepo->findPhonetically($param->entity); $client = $clientRepo->findPhonetically($param->entity);
} }
} }
if (! $client) {
$client = $this->state->current->client;
}
return $client; return $client;
} }

View File

@ -3,6 +3,7 @@
namespace App\Ninja\Intents; namespace App\Ninja\Intents;
use App\Models\Invoice; use App\Models\Invoice;
use App\Models\InvoiceStatus;
use Auth; use Auth;
use Exception; use Exception;
@ -104,4 +105,20 @@ class InvoiceIntent extends BaseIntent
return $invoiceItems; return $invoiceItems;
} }
protected function loadStatuses($entityType)
{
$statusIds = [];
$statuses = $this->getFields('State');
foreach ($statuses as $status) {
if ($statusId = InvoiceStatus::getIdFromAlias($status)) {
$statusIds[] = $statusId;
}
}
if (count($statusIds)) {
session(['entity_status_filter:' . $entityType => join(',', $statusIds)]);
}
}
} }

View File

@ -13,7 +13,7 @@ class CreateCreditIntent extends BaseIntent
//$invoiceItems = $this->requestInvoiceItems(); //$invoiceItems = $this->requestInvoiceItems();
$url = '/credits/create/' . $clientPublicId . '?'; $url = '/credits/create/' . $clientPublicId;
//$url .= $this->requestFieldsAsString(Invoice::$requestFields); //$url .= $this->requestFieldsAsString(Invoice::$requestFields);
return redirect($url); return redirect($url);

View File

@ -19,6 +19,9 @@ class CreateInvoiceIntent extends InvoiceIntent
$url = '/invoices/create/' . $clientPublicId . '?'; $url = '/invoices/create/' . $clientPublicId . '?';
$url .= $this->requestFieldsAsString(Invoice::$requestFields); $url .= $this->requestFieldsAsString(Invoice::$requestFields);
$url = rtrim($url, '?');
$url = rtrim($url, '&');
return redirect($url); return redirect($url);
} }
} }

View File

@ -17,6 +17,9 @@ class CreateQuoteIntent extends BaseIntent
$url = '/quotes/create/' . $clientPublicId . '?'; $url = '/quotes/create/' . $clientPublicId . '?';
$url .= $this->requestFieldsAsString(Invoice::$requestFields); $url .= $this->requestFieldsAsString(Invoice::$requestFields);
$url = rtrim($url, '?');
$url = rtrim($url, '&');
return redirect($url); return redirect($url);
} }
} }

View File

@ -17,6 +17,9 @@ class CreateRecurringInvoiceIntent extends BaseIntent
$url = '/recurring_invoices/create/' . $clientPublicId . '?'; $url = '/recurring_invoices/create/' . $clientPublicId . '?';
$url .= $this->requestFieldsAsString(Invoice::$requestFields); $url .= $this->requestFieldsAsString(Invoice::$requestFields);
$url = rtrim($url, '?');
$url = rtrim($url, '&');
return redirect($url); return redirect($url);
} }
} }

View File

@ -8,6 +8,8 @@ class ListClientIntent extends BaseIntent
{ {
public function process() public function process()
{ {
$this->loadStates(ENTITY_CLIENT);
return redirect('/clients'); return redirect('/clients');
} }
} }

View File

@ -8,6 +8,8 @@ class ListCreditIntent extends BaseIntent
{ {
public function process() public function process()
{ {
$this->loadStates(ENTITY_CREDIT);
if ($client = $this->requestClient()) { if ($client = $this->requestClient()) {
$url = $client->present()->url . '#credits'; $url = $client->present()->url . '#credits';
} else { } else {

View File

@ -8,6 +8,8 @@ class ListExpenseIntent extends BaseIntent
{ {
public function process() public function process()
{ {
$this->loadStates(ENTITY_EXPENSE);
return redirect('/expenses'); return redirect('/expenses');
} }
} }

View File

@ -8,6 +8,9 @@ class ListInvoiceIntent extends InvoiceIntent
{ {
public function process() public function process()
{ {
$this->loadStates(ENTITY_INVOICE);
$this->loadStatuses(ENTITY_INVOICE);
if ($client = $this->requestClient()) { if ($client = $this->requestClient()) {
$url = $client->present()->url . '#invoices'; $url = $client->present()->url . '#invoices';
} else { } else {

View File

@ -8,6 +8,8 @@ class ListPaymentIntent extends BaseIntent
{ {
public function process() public function process()
{ {
$this->loadStates(ENTITY_PAYMENT);
if ($client = $this->requestClient()) { if ($client = $this->requestClient()) {
$url = $client->present()->url . '#payments'; $url = $client->present()->url . '#payments';
} else { } else {

View File

@ -8,6 +8,8 @@ class ListProductIntent extends BaseIntent
{ {
public function process() public function process()
{ {
$this->loadStates(ENTITY_PRODUCT);
return redirect('/products'); return redirect('/products');
} }
} }

View File

@ -2,12 +2,15 @@
namespace App\Ninja\Intents\WebApp; namespace App\Ninja\Intents\WebApp;
use App\Ninja\Intents\BaseIntent; use App\Ninja\Intents\InvoiceIntent;
class ListQuotesIntent extends BaseIntent class ListQuoteIntent extends InvoiceIntent
{ {
public function process() public function process()
{ {
$this->loadStates(ENTITY_QUOTE);
$this->loadStatuses(ENTITY_QUOTE);
if ($client = $this->requestClient()) { if ($client = $this->requestClient()) {
$url = $client->present()->url . '#quotes'; $url = $client->present()->url . '#quotes';
} else { } else {

View File

@ -8,6 +8,8 @@ class ListRecurringInvoiceIntent extends BaseIntent
{ {
public function process() public function process()
{ {
$this->loadStates(ENTITY_RECURRING_INVOICE);
if ($client = $this->requestClient()) { if ($client = $this->requestClient()) {
$url = $client->present()->url . '#recurring_invoices'; $url = $client->present()->url . '#recurring_invoices';
} else { } else {

View File

@ -8,6 +8,8 @@ class ListTaskIntent extends BaseIntent
{ {
public function process() public function process()
{ {
$this->loadStates(ENTITY_TASK);
if ($client = $this->requestClient()) { if ($client = $this->requestClient()) {
$url = $client->present()->url . '#tasks'; $url = $client->present()->url . '#tasks';
} else { } else {

View File

@ -8,6 +8,8 @@ class ListVendorIntent extends BaseIntent
{ {
public function process() public function process()
{ {
$this->loadStates(ENTITY_VENDOR);
return redirect('/vendors'); return redirect('/vendors');
} }
} }

View File

@ -173,9 +173,10 @@
function onMicrophoneClick() { function onMicrophoneClick() {
//$('#search').val("show me all of edgar's credits"); //$('#search').val("show me all of edgar's credits");
$('#search').val("show me edgar's credits"); //$('#search').val("new invoice for joe, set the due date to today and the discount to ten percent");
$('#search-form').submit(); //$('#search').val("list joe's active and approved quotes");
return; //$('#search-form').submit();
//return;
$('.fa-microphone').hide(); $('.fa-microphone').hide();
$('#search').val("{{ trans('texts.listening') }}"); $('#search').val("{{ trans('texts.listening') }}");