Examples

View as Markdown

Follow the examples to see how’s easy to use the Relay SDK to interact with inbound or outbound calls.

Inbound Calls

Using RelayConsumer to manage inbound calls from both home and office contexts. Answer the call, ask the user to enter his PIN and playback the digits he sent if successful.

1<?php
2
3require dirname(__FILE__) . '/vendor/autoload.php';
4
5use Generator as Coroutine;
6
7class CustomConsumer extends SignalWire\Relay\Consumer {
8 public $project = 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX';
9 public $token = 'PTXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
10 public $contexts = ['home', 'office'];
11
12 public function onIncomingCall($call): Coroutine {
13 $answerResult = yield $call->answer();
14 if (!$answerResult->isSuccessful()) {
15 echo "Error answering the call..";
16 return;
17 }
18 $promptParams = [
19 'type' => 'digits',
20 'initial_timeout' => 10
21 'digits_max' => 4,
22 'text' => 'Welcome to SignalWire! Please, enter your PIN'
23 ];
24 $promptResult = yield $call->promptTTS($promptParams);
25 if ($promptResult->isSuccessful()) {
26 $pin = $promptResult->getResult();
27 yield $call->playTTS([ "text" => "You entered: {$pin}. Thanks and good bye!" ]);
28 }
29 yield $call->hangup();
30 }
31}
32
33$consumer = new CustomConsumer();
34$consumer->run();

Outbound Calls

Using RelayConsumer to make an outbound call and ask the user to enter his PIN. Once completed, print the collected digits.

1<?php
2
3require dirname(__FILE__) . '/vendor/autoload.php';
4
5use Generator as Coroutine;
6
7class CustomConsumer extends SignalWire\Relay\Consumer {
8 public $project = 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX';
9 public $token = 'PTXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
10 public $contexts = ['home', 'office'];
11
12 public function ready(): Coroutine {
13 $params = ['type' => 'phone', 'from' => '+1XXXXXXXXXX', 'to' => '+1YYYYYYYYYY'];
14 $dialResult = yield $this->client->calling->dial($params);
15 if (!$dialResult->isSuccessful()) {
16 echo "Dial error or call not answered by the remote peer..";
17 return;
18 }
19 $call = $dialResult->getCall();
20 $promptParams = [
21 'type' => 'digits',
22 'digits_max' => 4,
23 'digits_terminators' => '#',
24 'text' => 'Welcome at SignalWire. Please, enter your 4 digits PIN and then # to proceed'
25 ];
26 $promptResult = yield $call->promptTTS($promptParams);
27 if ($promptResult->isSuccessful()) {
28 $pin = $promptResult->getResult();
29 echo "User digits: {$pin} ..";
30 }
31 }
32}
33
34$consumer = new CustomConsumer();
35$consumer->run();