Python Voice Assistant - Chat Responses Overlapping with Commands

49 Views Asked by At

Background

My Personal Assistant Isn't Playing Fair: Unwanted Chat Responses I'm building a Python personal assistant with a chatbot feature, but I'm encountering a frustrating issue: overlapping responses. Sometimes, when I ask for a joke, both the joke command and the general chat response are spoken, creating an awkward and confusing experience.

Issue Description

  • This issue occurs sporadically, making it difficult to identify the exact cause.
  • When specific commands, like "joke," are triggered, the intended responses work as expected. However, the **chat response seems to activate afterward, causing it to overlap with the command's output.

Code Sample

if __name__ == "__main__":
    clear = lambda: os.system("cls")

    # This Function will clean any
    # command before the execution of this python file

    clear()

    mystical_greet()

    while True:
        query = takeCommand().lower()

        if "wikipedia" in query:
            speak("Searching Wikipedia...")
            query = query.replace("wikipedia", "")
            try:
                results = wikipedia.summary(query, sentences=5)
            except wikipedia.DisambiguationError as e:
                speak("There are multiple options. Please specify.")
                for i, option in enumerate(e.options, start=1):
                    speak(f"{i}. {option}")
                choice = int(input("Enter the number of your choice: "))
                results = wikipedia.summary(e.options[choice - 1], sentences=5)
            except Exception as error:
                pass
            speak("According to Wikipedia")
            speak(results)
        elif "open youtube" in query:
            speak("Here you go to Youtube")
            webbrowser.open("https://www.youtube.com")
        elif "youtube" in query:
            search_query = query.replace("youtube", "").strip()
            url_search = quote(search_query)
            webbrowser.open(
                f"https://www.youtube.com/results?search_query={url_search}"
            )
        elif "open google" in query:
            speak("Here you go to Google\n")
            webbrowser.open("google.com")
        elif "open stackoverflow" in query:
            speak("Here you go to Stack Over flow.Happy coding")
            webbrowser.open("stackoverflow.com")
         # rest of commands
   
        elif "joke" in query:
            joke = get_joke()
            speak(joke)
        # rest of the commands
 
        elif "news" in query:
            user_preference = get_user_preference()

            if "world" in user_preference:
                get_news("world")
            elif "national" in user_preference:
                get_news(
                    "general"
                )  # You can customize this to a specific category for Indian news
            elif "headlines" in user_preference:
                get_news("top-headlines")
            else:
                speak("Sorry, I couldn't understand your preference. Please try again.")

        elif "where is" in query:
            query = query.replace("where is", "")
            location = query
            speak("User asked to Locate")
            speak(location)
            webbrowser.open("https://www.google.nl / maps / place/" + location + "")
        # rest of commands
        else:
            text = quote(query)
            botname = quote(assistant_name)
            url = f"https://api.popcat.xyz/chatbot?msg={text}&owner=Pranjal+Prakarsh&botname={botname}"
            try:
                request = requests.get(url)
                data = request.json()
                output = data["response"]
                speak(output)
            except:
                speak("Sorry, I don't understand that")

Desired Behavior:

I want my personal assistant to respond only when necessary:

  • When I explicitly ask the chatbot something.
  • When none of the other defined commands are recognized as a fallback option.
  • Chat responses shouldn't interfere with the execution of other commands.
0

There are 0 best solutions below