Skip to content
This repository has been archived by the owner on Jan 2, 2023. It is now read-only.

Commit

Permalink
Date and time helper
Browse files Browse the repository at this point in the history
  • Loading branch information
eristemena committed Aug 28, 2018
1 parent 0378d00 commit b848ff7
Show file tree
Hide file tree
Showing 8 changed files with 458 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .phpdoc-md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ return (object)[
'\Dialogflow\RichMessage\Payload',
'\Dialogflow\RichMessage\Suggestion',
'\Dialogflow\RichMessage\Text',
'\Dialogflow\Action\Arguments',
'\Dialogflow\Action\Conversation',
'\Dialogflow\Action\Surface',
'\Dialogflow\Action\AvailableSurfaces',
'\Dialogflow\Action\Device',
'\Dialogflow\Action\Device\Location',
'\Dialogflow\Action\Device\Location\LatLang',
'\Dialogflow\Action\Questions\Confirmation',
'\Dialogflow\Action\Questions\DateTime',
'\Dialogflow\Action\Questions\ListCard',
'\Dialogflow\Action\Questions\ListCard\Option',
'\Dialogflow\Action\Questions\Permission',
Expand Down
63 changes: 63 additions & 0 deletions docs/Action/Arguments.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Dialogflow\Action\Arguments







## Methods

| Name | Description |
|------|-------------|
|[__construct](#arguments__construct)||
|[get](#argumentsget)|Get argument by name.|




### Arguments::__construct

**Description**

```php
public __construct (array $data)
```





**Parameters**

* `(array) $data`
: request array

**Return Values**




### Arguments::get

**Description**

```php
public get (string $name)
```

Get argument by name.



**Parameters**

* `(string) $name`

**Return Values**

`null|array|bool`




96 changes: 96 additions & 0 deletions docs/Action/Questions/DateTime.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Dialogflow\Action\Questions\DateTime



## Implements:
Dialogflow\Action\Interfaces\QuestionInterface



## Methods

| Name | Description |
|------|-------------|
|[__construct](#datetime__construct)|Constructor for DateTime object.|
|[renderRichResponseItem](#datetimerenderrichresponseitem)|Render a single Rich Response item as array.|
|[renderSystemIntent](#datetimerendersystemintent)|Render System Intent as array.|




### DateTime::__construct

**Description**

```php
public __construct (string $requestDateTimeText, string $requestDateText, string $requestTimeText)
```

Constructor for DateTime object.



**Parameters**

* `(string) $requestDateTimeText`
: initial question
* `(string) $requestDateText`
: follow up question about the exact date
* `(string) $requestTimeText`
: follow up question about the exact time

**Return Values**

`\Dialogflow\Action\Questions\DateTime`





### DateTime::renderRichResponseItem

**Description**

```php
public renderRichResponseItem (void)
```

Render a single Rich Response item as array.



**Parameters**

`This function has no parameters.`

**Return Values**

`null|array`





### DateTime::renderSystemIntent

**Description**

```php
public renderSystemIntent (void)
```

Render System Intent as array.



**Parameters**

`This function has no parameters.`

**Return Values**

`null|array`




2 changes: 2 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
* [RichMessage\Payload](RichMessage/Payload.md)
* [RichMessage\Suggestion](RichMessage/Suggestion.md)
* [RichMessage\Text](RichMessage/Text.md)
* [Action\Arguments](Action/Arguments.md)
* [Action\Conversation](Action/Conversation.md)
* [Action\Surface](Action/Surface.md)
* [Action\AvailableSurfaces](Action/AvailableSurfaces.md)
* [Action\Device](Action/Device.md)
* [Action\Device\Location](Action/Device/Location.md)
* [Action\Device\Location\LatLang](Action/Device/Location/LatLang.md)
* [Action\Questions\Confirmation](Action/Questions/Confirmation.md)
* [Action\Questions\DateTime](Action/Questions/DateTime.md)
* [Action\Questions\ListCard](Action/Questions/ListCard.md)
* [Action\Questions\ListCard\Option](Action/Questions/ListCard/Option.md)
* [Action\Questions\Permission](Action/Questions/Permission.md)
Expand Down
24 changes: 24 additions & 0 deletions src/Action/Arguments.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Dialogflow\Action;

use Carbon\Carbon;

class Arguments
{
/** @var array */
Expand All @@ -11,6 +13,7 @@ class Arguments
'CONFIRMATION' => 'getConfirmation',
'PERMISSION' => 'getPermission',
'OPTION' => 'getOption',
'DATETIME' => 'getDateTime',
];

/**
Expand Down Expand Up @@ -87,4 +90,25 @@ private function getOption($argument)
{
return $argument['textValue'];
}

/**
* Get datetime argument.
*
* @param array $argument
*
* @return Carbon\Carbon
*/
private function getDateTime($argument)
{
$datetimeValue = $argument['datetimeValue'];

$year = $datetimeValue['date']['year'];
$month = $datetimeValue['date']['month'];
$day = $datetimeValue['date']['day'];

$hours = $datetimeValue['time']['hours'];
$minutes = isset($datetimeValue['time']['minutes']) ? $datetimeValue['time']['minutes'] : 0;

return Carbon::create($year, $month, $day, $hours, $minutes, 0);
}
}
69 changes: 69 additions & 0 deletions src/Action/Questions/DateTime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Dialogflow\Action\Questions;

use Dialogflow\Action\Interfaces\QuestionInterface;

class DateTime implements QuestionInterface
{
/** @var string */
protected $requestDateTimeText;

/** @var string */
protected $requestDateText;

/** @var string */
protected $requestTimeText;

/**
* Constructor for DateTime object.
*
* @param string $requestDateTimeText initial question
* @param string $requestDateText follow up question about the exact date
* @param string $requestTimeText follow up question about the exact time
*
* @return Dialogflow\Action\Questions\DateTime
*/
public function __construct($requestDateTimeText, $requestDateText, $requestTimeText)
{
$this->requestDateTimeText = $requestDateTimeText;
$this->requestDateText = $requestDateText;
$this->requestTimeText = $requestTimeText;
}

/**
* Render a single Rich Response item as array.
*
* @return null|array
*/
public function renderRichResponseItem()
{
$out = [];

$out['simpleResponse'] = ['textToSpeech' => 'PLACEHOLDER'];

return $out;
}

/**
* Render System Intent as array.
*
* @return null|array
*/
public function renderSystemIntent()
{
$out = [];

$out['intent'] = 'actions.intent.DATETIME';
$out['data'] = [
'@type' => 'type.googleapis.com/google.actions.v2.DateTimeValueSpec',
'dialogSpec' => [
'requestDatetimeText' => $this->requestDateTimeText,
'requestDateText' => $this->requestDateText,
'requestTimeText' => $this->requestTimeText,
],
];

return $out;
}
}
68 changes: 68 additions & 0 deletions tests/Action/Questions/DateTimeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Dialogflow\tests\Action;

use Dialogflow\Action\Questions\DateTime;
use Dialogflow\WebhookClient;
use PHPUnit\Framework\TestCase;

class DateTimeTest extends TestCase
{
private function getConversation()
{
$data = json_decode(file_get_contents(__DIR__.'/../../stubs/request-v2-google.json'), true);

$agent = new WebhookClient($data);

return $agent->getActionConversation();
}

private function getConversationResponse()
{
$data = json_decode(file_get_contents(__DIR__.'/../../stubs/request-v2-google-datetime.json'), true);

$agent = new WebhookClient($data);

return $agent->getActionConversation();
}

public function testCreate()
{
$conv = $this->getConversation();

$conv->ask(new DateTime('When do you want to come in?', 'What is the best date to schedule your appointment?', 'What time of day works best for you?'));

$this->assertEquals([
'expectUserResponse' => true,
'richResponse' => [
'items' => [
0 => [
'simpleResponse' => [
'textToSpeech' => 'PLACEHOLDER',
],
],
],
],
'systemIntent' => [
'intent' => 'actions.intent.DATETIME',
'data' => [
'@type' => 'type.googleapis.com/google.actions.v2.DateTimeValueSpec',
'dialogSpec' => [
'requestDatetimeText' => 'When do you want to come in?',
'requestDateText' => 'What is the best date to schedule your appointment?',
'requestTimeText' => 'What time of day works best for you?',
],
],
],
], $conv->render());
}

public function testResponse()
{
$conv = $this->getConversationResponse();

$date = $conv->getArguments()->get('DATETIME');

$this->assertInstanceOf('Carbon\Carbon', $date);
}
}
Loading

0 comments on commit b848ff7

Please sign in to comment.