forked from ryanatwork/Yakbus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyakbus.rb
342 lines (250 loc) · 8.79 KB
/
yakbus.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# encoding: utf-8
require 'sinatra'
require 'connexionz'
require 'haml'
require 'tropo-webapi-ruby'
require 'json'
set :sender_phone, ENV['SC_PHONE']
set :va_phone, ENV['VA_PHONE']
set :char_phone, ENV['CHAR_PHONE']
set :spanish_sc, ENV['SPANISH_SC']
set :spanish_va, ENV['SPANISH_VA']
set :spanish_char, ENV['SPANISH_CHAR']
set :tri_phone, ENV['TRI_PHONE']
configure :production do
require 'newrelic_rpm'
end
use Rack::Session::Pool
post '/index.json' do
v = Tropo::Generator.parse request.env["rack.input"].read
session[:from] = v[:session][:from]
session[:to_phone] = v[:session][:to][:name]
session[:network] = v[:session][:to][:network]
session[:channel] = v[:session][:to][:channel]
t = Tropo::Generator.new
t.say "Welcome to yak bus"
t.ask :name => 'digit',
:timeout => 60,
:say => {:value => "Enter the five digit bus stop number"},
:choices => {:value => "[5 DIGITS]",:mode => "dtmf"}
t.on :event => 'continue', :next => '/continue.json'
t.response
end
post '/continue.json' do
v = Tropo::Generator.parse request.env["rack.input"].read
t = Tropo::Generator.new
answer = v[:result][:actions][:digit][:value]
if session[:to_phone] == settings.va_phone
stop = get_et_info('va', answer)
elsif session[:to_phone] == settings.char_phone
stop = get_et_info('char', answer)
elsif session[:to_phone] == settings.tri_phone
stop = get_et_info('tri', answer)
else
stop = get_et_info('sc', answer)
end
t.say(:value => stop)
t.on :event => 'continue', :next => '/next.json'
t.response
end
post '/next.json' do
v = Tropo::Generator.parse request.env["rack.input"].read
t = Tropo::Generator.new
t.ask :name => 'next', :bargein => true, :timeout => 60, :attempts => 1,
:say => [{:event => "nomatch:1", :value => "That wasn't a valid answer. "},
{:value => "Would you like hear another bus stop?
Press 1 for yes; Press 2 to end this call."}],
:choices => { :value => "true(1), false(2)"}
t.on :event => 'continue', :next => '/index.json'
t.on :event => 'hangup', :next => '/hangup.json'
t.response
end
post '/spanish.json' do
v = Tropo::Generator.parse request.env["rack.input"].read
session[:from] = v[:session][:from]
session[:to_phone] = v[:session][:to][:name]
session[:network] = v[:session][:to][:network]
session[:channel] = v[:session][:to][:channel]
t = Tropo::Generator.new
t.say "Bienvenido al bus yak", :voice =>"esperanza"
t.ask :name => 'digit',
:timeout => 60,
:say => {:value => "Introduzca los cinco dígitos del número parada de autobús"},
:voice => "esperanza",
:choices => {:value => "[5 DIGITS]"},
:recognizer => "es-mx"
t.on :event => 'continue', :next => '/continue_spanish.json'
t.response
end
post '/continue_spanish.json' do
v = Tropo::Generator.parse request.env["rack.input"].read
t = Tropo::Generator.new
answer = v[:result][:actions][:digit][:value]
if session[:to_phone] == settings.spanish_va
stop = get_et_info('va', answer)
elsif session[:to_phone] == settings.spanish_char
stop = get_et_info('char', answer)
else
stop = get_et_info('sc', answer)
end
if stop == "No bus stop found"
stop = "No encuentra la parada de autobús"
elsif stop == "No arrivals for next 30 minutes"
stop = "No hay llegadas para los próximos 30 minutos"
elsif stop == "No arrival for next 45 minutes"
stop = "No hay llegadas para los próximos 45 minutos"
else
stop = stop.gsub('Destination', 'Destino')
stop = stop.gsub('Route', 'Ruta')
stop = stop.gsub('minutes', 'minutos')
end
t.say(:value => stop, :voice =>"esperanza")
t.on :event => 'continue', :next => '/next_spanish.json'
t.response
end
post '/next_spanish.json' do
v = Tropo::Generator.parse request.env["rack.input"].read
t = Tropo::Generator.new
t.ask :name => 'next', :bargein => true, :timeout => 60, :attempts => 1,
:say => [{:event => "nomatch:1", :value => "Que no era una respuesta válida. "},
{:value => "¿Te gustaría escuchar otra parada de autobús?
Presione 1 para sí, Pulse 2 para poner fin a esta convocatoria."}],
:choices => { :value => "true(1), false(2)"},
:voice => "esperanza",
:recognizer => "es-mx"
t.on :event => 'continue', :next => '/spanish.json'
t.on :event => 'hangup', :next => '/hangup.json'
t.response
end
post '/sms_incoming.json' do
t = Tropo::Generator.new
v = Tropo::Generator.parse request.env["rack.input"].read
from = v[:session][:to][:id]
initial_text = v[:session][:initial_text]
if from == settings.va_phone.tr('+','')
stop = get_et_info('va', initial_text)
elsif from == settings.char_phone.tr('+','')
stop = get_et_info('char', initial_text)
elsif from == settings.tri_phone.tr('+','')
stop = get_et_info('tri', initial_text)
else
stop = get_et_info('sc', initial_text)
end
t.say(:value => stop)
t.hangup
t.on :event => 'hangup', :next => '/hangup.json'
t.response
end
post '/spanish_sms.json' do
t = Tropo::Generator.new
v = Tropo::Generator.parse request.env["rack.input"].read
from = v[:session][:to][:id]
initial_text = v[:session][:initial_text]
if from == settings.spanish_va.tr('+','')
stop = get_et_info('va', initial_text)
elsif from == settings.spanish_char.tr('+','')
stop = get_et_info('char', initial_text)
else
stop = get_et_info('sc', initial_text)
end
if stop == "No bus stop found"
stop = "No encuentra la parada de autobús"
elsif stop == "No arrivals for next 30 minutes"
stop = "No hay llegadas para los próximos 30 minutos"
elsif stop == "No arrival for next 45 minutes"
stop = "No hay llegadas para los próximos 45 minutos"
else
stop = stop.gsub('Destination', 'Destino')
stop = stop.gsub('Route', 'Ruta')
stop = stop.gsub('minutes', 'minutos')
end
t.say(:value => stop)
t.hangup
t.on :event => 'hangup', :next => '/hangup.json'
t.response
end
post '/hangup.json' do
v = Tropo::Generator.parse request.env["rack.input"].read
puts " Call complete (CDR received). Call duration: #{v[:result][:session_duration]} second(s)"
end
def get_et_info(location,platform)
begin
if location == "va"
@client = Connexionz::Client.new({:endpoint => "http://realtime.commuterpage.com"})
elsif location == "char"
@client = Connexionz::Client.new({:endpoint => "http://avlweb.charlottesville.org"})
elsif location == "tri"
@client = Connexionz::Client.new({:endpoint => "http://70.232.147.132"})
else
@client = Connexionz::Client.new({:endpoint => "http://12.233.207.166"})
end
@platform_info = @client.route_position_et({:platformno => platform})
if @platform_info.route_position_et.platform.nil?
sms_message = "No bus stop found"
else
name = @platform_info.route_position_et.platform.name
arrival_scope = @platform_info.route_position_et.content.max_arrival_scope
sms_message = ""
eta = ""
if @platform_info.route_position_et.platform.route.nil?
sms_message = "No arrivals for next #{arrival_scope} minutes"
elsif @platform_info.route_position_et.platform.route.is_a?(Array)
@platforms = @platform_info.route_position_et.platform.route
@platforms.each do |platform|
if platform.destination.is_a?(Array)
platform.destination.each do |dest|
sms_message += "#{platform.route_no}-#{dest.name}"
sms_message += "-ETA:#{multi_eta(dest.trip)} "
end
else
sms_message += "#{platform.route_no}-#{platform.destination.name}"
sms_message += "-ETA:#{multi_eta(platform.destination.trip)} "
end
end
else
route_no = @platform_info.route_position_et.platform.route.route_no
destination = @platform_info.route_position_et.platform.route.destination.name
eta = multi_eta(@platform_info.route_position_et.platform.route.destination.trip)
sms_message = "#{route_no}-#{destination}-ETA:#{eta}"
end
end
rescue
sms_message = "An error has occured please try again"
end
sms_message.rstrip
end
def multi_eta(eta)
multi_eta = ""
arr_eta = []
if eta.is_a?(Array)
eta.each do |mult_eta|
arr_eta.push(mult_eta.eta)
multi_eta = arr_eta.join(',')
end
else
multi_eta = eta.eta
end
multi_eta
end
##################
### WEB ROUTES ###
##################
get '/' do
haml :root
end
get '/sc/:name' do
#matches "GET /sc/19812"
get_et_info('sc',params[:name])
end
get '/va/:name' do
#matches "GET /va/41215"
get_et_info('va',params[:name])
end
get '/char/:name' do
#matches "GET /char/19812"
get_et_info('char',params[:name])
end
get '/tri/:name' do
#matches "GET /tri/81262"
get_et_info('tri',params[:name])
end