Skip to content

Commit

Permalink
DEVX-8677: Voice NCCO updates (#315)
Browse files Browse the repository at this point in the history
* Adding new properties to talk action

* Fixing implement for talk action URL handling

* Adding new properties to NCCO stream action

* Adding transcription property to record action

* Adding mode prepoerty to input NCCO

* Tidy up URL implementation
  • Loading branch information
superchilled authored Aug 22, 2024
1 parent 2edf796 commit e5c41f1
Show file tree
Hide file tree
Showing 14 changed files with 509 additions and 55 deletions.
8 changes: 6 additions & 2 deletions lib/vonage/voice/actions/connect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,13 @@ def verify_advanced_machine_detection_beep_timeout
end

def verify_event_url
uri = URI.parse(self.eventUrl)
unless self.eventUrl.is_a?(Array) && self.eventUrl.length == 1 && self.eventUrl[0].is_a?(String)
raise ClientError.new("Expected 'eventUrl' parameter to be an Array containing a single string item")
end

uri = URI.parse(self.eventUrl[0])

raise ClientError.new("Invalid 'eventUrl' value, must be a valid URL") unless uri.kind_of?(URI::HTTP) || uri.kind_of?(URI::HTTPS)
raise ClientError.new("Invalid 'eventUrl' value, array must contain a valid URL") unless uri.kind_of?(URI::HTTP) || uri.kind_of?(URI::HTTPS)

self.eventUrl
end
Expand Down
10 changes: 8 additions & 2 deletions lib/vonage/voice/actions/conversation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,15 @@ def after_initialize!
end

def verify_music_on_hold_url
uri = URI.parse(self.musicOnHoldUrl)
music_on_hold_url = self.musicOnHoldUrl

raise ClientError.new("Invalid 'musicOnHoldUrl' value, must be a valid URL") unless uri.kind_of?(URI::HTTP) || uri.kind_of?(URI::HTTPS)
unless music_on_hold_url.is_a?(Array) && music_on_hold_url.length == 1 && music_on_hold_url[0].is_a?(String)
raise ClientError.new("Expected 'musicOnHoldUrl' parameter to be an Array containing a single string item")
end

uri = URI.parse(music_on_hold_url[0])

raise ClientError.new("Invalid 'musicOnHoldUrl' value, array must contain a valid URL") unless uri.kind_of?(URI::HTTP) || uri.kind_of?(URI::HTTPS)

self.musicOnHoldUrl
end
Expand Down
22 changes: 19 additions & 3 deletions lib/vonage/voice/actions/input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@

module Vonage
class Voice::Actions::Input
attr_accessor :type, :dtmf, :speech, :eventUrl, :eventMethod
attr_accessor :type, :dtmf, :speech, :eventUrl, :eventMethod, :mode

def initialize(attributes = {})
@type = attributes.fetch(:type)
@dtmf = attributes.fetch(:dtmf, nil)
@speech = attributes.fetch(:speech, nil)
@eventUrl = attributes.fetch(:eventUrl, nil)
@eventMethod = attributes.fetch(:eventMethod, nil)
@mode = attributes.fetch(:mode, nil)

after_initialize!
end
Expand All @@ -33,6 +34,10 @@ def after_initialize!
if self.eventMethod
validate_event_method
end

if self.mode
validate_mode
end
end

def validate_type
Expand Down Expand Up @@ -83,9 +88,13 @@ def validate_speech
end

def validate_event_url
uri = URI.parse(self.eventUrl)
unless self.eventUrl.is_a?(Array) && self.eventUrl.length == 1 && self.eventUrl[0].is_a?(String)
raise ClientError.new("Expected 'eventUrl' parameter to be an Array containing a single string item")
end

uri = URI.parse(self.eventUrl[0])

raise ClientError.new("Invalid 'eventUrl' value, must be a valid URL") unless uri.kind_of?(URI::HTTP) || uri.kind_of?(URI::HTTPS)
raise ClientError.new("Invalid 'eventUrl' value, array must contain a valid URL") unless uri.kind_of?(URI::HTTP) || uri.kind_of?(URI::HTTPS)

self.eventUrl
end
Expand All @@ -96,6 +105,12 @@ def validate_event_method
raise ClientError.new("Invalid 'eventMethod' value. must be either: 'GET' or 'POST'") unless valid_methods.include?(self.eventMethod.upcase)
end

def validate_mode
valid_modes = ['asyncronous']

raise ClientError.new("Invalid 'mode' value, must be asyncronous'") unless valid_modes.include?(self.mode)
end

def action
create_input!(self)
end
Expand All @@ -112,6 +127,7 @@ def create_input!(builder)
ncco[0].merge!(speech: builder.speech) if builder.speech
ncco[0].merge!(eventUrl: builder.eventUrl) if builder.eventUrl
ncco[0].merge!(eventMethod: builder.eventMethod) if builder.eventMethod
ncco[0].merge!(mode: builder.mode) if builder.mode

ncco
end
Expand Down
11 changes: 8 additions & 3 deletions lib/vonage/voice/actions/notify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@ def after_initialize!
end

def validate_event_url
uri = URI.parse(self.eventUrl[0])
event_url = self.eventUrl

raise ClientError.new("Expected 'eventUrl' value to be an Array with a single string") unless self.eventUrl.is_a?(Array)
raise ClientError.new("Invalid 'eventUrl' value, must be a valid URL") unless uri.kind_of?(URI::HTTP) || uri.kind_of?(URI::HTTPS)
unless event_url.is_a?(Array) && event_url.length == 1 && event_url[0].is_a?(String)
raise ClientError.new("Expected 'eventUrl' parameter to be an Array containing a single string item")
end

uri = URI.parse(event_url[0])

raise ClientError.new("Invalid 'eventUrl' value, array must contain a valid URL") unless uri.kind_of?(URI::HTTP) || uri.kind_of?(URI::HTTPS)

self.eventUrl
end
Expand Down
56 changes: 52 additions & 4 deletions lib/vonage/voice/actions/record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module Vonage
class Voice::Actions::Record
attr_accessor :format, :split, :channels, :endOnSilence, :endOnKey, :timeOut, :beepStart, :eventUrl, :eventMethod
attr_accessor :format, :split, :channels, :endOnSilence, :endOnKey, :timeOut, :beepStart, :eventUrl, :eventMethod, :transcription

def initialize(attributes = {})
@format = attributes.fetch(:format, nil)
Expand All @@ -15,6 +15,7 @@ def initialize(attributes = {})
@beepStart = attributes.fetch(:beepStart, nil)
@eventUrl = attributes.fetch(:eventUrl, nil)
@eventMethod = attributes.fetch(:eventMethod, nil)
@transcription = attributes.fetch(:transcription, nil)

after_initialize!
end
Expand Down Expand Up @@ -55,6 +56,10 @@ def after_initialize!
if self.eventMethod
validate_event_method
end

if self.transcription
validate_transcription
end
end

def validate_format
Expand Down Expand Up @@ -90,17 +95,59 @@ def validate_beep_start
end

def validate_event_url
uri = URI.parse(self.eventUrl)
unless self.eventUrl.is_a?(Array) && self.eventUrl.length == 1 && self.eventUrl[0].is_a?(String)
raise ClientError.new("Expected 'eventUrl' parameter to be an Array containing a single string item")
end

raise ClientError.new("Invalid 'eventUrl' value, must be a valid URL") unless uri.kind_of?(URI::HTTP) || uri.kind_of?(URI::HTTPS)
uri = URI.parse(self.eventUrl[0])

raise ClientError.new("Invalid 'eventUrl' value, array must contain a valid URL") unless uri.kind_of?(URI::HTTP) || uri.kind_of?(URI::HTTPS)

self.eventUrl
end

def validate_event_method
valid_methods = ['GET', 'POST']

raise ClientError.new("Invalid 'eventMethod' value. must be either: 'GET' or 'POST'") unless valid_methods.include?(self.eventMethod.upcase)
raise ClientError.new("Invalid 'eventMethod' value. Must be either: 'GET' or 'POST'") unless valid_methods.include?(self.eventMethod.upcase)
end

def validate_transcription
raise ClientError.new("Expected 'transcription' parameter to be a Hash") unless self.transcription.is_a?(Hash)

if self.transcription[:language]
raise ClientError.new("Invalid 'language' value, must be a String") unless self.transcription[:language].is_a?(String)
end

if self.transcription[:eventUrl]
event_url = self.transcription[:eventUrl]

unless event_url.is_a?(Array) && event_url.length == 1 && event_url[0].is_a?(String)
raise ClientError.new("Expected 'eventUrl' parameter to be an Array containing a single string item")
end

uri = URI.parse(event_url[0])

raise ClientError.new("Invalid 'eventUrl' value, array must contain a valid URL") unless uri.kind_of?(URI::HTTP) || uri.kind_of?(URI::HTTPS)
end

if self.transcription[:eventMethod]
event_method = self.transcription[:eventMethod]
raise ClientError.new("Invalid 'eventMethod' value, must be either: 'GET' or 'POST'") unless ['GET', 'POST'].include?(event_method.upcase)
end

if self.transcription[:sentimentAnalysis]
sentiment_analysis = self.transcription[:sentimentAnalysis]
raise ClientError.new("Invalid 'sentimentAnalysis' value, must be a Boolean") unless sentiment_analysis == true || sentiment_analysis == false
end

# if self.dtmf[:maxDigits]
# raise ClientError.new("Expected 'maxDigits' to not be more than 22") if self.dtmf[:maxDigits] > 22
# end

# if self.dtmf[:submitOnHash]
# raise ClientError.new("Invalid 'submitOnHash' value, must be a Boolean") unless self.dtmf[:submitOnHash] == true || self.dtmf[:submitOnHash] == false
# end
end

def action
Expand All @@ -123,6 +170,7 @@ def create_record!(builder)
ncco[0].merge!(beepStart: builder.beepStart) if builder.beepStart
ncco[0].merge!(eventUrl: builder.eventUrl) if builder.eventUrl
ncco[0].merge!(eventMethod: builder.eventMethod) if builder.eventMethod
ncco[0].merge!(transcription: builder.transcription) if builder.transcription

ncco
end
Expand Down
52 changes: 48 additions & 4 deletions lib/vonage/voice/actions/stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@

module Vonage
class Voice::Actions::Stream
attr_accessor :streamUrl, :level, :bargeIn, :loop
attr_accessor :streamUrl, :level, :bargeIn, :loop, :eventOnCompletion, :eventUrl, :eventMethod

def initialize(attributes = {})
@streamUrl = attributes.fetch(:streamUrl)
@level = attributes.fetch(:level, nil)
@bargeIn = attributes.fetch(:bargeIn, nil)
@loop = attributes.fetch(:loop, nil)
@eventOnCompletion = attributes.fetch(:eventOnCompletion, nil)
@eventUrl = attributes.fetch(:eventUrl, nil)
@eventMethod = attributes.fetch(:eventMethod, nil)

after_initialize!
end
Expand All @@ -28,12 +31,28 @@ def after_initialize!
if self.loop
verify_loop
end

if self.eventOnCompletion || self.eventOnCompletion == false
verify_event_on_completion
end

if self.eventUrl
verify_event_url
end

if self.eventMethod
verify_event_method
end
end

def verify_stream_url
raise ClientError.new("Expected 'streamUrl' parameter to be an Array containing a single string item") unless self.streamUrl.is_a?(Array)
stream_url = self.streamUrl

unless stream_url.is_a?(Array) && stream_url.length == 1 && stream_url[0].is_a?(String)
raise ClientError.new("Expected 'streamUrl' parameter to be an Array containing a single string item")
end

uri = URI.parse(self.streamUrl[0])
uri = URI.parse(stream_url[0])

raise ClientError.new("Invalid 'streamUrl' value, must be a valid URL") unless uri.kind_of?(URI::HTTP) || uri.kind_of?(URI::HTTPS)
end
Expand All @@ -47,7 +66,29 @@ def verify_barge_in
end

def verify_loop
raise ClientError.new("Expected 'loop' value to be either 1 or 0") unless self.loop == 1 || self.loop == 0
raise ClientError.new("Expected 'loop' value to be either 0 or a positive integer") unless self.loop >= 0
end

def verify_event_on_completion
raise ClientError.new("Expected 'eventOnCompletion' value to be a Boolean") unless self.eventOnCompletion == true || self.eventOnCompletion == false
end

def verify_event_url
unless self.eventUrl.is_a?(Array) && self.eventUrl.length == 1 && self.eventUrl[0].is_a?(String)
raise ClientError.new("Expected 'eventUrl' parameter to be an Array containing a single string item")
end

uri = URI.parse(self.eventUrl[0])

raise ClientError.new("Invalid 'eventUrl' value, array must contain a valid URL") unless uri.kind_of?(URI::HTTP) || uri.kind_of?(URI::HTTPS)

self.eventUrl
end

def verify_event_method
valid_methods = ['GET', 'POST']

raise ClientError.new("Invalid 'eventMethod' value. must be either: 'GET' or 'POST'") unless valid_methods.include?(self.eventMethod.upcase)
end

def action
Expand All @@ -65,6 +106,9 @@ def create_stream!(builder)
ncco[0].merge!(level: builder.level) if builder.level
ncco[0].merge!(bargeIn: builder.bargeIn) if (builder.bargeIn || builder.bargeIn == false)
ncco[0].merge!(loop: builder.loop) if builder.loop
ncco[0].merge!(eventOnCompletion: builder.eventOnCompletion) if (builder.eventOnCompletion || builder.eventOnCompletion == false)
ncco[0].merge!(eventUrl: builder.eventUrl) if builder.eventUrl
ncco[0].merge!(eventMethod: builder.eventMethod) if builder.eventMethod

ncco
end
Expand Down
Loading

0 comments on commit e5c41f1

Please sign in to comment.