-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
118 lines (90 loc) · 3.45 KB
/
main.py
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
from gtts import gTTS
import speech_recognition as sr
import os
import re
import webbrowser
import smtplib
import requests
def talkToMe(audio):
# speaks audio passed as argument
print(audio)
for line in audio.splitlines():
os.system("say " + audio)
# use the system's inbuilt say command instead of mpg123
# text_to_speech = gTTS(text=audio, lang='en')
# text_to_speech.save('audio.mp3')
# os.system('mpg123 audio.mp3')
def myCommand():
# listens for commands
r = sr.Recognizer()
with sr.Microphone() as source:
print('Ready...')
r.pause_threshold = 1
r.adjust_for_ambient_noise(source, duration=1)
audio = r.listen(source)
try:
command = r.recognize_google(audio).lower()
print('You said: ' + command + '\n')
# loop back to continue to listen for commands if unrecognizable speech is received
except sr.UnknownValueError:
print('Your last command couldn\'t be heard')
command = myCommand();
return command
def assistant(command):
# if statements for executing commands
if 'open reddit' in command:
reg_ex = re.search('open reddit (.*)', command)
url = 'https://www.reddit.com/'
if reg_ex:
subreddit = reg_ex.group(1)
url = url + 'r/' + subreddit
webbrowser.open(url)
print('Done!')
elif 'open website' in command:
reg_ex = re.search('open website (.+)', command)
if reg_ex:
domain = reg_ex.group(1)
url = 'https://www.' + domain + '.com'
webbrowser.open(url)
print('Done!')
else:
pass
elif 'what\'s up' in command:
talkToMe('Just doing my thing')
elif 'joke' in command:
res = requests.get(
'https://icanhazdadjoke.com/',
headers={"Accept":"application/json"}
)
if res.status_code == requests.codes.ok:
talkToMe(str(res.json()['joke']))
else:
talkToMe('oops!I ran out of jokes')
elif 'email' in command:
talkToMe('Who is the recipient?')
recipient = myCommand()
if 'recipient' in recipient: # write the name of the recipient where recipient in quotes have written
talkToMe('What should I say?')
content = myCommand()
#init gmail SMTP
mail = smtplib.SMTP('smtp.gmail.com', 587) # using gmail for sending mails, you can change the site and mention it here if you want
# to send mail from different site
#identify to server
mail.ehlo()
#encrypt session
mail.starttls()
#login
mail.login('username', 'password') # Write username and password respectively to login to your gmail account
#send message
mail.sendmail('John Fisher', '[email protected]', content) # mention recipient's full name and email address
#end mail connection
mail.close()
talkToMe('Email sent.')
else:
talkToMe('I don\'t know what you mean!')
talkToMe('I am ready for your command')
#loop to continue executing multiple commands
while True:
assistant(myCommand())
elif "shutdown" in myCommand:
talkToMe("I am shutting down")