forked from codeforamerica/balance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
191 lines (177 loc) · 7.32 KB
/
app.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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
require 'sinatra'
require 'sinatra/json'
require 'twilio-ruby'
require 'rack/ssl'
require 'active_support/core_ext/time'
require File.expand_path('../lib/twilio_service', __FILE__)
require File.expand_path('../lib/state_handler', __FILE__)
require File.expand_path('../lib/phone_number_processor', __FILE__)
require File.expand_path('../lib/message_generator', __FILE__)
require File.expand_path('../lib/balance_log_analyzer', __FILE__)
class EbtBalanceSmsApp < Sinatra::Base
use Rack::SSL unless settings.environment == :development or settings.environment == :test
if settings.environment == :production
set :url_scheme, 'https'
else
set :url_scheme, 'http'
end
set :phone_number_processor, PhoneNumberProcessor.new
configure :production do
require 'newrelic_rpm'
end
helpers do
def valid_phone_number?(phone_number)
contains_good_number_of_phone_digits?(phone_number) && !one_of_our_twilio_numbers?(phone_number)
end
def contains_good_number_of_phone_digits?(phone_number)
is_it_valid = (phone_number.length == 10) || (phone_number.length == 11 && phone_number[0] == '1')
is_it_valid
end
def one_of_our_twilio_numbers?(phone_number)
formatted_phone_number = convert_to_e164_phone_number(phone_number)
settings.phone_number_processor.twilio_number?(formatted_phone_number)
end
def convert_to_e164_phone_number(phone_number)
if phone_number.length == 10
'+1' + phone_number
elsif phone_number.length == 11
'+' + phone_number
end
end
end
before do
puts "Request details — #{request.request_method} #{request.url}" unless settings.environment == :test
end
post '/' do
twilio_service = TwilioService.new(Twilio::REST::Client.new(ENV['TWILIO_SID'], ENV['TWILIO_AUTH']))
texter_phone_number = params["From"]
inbound_twilio_number = params["To"]
state_abbreviation = params["ToState"] || "no_state_abbreviation_received"
state_handler = StateHandler.for(state_abbreviation)
ebt_number = state_handler.extract_valid_ebt_number_from_text(params["Body"])
twiml_url = "#{settings.url_scheme}://"
twiml_url << "#{request.env['HTTP_HOST']}/get_balance"
twiml_url << "?phone_number=#{texter_phone_number}"
twiml_url << "&twilio_phone_number=#{inbound_twilio_number}"
twiml_url << "&state=#{state_abbreviation}"
twiml_url << "&ebt_number=#{ebt_number}"
twiml_url << "&balance_check_id=#{SecureRandom.hex}"
language = settings.phone_number_processor.language_for(inbound_twilio_number)
message_generator = MessageGenerator.new(language)
# Need to rescue Twilio API errors
begin
if ebt_number != :invalid_number
twilio_service.send_text(
to: texter_phone_number,
from: inbound_twilio_number,
body: message_generator.thanks_please_wait
)
twilio_service.make_call(
url: twiml_url,
to: state_handler.phone_number,
from: inbound_twilio_number,
method: "GET"
)
elsif params["Body"].downcase.include?('about')
twilio_service.send_text(
to: texter_phone_number,
from: inbound_twilio_number,
body: message_generator.more_info
)
else
twilio_service.send_text(
to: texter_phone_number,
from: inbound_twilio_number,
body: message_generator.sorry_try_again(state_handler.allowed_number_of_ebt_card_digits)
)
end
rescue Twilio::REST::RequestError => e
puts "Twilio API request error - \"#{e.message}\""
end
end
get '/get_balance' do
phone_number = params[:phone_number].strip
twilio_number = params[:twilio_phone_number].strip
balance_check_id = params[:balance_check_id]
state = params[:state]
state_handler = StateHandler.for(state)
Twilio::TwiML::Response.new do |r|
r.Play digits: state_handler.button_sequence(params['ebt_number'])
r.Record transcribe: true,
transcribeCallback: "#{settings.url_scheme}://#{request.env['HTTP_HOST']}/#{state}/#{phone_number}/#{twilio_number}/#{balance_check_id}/send_balance",
timeout: 10,
maxLength: state_handler.max_message_length
end.text
end
post '/get_balance' do
# Twilio posts unused data here; necessary simply to avoid 404 error in logs
response = <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<Response>
</Response>
EOF
end
post '/:state/:to_phone_number/:from_phone_number/:balance_check_id/send_balance' do
twilio_phone_number = params[:from_phone_number]
language = settings.phone_number_processor.language_for(twilio_phone_number)
handler = StateHandler.for(params[:state])
processed_balance_response_for_user = handler.transcribe_balance_response(params["TranscriptionText"], language)
twilio_service = TwilioService.new(Twilio::REST::Client.new(ENV['TWILIO_SID'], ENV['TWILIO_AUTH']))
twilio_service.send_text(
to: params[:to_phone_number].strip,
from: params[:from_phone_number],
body: processed_balance_response_for_user
)
end
post '/voice_call' do
twilio_service = TwilioService.new(Twilio::REST::Client.new(ENV['TWILIO_SID'], ENV['TWILIO_AUTH']))
caller_phone_number = params["From"]
inbound_twilio_number = params["To"]
state_handler = StateHandler.for(params["ToState"])
language = settings.phone_number_processor.language_for(inbound_twilio_number)
message_generator = MessageGenerator.new(language)
response = <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Gather timeout="10" action="http://twimlets.com/forward?PhoneNumber=#{state_handler.phone_number}" method="GET" numDigits="1">
<Play>#{message_generator.call_in_voice_file_url}</Play>
</Gather>
<Redirect method="GET">http://twimlets.com/forward?PhoneNumber=#{state_handler.phone_number}</Redirect>
</Response>
EOF
end
post '/welcome' do
puts "/welcome request params: #{params}" unless settings.environment == :test
digits_only_input = params['texter_phone_number'].gsub(/\D/, "")
if valid_phone_number?(digits_only_input)
formatted_phone_number = convert_to_e164_phone_number(digits_only_input)
inbound_twilio_number = params["inbound_twilio_number"]
language = settings.phone_number_processor.language_for(inbound_twilio_number)
message_generator = MessageGenerator.new(language)
twilio_service = TwilioService.new(Twilio::REST::Client.new(ENV['TWILIO_SID'], ENV['TWILIO_AUTH']))
twilio_service.send_text(
to: formatted_phone_number,
from: inbound_twilio_number,
body: message_generator.welcome
)
"Great! I just sent you a text message with instructions. I hope you find this service useful!"
else
"Sorry! That number doesn't look right. Please go back and try again."
end
end
get '/.well-known/status' do
client = Twilio::REST::Client.new(ENV['TWILIO_SID'], ENV['TWILIO_AUTH'])
messages = client.account.messages.list
delay_analysis = BalanceLogAnalyzer::DelayedBalanceResponseAnalysis.new(messages)
response_hash = Hash.new
response_hash[:dependencies] = [ "twilio" ]
response_hash[:updated] = Time.now.to_i
response_hash[:resources] = {}
if delay_analysis.messages_delayed?
response_hash[:status] = delay_analysis.problem_description
else
response_hash[:status] = 'ok'
end
json response_hash
end
end