Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating the format of the tipline message for the Civic API integration. #2118

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions app/models/bot/smooch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -412,11 +412,14 @@ def self.parse_message_based_on_state(message, app_id)
resource_id = Rails.cache.read("smooch_resource_waiting_for_user_input:#{uid}")
resource = TiplineResource.where(team_id: self.config['team_id'].to_i, id: resource_id.to_i).last
if resource.nil?
self.send_message_to_user(uid, 'Sorry, this request has expired. Please start again.') # FIXME: Localize this
self.send_message_to_user_with_main_menu_appended(uid, 'Sorry, this request has expired. Please start again.', workflow, language)
self.clear_user_bundled_messages(uid)
else
response = resource.handle_user_input(message)
self.send_message_to_user(uid, response) unless response.blank?
unless response.blank?
self.send_message_to_user(uid, response)
self.send_message_to_user_with_main_menu_appended(uid, 'Use the buttons to navigate.', workflow, language)
end
self.bundle_message(message)
self.delay_for(1.seconds, { queue: 'smooch', retry: false }).bundle_messages(uid, message['_id'], app_id, 'resource_requests', resource)
end
Expand Down
36 changes: 31 additions & 5 deletions app/models/tipline_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,46 @@ def format_as_tipline_message
def handle_user_input(message)
response = nil
if self.content_type == 'dynamic'

# FIXME: Here, it currently just supports Google Civic API, but, if it becomes a feature, it should support different external APIs. Since it's just a prototype, I didn't bother to localize the strings.
return 'This does not look like a valid address. Please try again later with a valid one.' if message['text'].to_s.size < 5 # At least a ZIP code

if CheckConfig.get('google_api_key')
begin
location_template = <<-TEXT.gsub(/^[\s\t]*/, '')
*%{name}*
📍 %{line} - %{city}, %{state} - %{zip}
📅 %{date}
🕒 %{hours}
TEXT

format_location = proc { |location|
(location_template.strip % {
name: location.dig('address', 'locationName').to_s.titleize,
line: location.dig('address', 'line1').to_s.titleize,
city: location.dig('address', 'city').to_s.titleize,
state: location.dig('address', 'state').to_s.upcase,
zip: location.dig('address', 'zip').to_s,
date: [location['startDate'].to_s, location['endDate'].to_s].uniq.map{ |date| Date.parse(date).strftime("%b %d, %Y") }.join(' - '),
hours: location['pollingHours'].to_s.split("\n").select{ |date| date =~ /#{Time.now.strftime("%b")}/ }.join(' / ')
}).strip + "\n"
}

url = "https://www.googleapis.com/civicinfo/v2/voterinfo?key=#{CheckConfig.get('google_api_key')}&address=#{ERB::Util.url_encode(message['text'].to_s.gsub(/\s+/, ' ').gsub(',', ''))}&electionId=9000"
data = JSON.parse(Net::HTTP.get(URI(url)))
return 'Nothing found. Please try again later with this same address or another address.' unless data.has_key?('pollingLocations')
output = ['Here are some polling locations and some early vote sites:', '', '*POLLING LOCATIONS*', '']
top_polling_locations = data.dig('pollingLocations').first(5)
top_polling_locations.each { |location| output << location['address'].values.reject{ |value| value.blank? }.map(&:titleize).join("\n") + "\n" }
output.concat(['', '*EARLY VOTE SITES*', ''])

output = ['Here are some polling locations and some early vote sites:', '']
output.concat(['*EARLY VOTE SITES*', ''])
top_early_vote_sites = data.dig('earlyVoteSites').to_a.first(5)
top_early_vote_sites.each { |location| output << location['address'].values.reject{ |value| value.blank? }.map(&:titleize).join("\n") + "\n" }
top_early_vote_sites.each { |location| output << format_location.call(location) }
output << "Early vote sites not available.\n" if top_early_vote_sites.blank?

output.concat(['*POLLING LOCATIONS*', ''])
top_polling_locations = data.dig('pollingLocations').first(5)
top_polling_locations.each { |location| output << format_location.call(location) }
response = output.join("\n")

rescue StandardError => e
CheckSentry.notify(e, bot: 'Smooch', context: 'Google Civic API')
response = 'Some error happened. Please try again later.'
Expand Down
Loading