-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart_call_operation.rb
57 lines (53 loc) · 1.9 KB
/
start_call_operation.rb
1
2
3
4
5
6
7
8
9
10
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
50
51
52
53
54
55
56
57
# frozen_string_literal: true
module Twilio
module Rails
module Phone
class StartCallOperation < ::Twilio::Rails::ApplicationOperation
input :tree, accepts: Twilio::Rails::Phone::Tree, type: :keyword, required: true
input :to, accepts: String, type: :keyword, required: true
input :from, accepts: [String, Twilio::Rails::PhoneNumber], type: :keyword, required: false
input :answering_machine_detection, accepts: [true, false], default: true, type: :keyword, required: false
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
::Rails.error.report(e,
handled: false,
context: {
message: "Failed to start Twilio phone call. Got REST error response.",
params: params,
}
)
raise
rescue => e
::Rails.error.report(e,
handled: false,
context: {
message: "Failed to start Twilio phone call. Got unknown error.",
params: params,
}
)
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
end
end
end
end