This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Look inside the hash to find coordinates | |
| def extract_coordinates(parsed) | |
| parsed['results'].first['geometry']['location'] | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Bot.on :message do |message| | |
| puts "Received '#{message.inspect}' from #{message.sender}" # debug purposes | |
| parsed_response = get_parsed_response(API_URL, message.text) # talk to Google API | |
| message.type # trick user into thinking we type something with our fingers, HA HA HA | |
| coord = extract_coordinates(parsed_response) # we have a separate method for that | |
| message.reply(text: "Latitude: #{coord['lat']}, Longitude: #{coord['lng']}") | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def process_coordinates | |
| Bot.on :message do |message| | |
| puts "Received '#{message.inspect}' from #{message.sender}" # debug purposes | |
| parsed_response = get_parsed_response(API_URL, message.text) # talk to Google API | |
| unless parsed_response # The input was unintelligible | |
| message.reply(text: "I did not quite get that. Bye!") | |
| return # bye bye | |
| end | |
| message.type # trick user into thinking we type something with our fingers, HA HA HA | |
| coord = extract_coordinates(parsed_response) # we have a separate method for that |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def wait_for_user_input | |
| Bot.on :message do |message| | |
| case message.text | |
| when /coord/i, /gps/i # we use regexp to match parts of strings | |
| message.reply(text: 'Enter destination') | |
| process_coordinates | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def process_coordinates | |
| Bot.on :message do |message| | |
| parsed_response = get_parsed_response(API_URL, message.text) | |
| if !parsed_response | |
| message.reply(text: 'There were no resutls. Ask me again, please') | |
| wait_for_user_input | |
| return # we need an early return if something went wrong, though your bot server will complain | |
| end | |
| message.type # let user know we're doing something | |
| coord = extract_coordinates(parsed_response) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def wait_for_user_input | |
| Bot.on :message do |message| | |
| case message.text | |
| when /coord/i, /gps/i # we use regexp to match parts of strings | |
| message.reply(text: 'Enter destination') | |
| process_coordinates | |
| when /full ad/i # we got the user even if he misspells address | |
| message.reply(text: 'Enter destination') | |
| show_full_address | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def show_full_address | |
| Bot.on :message do |message| | |
| parsed_response = get_parsed_response(API_URL, message.text) | |
| if !parsed_response | |
| message.reply(text: 'There were no resutls. Ask me again, please') | |
| wait_for_user_input | |
| return | |
| end | |
| message.type # let user know we're doing something | |
| full_address = extract_full_address(parsed_response) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def extract_full_address(parsed) | |
| parsed['results'].first['formatted_address'] | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def handle_api_request | |
| Bot.on :message do |message| | |
| puts "Received '#{message.inspect}' from #{message.sender}" # for development only | |
| parsed_response = get_parsed_response(API_URL, message.text) | |
| unless parsed_response | |
| message.reply(text: 'There were no resutls. Ask me again, please') | |
| wait_for_user_input | |
| return | |
| end | |
| message.type # let user know we're doing something |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def process_coordinates | |
| handle_api_request do |api_response, message| | |
| coord = extract_coordinates(api_response) | |
| message.reply(text: "Latitude: #{coord['lat']} / Longitude: #{coord['lng']}") | |
| end | |
| end | |
| def show_full_address | |
| handle_api_request do |api_response, message| | |
| full_address = extract_full_address(api_response) |