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\Models\SecurityCode;
use App\Models\User;
use App\Models\Client;
use App\Ninja\Intents\BaseIntent;
use App\Ninja\Mailers\UserMailer;
use Auth;
@ -100,9 +101,28 @@ class BotController extends Controller
public function handleCommand()
{
$data = $this->parseMessage(request()->command);
//dd($data);
$intent = BaseIntent::createIntent(BOT_PLATFORM_WEB_APP, false, $data);
return $intent->process();
// 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();
} 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)

View File

@ -13,4 +13,26 @@ class InvoiceStatus extends Eloquent
* @var bool
*/
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,14 +16,7 @@ class BaseIntent
{
//if (true) {
if (! $state || is_string($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 = [];
}
}
$state = static::blankState();
}
$this->state = $state;
@ -32,6 +25,20 @@ class BaseIntent
//var_dump($state);
}
public static function blankState()
{
$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 = [];
}
}
return $state;
}
public static function createIntent($platform, $state, $data)
{
if (! count($data->intents)) {
@ -67,7 +74,7 @@ class BaseIntent
//echo "Intent: $intent<p>";
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);
@ -84,6 +91,30 @@ class BaseIntent
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)
{
$fieldValue = $this->getField($field);
@ -158,10 +189,15 @@ class BaseIntent
foreach ($this->data->entities as $param) {
if ($param->type == 'Name') {
$param->type = rtrim($param->type, ' \' s');
$client = $clientRepo->findPhonetically($param->entity);
}
}
if (! $client) {
$client = $this->state->current->client;
}
return $client;
}

View File

@ -3,6 +3,7 @@
namespace App\Ninja\Intents;
use App\Models\Invoice;
use App\Models\InvoiceStatus;
use Auth;
use Exception;
@ -104,4 +105,20 @@ class InvoiceIntent extends BaseIntent
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();
$url = '/credits/create/' . $clientPublicId . '?';
$url = '/credits/create/' . $clientPublicId;
//$url .= $this->requestFieldsAsString(Invoice::$requestFields);
return redirect($url);

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -2,12 +2,15 @@
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()
{
$this->loadStates(ENTITY_QUOTE);
$this->loadStatuses(ENTITY_QUOTE);
if ($client = $this->requestClient()) {
$url = $client->present()->url . '#quotes';
} else {

View File

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

View File

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

View File

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

View File

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