Skip to content

Commit 28d6059

Browse files
committed
Added missing Input changes
1 parent 63ef8f7 commit 28d6059

File tree

8 files changed

+161
-7
lines changed

8 files changed

+161
-7
lines changed

src/Client.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333
*
3434
* @method \Nexmo\Account\Client account()
3535
* @method \Nexmo\Message\Client message()
36-
* @method \Nexmo\SMS\Client sms()
37-
* @method \Nexmo\Verify\Client verify()
3836
* @method \Nexmo\Application\Client applications()
3937
* @method \Nexmo\Conversion\Client conversion()
4038
* @method \Nexmo\Insights\Client insights()

src/Voice/NCCO/Action/Input.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ public static function factory(array $data): Input
123123
}
124124

125125
if (array_key_exists('eventUrl', $data)) {
126+
if (is_array($data['eventUrl'])) {
127+
$data['eventUrl'] = $data['eventUrl'][0];
128+
}
129+
126130
if (array_key_exists('eventMethod', $data)) {
127131
$webhook = new Webhook($data['eventUrl'], $data['eventMethod']);
128132
} else {
@@ -208,7 +212,7 @@ public function toNCCOArray(): array
208212

209213
$eventWebhook = $this->getEventWebhook();
210214
if ($eventWebhook) {
211-
$data['eventUrl'] = $eventWebhook->getUrl();
215+
$data['eventUrl'] = [$eventWebhook->getUrl()];
212216
$data['eventMethod'] = $eventWebhook->getMethod();
213217
}
214218

src/Voice/Webhook/Factory.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ public static function createFromArray(array $data)
3737
return new Notification($data);
3838
}
3939

40+
if (array_key_exists('speech', $data) || array_key_exists('dtmf', $data)) {
41+
return new Input($data);
42+
}
43+
4044
throw new \InvalidArgumentException('Unable to detect incoming webhook type');
4145
}
4246
}

src/Voice/Webhook/Input.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Nexmo\Voice\Webhook;
5+
6+
class Input
7+
{
8+
/**
9+
* @var array
10+
*/
11+
protected $speech;
12+
13+
/**
14+
* @var array
15+
*/
16+
protected $dtmf;
17+
18+
/**
19+
* @var string
20+
*/
21+
protected $from;
22+
23+
/**
24+
* @var string
25+
*/
26+
protected $to;
27+
28+
/**
29+
* @var string
30+
*/
31+
protected $uuid;
32+
33+
/**
34+
* @var string
35+
*/
36+
protected $conversationUuid;
37+
38+
/**
39+
* @var \DateTimeImmutable
40+
*/
41+
protected $timestamp;
42+
43+
public function __construct(array $data)
44+
{
45+
// GET requests push this in as a string
46+
if (is_string($data['speech'])) {
47+
$data['speech'] = json_decode($data['speech'], true);
48+
}
49+
$this->speech = $data['speech'];
50+
51+
// GET requests push this in as a string
52+
if (is_string($data['dtmf'])) {
53+
$data['dtmf'] = json_decode($data['dtmf'], true);
54+
}
55+
$this->dtmf = $data['dtmf'];
56+
$this->to = $data['to'];
57+
$this->from = $data['from'];
58+
$this->uuid = $data['uuid'];
59+
$this->conversationUuid = $data['conversation_uuid'];
60+
$this->timestamp = new \DateTimeImmutable($data['timestamp']);
61+
}
62+
63+
public function getSpeech() : array
64+
{
65+
return $this->speech;
66+
}
67+
68+
public function getDtmf() : array
69+
{
70+
return $this->dtmf;
71+
}
72+
73+
public function getFrom() : string
74+
{
75+
return $this->from;
76+
}
77+
78+
public function getTo() : string
79+
{
80+
return $this->to;
81+
}
82+
83+
public function getUuid() : string
84+
{
85+
return $this->uuid;
86+
}
87+
88+
public function getConversationUuid() : string
89+
{
90+
return $this->conversationUuid;
91+
}
92+
93+
public function getTimestamp() : \DateTimeImmutable
94+
{
95+
return $this->timestamp;
96+
}
97+
}

test/Voice/NCCO/Action/InputTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function testEventURLCanBeSetInFactory()
9292
{
9393
$data = [
9494
'action' => 'input',
95-
'eventUrl' => 'https://test.domain/events',
95+
'eventUrl' => ['https://test.domain/events'],
9696
'eventMethod' => 'POST',
9797
'speech' => [],
9898
];
@@ -103,15 +103,15 @@ public function testEventURLCanBeSetInFactory()
103103
$this->assertSame($data['eventUrl'], $ncco['eventUrl']);
104104
$this->assertSame($data['eventMethod'], $ncco['eventMethod']);
105105

106-
$this->assertSame($data['eventUrl'], $action->getEventWebhook()->getUrl());
106+
$this->assertSame($data['eventUrl'][0], $action->getEventWebhook()->getUrl());
107107
$this->assertSame($data['eventMethod'], $action->getEventWebhook()->getMethod());
108108
}
109109

110110
public function testEventMethodDefaultsToPostWhenNotSupplied()
111111
{
112112
$data = [
113113
'action' => 'input',
114-
'eventUrl' => 'https://test.domain/events',
114+
'eventUrl' => ['https://test.domain/events'],
115115
'dtmf' => []
116116
];
117117

@@ -121,7 +121,7 @@ public function testEventMethodDefaultsToPostWhenNotSupplied()
121121
$this->assertSame($data['eventUrl'], $ncco['eventUrl']);
122122
$this->assertSame('POST', $ncco['eventMethod']);
123123

124-
$this->assertSame($data['eventUrl'], $action->getEventWebhook()->getUrl());
124+
$this->assertSame($data['eventUrl'][0], $action->getEventWebhook()->getUrl());
125125
$this->assertSame('POST', $action->getEventWebhook()->getMethod());
126126
}
127127

test/Voice/Webhook/FactoryTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use InvalidArgumentException;
77
use Nexmo\Voice\Webhook\Error;
88
use Nexmo\Voice\Webhook\Event;
9+
use Nexmo\Voice\Webhook\Input;
910
use Nexmo\Voice\Webhook\Answer;
1011
use Nexmo\Voice\Webhook\Record;
1112
use PHPUnit\Framework\TestCase;
@@ -184,6 +185,42 @@ public function testCanGenerateANotificationPostWebhook()
184185
$this->assertEquals(new \DateTimeImmutable($expected['timestamp']), $notification->getTimestamp());
185186
}
186187

188+
public function testCanGenerateDtmfInputFromGetWebhook()
189+
{
190+
$request = $this->getRequest('dtmf-get');
191+
$expected = $this->getRequest('dtmf-get')->getQueryParams();
192+
193+
/** @var Input $input */
194+
$input = Factory::createFromRequest($request);
195+
196+
$this->assertTrue($input instanceof Input);
197+
$this->assertSame(json_decode($expected['speech'], true), $input->getSpeech());
198+
$this->assertSame(json_decode($expected['dtmf'], true), $input->getDtmf());
199+
$this->assertSame($expected['from'], $input->getFrom());
200+
$this->assertSame($expected['to'], $input->getTo());
201+
$this->assertSame($expected['uuid'], $input->getUuid());
202+
$this->assertSame($expected['conversation_uuid'], $input->getConversationUuid());
203+
$this->assertEquals(new \DateTimeImmutable($expected['timestamp']), $input->getTimestamp());
204+
}
205+
206+
public function testCanGenerateDtmfInputFromPostWebhook()
207+
{
208+
$request = $this->getRequest('dtmf-post');
209+
$expected = json_decode($this->getRequest('dtmf-post')->getBody()->getContents(), true);
210+
211+
/** @var Input $input */
212+
$input = Factory::createFromRequest($request);
213+
214+
$this->assertTrue($input instanceof Input);
215+
$this->assertSame($expected['speech'], $input->getSpeech());
216+
$this->assertSame($expected['dtmf'], $input->getDtmf());
217+
$this->assertSame($expected['from'], $input->getFrom());
218+
$this->assertSame($expected['to'], $input->getTo());
219+
$this->assertSame($expected['uuid'], $input->getUuid());
220+
$this->assertSame($expected['conversation_uuid'], $input->getConversationUuid());
221+
$this->assertEquals(new \DateTimeImmutable($expected['timestamp']), $input->getTimestamp());
222+
}
223+
187224
public function testThrowsExceptionOnUnknownWebhookData()
188225
{
189226
$this->expectException(\InvalidArgumentException::class);

test/Voice/requests/dtmf-get.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
GET /webhooks/dtmf?speech=%7B%22error%22:%22Speech%20was%20not%20enabled%22%7D&dtmf=%7B%22digits%22:%223%22,%22timed_out%22:true%7D&from=16105553939&to=14845552121&uuid=0A0000000123ABCD1&conversation_uuid=CON-2541d01c-253e-48be-a8e0-da4bbe4c3722&timestamp=2020-07-30T03:58:48.984Z HTTP/1.1
2+
User-Agent: Apache-HttpAsyncClient/4.1 (Java/1.8.0_66)
3+
Host: dragonmantank.ngrok.io
4+
Cache-Control: max-age=259200
5+
X-Forwarded-For: 169.63.86.173

test/Voice/requests/dtmf-post.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
POST /webhooks/dtmf HTTP/1.1
2+
Content-Type: application/json
3+
Content-Length: 268
4+
User-Agent: Apache-HttpAsyncClient/4.1 (Java/1.8.0_66)
5+
Host: dragonmantank.ngrok.io
6+
Cache-Control: max-age=259200
7+
X-Forwarded-For: 169.63.86.173
8+
9+
{"speech":{"error":"Speech was not enabled"},"dtmf":{"digits":"2","timed_out":true},"from":"16105553939","to":"14845552121","uuid":"0A0000000123ABCD1","conversation_uuid":"CON-2541d01c-253e-48be-a8e0-da4bbe4c3722","timestamp":"2020-07-29T19:20:33.015Z"}

0 commit comments

Comments
 (0)