Skip to content

Commit 4c3c895

Browse files
author
Mofizur Rahman
committed
add play dtmf to opentok
1 parent 0a80d11 commit 4c3c895

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

src/main/java/com/opentok/OpenTok.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,28 @@ public Sip dial(String sessionId, String token, SipProperties properties) throws
700700
}
701701
}
702702

703+
/**
704+
* Play DTMF to all clients in a session
705+
*
706+
* @param sessionId
707+
* @param dtmfDigits
708+
* @throws OpenTokException
709+
*/
710+
public void playDTMF(String sessionId, String dtmfDigits) throws OpenTokException {
711+
client.playDtmfAll(sessionId, dtmfDigits);
712+
}
713+
714+
/**
715+
* Play DTMF to a specific client in a session
716+
* @param sessionId
717+
* @param connectionId
718+
* @param dtmfDigits
719+
* @throws OpenTokException
720+
*/
721+
public void playDTMF(String sessionId, String connectionId, String dtmfDigits) throws OpenTokException {
722+
client.playDtmfSingle(sessionId, connectionId, dtmfDigits);
723+
}
724+
703725
/**
704726
* Used to create an OpenTok object with advanced settings. You can set
705727
* the request timeout for API calls and a proxy to use for API calls.

src/main/java/com/opentok/util/HttpClient.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,54 @@ public String sipDial(String sessionId, String token, SipProperties props) throw
775775
}
776776
return responseString;
777777
}
778+
779+
public String playDtmf(String url, String dtmfDigits) throws OpenTokException {
780+
String responseString = null;
781+
String requestBody = null;
782+
783+
JsonNodeFactory nodeFactory = JsonNodeFactory.instance;
784+
ObjectNode requestJson = nodeFactory.objectNode();
785+
786+
requestJson.put("digits", dtmfDigits);
787+
try {
788+
requestBody = new ObjectMapper().writeValueAsString(requestJson);
789+
} catch (JsonProcessingException e) {
790+
throw new OpenTokException("Could not send a signal. The JSON body encoding failed.", e);
791+
}
792+
793+
Future<Response> request = this.preparePost(url)
794+
.setBody(requestBody)
795+
.setHeader("Content-Type", "application/json")
796+
.execute();
797+
798+
try {
799+
Response response = request.get();
800+
switch (response.getStatusCode()) {
801+
case 200:
802+
responseString = response.getResponseBody();
803+
break;
804+
default:
805+
throw new RequestException("Could not get a proper response. response code: " +
806+
response.getStatusCode());
807+
}
808+
} catch (InterruptedException | ExecutionException e) {
809+
throw new RequestException("Could not play dtmf");
810+
}
811+
812+
return responseString;
813+
}
814+
815+
public String playDtmfAll(String sessionId, String dtmfDigits) throws OpenTokException {
816+
String url = this.apiUrl + "/v2/project/" + this.apiKey + "/session/" + sessionId + "/play-dtmf";
817+
return playDtmf(url, dtmfDigits);
818+
}
819+
820+
public String playDtmfSingle(String sessionId, String connectionId, String dtmfDigits) throws OpenTokException {
821+
String url = this.apiUrl + "/v2/project/" + this.apiKey + "/session/" + sessionId +
822+
"/connection/"+ connectionId + "/play-dtmf";
823+
return playDtmf(url, dtmfDigits);
824+
}
825+
778826
public String getStream(String sessionId, String streamId) throws RequestException {
779827
String responseString = null;
780828
String url = this.apiUrl + "/v2/project/" + this.apiKey + "/session/" + sessionId + "/stream/" + streamId;

src/test/java/com/opentok/test/OpenTokTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2095,6 +2095,43 @@ public void testSipDial() throws OpenTokException {
20952095
findAll(postRequestedFor(urlMatching(url)))));
20962096
Helpers.verifyUserAgent();
20972097
}
2098+
2099+
@Test
2100+
public void testPlayDtmfAll() throws OpenTokException {
2101+
String sessionId = "SESSIONID";
2102+
String path = "/v2/project/" + apiKey + "/session/" + sessionId + "/play-dtmf";
2103+
stubFor(post(urlEqualTo(path))
2104+
.willReturn(aResponse()
2105+
.withStatus(200)));
2106+
2107+
String dtmfString = "0p6p4p4pp60p#";
2108+
2109+
sdk.playDTMF(sessionId, dtmfString);
2110+
verify(postRequestedFor(urlMatching(path)));
2111+
assertTrue(Helpers.verifyTokenAuth(apiKey, apiSecret,
2112+
findAll(deleteRequestedFor(urlMatching(path)))));
2113+
Helpers.verifyUserAgent();
2114+
}
2115+
2116+
@Test
2117+
public void testPlayDtmfSingle() throws OpenTokException {
2118+
String sessionId = "SESSIONID";
2119+
String connectionId = "CONNECTIONID";
2120+
String path = "/v2/project/" + apiKey + "/session/" + sessionId +
2121+
"/connection/" + connectionId +"/play-dtmf";
2122+
stubFor(post(urlEqualTo(path))
2123+
.willReturn(aResponse()
2124+
.withStatus(200)));
2125+
2126+
String dtmfString = "0p6p4p4pp60p#";
2127+
2128+
sdk.playDTMF(sessionId, connectionId, dtmfString);
2129+
verify(postRequestedFor(urlMatching(path)));
2130+
assertTrue(Helpers.verifyTokenAuth(apiKey, apiSecret,
2131+
findAll(deleteRequestedFor(urlMatching(path)))));
2132+
Helpers.verifyUserAgent();
2133+
}
2134+
20982135
@Test
20992136
public void testforceDisconnect() throws OpenTokException {
21002137
String sessionId = "SESSIONID";

0 commit comments

Comments
 (0)