|
| 1 | +import speech_recognition as sr |
| 2 | +import pyttsx3 |
| 3 | +import pywhatkit |
| 4 | +import datetime |
| 5 | +import wikipedia |
| 6 | +import pyjokes |
| 7 | + |
| 8 | +listener = sr.Recognizer() |
| 9 | +engine = pyttsx3.init() |
| 10 | +voices = engine.getProperty('voices') |
| 11 | +engine.setProperty('voice', voices[1].id) |
| 12 | + |
| 13 | + |
| 14 | +def talk(text): |
| 15 | + engine.say(text) |
| 16 | + engine.runAndWait() |
| 17 | + |
| 18 | + |
| 19 | +def take_command(): |
| 20 | + try: |
| 21 | + with sr.Microphone() as source: |
| 22 | + print('listening...') |
| 23 | + voice = listener.listen(source) |
| 24 | + command = listener.recognize_google(voice) |
| 25 | + command = command.lower() |
| 26 | + if 'alexa' in command: |
| 27 | + command = command.replace('alexa', '') |
| 28 | + print(command) |
| 29 | + except: |
| 30 | + pass |
| 31 | + return command |
| 32 | + |
| 33 | + |
| 34 | +def run_alexa(): |
| 35 | + command = take_command() |
| 36 | + print(command) |
| 37 | + if 'play' in command: |
| 38 | + song = command.replace('play', '') |
| 39 | + talk('playing ' + song) |
| 40 | + pywhatkit.playonyt(song) |
| 41 | + elif 'time' in command: |
| 42 | + time = datetime.datetime.now().strftime('%I:%M %p') |
| 43 | + talk('Current time is ' + time) |
| 44 | + elif 'who the heck is' in command: |
| 45 | + person = command.replace('who the heck is', '') |
| 46 | + info = wikipedia.summary(person, 1) |
| 47 | + print(info) |
| 48 | + talk(info) |
| 49 | + elif 'date' in command: |
| 50 | + talk('sorry, I have a headache') |
| 51 | + elif 'are you single' in command: |
| 52 | + talk('I am in a relationship with wifi') |
| 53 | + elif 'joke' in command: |
| 54 | + talk(pyjokes.get_joke()) |
| 55 | + else: |
| 56 | + talk('Please say the command again.') |
| 57 | + |
| 58 | + |
| 59 | +while True: |
| 60 | + run_alexa() |
0 commit comments