Class: Plivo::Resources::CallInterface

Inherits:
Base::ResourceInterface show all
Defined in:
lib/plivo/resources/calls.rb

Instance Method Summary collapse

Methods included from Utils

expected_type?, expected_value?, raise_invalid_request, valid_account?, valid_mainaccount?, valid_param?, valid_signature?, valid_subaccount?

Constructor Details

#initialize(client, resource_list_json = nil) ⇒ CallInterface

Returns a new instance of CallInterface.



235
236
237
238
239
240
# File 'lib/plivo/resources/calls.rb', line 235

def initialize(client, resource_list_json = nil)
  @_name = 'Call'
  @_resource_type = Call
  @_identifier_string = 'call_uuid'
  super
end

Instance Method Details

#cancel_request(call_uuid) ⇒ Object

Parameters:

  • call_uuid (String)


512
513
514
515
# File 'lib/plivo/resources/calls.rb', line 512

def cancel_request(call_uuid)
  valid_param?(:call_uuid, call_uuid, [String, Symbol], true)
  Call.new(@_client, resource_id: call_uuid).cancel_request
end

#create(from, to, answer_url, answer_method = 'POST', options = nil) ⇒ Call

Makes an outbound call

Parameters:

  • from (String)
  • to (Array)
  • answer_url (String)
  • answer_method (String) (defaults to: 'POST')
  • options (Hash) (defaults to: nil)

Options Hash (options):

  • :answer_method (String)
    • The method used to call the answer_url. Defaults to POST.

  • :ring_url (String)
    • The URL that is notified by Plivo when the call is ringing. Defaults not set.

  • :ring_method (String)
    • The method used to call the ring_url. Defaults to POST.

  • :hangup_url (String)
    • The URL that will be notified by Plivo when the call hangs up. Defaults to answer_url.

  • :hangup_method (String)
    • The method used to call the hangup_url. Defaults to POST.

  • :fallback_url (String)
    • Invoked by Plivo only if answer_url is unavailable or the XML response is invalid. Should contain a XML response.

  • :fallback_method (String)
    • The method used to call the fallback_answer_url. Defaults to POST.

  • :caller_name (String)
    • Caller name to use with the call.

  • :send_digits (String)
    • Plivo plays DTMF tones when the call is answered. This is useful when dialing a phone number and an extension. Plivo will dial the number, and when the automated system picks up, sends the DTMF tones to connect to the extension. E.g. If you want to dial the 2410 extension after the call is connected, and you want to wait for a few seconds before sending the extension, add a few leading ‘w’ characters. Each ‘w’ character waits 0.5 second before sending a digit. Each ‘W’ character waits 1 second before sending a digit. You can also add the tone duration in ms by appending @duration after the string (default duration is 2000 ms). For example, 1w2w3@1000 See the DTMF API for additional information.

  • :send_on_preanswer (Boolean)
    • If set to true and send_digits is also set, digits are sent when the call is in preanswer state. Defaults to false.

  • :time_limit (Int)
    • Schedules the call for hangup at a specified time after the call is answered. Value should be an integer > 0(in seconds).

  • :hangup_on_ring (Int)
    • Schedules the call for hangup at a specified time after the call starts ringing. Value should be an integer >= 0 (in seconds).

  • :machine_detection (String)
    • Used to detect if the call has been answered by a machine. The valid values are true and hangup. Default time to analyze is 5000 milliseconds (or 5 seconds). You can change it with the machine_detection_time parameter. Note that no XML is processed during the analysis phase. If a machine is detected during the call and machine_detection is set to true, the Machine parameter will be set to true and will be sent to the answer_url, hangup_url, or any other URL that is invoked by the call. If a machine is detected during the call and machine_detection is set to hangup, the call hangs up immediately and a request is made to the hangup_url with the Machine parameter set to true

  • :machine_detection_time (Int)
    • Time allotted to analyze if the call has been answered by a machine. It should be an integer >= 2000 and <= 10000 and the unit is ms. The default value is 5000 ms.

  • :machine_detection_url (String)
    • A URL where machine detection parameters will be sent by Plivo. This parameter should be used to make machine detection asynchronous

  • :machine_detection_method (String)
    • The HTTP method which will be used by Plivo to request the machine_detection_url. Defaults to POST.

  • :sip_headers- (String)

    List of SIP headers in the form of ‘key=value’ pairs, separated by commas. E.g. head1=val1,head2=val2,head3=val3,…,headN=valN. The SIP headers are always prefixed with X-PH-. The SIP headers are present for every HTTP request made by the outbound call. Only [A-Z], [a-z] and [0-9] characters are allowed for the SIP headers key and value. Additionally, the ‘%’ character is also allowed for the SIP headers value so that you can encode this value in the URL.

  • :ring_timeout (Int)
    • Determines the time in seconds the call should ring. If the call is not answered within the ring_timeout value or the default value of 120s, it is canceled.

  • :parent_call_uuid (String)
    • The call_uuid of the first leg in an ongoing conference call. It is recommended to use this parameter in scenarios where a member who is already present in the conference intends to add new members by initiating outbound API calls. This minimizes the delay in adding a new memeber to the conference.

  • :error_parent_not_found (Boolean)
    • if set to true and the parent_call_uuid cannot be found, the API request would return an error. If set to false, the outbound call API request will be executed even if the parent_call_uuid is not found. Defaults to false.

Returns:



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/plivo/resources/calls.rb', line 271

def create(from, to, answer_url, answer_method = 'POST', options = nil)
  valid_param?(:from, from, [String, Symbol, Integer], true)
  valid_param?(:to, to, Array, true)
  to.each do |to_num|
    valid_param?(:to_num, to_num, [Integer, String, Symbol], true)
  end
  valid_param?(:answer_url, answer_url, [String, Symbol], true)
  valid_param?(:answer_method, answer_method, [String, Symbol],
               true, %w[GET POST])

  params = {
    from: from,
    to: to.join('<'),
    answer_url: answer_url,
    answer_method: answer_method
  }

  return perform_create(params) if options.nil?

  perform_create(params.merge(options))
end

#delete(call_uuid) ⇒ Object

Parameters:

  • call_uuid (String)


427
428
429
430
# File 'lib/plivo/resources/calls.rb', line 427

def delete(call_uuid)
  valid_param?(:call_uuid, call_uuid, [String, Symbol], true)
  Call.new(@_client, resource_id: call_uuid).delete
end

#eachObject



375
376
377
378
379
380
381
382
383
# File 'lib/plivo/resources/calls.rb', line 375

def each
  offset = 0
  loop do
    call_list = list(offset: offset)
    call_list[:objects].each { |call| yield call }
    offset += 20
    return unless call_list.length == 20
  end
end

#each_liveObject



401
402
403
404
# File 'lib/plivo/resources/calls.rb', line 401

def each_live
  call_list = list_live
  call_list[:calls].each { |call| yield call }
end

#each_queuedObject



406
407
408
409
# File 'lib/plivo/resources/calls.rb', line 406

def each_queued
  call_queued = list_queued
  call_queued[:calls].each { |call| yield call}
end

#get(call_uuid) ⇒ Object

Get details of a call

Parameters:

  • call_uuid (String)


296
297
298
299
# File 'lib/plivo/resources/calls.rb', line 296

def get(call_uuid)
  valid_param?(:call_uuid, call_uuid, [String, Symbol], true)
  perform_get(call_uuid)
end

#get_live(call_uuid) ⇒ Object

Parameters:

  • call_uuid (String)


302
303
304
# File 'lib/plivo/resources/calls.rb', line 302

def get_live(call_uuid)
  perform_get(call_uuid, status: 'live')
end

#get_queued(call_uuid) ⇒ Object

Parameters:

  • call_uuid (String)


307
308
309
# File 'lib/plivo/resources/calls.rb', line 307

def get_queued(call_uuid)
  perform_get(call_uuid, status: 'queued')
end

#list(options = nil) ⇒ Object

Parameters:

  • options (Hash) (defaults to: nil)

Options Hash (options):

  • :subaccount (String)
    • The id of the subaccount, if call details of the subaccount are needed.

  • :call_direction (String)
    • Filter the results by call direction. The valid inputs are inbound and outbound.

  • :from_number (String)
    • Filter the results by the number from where the call originated. For example:

    • To filter out those numbers that contain a particular number sequence, use from_number={ sequence}

    • To filter out a number that matches an exact number, use from_number={ exact_number}

  • :to_number (String)
    • Filter the results by the number to which the call was made. Tips to use this filter are:

    • To filter out those numbers that contain a particular number sequence, use to_number={ sequence}

    • To filter out a number that matches an exact number, use to_number={ exact_number}

  • :bill_duration (String)
    • Filter the results according to billed duration. The value of billed duration is in seconds. The filter can be used in one of the following five forms:

    • bill_duration: Input the exact value. E.g., to filter out calls that were exactly three minutes long, use bill_duration=180

    • bill_duration__gt: gt stands for greater than. E.g., to filter out calls that were more than two hours in duration bill_duration__gt=7200

    • bill_duration__gte: gte stands for greater than or equal to. E.g., to filter out calls that were two hours or more in duration bill_duration__gte=7200

    • bill_duration__lt: lt stands for lesser than. E.g., to filter out calls that were less than seven minutes in duration bill_duration__lt=420

    • bill_duration__lte: lte stands for lesser than or equal to. E.g., to filter out calls that were two hours or less in duration bill_duration__lte=7200

  • :end_time (String)
    • Filter out calls according to the time of completion. The filter can be used in the following five forms:

    • end_time: The format expected is YYYY-MM-DD HH:MM[:ss]. E.g., To get all calls that ended at 2012-03-21 11:47, use end_time=2012-03-21 11:47

    • end_time__gt: gt stands for greater than. The format expected is YYYY-MM-DD HH:MM[:ss]. E.g., To get all calls that ended after 2012-03-21 11:47, use end_time__gt=2012-03-21 11:47

    • end_time__gte: gte stands for greater than or equal. The format expected is YYYY-MM-DD HH:MM[:ss]. E.g., To get all calls that ended after or exactly at 2012-03-21 11:47, use end_time__gte=2012-03-21 11:47

    • end_time__lt: lt stands for lesser than. The format expected is YYYY-MM-DD HH:MM[:ss]. E.g., To get all calls that ended before 2012-03-21 11:47, use end_time__lt=2012-03-21 11:47

    • end_time__lte: lte stands for lesser than or equal. The format expected is YYYY-MM-DD HH:MM[:ss]. E.g., To get all calls that ended before or exactly at 2012-03-21 11:47, use end_time__lte=2012-03-21 11:47

    • Note: The above filters can be combined to get calls that ended in a particular time range. The timestamps need to be UTC timestamps.

  • :limit (Int)
    • Used to display the number of results per page. The maximum number of results that can be fetched is 20.

  • :offset (Int)
    • Denotes the number of value items by which the results should be offset. E.g., If the result contains a 1000 values and limit is set to 10 and offset is set to 705, then values 706 through 715 are displayed in the results. This parameter is also used for pagination of the results.



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/plivo/resources/calls.rb', line 336

def list(options = nil)
  return perform_list if options.nil?
  valid_param?(:options, options, Hash, true)

  params = {}
  params_expected = %i[
    subaccount bill_duration bill_duration__gt bill_duration__gte
    bill_duration__lt bill_duration__lte end_time end_time__gt
    end_time__gte end_time__lt end_time__lte parent_call_uuid
  ]
  params_expected.each do |param|
    if options.key?(param) &&
       valid_param?(param, options[param], [String, Symbol], true)
      params[param] = options[param]
    end
  end

  if options.key?(:call_direction) &&
     valid_param?(:call_direction, options[:call_direction],
                  [String, Symbol], true, %w[inbound outbound])
    params[:call_direction] = options[:call_direction]
  end

  %i[offset limit].each do |param|
    if options.key?(param) && valid_param?(param, options[param], [Integer, Integer], true)
      params[param] = options[param]
    end
  end

  raise_invalid_request("Offset can't be negative") if options.key?(:offset) && options[:offset] < 0

  if options.key?(:limit) && (options[:limit] > 20 || options[:limit] <= 0)
    raise_invalid_request('The maximum number of results that can be '\
    "fetched is 20. limit can't be more than 20 or less than 1")
  end

  perform_list(params)
end

#list_liveObject



385
386
387
388
389
390
391
# File 'lib/plivo/resources/calls.rb', line 385

def list_live
  perform_list_without_object(status: 'live')
  {
    api_id: @api_id,
    calls: @calls
  }
end

#list_queuedObject



393
394
395
396
397
398
399
# File 'lib/plivo/resources/calls.rb', line 393

def list_queued
 perform_list_without_object(status: 'queued')
 {
     api_id: @api_id,
     calls: @calls
 }
end

#play(call_uuid, urls, options = nil) ⇒ Object

Parameters:

  • call_uuid (String)
  • urls (Array)
  • options (Hash) (defaults to: nil)

Options Hash (options):

  • :urls (Array of strings)
    • A single URL or a list of comma separated URLs linking to an mp3 or wav file.

  • :length (Int)
    • Maximum length in seconds that the audio should be played.

  • :legs (String)
    • The leg on which the music will be played, can be aleg (i.e., A-leg is the first leg of the call or current call), bleg (i.e., B-leg is the second leg of the call),or both (i.e., both legs of the call).

  • :loop (Boolean)
    • If set to true, the audio file will play indefinitely.

  • :mix (Boolean)
    • If set to true, sounds are mixed with current audio flow.



472
473
474
475
476
# File 'lib/plivo/resources/calls.rb', line 472

def play(call_uuid, urls, options = nil)
  valid_param?(:call_uuid, call_uuid, [String, Symbol], true)
  valid_param?(:urls, urls, Array, true)
  Call.new(@_client, resource_id: call_uuid).play(urls, options)
end

#record(call_uuid, options = nil) ⇒ Object

Parameters:

  • call_uuid (String)
  • options (Hash) (defaults to: nil)

Options Hash (options):

  • :time_limit (Int)
    • Max recording duration in seconds. Defaults to 60.

  • :file_format (String)
    • The format of the recording. The valid formats are mp3 and wav formats. Defaults to mp3.

  • :transcription_type (String)
    • The type of transcription required. The following values are allowed:

    • auto - This is the default value. Transcription is completely automated; turnaround time is about 5 minutes.

    • hybrid - Transcription is a combination of automated and human verification processes; turnaround time is about 10-15 minutes.

    • *Our transcription service is primarily for the voicemail use case (limited to recorded files lasting for up to 2 minutes). Currently the service is available only in English and you will be charged for the usage. Please check out the price details.

  • :transcription_url (String)
    • The URL where the transcription is available.

  • :transcription_method (String)
    • The method used to invoke the transcription_url. Defaults to POST.

  • :callback_url (String)
    • The URL invoked by the API when the recording ends. The following parameters are sent to the callback_url:

    • api_id - the same API ID returned by the call record API.

    • record_url - the URL to access the recorded file.

    • call_uuid - the call uuid of the recorded call.

    • recording_id - the recording ID of the recorded call.

    • recording_duration - duration in seconds of the recording.

    • recording_duration_ms - duration in milliseconds of the recording.

    • recording_start_ms - when the recording started (epoch time UTC) in milliseconds.

    • recording_end_ms - when the recording ended (epoch time UTC) in milliseconds.

  • :callback_method (String)
    • The method which is used to invoke the callback_url URL. Defaults to POST.



452
453
454
455
# File 'lib/plivo/resources/calls.rb', line 452

def record(call_uuid, options = nil)
  valid_param?(:call_uuid, call_uuid, [String, Symbol], true)
  Call.new(@_client, resource_id: call_uuid).record(options)
end

#send_digits(call_uuid, digits, leg = nil) ⇒ Object

Parameters:

  • call_uuid (String)
  • digits (String)
    • Digits to be sent.

  • leg (String) (defaults to: nil)
    • The leg to be used, can be aleg (the current call) or bleg (the other party in a Dial). Defaults to aleg.



506
507
508
509
# File 'lib/plivo/resources/calls.rb', line 506

def send_digits(call_uuid, digits, leg = nil)
  valid_param?(:call_uuid, call_uuid, [String, Symbol], true)
  Call.new(@_client, resource_id: call_uuid).send_digits(digits, leg)
end

#speak(call_uuid, text, options = nil) ⇒ Object

Parameters:

  • call_uuid (String)
  • text (String)
  • options (Hash) (defaults to: nil)

Options Hash (options):

  • :voice (String)
    • The voice to be used, can be MAN, WOMAN.

  • :language (Int)
  • :legs (String)
    • The leg on which the music will be played, can be aleg (i.e., A-leg is the first leg of the call or current call), bleg (i.e., B-leg is the second leg of the call),or both (i.e., both legs of the call).

  • :loop (Boolean)
    • If set to true, the audio file will play indefinitely.

  • :mix (Boolean)
    • If set to true, sounds are mixed with current audio flow.



492
493
494
495
# File 'lib/plivo/resources/calls.rb', line 492

def speak(call_uuid, text, options = nil)
  valid_param?(:call_uuid, call_uuid, [String, Symbol], true)
  Call.new(@_client, resource_id: call_uuid).speak(text, options)
end

#stop_play(call_uuid) ⇒ Object

Parameters:

  • call_uuid (String)


479
480
481
482
# File 'lib/plivo/resources/calls.rb', line 479

def stop_play(call_uuid)
  valid_param?(:call_uuid, call_uuid, [String, Symbol], true)
  Call.new(@_client, resource_id: call_uuid).stop_play
end

#stop_record(call_uuid, url = nil) ⇒ Object

Parameters:

  • call_uuid (String)
  • url (String) (defaults to: nil)
    • You can specify a record URL to stop only one record. By default all recordings are stopped.



459
460
461
462
# File 'lib/plivo/resources/calls.rb', line 459

def stop_record(call_uuid, url = nil)
  valid_param?(:call_uuid, call_uuid, [String, Symbol], true)
  Call.new(@_client, resource_id: call_uuid).stop_record(url)
end

#stop_speak(call_uuid) ⇒ Object

Parameters:

  • call_uuid (String)


498
499
500
501
# File 'lib/plivo/resources/calls.rb', line 498

def stop_speak(call_uuid)
  valid_param?(:call_uuid, call_uuid, [String, Symbol], true)
  Call.new(@_client, resource_id: call_uuid).stop_speak
end

#update(call_uuid, details) ⇒ Call

Transfer a call

Parameters:

  • call_uuid (String)
  • details (Hash)

Options Hash (details):

  • :legs (String)
    • aleg, bleg or both Defaults to aleg. aleg will transfer call_uuid ; bleg will transfer the bridged leg (if found) of call_uuid ; both will transfer call_uuid and bridged leg of call_uuid

  • :aleg_url (String)
    • URL to transfer for aleg, if legs is aleg or both, then aleg_url has to be specified.

  • :aleg_method (String)
    • HTTP method to invoke aleg_url. Defaults to POST.

  • :bleg_url (String)
    • URL to transfer for bridged leg, if legs is bleg or both, then bleg_url has to be specified.

  • :bleg_method (String)
    • HTTP method to invoke bleg_url. Defaults to POST.

Returns:



421
422
423
424
# File 'lib/plivo/resources/calls.rb', line 421

def update(call_uuid, details)
  valid_param?(:call_uuid, call_uuid, [String, Symbol], true)
  Call.new(@_client, resource_id: call_uuid).update(details)
end