Class: Plivo::Resources::CallInterface

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

Constant Summary

Constants included from Utils

Utils::TYPE_WHITELIST

Instance Method Summary collapse

Methods included from Utils

GetSortedQueryParamString?, compute_signatureV3?, expected_type?, expected_value?, generate_url?, getMapFromQueryString?, is_one_among_string_url?, multi_valid_param?, raise_invalid_request, valid_account?, valid_date_format?, valid_mainaccount?, valid_multiple_destination_integers?, valid_multiple_destination_nos?, valid_param?, valid_range?, valid_signature?, valid_signatureV3?, valid_subaccount?, valid_url?

Constructor Details

#initialize(client, resource_list_json = nil) ⇒ CallInterface

Returns a new instance of CallInterface.



245
246
247
248
249
250
251
# File 'lib/plivo/resources/calls.rb', line 245

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

Instance Method Details

#cancel_request(call_uuid) ⇒ Object

Parameters:

  • call_uuid (String)


576
577
578
579
# File 'lib/plivo/resources/calls.rb', line 576

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, options = nil) ⇒ Call

Makes an outbound call

Parameters:

  • from (String)
  • to (Array)
  • answer_url (String)
  • 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:



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/plivo/resources/calls.rb', line 281

def create(from, to, answer_url, 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)


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

  return perform_create(params, false) if options.nil?

  perform_create(params.merge(options), false)
end

#delete(call_uuid) ⇒ Object

Parameters:

  • call_uuid (String)


487
488
489
490
# File 'lib/plivo/resources/calls.rb', line 487

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

#eachObject



402
403
404
405
406
407
408
409
410
# File 'lib/plivo/resources/calls.rb', line 402

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



461
462
463
464
# File 'lib/plivo/resources/calls.rb', line 461

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

#each_queuedObject



466
467
468
469
# File 'lib/plivo/resources/calls.rb', line 466

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)


304
305
306
307
# File 'lib/plivo/resources/calls.rb', line 304

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)


310
311
312
# File 'lib/plivo/resources/calls.rb', line 310

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

#get_queued(call_uuid) ⇒ Object

Parameters:

  • call_uuid (String)


315
316
317
# File 'lib/plivo/resources/calls.rb', line 315

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.

  • :parent_call_uuid (String)
    • Filter the results by parent call uuid.

  • :hangup_source (String)
    • Filter the results by hangup source

  • :hangup_cause_code (String)
    • Filter the results by hangup cause code

  • :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.



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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/plivo/resources/calls.rb', line 347

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

  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

  # initial list of possible params
  params = %i[
    bill_duration
    bill_duration__gt
    bill_duration__gte
    bill_duration__lt
    bill_duration__lte
    call_direction
    end_time
    end_time__gt
    end_time__gte
    end_time__lt
    end_time__lte
    from_number
    hangup_cause_code
    hangup_source
    limit
    offset
    parent_call_uuid
    subaccount
    to_number
    stir_verification
  ].reduce({}) do |result_hash, param|
    if options.key?(param)
      if param == :call_direction
        if valid_param?(:call_direction, options[:call_direction],
            [String, Symbol], true, %w[inbound outbound])
          result_hash[:call_direction] = options[:call_direction]
        end
      elsif %i[offset limit hangup_cause_code].include?(param)
        if valid_param?(param, options[param], [Integer, Integer], true)
          result_hash[param] = options[param]
        end
      elsif valid_param?(param, options[param], [String, Symbol], true)
        result_hash[param] = options[param]
      end
    end

    result_hash
  end

  perform_list(params)
end

#list_live(options = nil) ⇒ Object

Parameters:

  • options (Hash) (defaults to: nil)

Options Hash (options):

  • :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}



420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
# File 'lib/plivo/resources/calls.rb', line 420

def list_live(options = nil)

  if options.nil?
    options = {}
  else
    valid_param?(:options, options, Hash, true)
  end

  params = {}
  params[:status] = 'live'
  params_expected = %i[
    from_number to_number
  ]
  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

  perform_list_without_object(params)
  {
    api_id: @api_id,
    calls: @calls
  }
end

#list_queuedObject



453
454
455
456
457
458
459
# File 'lib/plivo/resources/calls.rb', line 453

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.



536
537
538
539
540
# File 'lib/plivo/resources/calls.rb', line 536

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.



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

def record(call_uuid, options = nil)
  valid_param?(:call_uuid, call_uuid, [String, Symbol], true)
  response = Call.new(@_client, resource_id: call_uuid).record(options)
  return Base::Response.new(Hash["api_id" => response.api_id,
                                 "recording_id" => response.recording_id,
                                 "message" => response.message,
                                 "url" => response.url])
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.



570
571
572
573
# File 'lib/plivo/resources/calls.rb', line 570

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.



556
557
558
559
# File 'lib/plivo/resources/calls.rb', line 556

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)


543
544
545
546
# File 'lib/plivo/resources/calls.rb', line 543

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.



523
524
525
526
# File 'lib/plivo/resources/calls.rb', line 523

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)


562
563
564
565
# File 'lib/plivo/resources/calls.rb', line 562

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:



481
482
483
484
# File 'lib/plivo/resources/calls.rb', line 481

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