0

I have a python virtuall assistant named Anna. She can listen for commands, but what I want is to have a secondary command after you ask her the first command. For example, tell her you want to go to stack overflow. Then she can ask you if you want to go to your profile or not. How can you make it so if you say "Yes" she will. and if you say "No" she won't?

Here is my code:

import speech_recognition as sr
import pyttsx3
import time as tme


tts = pyttsx3.init()

voices = tts.getProperty('voices')
tts.setProperty('voice', voices[1].id)

def takeCommand():

    r = sr.Recognizer()

    with sr.Microphone() as source:
        r.adjust_for_ambient_noise(source)
        print('Listening...')

        r.pause_threshold = 0.5
        audio = r.listen(source)
        
        try:
            print("Recognizing")
            
            Query = r.recognize_google(audio, language='en-us')
            print("the command is printed=", Query)
            
            
        except Exception as e:
            print(e)
            print("Say that again")
            return "None"
        if "Anna" in Query:
            return Query
        else:
            return "None"

def Take_query():

    while(True):

        query = takeCommand().lower()
        if "Hello" in query:
            tts.say('Hello')
            tts.runAndWait()
            continue

        elif "how are you" in query:
            tme.sleep(.5)
            tts.say('I am good')
            tts.runAndWait()
            continue

if __name__ == '__main__':
    Take_query()

Thank you!

Rubén
  • 34,714
  • 9
  • 70
  • 166

0 Answers0