Skip to content

Commit 2e08a6b

Browse files
Passing FROM to an outbound call is now not required, and instead sets the random flag (#288)
1 parent da5a2d4 commit 2e08a6b

File tree

5 files changed

+82
-6
lines changed

5 files changed

+82
-6
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,10 @@ $outboundCall
265265
$response = $client->voice()->createOutboundCall($outboundCall);
266266
```
267267

268+
If you would like to have the system randomly pick a FROM number from the numbers linked to an application, you can
269+
leave off the second parameter to `\Vonage\Voice\OutboundCall`'s constructor, and the system will select a number
270+
at random for you.
271+
268272
### Building a call with NCCO Actions
269273

270274
Full parameter lists for NCCO Actions can be found in the [Voice API Docs](https://developer.nexmo.com/voice/voice-api/ncco-reference).

src/Voice/Client.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,14 @@ public function createOutboundCall(OutboundCall $call): Event
5555
{
5656
$json = [
5757
'to' => [$call->getTo()],
58-
'from' => $call->getFrom(),
5958
];
6059

60+
if ($call->getFrom()) {
61+
$json['from'] = $call->getFrom();
62+
} else {
63+
$json['random_from_number'] = true;
64+
}
65+
6166
if (null !== $call->getAnswerWebhook()) {
6267
$json['answer_url'] = [$call->getAnswerWebhook()->getUrl()];
6368
$json['answer_method'] = $call->getAnswerWebhook()->getMethod();
@@ -86,7 +91,9 @@ public function createOutboundCall(OutboundCall $call): Event
8691

8792
$event = $this->api->create($json);
8893
$event['to'] = $call->getTo()->getId();
89-
$event['from'] = $call->getFrom()->getId();
94+
if ($call->getFrom()) {
95+
$event['from'] = $call->getFrom()->getId();
96+
}
9097
$event['timestamp'] = (new DateTimeImmutable("now", new DateTimeZone("UTC")))->format(DATE_ATOM);
9198

9299
return new Event($event);

src/Voice/OutboundCall.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ class OutboundCall
5454
*/
5555
protected $ncco;
5656

57+
/**
58+
* Whether or not to use random numbers linked on the application
59+
*
60+
* @var bool
61+
*/
62+
protected $randomFrom = false;
63+
5764
/**
5865
* Length of time Vonage will allow a phone number to ring before hanging up
5966
*
@@ -66,10 +73,23 @@ class OutboundCall
6673
*/
6774
protected $to;
6875

69-
public function __construct(EndpointInterface $to, Phone $from)
76+
/**
77+
* Creates a new Outbound Call object
78+
* If no `$from` parameter is passed, the system will use a random number
79+
* that is linked to the application instead.
80+
*
81+
* @param EndpointInterface $to
82+
* @param Phone|null $from
83+
* @return void
84+
*/
85+
public function __construct(EndpointInterface $to, Phone $from = null)
7086
{
7187
$this->to = $to;
7288
$this->from = $from;
89+
90+
if (!$from) {
91+
$this->randomFrom = true;
92+
}
7393
}
7494

7595
public function getAnswerWebhook(): ?Webhook
@@ -82,7 +102,7 @@ public function getEventWebhook(): ?Webhook
82102
return $this->eventWebhook;
83103
}
84104

85-
public function getFrom(): Phone
105+
public function getFrom(): ?Phone
86106
{
87107
return $this->from;
88108
}
@@ -175,4 +195,9 @@ public function setRingingTimer(int $timer): self
175195

176196
return $this;
177197
}
198+
199+
public function getRandomFrom(): bool
200+
{
201+
return $this->randomFrom;
202+
}
178203
}

src/Voice/Webhook/Event.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class Event
108108
*/
109109
public function __construct(array $event)
110110
{
111-
$this->from = $event['from'];
111+
$this->from = $event['from'] ?? null;
112112
$this->to = $event['to'];
113113
$this->uuid = $event['uuid'] ?? $event['call_uuid'];
114114
$this->conversationUuid = $event['conversation_uuid'];
@@ -150,7 +150,7 @@ public function getDirection(): string
150150
return $this->direction;
151151
}
152152

153-
public function getFrom(): string
153+
public function getFrom(): ?string
154154
{
155155
return $this->from;
156156
}

test/Voice/ClientTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,46 @@ public function testCanCreateOutboundCall(): void
110110
$this->assertEquals('2541d01c-253e-48be-a8e0-da4bbe4c3722', $callData->getConversationUuid());
111111
}
112112

113+
public function testCanCreateOutboundCallWithRandomFromNumber(): void
114+
{
115+
$payload = [
116+
'to' => [
117+
[
118+
'type' => 'phone',
119+
'number' => '15555555555'
120+
]
121+
],
122+
'random_from_number' => true,
123+
'answer_url' => ['http://domain.test/answer'],
124+
'answer_method' => 'POST',
125+
'event_url' => ['http://domain.test/event'],
126+
'event_method' => 'POST',
127+
'machine_detection' => 'hangup',
128+
'length_timer' => '7200',
129+
'ringing_timer' => '60',
130+
];
131+
132+
$this->vonageClient->send(Argument::that(function (RequestInterface $request) use ($payload) {
133+
$this->assertRequestUrl('api.nexmo.com', '/v1/calls', 'POST', $request);
134+
$this->assertRequestBodyIsJson(json_encode($payload), $request);
135+
136+
return true;
137+
}))->willReturn($this->getResponse('create-outbound-call-success', 201));
138+
139+
$outboundCall = (new OutboundCall(new Phone('15555555555')))
140+
->setEventWebhook(new Webhook('http://domain.test/event'))
141+
->setAnswerWebhook(new Webhook('http://domain.test/answer'))
142+
->setRingingTimer((int)$payload['ringing_timer'])
143+
->setLengthTimer((int)$payload['length_timer'])
144+
->setMachineDetection(OutboundCall::MACHINE_HANGUP);
145+
$callData = $this->voiceClient->createOutboundCall($outboundCall);
146+
147+
$this->assertEquals('e46fd8bd-504d-4044-9600-26dd18b41111', $callData->getUuid());
148+
$this->assertEquals('started', $callData->getStatus());
149+
$this->assertEquals('outbound', $callData->getDirection());
150+
$this->assertEquals('2541d01c-253e-48be-a8e0-da4bbe4c3722', $callData->getConversationUuid());
151+
}
152+
113153
/**
114154
* @throws ClientExceptionInterface
115155
* @throws Client\Exception\Exception

0 commit comments

Comments
 (0)