def weather_temperature(req): """Returns a string containing text with a response to the user with a indication if temperature provided is consisting with the current weather or a prompt for more information Takes a city, temperature and (optional) dates. Temperature ranges for hot, cold, chilly and warm can be configured in config.py uses the template responses found in weather_responses.py as templates """ parameters = req['queryResult']['parameters'] # validate request parameters, return an error if there are issues error, forecast_params = validate_params(parameters) if error: return error # If the user didn't specify a temperature, get the weather for them if not forecast_params['temperature']: return weather(req) # create a forecast object which retrieves the forecast from a external API try: forecast = Forecast(forecast_params) # return an error if there is an error getting the forecast except (ValueError, IOError) as error: return error return forecast.get_temperature_response()
def weather_condition(req): """Returns a string containing a human-readable response to the user with the probability of the provided weather condition occurring or a prompt for more information Takes a city, condition and (optional) dates uses the template responses found in weather_responses.py as templates and the conditions listed in weather_entities.py """ # validate request parameters, return an error if there are issues error, forecast_params = validate_params(req['queryResult']['parameters']) if error: return error # Check to make sure there is a activity, if not return an error if not forecast_params['condition']: return 'What weather condition would you like to check?' # create a forecast object which retrieves the forecast from a external API try: forecast = Forecast(forecast_params) # return an error if there is an error getting the forecast except (ValueError, IOError) as error: return error # get the response return forecast.get_condition_response()
def weather_outfit(req): """Returns a string containing text with a response to the user with a indication if the outfit provided is appropriate for the current weather or a prompt for more information Takes a city, outfit and (optional) dates uses the template responses found in weather_responses.py as templates and the outfits listed in weather_entities.py """ # validate request parameters, return an error if there are issues error, forecast_params = validate_params(req['queryResult']['parameters']) if error: return error # Validate that there are the required parameters to retrieve a forecast if not forecast_params['outfit']: return 'What are you planning on wearing?' # create a forecast object which retrieves the forecast from a external API try: forecast = Forecast(forecast_params) # return an error if there is an error getting the forecast except (ValueError, IOError) as error: return error return forecast.get_outfit_response()
def weather_activity(req): """Returns a string containing text with a response to the user with a indication if the activity provided is appropriate for the current weather or a prompt for more information Takes a city, activity and (optional) dates uses the template responses found in weather_responses.py as templates and the activities listed in weather_entities.py """ # validate request parameters, return an error if there are issues error, forecast_params = validate_params(req['queryResult']['parameters']) if error: return error # Check to make sure there is a activity, if not return an error if not forecast_params['activity']: return 'What activity were you thinking of doing?' # create a forecast object which retrieves the forecast from a external API try: forecast = Forecast(forecast_params) # return an error if there is an error getting the forecast except (ValueError, IOError) as error: return error # get the response return forecast.get_activity_response()
def weather(req): """Returns a string containing text with a response to the user with the weather forecast or a prompt for more information Takes the city for the forecast and (optional) dates uses the template responses found in weather_responses.py as templates """ parameters = req['queryResult']['parameters'] print('Dialogflow Parameters:') print(json.dumps(parameters, indent=4)) # validate request parameters, return an error if there are issues error, forecast_params = validate_params(parameters) if error: print(error, forecast_params) return str(error) # create a forecast object which retrieves the forecast from a external API try: forecast = Forecast(forecast_params) # return an error if there is an error getting the forecast except (ValueError, IOError) as error: print('Forecast error') print(error, forecast_params) return str(error) # If the user requests a datetime period (a date/time range), get the # response if forecast.datetime_start and forecast.datetime_end: response = forecast.get_datetime_period_response() # If the user requests a specific datetime, get the response elif forecast.datetime_start: response = forecast.get_datetime_response() # If the user doesn't request a date in the request get current conditions else: response = forecast.get_current_response() return response
def weather(req): """Returns a string containing text with a response to the user with the weather forecast or a prompt for more information Takes the city for the forecast and (optional) dates uses the template responses found in weather_responses.py as templates """ parameters = req['queryResult']['parameters'] print('Dialogflow Parameters:') print(json.dumps(parameters, indent=4)) # validate request parameters, return an error if there are issues error, forecast_params = validate_params(parameters) if error: return error # create a forecast object which retrieves the forecast from a external API try: forecast = Forecast(forecast_params) # return an error if there is an error getting the forecast except (ValueError, IOError) as error: return error # If the user requests a datetime period (a date/time range), get the # response if forecast.datetime_start and forecast.datetime_end: response = forecast.get_datetime_period_response() # If the user requests a specific datetime, get the response elif forecast.datetime_start: response = forecast.get_datetime_response() # If the user doesn't request a date in the request get current conditions else: response = forecast.get_current_response() return response