Class: Twilio::Rails::Phone::StartCallOperation

Inherits:
ApplicationOperation show all
Defined in:
app/operations/twilio/rails/phone/start_call_operation.rb

Instance Method Summary collapse

Instance Method Details

#executeObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/operations/twilio/rails/phone/start_call_operation.rb', line 11

def execute
  from = if self.from.is_a?(Twilio::Rails::PhoneNumber)
    self.from.number
  elsif self.from.present?
    self.from
  else
    Twilio::Rails.config.default_outgoing_phone_number
  end

  params = {
    "CallSid" => nil,
    "direction" => "outbound",
    "To" => from,
    "From" => to,
  }

  begin
    sid = Twilio::Rails::Client.start_call(url: tree.outbound_url, to: to, from: from, answering_machine_detection: answering_machine_detection)
    params["CallSid"] = sid
  rescue Twilio::REST::TwilioError => e
    Twilio::Rails.notify_exception(e,
      message: "Failed to start Twilio phone call. Got REST error response.",
      context: { params: params },
      exception_binding: binding
    )
    raise
  rescue => e
    Twilio::Rails.notify_exception(e,
      message: "Failed to start Twilio phone call. Got unknown error.",
      context: { params: params },
      exception_binding: binding
    )
    raise
  end

  # TODO: I think this may be a race condition
  phone_call = Twilio::Rails::Phone::CreateOperation.call(params: params, tree: tree)
  phone_call
end