Skip to content

Commit 240efc2

Browse files
committed
Backed off a bunch of voice options, and allow Conversations to have an eventUrl
1 parent 31ec2b8 commit 240efc2

File tree

15 files changed

+395
-115
lines changed

15 files changed

+395
-115
lines changed

src/Voice/Client.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,13 @@ public function createOutboundCall(OutboundCall $call) : Event
5858
$json['machine_detection'] = $call->getMachineDetection();
5959
}
6060

61-
$json['length_timer'] = (string) $call->getLengthTimer();
62-
$json['ringing_timer'] = (string) $call->getRingingTimer();
61+
if (!is_null($call->getLengthTimer())) {
62+
$json['length_timer'] = (string) $call->getLengthTimer();
63+
}
64+
65+
if (!is_null($call->getRingingTimer())) {
66+
$json['ringing_timer'] = (string) $call->getRingingTimer();
67+
}
6368

6469
$event = $this->api->create($json);
6570
$event['to'] = $call->getTo()->getId();
@@ -117,12 +122,10 @@ public function playDTMF(string $callId, string $digits) : array
117122
*/
118123
public function playTTS(string $callId, Talk $action) : array
119124
{
120-
$response = $this->api->update($callId . '/talk', [
121-
'text' => $action->getText(),
122-
'voice_name' => $action->getVoiceName(),
123-
'loop' => (string) $action->getLoop(),
124-
'level' => (string) $action->getLevel(),
125-
]);
125+
$payload = $action->toNCCOArray();
126+
unset($payload['action']);
127+
128+
$response = $this->api->update($callId . '/talk', $payload);
126129

127130
return $response;
128131
}

src/Voice/NCCO/Action/Connect.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ class Connect implements ActionInterface
3131
/**
3232
* @var int
3333
*/
34-
protected $timeout = 60;
34+
protected $timeout;
3535

3636
/**
3737
* @var int
3838
*/
39-
protected $limit = 7200;
39+
protected $limit;
4040

4141
/**
4242
* @var string
4343
*/
44-
protected $machineDetection = 'continue';
44+
protected $machineDetection;
4545

4646
/**
4747
* @var ?Webhook
@@ -75,11 +75,20 @@ public function toNCCOArray(): array
7575
$data = [
7676
'action' => 'connect',
7777
'endpoint' => [$this->endpoint->toArray()],
78-
'timeout' => $this->getTimeout(),
79-
'limit' => $this->getLimit(),
80-
'machineDetection' => $this->getMachineDetection(),
8178
];
8279

80+
if ($this->getTimeout()) {
81+
$data['timeout'] = $this->getTimeout();
82+
}
83+
84+
if ($this->getLimit()) {
85+
$data['limit'] = $this->getLimit();
86+
}
87+
88+
if ($this->getMachineDetection()) {
89+
$data['machineDetection'] = $this->getMachineDetection();
90+
}
91+
8392
$from = $this->getFrom();
8493
if ($from) {
8594
$data['from'] = $from;
@@ -130,7 +139,7 @@ public function setEventType(string $eventType) : self
130139
return $this;
131140
}
132141

133-
public function getTimeout() : int
142+
public function getTimeout() : ?int
134143
{
135144
return $this->timeout;
136145
}
@@ -141,7 +150,7 @@ public function setTimeout(int $timeout) : self
141150
return $this;
142151
}
143152

144-
public function getLimit() : int
153+
public function getLimit() : ?int
145154
{
146155
return $this->limit;
147156
}
@@ -152,7 +161,7 @@ public function setLimit(int $limit) : self
152161
return $this;
153162
}
154163

155-
public function getMachineDetection() : string
164+
public function getMachineDetection() : ?string
156165
{
157166
return $this->machineDetection;
158167
}

src/Voice/NCCO/Action/Conversation.php

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
namespace Nexmo\Voice\NCCO\Action;
55

6+
use Nexmo\Voice\Webhook;
7+
68
class Conversation implements ActionInterface
79
{
810
/**
@@ -18,17 +20,17 @@ class Conversation implements ActionInterface
1820
/**
1921
* @var bool
2022
*/
21-
protected $startOnEnter = true;
23+
protected $startOnEnter;
2224

2325
/**
2426
* @var bool
2527
*/
26-
protected $endOnExit = false;
28+
protected $endOnExit;
2729

2830
/**
2931
* @var bool
3032
*/
31-
protected $record = false;
33+
protected $record;
3234

3335
/**
3436
* @var ?array<string>
@@ -40,6 +42,11 @@ class Conversation implements ActionInterface
4042
*/
4143
protected $canHear;
4244

45+
/**
46+
* @var Webhook
47+
*/
48+
protected $eventWebhook;
49+
4350
public function __construct(string $name)
4451
{
4552
$this->name = $name;
@@ -61,7 +68,7 @@ public function setMusicOnHoldUrl(string $musicOnHoldUrl) : self
6168
return $this;
6269
}
6370

64-
public function getStartOnEnter() : bool
71+
public function getStartOnEnter() : ?bool
6572
{
6673
return $this->startOnEnter;
6774
}
@@ -72,7 +79,7 @@ public function setStartOnEnter(bool $startOnEnter) : self
7279
return $this;
7380
}
7481

75-
public function getEndOnExit() : bool
82+
public function getEndOnExit() : ?bool
7683
{
7784
return $this->endOnExit;
7885
}
@@ -83,7 +90,7 @@ public function setEndOnExit(bool $endOnExit) : self
8390
return $this;
8491
}
8592

86-
public function getRecord() : bool
93+
public function getRecord() : ?bool
8794
{
8895
return $this->record;
8996
}
@@ -183,6 +190,20 @@ public static function factory(string $name, array $data): Conversation
183190
if (array_key_exists('canHear', $data)) {
184191
$talk->setCanHear($data['canHear']);
185192
}
193+
194+
if (array_key_exists('eventUrl', $data)) {
195+
if (is_array($data['eventUrl'])) {
196+
$data['eventUrl'] = $data['eventUrl'][0];
197+
}
198+
199+
if (array_key_exists('eventMethod', $data)) {
200+
$webhook = new Webhook($data['eventUrl'], $data['eventMethod']);
201+
} else {
202+
$webhook = new Webhook($data['eventUrl']);
203+
}
204+
205+
$talk->setEventWebhook($webhook);
206+
}
186207

187208
return $talk;
188209
}
@@ -203,11 +224,20 @@ public function toNCCOArray() : array
203224
$data = [
204225
'action' => 'conversation',
205226
'name' => $this->getName(),
206-
'startOnEnter' => $this->getStartOnEnter() ? 'true' : 'false',
207-
'endOnExit' => $this->getEndOnExit() ? 'true' : 'false',
208-
'record' => $this->getRecord() ? 'true' : 'false',
209227
];
210228

229+
if (!is_null($this->getStartOnEnter())) {
230+
$data['startOnEnter'] = $this->getStartOnEnter() ? 'true' : 'false';
231+
}
232+
233+
if (!is_null($this->getEndOnExit())) {
234+
$data['endOnExit'] = $this->getEndOnExit() ? 'true' : 'false';
235+
}
236+
237+
if (!is_null($this->getRecord())) {
238+
$data['record'] = $this->getRecord() ? 'true' : 'false';
239+
}
240+
211241
$music = $this->getMusicOnHoldUrl();
212242
if ($music) {
213243
$data['musicOnHoldUrl'] = [$music];
@@ -223,6 +253,22 @@ public function toNCCOArray() : array
223253
$data['canHear'] = $canHear;
224254
}
225255

256+
if ($this->getEventWebhook()) {
257+
$data['eventUrl'] = [$this->getEventWebhook()->getUrl()];
258+
$data['eventMethod'] = $this->getEventWebhook()->getMethod();
259+
}
260+
226261
return $data;
227262
}
263+
264+
public function getEventWebhook() : ?Webhook
265+
{
266+
return $this->eventWebhook;
267+
}
268+
269+
public function setEventWebhook(Webhook $eventWebhook)
270+
{
271+
$this->eventWebhook = $eventWebhook;
272+
return $this;
273+
}
228274
}

0 commit comments

Comments
 (0)