|
| 1 | +import pyttsx3 |
| 2 | +import datetime |
| 3 | +import speech_recognition as sr |
| 4 | +import pyaudio |
| 5 | +import wikipedia |
| 6 | +import webbrowser |
| 7 | +import smtplib |
| 8 | +import pywhatkit as kit |
| 9 | +import pyjokes |
| 10 | +import time |
| 11 | +import sys |
| 12 | + |
| 13 | +print("Your Assistant is starting......") |
| 14 | + |
| 15 | +engine = pyttsx3.init('sapi5') |
| 16 | +voices= engine.getProperty('voices') |
| 17 | +engine.setProperty('voice', voices[1].id) |
| 18 | +engine.setProperty('rate', 190) |
| 19 | + |
| 20 | +Master=input("Enter your name: ") # when you start using the assistant enter your name here |
| 21 | + |
| 22 | +def speak(audio): |
| 23 | + engine.say(audio) |
| 24 | + engine.runAndWait() |
| 25 | + |
| 26 | +def wish_user(): |
| 27 | + hour= int(datetime.datetime.now().hour) |
| 28 | + if hour>=0 and hour<12: |
| 29 | + speak("Good Morning!" + Master) |
| 30 | + |
| 31 | + elif hour>=12 and hour<16: |
| 32 | + speak("Good Afternoon!" + Master) |
| 33 | + |
| 34 | + else: |
| 35 | + speak("Good Evening" + Master) |
| 36 | + |
| 37 | + speak("I am your Desktop-Assistant! How may I help you?") |
| 38 | + |
| 39 | +def user_command(): |
| 40 | + # takes microphone command and converts to string |
| 41 | + |
| 42 | + r= sr.Recognizer() |
| 43 | + with sr.Microphone() as source: |
| 44 | + print("I am listening....") |
| 45 | + r.pause_threshold=1 |
| 46 | + audio=r.listen(source) |
| 47 | + |
| 48 | + try: |
| 49 | + print("Recognising.....") |
| 50 | + query=r.recognize_google(audio,language='en-in') |
| 51 | + print(f"User said: {query}\n") |
| 52 | + |
| 53 | + except Exception as e: |
| 54 | + print("I am sorry I don't understand, Say that again please...") |
| 55 | + return "None" |
| 56 | + return query |
| 57 | + |
| 58 | +def mailSent(to, content): |
| 59 | + server=smtplib.SMTP('smtp.gmail.com', 587) |
| 60 | + server.ehlo() |
| 61 | + server.starttls() |
| 62 | + server.login('your email', 'your app password') # when you start working with the assistant, save this on your device |
| 63 | + #check READme.md for creating an app password |
| 64 | + server.sendmail('Your email',to,content) |
| 65 | + |
| 66 | + server.close() |
| 67 | + |
| 68 | +if __name__=='__main__': |
| 69 | + |
| 70 | + wish_user() |
| 71 | + while True: |
| 72 | + query= user_command().lower() |
| 73 | + |
| 74 | + if 'wikipedia' in query: |
| 75 | + speak('Give me sometime I am looking into Wikipedia') |
| 76 | + query= query.replace("wikipedia","") |
| 77 | + results= wikipedia.summary(query, sentences=5) |
| 78 | + speak("This is what I found!") |
| 79 | + speak(results) |
| 80 | + |
| 81 | + elif 'open youtube' in query: |
| 82 | + webbrowser.open("youtube.com") |
| 83 | + |
| 84 | + elif 'search google' in query: |
| 85 | + webbrowser.open("google.com") |
| 86 | + |
| 87 | + elif 'play music' in query: |
| 88 | + webbrowser.open("spotify.com") |
| 89 | + # you can use API as well, with the help of spotipy module |
| 90 | + |
| 91 | + elif 'time' in query: |
| 92 | + time= datetime.datetime.now().strftime("%H:%M") |
| 93 | + speak(f"Its {time} now") |
| 94 | + |
| 95 | + elif 'date today' in query: |
| 96 | + date=datetime.datetime.today() |
| 97 | + speak(f"Today is {date}") |
| 98 | + |
| 99 | + elif 'send email' in query: |
| 100 | + try: |
| 101 | + speak("please tell me the content of the email") |
| 102 | + content=user_command() |
| 103 | + speak(content) |
| 104 | + to=input() |
| 105 | + speak(to) |
| 106 | + mailSent(to, content) |
| 107 | + speak(f"successfully sent the email to {to}") |
| 108 | + except Exception as e: |
| 109 | + print(e) |
| 110 | + speak("sorry! i was unable to send the mail") |
| 111 | + |
| 112 | + elif 'send whatsapp message' in query: #you should be logged in into whatsapp web for this |
| 113 | + speak("To whom should I send the message?") |
| 114 | + number=int(input()) |
| 115 | + speak("Tell me the message please") |
| 116 | + message=user_command() |
| 117 | + speak("At what time should I send?") |
| 118 | + speak("At what time? (24 hours system)") |
| 119 | + hr=int(input("Hours: ")) |
| 120 | + mins=int(input("Minutes: ")) |
| 121 | + kit.sendwhatmsg(number,message,hr,mins) |
| 122 | + # this should be in the format ("+91xxxxxxxxxx","This is message", 15, 20) |
| 123 | + |
| 124 | + elif 'open facebook' in query: |
| 125 | + webbrowser.open("facebook.com") |
| 126 | + |
| 127 | + elif 'make me laugh' in query: |
| 128 | + joke=pyjokes.get_joke() |
| 129 | + speak(joke) |
| 130 | + |
| 131 | + elif 'no thanks' in query: |
| 132 | + speak("thanks for using me! Have a good day") |
| 133 | + sys.exit() |
| 134 | + |
| 135 | + time.sleep(5) |
| 136 | + speak("do you have any other work?") |
| 137 | + |
| 138 | + |
0 commit comments