Skip to content

Commit 4fc7997

Browse files
committed
Added DTMF API
1 parent 5806f19 commit 4fc7997

File tree

6 files changed

+154
-0
lines changed

6 files changed

+154
-0
lines changed

src/OpenTok/OpenTok.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,29 @@ public function dial($sessionId, $token, $sipUri, $options = [])
764764
return new SipCall($sipJson);
765765
}
766766

767+
/**
768+
* Plays a DTMF string into a session or to a specific connection
769+
*
770+
* @param string $sessionId The ID of the OpenTok session that the participant being called
771+
* will join.
772+
*
773+
* @param string $digits DTMF digits to play
774+
* Valid DTMF digits are 0-9, p, #, and * digits. 'p' represents a 500ms pause if a delay is
775+
* needed during the input process.
776+
*
777+
* @param string $connectionId An optional parameter used to send the DTMF tones to a specific connection in a session.
778+
*
779+
* @return void
780+
*/
781+
public function playDTMF(string $sessionId, string $digits, string $connectionId = null): void
782+
{
783+
Validators::validateSessionIdBelongsToKey($sessionId, $this->apiKey);
784+
Validators::validateDTMFDigits($digits);
785+
786+
$this->client->playDTMF($sessionId, $digits, $connectionId);
787+
788+
}
789+
767790
/**
768791
* Sends a signal to clients (or a specific client) connected to an OpenTok session.
769792
*

src/OpenTok/Util/Client.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,31 @@ public function dial($sessionId, $token, $sipUri, $options)
532532
return $sipJson;
533533
}
534534

535+
public function playDTMF(string $sessionId, string $digits, string $connectionId = null): void
536+
{
537+
$route = sprintf('/v2/projects/%s/session/%s/play-dtmf', $this->apiKey, $sessionId);
538+
if ($connectionId) {
539+
$route = sprintf(
540+
'/v2/projects/%s/session/%s/connection/%s/play-dtmf',
541+
$this->apiKey,
542+
$sessionId,
543+
$connectionId
544+
);
545+
}
546+
547+
$request = new Request('POST', $route);
548+
try {
549+
$this->client->send($request, [
550+
'debug' => $this->isDebug(),
551+
'json' => [
552+
'digits' => $digits
553+
]
554+
]);
555+
} catch (\Exception $e) {
556+
$this->handleException($e);
557+
}
558+
}
559+
535560
/**
536561
* Signal either an entire session or a specific connection in a session
537562
*

src/OpenTok/Util/Validators.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,15 @@ public static function validateDefaultTimeout($timeout)
327327
}
328328
}
329329

330+
public static function validateDTMFDigits(string $digits): void
331+
{
332+
if (preg_match('/^[\dp\#\*]+$/', $digits)) {
333+
return;
334+
}
335+
336+
throw new InvalidArgumentException('DTMF digits can only support 0-9, p, #, and * characters');
337+
}
338+
330339
// Helpers
331340

332341
// credit: http://stackoverflow.com/a/173479

tests/OpenTokTest/OpenTokTest.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,22 @@
77
use OpenTok\OpenTok;
88
use OpenTok\MediaMode;
99
use ArgumentCountError;
10+
use DomainException;
11+
use Exception;
12+
use GuzzleHttp\Exception\GuzzleException;
1013
use OpenTok\OutputMode;
1114
use OpenTok\ArchiveMode;
1215
use OpenTok\Util\Client;
1316
use GuzzleHttp\Middleware;
1417
use GuzzleHttp\HandlerStack;
1518
use PHPUnit\Framework\TestCase;
1619
use GuzzleHttp\Handler\MockHandler;
20+
use InvalidArgumentException as GlobalInvalidArgumentException;
21+
use OpenTok\Exception\AuthenticationException;
22+
use OpenTok\Exception\DomainException as ExceptionDomainException;
1723
use OpenTok\Exception\InvalidArgumentException;
24+
use RuntimeException;
25+
use OpenTok\Exception\UnexpectedValueException;
1826

1927
define('OPENTOK_DEBUG', true);
2028

@@ -1664,6 +1672,87 @@ public function testSipCallVideo()
16641672
$this->assertEquals(true, $body->sip->video);
16651673
}
16661674

1675+
public function testPlayDTMF()
1676+
{
1677+
$this->setupOTWithMocks([[
1678+
'code' => 200,
1679+
'headers' => [
1680+
'Content-Type' => 'application/json'
1681+
],
1682+
'path' => 'v2/project/APIKEY/session/SESSIONID/play-dtmf'
1683+
]]);
1684+
1685+
$sessionId = '1_MX4xMjM0NTY3OH4-VGh1IEZlYiAyNyAwNDozODozMSBQU1QgMjAxNH4wLjI0NDgyMjI';
1686+
$digits = '1p713#';
1687+
1688+
$this->opentok->playDTMF($sessionId, $digits);
1689+
1690+
$this->assertCount(1, $this->historyContainer);
1691+
$request = $this->historyContainer[0]['request'];
1692+
1693+
$body = json_decode($request->getBody());
1694+
$this->assertEquals($digits, $body->digits);
1695+
}
1696+
1697+
public function testPlayDTMFIntoConnection()
1698+
{
1699+
$this->setupOTWithMocks([[
1700+
'code' => 200,
1701+
'headers' => [
1702+
'Content-Type' => 'application/json'
1703+
],
1704+
'path' => 'v2/project/APIKEY/session/SESSIONID/play-dtmf'
1705+
]]);
1706+
1707+
$sessionId = '1_MX4xMjM0NTY3OH4-VGh1IEZlYiAyNyAwNDozODozMSBQU1QgMjAxNH4wLjI0NDgyMjI';
1708+
$connectionId = 'da9cb410-e29b-4c2d-ab9e-fe65bf83fcaf';
1709+
$digits = '1p713#';
1710+
1711+
$this->opentok->playDTMF($sessionId, $digits, $connectionId);
1712+
1713+
$this->assertCount(1, $this->historyContainer);
1714+
$request = $this->historyContainer[0]['request'];
1715+
1716+
$body = json_decode($request->getBody());
1717+
$this->assertEquals($digits, $body->digits);
1718+
}
1719+
1720+
public function testDTMFFailsValidation()
1721+
{
1722+
$this->expectException(\InvalidArgumentException::class);
1723+
$this->expectExceptionMessage('DTMF digits can only support 0-9, p, #, and * characters');
1724+
1725+
$this->setupOT();
1726+
$sessionId = '1_MX4xMjM0NTY3OH4-VGh1IEZlYiAyNyAwNDozODozMSBQU1QgMjAxNH4wLjI0NDgyMjI';
1727+
$this->opentok->playDTMF($sessionId, 'bob');
1728+
}
1729+
1730+
/**
1731+
* Tests that we properly handle a 400 error from the API
1732+
* Ideally with the validator we fail before the API request is even made,
1733+
* but this will make sure that we still properly handle a 400 error. For
1734+
* this to work we do send a valid DTMF string however, to satisfy the
1735+
* validator.
1736+
*/
1737+
public function testPlayDTMFThrows400(): void
1738+
{
1739+
$this->expectException(DomainException::class);
1740+
$this->expectExceptionMessage('The OpenTok API request failed: Invalid DTMF Digits');
1741+
1742+
$this->setupOTWithMocks([[
1743+
'code' => 400,
1744+
'headers' => [
1745+
'Content-Type' => 'application/json'
1746+
],
1747+
'path' => 'v2/project/APIKEY/session/SESSIONID/play-dtmf-400'
1748+
]]);
1749+
1750+
$sessionId = '1_MX4xMjM0NTY3OH4-VGh1IEZlYiAyNyAwNDozODozMSBQU1QgMjAxNH4wLjI0NDgyMjI';
1751+
$digits = '1p713#';
1752+
1753+
$this->opentok->playDTMF($sessionId, $digits);
1754+
}
1755+
16671756
public function testSignalData()
16681757
{
16691758
// Arrange
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"code" : 200,
3+
"message" : "OK"
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"code" : 400,
3+
"message" : "Invalid DTMF Digits"
4+
}

0 commit comments

Comments
 (0)