Class: LiveCallRouting::Twilio::V3

Inherits:
Integration
  • Object
show all
Defined in:
app/models/pager_tree/integrations/live_call_routing/twilio/v3.rb

Constant Summary collapse

OPTIONS =
[
  {key: :account_sid, type: :string, default: nil},
  {key: :api_key, type: :string, default: nil},
  {key: :api_secret, type: :string, default: nil},
  {key: :force_input, type: :boolean, default: false},
  {key: :record, type: :boolean, default: false},
  {key: :record_email, type: :string, default: ""}
]
SPEAK_OPTIONS =
{
  language: "en",
  voice: "man"
}

Instance Method Summary collapse

Instance Method Details

#adapter_actionObject



109
110
111
# File 'app/models/pager_tree/integrations/live_call_routing/twilio/v3.rb', line 109

def adapter_action
  :create
end

#adapter_incoming_can_defer?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'app/models/pager_tree/integrations/live_call_routing/twilio/v3.rb', line 101

def adapter_incoming_can_defer?
  false
end

#adapter_outgoing_interest?(event_name) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
# File 'app/models/pager_tree/integrations/live_call_routing/twilio/v3.rb', line 97

def adapter_outgoing_interest?(event_name)
  ["alert_acknowledged", "alert_dropped"].include?(event_name) && adapter_alert.source_id == id
end

#adapter_process_createObject



117
118
119
120
121
122
123
124
125
126
# File 'app/models/pager_tree/integrations/live_call_routing/twilio/v3.rb', line 117

def adapter_process_create
  Alert.new(
    title: _title,
    urgency: urgency,

    thirdparty_id: _thirdparty_id,
    dedup_keys: [],
    additional_data: _additional_datums
  )
end

#adapter_process_outgoingObject



276
277
278
279
280
281
282
283
# File 'app/models/pager_tree/integrations/live_call_routing/twilio/v3.rb', line 276

def adapter_process_outgoing
  event = adapter_outgoing_event.event_name.to_s
  if event == "alert_acknowledged"
    _on_acknowledge
  elsif event == "alert_dropped"
    _on_drop
  end
end

#adapter_process_queue_status_deferredObject



262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'app/models/pager_tree/integrations/live_call_routing/twilio/v3.rb', line 262

def adapter_process_queue_status_deferred
  queue_result = adapter_incoming_request_params.dig("QueueResult")
  adapter_source_log&.sublog("Processing queus status #{queue_result}")

  if queue_result == "hangup"
    self.adapter_alert = alerts.find_by(thirdparty_id: _thirdparty_id)
    adapter_alert.logs.create!(message: "Caller hungup while waiting in queue.")
    adapter_alert.resolve!(self)
    queue_destroy
  end

  adapter_source_log&.save!
end

#adapter_response_disabledObject



194
195
196
197
198
199
# File 'app/models/pager_tree/integrations/live_call_routing/twilio/v3.rb', line 194

def adapter_response_disabled
  _twiml.say(message: "This integration is currently disabled. Goodbye!", **SPEAK_OPTIONS)
  _twiml.hangup

  adapter_controller&.render(xml: _twiml.to_xml)
end

#adapter_response_droppedObject



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'app/models/pager_tree/integrations/live_call_routing/twilio/v3.rb', line 220

def adapter_response_dropped
  recording_url = adapter_incoming_request_params.dig("RecordingUrl")

  if recording_url
    _twiml.play(url: option_no_answer_thank_you_media_url)
    _twiml.hangup

    adapter_alert.additional_data.push(AdditionalDatum.new(format: "link", label: "Voicemail", value: recording_url).to_h)
    adapter_alert.save!

    adapter_alert.logs.create!(message: "Caller left a <a href='#{recording_url}' target='_blank'>voicemail</a>.")

    if option_record_emails.any?
      emails = option_record_emails.map do |x|
        if x == "team"
          Array(adapter_alert.destination_teams.map(&:admin_users).flatten&.map(&:email)) + Array(adapter_alert.destination_teams.map(&:member_users).flatten&.map(&:email))
        elsif x == "team-admin"
          Array(adapter_alert.destination_teams.map(&:admin_users).flatten&.map(&:email))
        elsif x == "on-call"
          adapter_alert.destination_teams.map do |t|
            Array(t.schedule.current_oncall_event_occurrences.map(&:attendees).flatten.map(&:attendee).uniq.map(&:email))
          end
        else
          x
        end
      end.flatten.compact_blank.uniq

      emails.each do |email|
        LiveCallRouting::Twilio::V3Mailer.with(email: email, alert: adapter_alert, from: adapter_incoming_request_params.dig("From"), recording_url: recording_url).call_recording.deliver_later
      end
    end
  elsif option_record
    _twiml.play(url: option_no_answer_media_url)
    _twiml.record(max_length: 60)
  else
    _twiml.say(message: "No one is available to answer this call. Goodbye.", **SPEAK_OPTIONS)
    _twiml.hangup
  end

  adapter_controller.render(xml: _twiml.to_xml)
end

#adapter_response_incomingObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'app/models/pager_tree/integrations/live_call_routing/twilio/v3.rb', line 128

def adapter_response_incoming
  if _teams_size == 0
    _twiml.say(message: "This integration is not configured to route to any teams. Goodbye", **SPEAK_OPTIONS)
    _twiml.hangup
    return adapter_controller&.render(xml: _twiml.to_xml)
  end

  if !adapter_alert.meta["live_call_welcome"] && option_welcome_media.present?
    adapter_alert.logs.create!(message: "Play welcome media to caller.")
    _twiml.play(url: option_welcome_media.url)
    adapter_alert.meta["live_call_welcome"] = true
    adapter_alert.save!
  end

  if selected_team
    adapter_alert.logs.create!(message: "Caller selected team '#{selected_team.name}'.") if _teams_size > 1 || option_force_input

    adapter_alert.logs.create!(message: "Play please wait media to caller.")
    _twiml.play(url: option_please_wait_media_url)
    friendly_name = adapter_alert.id

    # create the queue and save it off
    queue = _client.queues.create(friendly_name: friendly_name)
    adapter_alert.meta["live_call_queue_sid"] = queue.sid

    adapter_alert.destination_teams = [selected_team]

    # save the alert
    adapter_alert.save!

    _twiml.enqueue(
      name: friendly_name,
      action: PagerTree::Integrations::Engine.routes.url_helpers.queue_status_live_call_routing_twilio_v3_path(id, thirdparty_id: _thirdparty_id),
      method: "POST",
      wait_url: PagerTree::Integrations::Engine.routes.url_helpers.music_live_call_routing_twilio_v3_path(id, thirdparty_id: _thirdparty_id),
      wait_url_method: "GET"
    )
    adapter_alert.logs.create!(message: "Enqueue caller in Twilio queue '#{friendly_name}'.")

    # kick off the alert workflow
    adapter_alert.route_later
    adapter_alert.logs.create!(message: "Successfully enqueued alert team workflow.")
  else
    adapter_alert.meta["live_call_repeat_count"] ||= 0
    adapter_alert.meta["live_call_repeat_count"] += 1
    adapter_alert.save!

    if adapter_alert.meta["live_call_repeat_count"] <= 3
      adapter_alert.logs.create!(message: "Caller has not selected a team. Playing team options.")
      _twiml.gather numDigits: _teams_size.to_s.size, timeout: 30 do |g|
        3.times do
          g.say(message: _teams_message, **SPEAK_OPTIONS)
          g.pause(length: 1)
        end
      end
    else
      adapter_alert.logs.create!(message: "Caller input bad input (too many times). Hangup.")
      adapter_alert.resolve!(self)
      _twiml.say(message: "Too much invalid input. Goodbye.", **SPEAK_OPTIONS)
      _twiml.hangup
    end
  end

  adapter_controller&.render(xml: _twiml.to_xml)
end

#adapter_response_maintenance_modeObject



208
209
210
211
212
213
# File 'app/models/pager_tree/integrations/live_call_routing/twilio/v3.rb', line 208

def adapter_response_maintenance_mode
  _twiml.say(message: "This integration is currently in maintenance mode. Goodbye!", **SPEAK_OPTIONS)
  _twiml.hangup

  adapter_controller&.render(xml: _twiml.to_xml)
end

#adapter_response_musicObject



215
216
217
218
# File 'app/models/pager_tree/integrations/live_call_routing/twilio/v3.rb', line 215

def adapter_response_music
  _twiml.play(url: option_music_media_url, loop: 0)
  adapter_controller&.render(xml: _twiml.to_xml)
end

#adapter_response_upgradeObject



201
202
203
204
205
206
# File 'app/models/pager_tree/integrations/live_call_routing/twilio/v3.rb', line 201

def adapter_response_upgrade
  _twiml.say(message: "This account must be upgraded to use live call routing. Goodbye!", **SPEAK_OPTIONS)
  _twiml.hangup

  adapter_controller&.render(xml: _twiml.to_xml)
end

#adapter_supports_incoming?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'app/models/pager_tree/integrations/live_call_routing/twilio/v3.rb', line 89

def adapter_supports_incoming?
  true
end

#adapter_supports_outgoing?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'app/models/pager_tree/integrations/live_call_routing/twilio/v3.rb', line 93

def adapter_supports_outgoing?
  true
end

#adapter_thirdparty_idObject



113
114
115
# File 'app/models/pager_tree/integrations/live_call_routing/twilio/v3.rb', line 113

def adapter_thirdparty_id
  _thirdparty_id
end

#adapter_will_route_alert?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'app/models/pager_tree/integrations/live_call_routing/twilio/v3.rb', line 105

def adapter_will_route_alert?
  true
end

#option_connect_now_media_urlObject



41
42
43
# File 'app/models/pager_tree/integrations/live_call_routing/twilio/v3.rb', line 41

def option_connect_now_media_url
  option_connect_now_media.present? ? option_connect_now_media.url : URI.join(Rails.application.routes.url_helpers.root_url, "audios/you-are-now-being-connected.mp3").to_s
end

#option_music_media_urlObject



45
46
47
# File 'app/models/pager_tree/integrations/live_call_routing/twilio/v3.rb', line 45

def option_music_media_url
  option_music_media.present? ? option_music_media.url : "http://com.twilio.sounds.music.s3.amazonaws.com/oldDog_-_endless_goodbye_%28instr.%29.mp3"
end

#option_no_answer_media_urlObject



53
54
55
# File 'app/models/pager_tree/integrations/live_call_routing/twilio/v3.rb', line 53

def option_no_answer_media_url
  option_no_answer_media.present? ? option_no_answer_media.url : URI.join(Rails.application.routes.url_helpers.root_url, "audios/no-answer.mp3").to_s
end

#option_no_answer_thank_you_media_urlObject



57
58
59
# File 'app/models/pager_tree/integrations/live_call_routing/twilio/v3.rb', line 57

def option_no_answer_thank_you_media_url
  option_no_answer_thank_you_media.present? ? option_no_answer_thank_you_media.url : URI.join(Rails.application.routes.url_helpers.root_url, "audios/thanks-for-message.mp3").to_s
end

#option_please_wait_media_urlObject



49
50
51
# File 'app/models/pager_tree/integrations/live_call_routing/twilio/v3.rb', line 49

def option_please_wait_media_url
  option_please_wait_media.present? ? option_please_wait_media.url : URI.join(Rails.application.routes.url_helpers.root_url, "audios/please-wait.mp3").to_s
end

#option_record_emailsObject



65
66
67
# File 'app/models/pager_tree/integrations/live_call_routing/twilio/v3.rb', line 65

def option_record_emails
  self.option_record_email.split(",")
end

#option_record_emails=(x) ⇒ Object



61
62
63
# File 'app/models/pager_tree/integrations/live_call_routing/twilio/v3.rb', line 61

def option_record_emails=(x)
  self.option_record_email = Array(x).join(",")
end

#option_record_emails_listObject



81
82
83
# File 'app/models/pager_tree/integrations/live_call_routing/twilio/v3.rb', line 81

def option_record_emails_list
  option_record_emails
end

#option_record_emails_list=(x) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'app/models/pager_tree/integrations/live_call_routing/twilio/v3.rb', line 69

def option_record_emails_list=(x)
  # what comes in as json, via tagify
  uniq_array = []
  begin
    uniq_array = JSON.parse(x).map { |y| y["value"] }.uniq
  rescue JSON::ParserError => exception
    Rails.logger.debug(exception)
  end

  self.option_record_emails = uniq_array
end

#validate_record_emailsObject



85
86
87
# File 'app/models/pager_tree/integrations/live_call_routing/twilio/v3.rb', line 85

def validate_record_emails
  errors.add(:record_emails, "must be a valid email") if option_record_emails.any? { |x| !(x.match(URI::MailTo::EMAIL_REGEXP) || ["team", "team-admin", "on-call"].include?(x)) }
end