Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEVX-5655 Implement dial DTMF digits feature #234

Merged
merged 5 commits into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions lib/opentok/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,52 @@ def dial(session_id, token, sip_uri, opts)
raise OpenTokError, "Failed to connect to OpenTok. Response code: #{e.message}"
end

def play_dtmf_to_connection(session_id, connection_id, dtmf_digits)
body = { "digits" => dtmf_digits }

response = self.class.post("/v2/project/#{@api_key}/session/#{session_id}/connection/#{connection_id}/play-dtmf", {
:body => body.to_json,
:headers => generate_headers("Content-Type" => "application/json")
})
case response.code
when 200
response
when 400
raise ArgumentError, "One of the properties — dtmf_digits #{dtmf_digits} or session_id #{session_id} — is invalid."
when 403
raise OpenTokAuthenticationError, "Authentication failed. This can occur if you use an invalid OpenTok API key or an invalid JSON web token. API Key: #{@api_key}"
when 404
raise OpenTokError, "The specified session #{session_id} does not exist or the client specified by the #{connection_id} property is not connected to the session."
else
raise OpenTokError, "An error occurred when attempting to play DTMF digits to the session"
end
rescue StandardError => e
raise OpenTokError, "Failed to connect to OpenTok. Response code: #{e.message}"
end

def play_dtmf_to_session(session_id, dtmf_digits)
body = { "digits" => dtmf_digits }

response = self.class.post("/v2/project/#{@api_key}/session/#{session_id}/play-dtmf", {
:body => body.to_json,
:headers => generate_headers("Content-Type" => "application/json")
})
case response.code
when 200
response
when 400
raise ArgumentError, "One of the properties — dtmf_digits #{dtmf_digits} or session_id #{session_id} — is invalid."
when 403
raise OpenTokAuthenticationError, "Authentication failed. This can occur if you use an invalid OpenTok API key or an invalid JSON web token. API Key: #{@api_key}"
when 404
raise OpenTokError, "The specified session does not exist. Session ID: #{session_id}"
else
raise OpenTokError, "An error occurred when attempting to play DTMF digits to the session"
end
rescue StandardError => e
raise OpenTokError, "Failed to connect to OpenTok. Response code: #{e.message}"
end

def info_stream(session_id, stream_id)
streamId = stream_id.to_s.empty? ? '' : "/#{stream_id}"
url = "/v2/project/#{@api_key}/session/#{session_id}/stream#{streamId}"
Expand Down
28 changes: 28 additions & 0 deletions lib/opentok/sip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,36 @@ def dial(session_id, token, sip_uri, opts)
response = @client.dial(session_id, token, sip_uri, opts)
end

# Sends DTMF digits to a specific client connected to an OpnTok session.
#
# @param [String] session_id The session ID.
# @param [String] connection_id The connection ID of the specific connection that
# the DTMF signal is being sent to.
# @param [String] dtmf_digits The DTMF digits to send. This can include 0-9, "*", "#", and "p".
# A p indicates a pause of 500ms (if you need to add a delay in sending the digits).
def play_dtmf_to_connection(session_id, connection_id, dtmf_digits)
raise ArgumentError, "invalid DTMF digits" unless dtmf_digits_valid?(dtmf_digits)
response = @client.play_dtmf_to_connection(session_id, connection_id, dtmf_digits)
end

# Sends DTMF digits to all clients connected to an OpnTok session.
#
# @param [String] session_id The session ID.
# @param [String] dtmf_digits The DTMF digits to send. This can include 0-9, "*", "#", and "p".
# A p indicates a pause of 500ms (if you need to add a delay in sending the digits).
def play_dtmf_to_session(session_id, dtmf_digits)
raise ArgumentError, "invalid DTMF digits" unless dtmf_digits_valid?(dtmf_digits)
response = @client.play_dtmf_to_session(session_id, dtmf_digits)
end

def initialize(client)
@client = client
end

private

def dtmf_digits_valid?(dtmf_digits)
dtmf_digits.match?(/^[0-9*#p]+$/)
end
end
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions spec/opentok/sip_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
let(:api_key) { "123456" }
let(:api_secret) { "1234567890abcdef1234567890abcdef1234567890" }
let(:session_id) { "SESSIONID" }
let(:connection_id) { "CONNID" }
let(:expiring_token) { "TOKENID" }
let(:sip_uri) { "sip:+15128675309@acme.pstn.example.com;transport=tls" }
let(:sip_username) { "bob" }
let(:sip_password) { "abc123" }
let(:opentok) { OpenTok::OpenTok.new api_key, api_secret }
let(:sip) { opentok.sip }
let(:valid_dtmf_digits) { "0123456789*#p" }
let(:invalid_dtmf_digits) { "0123456789*#pabc" }
subject { sip }

it "receives a valid response", :vcr => { :erb => { :version => OpenTok::VERSION + "-Ruby-Version-#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"} } do
Expand All @@ -29,4 +32,30 @@
response = sip.dial(session_id, expiring_token, sip_uri, opts)
expect(response).not_to be_nil
end

describe "#play_dtmf_to_connection" do
it "raises an ArgumentError when passed an invalid dtmf digit string" do
expect {
sip.play_dtmf_to_connection(session_id, connection_id, invalid_dtmf_digits)
}.to raise_error(ArgumentError)
end

it "returns a 200 response code when passed a valid dtmf digit string", :vcr => { :erb => { :version => OpenTok::VERSION + "-Ruby-Version-#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"} } do
response = sip.play_dtmf_to_connection(session_id, connection_id, valid_dtmf_digits)
expect(response.code).to eq(200)
end
end

describe "#play_dtmf_to_session" do
it "raises an ArgumentError when passed an invalid dtmf digit string" do
expect {
sip.play_dtmf_to_session(session_id, invalid_dtmf_digits)
}.to raise_error(ArgumentError)
end

it "returns a 200 response code when passed a valid dtmf digit string", :vcr => { :erb => { :version => OpenTok::VERSION + "-Ruby-Version-#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"} } do
response = sip.play_dtmf_to_session(session_id, valid_dtmf_digits)
expect(response.code).to eq(200)
end
end
end