-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjarvis_assistant.py
More file actions
142 lines (110 loc) · 4.15 KB
/
jarvis_assistant.py
File metadata and controls
142 lines (110 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import pyttsx3
import datetime
import speech_recognition as sr
import wikipedia
import webbrowser
import os
import smtplib
# defining the engine function
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices') # fetches the available voices in this device
engine.setProperty('voice', voices[1].id) # we are going to use the microsoft Zira as our assistant's voice
# print(voices[1].id)
# defining the speak function\
def speak(audio):
engine.say(audio)
engine.runAndWait()
# defining wishMe function
def wishMe():
hour = int(datetime.datetime.now().hour)
# running the loop function for whishing
if 0<=hour>12:
print("Good Morning")
speak("Good Morning")
elif 12<=hour>18:
print("Good Afternoon")
speak("Good Afternoon")
else:
print("Good Evening")
speak("Good Evening")
speak("I am your personal assistant so tell me how can i help you")
# defing the function to take all the commands
def takeCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
speak("Listening...........")
r.pause_threshold = 1
audio = r.listen(source)
# try and except will be better as it will not throw the error to stop the program
try:
print("Recognising the text..............")
query = r.recognize_google(audio, language='en-in') # google is beeteer fo audio enhancements
print(f"You Said: {query}/n")
except Exception as e:
# print(e)
print("Say again please: ")
return "None"
return query
#now let's create the sendMail function for sending emails
def sendEmail(to, content):
server = smtplib.SMTP('smtp.gmail.com', 587) # 587 is the port no.
server.ehlo()
server.starttls()
server.login('enteryouremail@gmail.com', 'enter your password')
server.sendmail('enteryouremail@gmail.com', to, content)
server.close()
# defing the main funtion unde which all our defined functions will work
if __name__=="__main__":
wishMe()
while True:
query = takeCommand().lower()
if 'hello' in query:
print("heey bro")
speak("heey bro")
elif 'how are you' in query:
print("i am fine bro")
speak("i am fine bro")
elif 'wikipedia' in query:
speak("Seraching wikipedia........")
query = query.replace("wikipedia", " ")
# semtences are use to print or speak the jno of line that the user wants
results = wikipedia.summary(query, sentences = 1)
speak("According to wikipedia.........")
print(results)
speak(results)
elif 'google'in query:
speak("opening google")
webbrowser.open("google.com")
elif 'youtube' in query:
speak("opening youtube")
webbrowser.open("youtube.com")
elif 'play song' in query:
music_directory = "C:/Users/LITU_LISA_SAI/Documents/music_directory"
songs = os.listdir(music_directory)
print(songs)
os.startfile(os.path.join(music_directory, songs[0]))
elif 'show me time' in query:
strTime = datetime.datetime.now().strftime("%H:%M:%S")
print(strTime)
speak(f"the time is {strTime}")
elif 'thank you' in query:
print("welcome")
speak("you are welcome")
elif 'open mail' in query:
speak("opening mail")
webbrowser.open("you email link")
elif 'email me' in query:
try:
speak("enter the text")
content = takeCommand()
to = "your email"
sendEmail(to, content)
speak("your email has been sent")
except Exception as e:
print(e)
speak("Sorry could'nt transfer your mail")
elif 'exit' in query:
print("bye bye")
speak("bye bye")
exit()
# SO OUR PROJECT JARVIS IS COMPLETE NOW SO LET'S TEST IT