def play_music(sentence): """ Dispatch: play Function to play music. Requires pianobar which is a Pandora from the terminal. Also you must set up config file in .config/pianobar/config if you don't want to log on. Example: "Play me some music." """ import alan import environment.system # TODO check for config file and add one if not present. alan.speak("Playing music from pandora") environment.system.run_service("pianobar") import time time.sleep(10) alan.listen()
def send_email(sentence): """ Dispatch: send Function to send an email. Example: "Send an email." """ import smtplib from email.mime.text import MIMEText as text import alan import language.questions try: # Send the mail. alan.speak("I'm preparing to send an email.") server = smtplib.SMTP('smtp.gmail.com', 587) server.starttls() # TODO add alan's email and password here email = language.questions.ask_for_email("What is the sender email?") alan.speak("What is your password") password = alan.listen() server.login(email, password) recipient = language.questions.ask_for_email("What is the recipient email?") message = language.questions.ask_for_text("What is the message?") mime_message = text(message) mime_message['Subject'] = language.questions.ask_for_text("What is the subject?") server.sendmail(email, recipient, mime_message.as_string()) server.quit() return "Email sent." except: alan.speak("Something went wrong.") if "gmail" in email: return "Try allowing less secure apps in your gmail settings." else: return "I can't seem to send email right now."
def ask_for_long_text(): """ Function to get a longer amount of text. Runs until user says they have completed the input text. """ from memory import context done = False text_block = current_block = "" while not done: if text_block is not "" and current_block is "": done = binary_question("Is that all?") if not done: alan.speak("Please continue") else: context.no_prompt = False return text_block answer = alan.listen() current_block = (text_block + " " + answer) alan.speak("Here is what I have: " + current_block) end_block_command = binary_question("Is this correct?") if not end_block_command: # Do not save text block, redo alan.speak("Ok, let's try that again.") else: # Append to final text block current_block = "" text_block = (text_block + " " + answer)
def remember(sentence): """ Dispatch: remember Function to remember something in short term. Key value storage dict. """ import alan import memory.context memory = memory.context.short_term_memory concept_key = " ".join([word[0] for word in sentence if 'N' in word[1]]) alan.speak("Tell me what you want me to remember.") concept_value = alan.listen() memory.remember_concept(concept_key, concept_value) return "I will remember that as '" + concept_key + "'"
def ask_for_text(question): """ Function to get a short amount of text. The user can only input one installment. If you need more text use ask_for_long_text """ confirmed = False while not confirmed: alan.speak(question) answer = alan.listen() alan.speak("I have heard that as " + answer) confirmed = binary_question("Is that correct?") if not confirmed: alan.speak("Ok, let's try that again.") return answer
def binary_question(question): """ Function to help with yes or no questions. Needs to include more options than yes or no (ya, nope, sure etc.) Args: question (String): the question you are trying to ask. Returns: bool : returns True for yes and False for no """ alan.speak(question) answer = alan.listen() if "yes" in answer: return True elif "no" in answer: return False else: alan.speak("I was expecting a yes or no answer but you said " + answer)
def interpret(plugin): """ Function that interprets plugin data from stdin and writes to stdout. Args: plugin (subprocess.Popen object): The process running the plugin. Returns: (String) : status of the plugin # TODO I remember reading that stdin.write() and stdout.readline() is not ideal so they might need to be replaced. """ #TODO should be migrated to a class. Also it will eventually handle many commands other than :speak: and :listen: while True: line = plugin.stdout.readline() if line != '': if ':listen:' in line: try: plugin.stdin.write(str(alan.listen()) + "\n") except: return "Exiting plugin" if ':speak:' in line: line = line.replace(":speak:", "") line = line.replace(":listen:", "") alan.speak(line) if ':release:' in line: # The plugin goes into background mode and spawns a notification_listener thread. background_listener = threading.Thread( target=attach_notification_listener, args=[plugin]) background_listener.start() return "Plugin is now running in the background." else: print line.rstrip() else: break return "Finished running plugin."
def interpret(plugin): """ Function that interprets plugin data from stdin and writes to stdout. Args: plugin (subprocess.Popen object): The process running the plugin. Returns: (String) : status of the plugin # TODO I remember reading that stdin.write() and stdout.readline() is not ideal so they might need to be replaced. """ #TODO should be migrated to a class. Also it will eventually handle many commands other than :speak: and :listen: while True: line = plugin.stdout.readline() if line != '': if ':listen:' in line: try: plugin.stdin.write(str(alan.listen()) + "\n") except: return "Exiting plugin" if ':speak:' in line: line = line.replace(":speak:", "") line = line.replace(":listen:", "") alan.speak(line) if ':release:' in line: # The plugin goes into background mode and spawns a notification_listener thread. background_listener = threading.Thread(target=attach_notification_listener, args=[plugin]) background_listener.start() return "Plugin is now running in the background." else: print line.rstrip() else: break return "Finished running plugin."
def ask_for_email(question): """ Function to help with obtaining an email address. Args: question (String): the question you are trying to ask. Returns: String : the email address gained from the user """ confirmed = False while not confirmed: alan.speak(question) answer = alan.listen() email = answer.replace(" at ", "@").replace(" ", "") to_lower = binary_question("Are all of the letters in your email lower case?") if to_lower: email = email.lower() alan.speak("I have heard your email address as " + email) confirmed = binary_question("Is that correct?") if not confirmed: alan.speak("Ok, let's try that again. If you haven't already try spelling it out for me.") return email