Home > Blockchain >  Having trouble with a simple python bot. Responding to all my commands with the same repsonse
Having trouble with a simple python bot. Responding to all my commands with the same repsonse

Time:12-29

Im making a simple python bot that was just supposed to turn off my computer at first, What i would say was "Computer off now" And my bot would say Good night (or good morning / afternoon / evening depending on the time) And turn off my computer, But then i also wanted to add a feature where if i asked "Victoria, are you there" It would respond "I'm here." But my bot only responds with "good night" (since i've been working on this for around a night, i don't know if it changes according to the time.) Sorry i don't really know where to put the code, so i'll put it here in the details tab.

`

import speech_recognition as sr
import pyttsx3
import os
import datetime

# Set up the speech recognition
r = sr.Recognizer()
mic = sr.Microphone()
engine = pyttsx3.init()

def check_audio():
    # Listen for a command
    with mic as source:
        audio = r.listen(source)

    # Try to recognize the command
    try:
        command = r.recognize_google(audio)
        print("You said: "   command)
    except sr.UnknownValueError:
        print("Could not understand audio")
    except sr.RequestError as e:
        print("Error processing request: "   e)

    # If the command is "Computer off now", turn off the computer
    if command == "Computer off now":
        os.system("shutdown /s /t 0")
        engine.say("Good night")
        engine.runAndWait()
    elif command == "Victoria, are you there?":
        engine.say("I'm here Carl.")
        engine.runAndWait()
    else:
        # Get the current time
        now = datetime.datetime.now()
        hour = now.hour

        # Determine the appropriate greeting based on the time of day
        if hour < 7:
            greeting = "Good night"
        elif hour < 12:
            greeting = "Good morning"
        elif hour < 18:
            greeting = "Good afternoon"
        else:
            greeting = "Good evening"

        # Use pyttsx3 to greet the user
        engine.say(f"{greeting}")
        engine.runAndWait()

# Set up the timer to periodically check
while True:
    check_audio()

`

I tried saying "Victora, are you there?" And i was expecting my bot to say "I'm here." Instead it said "good night"

CodePudding user response:

I fixed it by removing the , and ? And not being as picky with the grammar. Sorry for the waste of time!

CodePudding user response:

You're saying the wrong command. Victoria is listening for "Victoria, are you there?". What you're saying is "Are you there Victoria?". Solution is either make Victoria listen for "Are you there Victoria?" or change your speech pattern.

  • Related