def main(event): '''Get the users weather from the infromation in the users database''' response = {"type": "success", "text": None, "data": {}} db = event["db"] username = event["username"] user_table = db["users"].find_one(username=username) if (user_table["city"] and user_table["country"]): if user_table["state"]: fetch_str = "{0}, {1}".format(user_table["city"], user_table["state"]) else: fetch_str = "{0}, {1}".format(user_table["city"], user_table["country"]) pyowm_key = tools.load_key("pyowm", db) owm = pyowm.OWM(pyowm_key) observation = owm.weather_at_place(fetch_str) w = observation.get_weather() status = w.get_detailed_status() temperature = w.get_temperature('fahrenheit') weather_str = "Weather for {0} is {1}, with a temperature of {2} F".format( fetch_str, status, temperature["temp"]) response["text"] = weather_str else: # TODO: try to use ip data response["type"] = "error" response["text"] = "Can't find location data for user. If you haven't added your location yet, do so now at " \ "http://willbeddow.com/settings" return response
def __init__(self, notification, db): '''Determine what notifiications to send and send them''' username = notification['username'] user_table = db['users'].find_one(username=username) user_handlers_json = user_table['notifications'] user_notifications = json.loads(user_handlers_json) log.debug("Notifcation handlers for user {0} are {1}".format( username, user_notifications )) for handler in user_notifications: if handler == "email": log.debug("Emailing user {0} notification {1}".format( username, notification )) mailgun_key, mailgun_url = tools.load_key('mailgun', db, load_url=True) msg = notification["value"] if "summary" in notification.keys(): msg_summary = notification["summary"] else: words = msg.split() words_num = len(words) if words_num >= 5: msg_summary = ' '.join(words[0:5]) else: msg_summary = msg msg_summary = "W.I.L.L - "+msg_summary user_email = user_table["email"] first_name = user_table["first_name"] last_name = user_table["last_name"] log.info("Sending email with subject {0} to email {1}".format(msg_summary, user_email)) send_notification.email(mailgun_key, mailgun_url, user_email, msg_summary, msg, first_name, last_name) log.debug("Successfully sent email with subject {0} to email {1}".format(msg_summary, user_email)) #TODO: add options for more notification methods (phone, W.I.L.L-Telegram, etc.)
def main(data): '''Start the search''' response = {"text": None, "data": {}, "type": "success"} query = data["command"] log.info("In main search function with query {0}".format(query)) db = data["db"] answer = False wolfram_key = tools.load_key("wolfram", db) wolfram_response = search_wolfram(query, wolfram_key) # If it found an answer answer will be set to that, if not it'll still be false answer = wolfram_response if answer: response["text"] = answer else: response["text"] = search_google(query) return response
def weather_main(event): '''Get the users weather from the infromation in the users database''' log.info("This function is {0}".format(weather_main)) response = {"type": "success", "text": None, "data": {}} db = event["db"] username = event["username"] user_table = db["users"].find_one(username=username) if (user_table["city"] and user_table["country"]): if user_table["state"]: fetch_str = "{0}, {1}".format(user_table["city"], user_table["state"]) else: fetch_str = "{0}, {1}".format(user_table["city"], user_table["country"]) pyowm_key = tools.load_key("pyowm", db) owm = pyowm.OWM(pyowm_key) observation = owm.weather_at_place(fetch_str) w = observation.get_weather() status = w.get_detailed_status() temp_sym = "F" if "temp_unit" in user_table.keys(): user_temp_unit = user_table["temp_unit"] temperature = w.get_temperature(user_temp_unit) if user_temp_unit == "celsius": temp_sym = "C" else: temperature = w.get_temperature('fahrenheit') weather_str = "Weather for {0} is {1}, with a temperature of {2} {3}".format( fetch_str, status, temperature["temp"], temp_sym) response["text"] = weather_str else: response["type"] = "success" command_id = event["command_id"] session_id = event["session"]["id"] response["text"] = "What city are you in?" #Add a pointer to this function to the event, because for some reason the code in the above function can't find it tools.set_response(session_id, command_id, event, ask_country) response["data"] = {"response": command_id} return response
def test_key_sort(self): key = tools.load_key("wolfram", db) self.assertEqual(True, True)