Skip to content

Commit 9586304

Browse files
authored
Merge branch 'dev' into feature/sip-video-flag
2 parents 2545fad + 216c2f8 commit 9586304

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

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

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

703+
/**
704+
* Send DTMF digits to all clients in a session.
705+
*
706+
* @param sessionId The session ID.
707+
* @param dtmfDigits The string of DTMF digits to send. This can include 0-9, "*", "#",
708+
* and "p". A p indicates a pause of 500ms (if you need to add a delay in sending the digits).
709+
*
710+
* @throws OpenTokException
711+
*/
712+
public void playDTMF(String sessionId, String dtmfDigits) throws OpenTokException {
713+
client.playDtmfAll(sessionId, dtmfDigits);
714+
}
715+
716+
/**
717+
* Send DTMF digits a specific client in a session.
718+
*
719+
* @param sessionId The session ID.
720+
* @param connectionId The session ID of the client to receive the DTMF digits.
721+
* @param dtmfDigits The string of DTMF digits to send. This can include 0-9, "*", "#",
722+
* and "p". A p indicates a pause of 500ms (if you need to add a delay in sending the digits).
723+
*
724+
* @throws OpenTokException
725+
*/
726+
public void playDTMF(String sessionId, String connectionId, String dtmfDigits) throws OpenTokException {
727+
client.playDtmfSingle(sessionId, connectionId, dtmfDigits);
728+
}
729+
703730
/**
704731
* Used to create an OpenTok object with advanced settings. You can set
705732
* 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
@@ -781,6 +781,54 @@ public String sipDial(String sessionId, String token, SipProperties props) throw
781781
}
782782
return responseString;
783783
}
784+
785+
public String playDtmf(String url, String dtmfDigits) throws OpenTokException {
786+
String responseString = null;
787+
String requestBody = null;
788+
789+
JsonNodeFactory nodeFactory = JsonNodeFactory.instance;
790+
ObjectNode requestJson = nodeFactory.objectNode();
791+
792+
requestJson.put("digits", dtmfDigits);
793+
try {
794+
requestBody = new ObjectMapper().writeValueAsString(requestJson);
795+
} catch (JsonProcessingException e) {
796+
throw new OpenTokException("Could not send a signal. The JSON body encoding failed.", e);
797+
}
798+
799+
Future<Response> request = this.preparePost(url)
800+
.setBody(requestBody)
801+
.setHeader("Content-Type", "application/json")
802+
.execute();
803+
804+
try {
805+
Response response = request.get();
806+
switch (response.getStatusCode()) {
807+
case 200:
808+
responseString = response.getResponseBody();
809+
break;
810+
default:
811+
throw new RequestException("Could not get a proper response. response code: " +
812+
response.getStatusCode());
813+
}
814+
} catch (InterruptedException | ExecutionException e) {
815+
throw new RequestException("Could not play dtmf");
816+
}
817+
818+
return responseString;
819+
}
820+
821+
public String playDtmfAll(String sessionId, String dtmfDigits) throws OpenTokException {
822+
String url = this.apiUrl + "/v2/project/" + this.apiKey + "/session/" + sessionId + "/play-dtmf";
823+
return playDtmf(url, dtmfDigits);
824+
}
825+
826+
public String playDtmfSingle(String sessionId, String connectionId, String dtmfDigits) throws OpenTokException {
827+
String url = this.apiUrl + "/v2/project/" + this.apiKey + "/session/" + sessionId +
828+
"/connection/"+ connectionId + "/play-dtmf";
829+
return playDtmf(url, dtmfDigits);
830+
}
831+
784832
public String getStream(String sessionId, String streamId) throws RequestException {
785833
String responseString = null;
786834
String url = this.apiUrl + "/v2/project/" + this.apiKey + "/session/" + sessionId + "/stream/" + streamId;

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2111,6 +2111,42 @@ public void testSipDial() throws OpenTokException {
21112111
Helpers.verifyUserAgent();
21122112
}
21132113

2114+
@Test
2115+
public void testPlayDtmfAll() throws OpenTokException {
2116+
String sessionId = "SESSIONID";
2117+
String path = "/v2/project/" + apiKey + "/session/" + sessionId + "/play-dtmf";
2118+
stubFor(post(urlEqualTo(path))
2119+
.willReturn(aResponse()
2120+
.withStatus(200)));
2121+
2122+
String dtmfString = "0p6p4p4pp60p#";
2123+
2124+
sdk.playDTMF(sessionId, dtmfString);
2125+
verify(postRequestedFor(urlMatching(path)));
2126+
assertTrue(Helpers.verifyTokenAuth(apiKey, apiSecret,
2127+
findAll(deleteRequestedFor(urlMatching(path)))));
2128+
Helpers.verifyUserAgent();
2129+
}
2130+
2131+
@Test
2132+
public void testPlayDtmfSingle() throws OpenTokException {
2133+
String sessionId = "SESSIONID";
2134+
String connectionId = "CONNECTIONID";
2135+
String path = "/v2/project/" + apiKey + "/session/" + sessionId +
2136+
"/connection/" + connectionId +"/play-dtmf";
2137+
stubFor(post(urlEqualTo(path))
2138+
.willReturn(aResponse()
2139+
.withStatus(200)));
2140+
2141+
String dtmfString = "0p6p4p4pp60p#";
2142+
2143+
sdk.playDTMF(sessionId, connectionId, dtmfString);
2144+
verify(postRequestedFor(urlMatching(path)));
2145+
assertTrue(Helpers.verifyTokenAuth(apiKey, apiSecret,
2146+
findAll(deleteRequestedFor(urlMatching(path)))));
2147+
Helpers.verifyUserAgent();
2148+
}
2149+
21142150
@Test
21152151
public void testforceDisconnect() throws OpenTokException {
21162152
String sessionId = "SESSIONID";

0 commit comments

Comments
 (0)