Untitled
unknown
plain_text
20 days ago
2.6 kB
4
Indexable
import speech_recognition as sr import pyttsx3 import datetime # Initialize the text-to-speech engine engine = pyttsx3.init() # Define your Under 25 App username username = "e/beetlebloom" # Function to greet the user def greet_user(): current_time, current_date = get_time_date() greeting = f"Hello {username}! How can I assist you today?" print(greeting) # Display the greeting text on the screen engine.say(greeting) engine.runAndWait() # Function to get the current date and time in a readable format def get_time_date(): now = datetime.datetime.now() current_time = now.strftime("%I:%M %p") # 12-hour format with AM/PM current_date = now.strftime("%Y-%m-%d") return current_time, current_date # Function to recognize and respond to voice commands def recognize_command(): recognizer = sr.Recognizer() with sr.Microphone() as source: print("Listening...") audio = recognizer.listen(source) try: command = recognizer.recognize_google(audio) print(f"You said: {command}") # Respond to "date" or "time" commands if "date" in command or "time" in command: current_time, current_date = get_time_date() response = f"The current date is {current_date} and the time is {current_time}." print(response) engine.say(response) engine.runAndWait() # Respond to an exit or stop command elif "stop" in command or "exit" in command: farewell = "Goodbye! Have a great day!" print(farewell) engine.say(farewell) engine.runAndWait() return False # End the loop # Handle unrecognized commands else: message = "Sorry, I didn't understand. Please ask for the date or time." print(message) engine.say(message) engine.runAndWait() except sr.UnknownValueError: error_message = "Sorry, I couldn't understand your voice." print(error_message) engine.say(error_message) engine.runAndWait() return True # Keep the loop running # Main function to run the bot def main(): greet_user() keep_running = True while keep_running: keep_running = recognize_command() if __name__ == "__main__": main()
Editor is loading...
Leave a Comment