def compare(request): # Grab locational data from POST request start_lat = request['start_lat'] start_lng = request['start_lng'] end_lat = request['end_lat'] end_lng = request['end_lng'] # Create params for API requests to Lyft and Uber lyft_params = { 'start_lat': start_lat, 'start_lng': start_lng, 'end_lat': end_lat, 'end_lng': end_lng } lyft_headers = {'Authorization': 'bearer ' + LYFT_KEY} uber_params = { 'start_latitude': start_lat, 'start_longitude': start_lng, 'end_latitude': end_lat, 'end_longitude': end_lng } uber_headers = {'Authorization': 'Token ' + UBER_KEY} # Do API Requests to Lyft and Uber lyft_request = requests.get(LYFT_URL, params=lyft_params, headers=lyft_headers) uber_request = requests.get(UBER_URL, params=uber_params, headers=uber_headers) # Check if both requests were succesful if lyft_request.status_code != 200 or uber_request.status_code != 200: return response({'success': False}) # Get estimates from both requests lyft_cost_estimates = lyft_request.json()['cost_estimates'] uber_cost_estimates = uber_request.json()['prices'] # Check to see what is cheaper and send it back to the user lyft_low = sys.maxint uber_low = sys.maxint for estimate in lyft_cost_estimates: cost = (estimate['estimated_cost_cents_min'] / 100) lyft_low = min(lyft_low, int(cost)) for estimate in uber_cost_estimates: cost = estimate['low_estimate'] uber_low = min(uber_low, cost) if lyft_low > uber_low: # Uber is cheaper return response({'success': True, 'winner': 'uber', 'cost': uber_low}) return response({'success': True, 'winner': 'lyft', 'cost': lyft_low})
def response_handler(request): # Grabs JSON request and makes it MessengerParser object for easy use received_message = MessengerParser(request) # Uses translate_message function in google_api_requests.py to translate user message translation = translate_message(received_message.text, TARGET_LANGUAGE) # Uses send_message function in messenger_parser.py to send translated message back to user send_message(received_message.messenger_id, translation) # Ends FB's webhook request with a response with a 200 success code return response()
def feedback_handler(event, _): # Try to decode a JSON document from the body try: body = json.loads(event["body"]) except json.JSONDecodeError: return helpers.message({"message": "Invalid JSON document"}, 400) # Validate the JSON document if "photo" not in body: return helpers.message({"message": "Missing 'photo' key in body"}, 400) if "dog" not in body: return helpers.message({"message": "Missing 'dog' key in body"}, 400) user_dog = body["dog"] # Try to extract the photo from the JSON document try: photo = base64.b64decode(body["photo"]) except binascii.Error: return helpers.message({"message": "Invalid base64 string for 'photo'"}, 400) # Check if the system finds a dog dog = has_dog(photo) # Store if there was a dog or not as a custom metric helpers.metric("DogNoDog", "Dog", int(dog)) # Store the type of error as a custom metric. # FalsePositive: there are no dogs in the picture but the system detected one. # FalseNegative: there is a dog in the picture but the system did not detect it. # Match: the system correctly detected that there is a dog. if dog and not user_dog: helpers.metric("DogNoDog", "FalsePositive", 1) elif not dog and user_dog: helpers.metric("DogNoDog", "FalseNegative", 1) else: helpers.metric("DogNoDog", "Match", 1) # Store the feedback on S3 save_feedback(photo, user_dog, dog) # Send message back to the user return helpers.response({"message": "Feedback received"})
def response_handler(request): # Parse request and get the data we want out of it like messenger_id, text messenger_parser = MessengerParser(request) # Get user object from database so we can see the state of our user. try: user = User.select().where( User.messenger_id == messenger_parser.messenger_id).get() except: # If user doesn't exist, we create them. This would be a first time user. user = User.create(messenger_id=messenger_parser.messenger_id, state='start', score=0) # Here we need to decide what we need to do next for our user if user.state == 'start': # generic intro start(messenger_parser, user) elif user.state == 'question1': question1(messenger_parser, user) elif user.state == 'question2': question2(messenger_parser, user) elif user.state == 'question3': question3(messenger_parser, user) elif user.state == 'question4': question4(messenger_parser, user) elif user.state == 'question5': question5(messenger_parser, user) elif user.state == 'question6': question6(messenger_parser, user) elif user.state == 'question7': question7(messenger_parser, user) elif user.state == 'question8': question8(messenger_parser, user) elif user.state == 'question9': question9(messenger_parser, user) elif user.state == 'question10': question10(messenger_parser, user) else: results_handler(messenger_parser, user) # return the response to Facebook. return response()
def response_handler(request): # Parse request and get the data we want out of it like messenger_id, text, and coordinates. messenger_parser = MessengerParser(request) # Get user object from database so we can see the state of our user. try: user = User.select().where(User.messenger_id == messenger_parser.messenger_id).get() except: # If user doesn't exist, we create them. This would be a first time user. user = User.create(messenger_id=messenger_parser.messenger_id, state='ask_type') # Here we need to decide what we need to do next for our user if user.state == 'ask_type': # ask user what genre type_handler(messenger_parser, user) else: # call api and get results results_handler(messenger_parser, user) # return the response to Facebook. return response()
def random_wine_handler(event, _): tree = ET.parse('systembolaget.xml') root = tree.getroot() wines = [] for artikel in root.findall('artikel'): product_group = artikel.find('Varugrupp').text name = artikel.find('Namn').text product_id = artikel.find('Varnummer').text if "vin" in product_group: wines.append({ "productId": product_id, "name": name, "productGroup": product_group }) wine = random.choice(wines) return helpers.response(wine)
def has_dog_handler(event, _): # Try to decode a JSON document from the body try: body = json.loads(event["body"]) except json.JSONDecodeError: return helpers.message({"message": "Invalid JSON document"}, 400) # Validate the JSON document if "photo" not in body: return helpers.message({"message": "Missing 'photo' key in body"}, 400) # Try to extract the photo from the JSON document try: photo = base64.b64decode(body["photo"]) except binascii.Error: return helpers.message({"message": "Invalid base64 string for 'photo'"}, 400) # Check if there is a dog dog = has_dog(photo) # Store if there was a dog or not as a custom metric helpers.metric("DogNoDog", "Dog", int(dog)) return helpers.response({"dog": dog})
def has_dog_handler(event, _): # TODO: implement logic here return helpers.response("Hello, world!")