def get_emails(): try: gmail_url = "imap.gmail.com" connection = imaplib.IMAP4_SSL(gmail_url) email_address, password = get_login_details() connection.login(email_address, password) connection.select("INBOX") # retrieve all the unread email id's (return_code, emails) = connection.search(None, '(UNSEEN)') unread_email_numbers = emails[0].split() print(unread_email_numbers) # retrieve the latest unread email latest_unread_number = unread_email_numbers[-1] print(latest_unread_number) category, data = connection.fetch(latest_unread_number, "(RFC822)") for response_part in data: if isinstance(response_part, tuple): latest_unread = email.message_from_bytes(response_part[1]) latest_unread_from = latest_unread['from'].split()[0] latest_unread_subject = latest_unread["subject"] latest_unread_body = get_email_body(latest_unread) # format these for speaking email_details = "the last un read email is from {} with the subject {} and main body {}".format( latest_unread_from, latest_unread_subject, latest_unread_body) print(email_details) return email_details except: speak.speak_to_user("your inbox is empty") return None
def main(): # dictionary with the command mapped to the keywords needed to activate it commands_available = { "to turn off the smart plug" : "turn plug off", "to turn on the smart plug": "turn plug off", "to get the news headlines" : "news", "to get the weather forecast" : "weather", "to send an email to alex" : "send email alex", "to read the email inbox" : "read email", "to play a song": "play and the songs name", "to pause the song" : "hotword", "to play the song" : "play song", "to stop the song" : "stop song", "to shuffle your playlist": "shuffle playlist", "to translate a sentence" : "translate followed by the sentence and then to followed by the destination language", "to search wikipedia": "what is and the search term", "to turn on the candle" : "candle on", "to turn off the candle" :"candle off" } help_information = "" for (command, key_words) in commands_available.items(): help_information = help_information + ("{} say a sentence consisting of {}".format(command, key_words)) # hotword detection in background while speaking subprocess.Popen(["python3", "/home/pi/2019-ca400-randlea2/src/hotword.py","&"]) speak.speak_to_user(help_information)
def finish_pair(num_devices): # wait 65 seconds for them to reset zigbee device time.sleep(65) new_num_devices = get_num_devices(device_json_file) # check if theirs a new device in file if new_num_devices > num_devices: speak.speak_to_user("Device paired successfully") else: speak.speak_to_user("Unable to pair device please try again")
def main(spoken_words): # parse the destination language and sentence from user input destination_language, sentence = query_user(spoken_words) print(destination_language) # translate sentence and speak to user translation = translate(destination_language, sentence) speak.speak_to_user(translation) # start hotword again hotword.detect_hotword()
def read_emails(): unread_email = get_emails() if unread_email is None: speak.speak_to_user("there are no unread emails") else: print(unread_email) speak.speak_to_user(unread_email) hotword.detect_hotword() return None
def get_login_details(): try: login_details_file = open(login_file) login_details = json.load(login_details_file) email_address = login_details["email_address"] password = login_details["password"] return email_address, password except: speak.speak_to_user("problem connecting to g mail please try again") return None
def query_user(spoken_words): if spoken_words is None: spoken_words = sys.argv[1:] language = search_languages(spoken_words) sentence = find_sentence(spoken_words) print(sentence, language) print(spoken_words) if language is None: speak.speak_to_user("language not found") hotword.detect_hotword() return language, sentence
def check_security(): # connection to database which stores all security information with sqlite3.connect(security_db) as connection: cursor = connection.cursor() cursor.execute("SELECT * FROM security") # return the universal or personal model depending on settings hotword_file = cursor.fetchall()[0][1] if hotword_file == "snowboy.pmdl": speak.speak_to_user("security setting is currently on") else: speak.speak_to_user("security setting is currently off") return None
def speak_activation_sentence(command): # if there is a an activation sentence speak it # general an activation sentence for commands that take a long time to execute with sqlite3.connect(command_db) as connection: cursor = connection.cursor() cursor.execute( "SELECT activation_speech FROM commands where command=?", (command[0], )) activation_sentence = cursor.fetchone()[0] if activation_sentence != "": speak.speak_to_user(activation_sentence, background=True) print("speaking activation sentence") else: print("not speaking activation sentence") return None
def query_user(): speaking_leds.speaking_leds() subprocess.call(["/home/pi/2019-ca400-randlea2/src/stop_arecord.sh"]) spoken_words = translate_speech.start_translator() create_command_table() add_all_commands() command = find_matched_keywords(spoken_words) # find command if words spoken otherwise do nothing if spoken_words == "": hotword.detect_hotword() elif command is not None: command_script = find_module_location(command) print(command_script) run_script(command_script, command, spoken_words) else: speak.speak_to_user("command not found") hotword.detect_hotword()
def record_code(): speak.speak_to_user("Please press the button ") code = get_code() # if no button pressed if code is None: speak.speak_to_user("Unable to save, please try again ") else: speak.speak_to_user("What is the name of the button ") name = translate_speech.start_translator() save_code(name, code) speak.speak_to_user("Button saved")
def play_song(song_name): playlist_keywords = ['shuffle', 'playlist'] # check if there are activating playlist if set(playlist_keywords).issubset(set(song_name.strip().split())): speak.speak_to_user("shuffling playlist") print("playlist starting") shuffle_playlist() elif get_song_details(song_name) is None: speak.speak_to_user("searching for song") url = create_youtube_url(song_name) youtube_url = get_first_result(url) song_url, song_title = get_song(youtube_url) # add song details to database for further searches add_song_details(song_name, song_title, youtube_url, song_url) play_url(song_url) else: # song in database, retrieve song url and play it song_url = get_song_url(song_name) play_url(song_url)
def shuffle_playlist(): with sqlite3.connect(database) as connection: cursor = connection.cursor() # retrieve all previously searched songs cursor.execute("SELECT * FROM songs") songs = cursor.fetchall() if len(songs) > 0: random_song = random.choice(songs) while len(songs) >= 1: song_name, song_url = random_song[1], random_song[3] # ensure a valid url is provided speak.speak_to_user("playing {}".format(song_name)) play_url(song_url) # remove songs that have been played songs.remove(random_song) random_song = random.choice(songs) else: speak.speak_to_user("No songs currently in your playlist") hotword.detect_hotword() return None
def send_email(receiver_email, subject, message_content): try: # connect to gmail smtp server gmail_server = smtplib.SMTP("smtp.gmail.com:587") gmail_server.ehlo() # start connection gmail_server.starttls() # login to server email_address, password = get_login_details() gmail_server.login(email_address, password) # create the email message = "subject: {}\n\n{}".format(subject, message_content) # send email gmail_server.sendmail(email_address, receiver_email, message) # close the connection gmail_server.quit() speak.speak_to_user("email sent successfully") print("email sent successfully") except: speak.speak_to_user( "problem sending email, check your internet connection")
def change_security(spoken_words): # check if user wants to turn security on or off if "on" in spoken_words: request_level = "true" elif "off" in spoken_words: request_level = "false" with sqlite3.connect(security_db) as connection: cursor = connection.cursor() # delete the current settings cursor.execute("DELETE from security") hotword_levels = {"true": "snowboy.pmdl", "false": "snowboy.umdl"} cursor.execute( "INSERT or IGNORE into security(security_lock,hotword_file)values(?,?) ", ( request_level, hotword_levels[request_level], )) cursor.execute("SELECT * from security") results = cursor.fetchall() print(results) speak.speak_to_user("security settings successfully changed") return None
def main(spoken_words): recipient = find_recipient(spoken_words) if recipient is not None: speak.speak_to_user("sending email to {}".format(recipient)) speak.speak_to_user("what is the subject matter") subject = translate_speech.start_translator() # subject = "this is a test" speak.speak_to_user("what is main body") message_content = translate_speech.start_translator() # message_content = "testing" send_email(recipient, subject, message_content) else: speak.speak_to_user("email address not found") # start hotword detection after email sent hotword.detect_hotword()
def main(): current_time = get_time() speak.speak_to_user(current_time) hotword.detect_hotword()
def tell_joke(joke_file): load_file = open(joke_file) joke_list = json.load(load_file) random_joke = random.choice(joke_list["jokes"]) speak.speak_to_user(random_joke) hotword.detect_hotword()
def main(): weather_forecast = get_forecast() speak.speak_to_user(weather_forecast) hotword.detect_hotword()
def speak_results(results): if results != "": speak.speak_to_user(results) else: speak.speak_to_user("No information found")
def get_song_name(): speak.speak_to_user("what song would you like to play") song_name = convert_speech_to_text() split_song_name = song_name.split(" ") return split_song_name
def start_pair(): speak.speak_to_user( "Waiting 60 seconds for you to reset your zig bee device") # start pairing script subprocess.Popen(["node", "/home/pi/register_zigbee.js", "&"])
def main(): sports = get_sports() # speak in background while listening for hotword speak.speak_to_user(sports, background=True) hotword.detect_hotword()