-
Notifications
You must be signed in to change notification settings - Fork 1
/
techno.rb
219 lines (180 loc) · 7.83 KB
/
techno.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
require 'sinatra'
require 'json'
require 'net/http'
require 'haml'
require 'windy'
require 'geocoder'
require 'tropo-webapi-ruby'
set :socrata, ENV['SOCRATA_KEY']
use Rack::Session::Pool
post '/index.json' do
v = Tropo::Generator.parse request.env["rack.input"].read
session[:from] = v[:session][:from]
session[:network] = v[:session][:to][:network]
session[:channel] = v[:session][:to][:channel]
t = Tropo::Generator.new
if v[:session][:initial_text]
t.ask :name => 'initial_text', :choices => { :value => "[ANY]"}
session[:zip] = v[:session][:initial_text]
else
t.say "Welcome to techno finder; search for public technology resources in Chicago"
t.ask :name => 'zip', :bargein => true, :timeout => 60, :attempts => 2,
:say => [{:event => "timeout", :value => "Sorry, I did not hear anything."},
{:event => "nomatch:1 nomatch:2", :value => "Oops, that wasnt a five-digit zip code."},
{:value => "Please enter your zip code to search."}],
:choices => { :value => "[5 DIGITS]", :mode => "dtmf"}
end
t.on :event => 'hangup', :next => '/hangup.json'
t.on :event => 'continue', :next => '/process_zip.json'
t.response
end
post '/process_zip.json' do
v = Tropo::Generator.parse request.env["rack.input"].read
t = Tropo::Generator.new
session[:zip] = v[:result][:actions][:zip][:value].gsub(" ","") unless session[:zip]
begin
Windy.app_token = settings.socrata
technology = Windy.views.find_by_id("nen3-vcxj")
places = technology.rows
session[:data] = places.find_all_by_zip(session[:zip])
rescue
t.say "It looks like something went wrong with our data source. Please try again later."
t.hangup
end
if session[:data].size > 0
t.say "Here are #{session[:data].size} locations. Press the location number you want more information about."
items_say = []
session[:data].each_with_index{|item,i| items_say << "Location ##{i+1} #{item.facility}"}
t.ask :name => 'selection', :bargein => true, :timeout => 60, :attempts => 1,
:say => [{:event => "nomatch:1", :value => "That wasn't a one-digit location. Here are your choices: "},
{:value => items_say.join(", ")}], :choices => { :value => "[1 DIGITS]", :mode => "dtmf"}
else
t.say "No public technology resources found in that zip code. Please try again later."
end
t.on :event => 'continue', :next => '/process_selection.json'
t.on :event => 'hangup', :next => '/hangup.json'
t.response
end
post '/process_selection.json' do
v = Tropo::Generator.parse request.env["rack.input"].read
t = Tropo::Generator.new
if v[:result][:actions][:selection][:value]
item = session[:data][v[:result][:actions][:selection][:value].to_i-1]
say_string = ""
say_string += "Information about location #{item.facility} is as follows: "
say_string += "Location: #{item.street_address};"
say_string += "Type: #{item.type};"
say_string += "Hours: #{fix_hours(item.hours)};"
say_string += "Phone: #{item.phone};"
say_string += "Appointment: #{item.appointment};"
say_string += "Internet: #{item.internet ? 'Yes' : 'No'};"
say_string += "WiFi: #{item.wifi ? 'Yes' : 'No'} "
say_string += "Training: #{item.training ? 'Yes' : 'No'};"
t.say say_string
session[:say_string] = "" # storing in a session variable to send it via text message later (if the user wants)
session[:say_string] += "#{item.facility} "
session[:say_string] += "#{item.street_address} "
session[:say_string] += "Type:#{item.type} "
session[:say_string] += "Hours:#{item.hours} "
session[:say_string] += "Phone:#{item.phone} "
session[:say_string] += "Appointment:#{item.appointment} "
session[:say_string] += "Internet:#{item.internet ? 'Yes' : 'No'} "
session[:say_string] += "WiFi:#{item.wifi ? 'Yes' : 'No'} "
session[:say_string] += "Training:#{item.training ? 'Yes' : 'No'} "
t.ask :name => 'send_sms', :bargein => true, :timeout => 60, :attempts => 1,
:say => [{:event => "nomatch:1", :value => "That wasnt a valid answer. "},
{:value => "Would you like to have a text message sent to you?
Press 1 to get a text message; Press 2 to conclude this session."}],
:choices => { :value => "true(1), false(2)", :mode => "dtmf"}
else
t.say "No location with that value. Please try again."
end
t.on :event => 'continue', :next => '/send_text_message.json'
t.on :event => 'hangup', :next => '/hangup.json'
t.response
end
post '/send_text_message.json' do
v = Tropo::Generator.parse request.env["rack.input"].read
t = Tropo::Generator.new
if v[:result][:actions][:number_to_text] # The caller provided a phone # to text message
t.message({
:to => v[:result][:actions][:number_to_text][:value],
:network => "SMS",
:say => {:value => session[:say_string]}})
t.say "Message sent."
else # We dont have a number, so either ask for it if they selected to send a text message, or send to goodbye.json
if v[:result][:actions][:send_sms][:value] == "true"
t.ask :name => 'number_to_text', :bargein => true, :timeout => 60, :required => false, :attempts => 2,
:say => [{:event => "timeout", :value => "Sorry, I did not hear anything."},
{:event => "nomatch:1 nomatch:2", :value => "Oops, that wasn't a 10-digit number."},
{:value => "What 10-digit phone number would you like to send the information to?"}],
:choices => { :value => "[10 DIGITS]", :mode => "dtmf"}
next_url = '/send_text_message.json'
end
end
next_url = '/goodbye.json' if next_url.nil?
t.on :event => 'continue', :next => next_url
t.on :event => 'hangup', :next => '/hangup.json'
t.response
end
post '/goodbye.json' do
v = Tropo::Generator.parse request.env["rack.input"].read
# Create a Tropo::Generator object which is used to build the resulting JSON response
t = Tropo::Generator.new
if session[:channel] == "VOICE"
t.say "That's all. Communication services donated by tropo dot com, data by data dot city of chicago dot org. Have a nice day. Goodbye."
else
t.say "That's all. Communication services donated by http://Tropo.com; data by http://data.cityofchicago.org/"
end
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 fix_hours(hours)
hours.gsub('-', ' to ')
end
def get_technology(zip_code)
Windy.app_token = settings.socrata
technology = Windy.views.find_by_id("nen3-vcxj")
places = technology.rows
if zip_code == "all"
get_technology = places
elsif zip_code == "wifi"
get_technology = places.find_all_by_wifi(true)
else
get_technology = places.find_all_by_zip(zip_code)
end
end
get '/' do
@tech = get_technology("60647")
@items_say = []
@tech.each_with_index{|item,i| @items_say << "Location ##{i+1} #{item.facility}"}
@places = get_technology("all")
haml :index
end
get '/address/:location' do
b = JSON.parse(Net::HTTP.get(URI.parse("http://data.cityofchicago.org/api/views/nen3-vcxj/rows.json")))
if params[:location].nil?
location = [41.864447,-87.644806]
else
location = params[:location]
end
us = Geocoder.coordinates(location)
a = b["data"].min_by {|x| dist(x,us)}
"#{a[1]}"
end
get '/mobile' do
@tech = get_technology("wifi")
erb :mobile, :layout => false
end
get '/map' do
@places = get_technology("all")
erb :map, :layout => false
end
def dist(entry,loc)
entry.last[1..2].map(&:to_f).zip(loc).inject(0) {|s,(c1,c2)| s+(c1-c2)**2}
end