class Xip::Services::Twilio::ReplyHandler

Constants

ALPHA_ORDINALS

Attributes

recipient_id[R]
reply[R]

Public Class Methods

new(recipient_id: nil, reply: nil) click to toggle source
# File lib/xip/services/twilio/reply_handler.rb, line 12
def initialize(recipient_id: nil, reply: nil)
  @recipient_id = recipient_id
  @reply = reply
end

Public Instance Methods

audio() click to toggle source
# File lib/xip/services/twilio/reply_handler.rb, line 57
def audio
  check_text_length

  format_response({ body: reply['text'], media_url: reply['audio_url'] })
end
delay() click to toggle source
# File lib/xip/services/twilio/reply_handler.rb, line 75
def delay

end
file() click to toggle source
# File lib/xip/services/twilio/reply_handler.rb, line 69
def file
  check_text_length

  format_response({ body: reply['text'], media_url: reply['file_url'] })
end
image() click to toggle source
# File lib/xip/services/twilio/reply_handler.rb, line 51
def image
  check_text_length

  format_response({ body: reply['text'], media_url: reply['image_url'] })
end
text() click to toggle source
# File lib/xip/services/twilio/reply_handler.rb, line 17
def text
  check_text_length

  translated_reply = reply['text']

  suggestions = generate_suggestions(suggestions: reply['suggestions'])
  buttons = generate_buttons(buttons: reply['buttons'])

  if suggestions.present?
    translated_reply = [
      translated_reply,
      'Reply with:'
    ].join("\n\n")

    suggestions.each_with_index do |suggestion, i|
      translated_reply = [
        translated_reply,
        "\"#{ALPHA_ORDINALS[i]}\" for #{suggestion}"
      ].join("\n")
    end
  end

  if buttons.present?
    buttons.each do |button|
      translated_reply = [
        translated_reply,
        button
      ].join("\n\n")
    end
  end

  format_response({ body: translated_reply })
end
video() click to toggle source
# File lib/xip/services/twilio/reply_handler.rb, line 63
def video
  check_text_length

  format_response({ body: reply['text'], media_url: reply['video_url'] })
end

Private Instance Methods

check_text_length() click to toggle source
# File lib/xip/services/twilio/reply_handler.rb, line 81
def check_text_length
  if reply['text'].present? && reply['text'].size > 1600
    raise(ArgumentError, 'Text messages must be 1600 characters or less.')
  end
end
format_response(response) click to toggle source
# File lib/xip/services/twilio/reply_handler.rb, line 87
def format_response(response)
  sender_info = {
    from: Xip.config.twilio.from_phone,
    to: recipient_id
  }
  response.merge(sender_info)
end
generate_buttons(buttons:) click to toggle source
# File lib/xip/services/twilio/reply_handler.rb, line 103
def generate_buttons(buttons:)
  return if buttons.blank?

  sms_buttons = buttons.map do |button|
    case button['type']
    when 'url'
      "#{button['text']}: #{button['url']}"
    when 'payload'
      "To #{button['text'].downcase}: Text #{button['payload'].upcase}"
    when 'call'
      "#{button['text']}: #{button['phone_number']}"
    else # Don't raise for unsupported buttons
      next
    end
  end.compact

  sms_buttons
end
generate_suggestions(suggestions:) click to toggle source
# File lib/xip/services/twilio/reply_handler.rb, line 95
def generate_suggestions(suggestions:)
  return if suggestions.blank?

  mf = suggestions.collect do |suggestion|
    suggestion['text']
  end.compact
end