I have a Ruby on Rails application where I was previously using the plivohelper gem
for call services. Now, I need to migrate to the vonage gem
and make the required changes to ensure the entire call system works seamlessly.
Here's the original code that uses plivohelper
app/interactors/call_activity/connect_backoffice.rb
def dialnode number = app_config.voice[@call.state['backoffice_type']].number timeout = app_config.voice[@call.state['backoffice_type']].timeout # Here we try to reach the backoffice number within a few seconds. If the call is not answered, the control will be switched to the next activity dial_node_options = { dialMusic: sound_callback_path('forwarding_to_backoffice'), callbackUrl: callback_path('on_bridge'), callerName: (@call.external_number(true) || '498910003000') } if @call.state['backoffice_type'] == 'barred' dial_node_options[:confirm_sound] = sound_callback_path('barred_user') dial_node_options[:confirm_key] = '*' end dial_node = Plivo::Dial.new(dial_node_options) dial_node.append(Plivo::Number.new(format_number(number), gateways: Rails.application.credentials.voice.dig(:gateways), extraDialString: "leg_timeout=#{timeout}")) dial_node dial_node endend
app/interactors/call_activity/base.rb
def sound_callback_path(message)"http://#{Rails.application.credentials.general.dig(:internal_url)}/callback/call/sound?url=#{voice_message_url(message)}"end
app/interactors/call_activity/base.rb
def callback_path(action, params = nil) url = "http://#{Rails.application.credentials.general.dig(:internal_url)}/callback/call/#{action}" if params.present? uri = Addressable::URI.new uri.query_values = params url += '?'+ uri.query else return url end end
I have a set of events that I have used using gem aasm
to transit one event to another based on action.
app/interactors/call_activity/connect_supporter.rb
def dialplan_connecting internal = request.supporter.supporter_settings.internal_sip response = Plivo::Response.new if app_config.voice.recording response.addRecord(bothLegs: true, filePath: Rails.application.credentials.dig(:voice.calls_path), fileName: recording_file_name(false), maxLength: 100000) call_recording_announcement(response) end dial_node_options = { timeout: 15, callerName: '498910002000', dialMusic: sound_callback_path('forwarding_to_supporter'), callbackUrl: callback_path('on_bridge'), confirmKey: (request.started? || internal) ? nil : '*', confirmSound: (request.started? || internal) ? nil : sound_callback_path('dial_star_accept') }.compact dial_node = Plivo::Dial.new(dial_node_options) if internal dial_node.append(Plivo::Number.new(request.supporter.id.to_s, gateways: Rails.application.credentials.voice.dig(:internal_sip_gateway), extraDialString: 'leg_timeout=30')) else dial_node.append(Plivo::Number.new(format_number(request.supporter.personal_phone_number), gateways: Rails.application.credentials.voice.dig(:gateways), extraDialString: 'leg_timeout=30')) end response.append(dial_node) response.addRedirect(callback_path('next')) response end
I have removed the plivohelper gem
and did this
config/initializers/vonage.rb
require 'vonage'# Vonage configurationVonage.config.api_key = ENV['VONAGE_API_KEY']Vonage.config.api_secret = ENV['VONAGE_API_SECRET']Vonage.config.application_id = ENV['APPLICATION_ID']Vonage.config.private_key = ENV['VONAGE_PRIVATE_KEY']# Virtual numberVONAGE_VIRTUAL_NUMBER = ENV['VONAGE_VIRTUAL_NUMBER'].freeze# JWT token# VONAGE_JWT_TOKEN = generate_jwt_token# Vonage clientVONAGE_CLIENT = Vonage::Client.new( api_key: Vonage.config.api_key, api_secret: Vonage.config.api_secret, application_id: Vonage.config.application_id, private_key: Vonage.config.private_key)
Now, how can I replace the entire work with vonage gem
modifying the necessary code and keys so that the features work the same like before?