-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
389 lines (340 loc) · 13.7 KB
/
Copy pathmain.py
File metadata and controls
389 lines (340 loc) · 13.7 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
import pickle
import os.path
import re
import pyttsx3
import speech_recognition as sr
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from email.mime.text import MIMEText
import base64
import pyaudio
import pygame
import threading
import tkinter as tk
from tkinter import BOTTOM, SUNKEN, W, X, Frame, Label, StringVar, ttk
from tkinter import messagebox
import time
# Define the scope of the Gmail API
SCOPES = ['https://www.googleapis.com/auth/gmail.readonly', 'https://www.googleapis.com/auth/gmail.send']
WAKE = {"start", "new email", "compose","compose message", "write email", "write new email", "to"}
SUBJECT = "subject"
MESSAGE = "message"
SEND = {"send", "send email", "proceed", "yes"}
STOP = {"stop", "goodbye", "bye", "close", "abort", "cancel"}
INBOX = {"inbox", "read", "open inbox", "read email", "unread emails"}
# Define the email pattern as a regular expression
email_pattern = r"[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+"
# Import tkinter
import tkinter as tk
pygame.mixer.init()
# Create the main window
window = tk.Tk()
window.title("Voice Based Email")
window.geometry("500x400")
window.iconbitmap("images/logo.ico")
window.configure(bg="#f8f8f8")
# Define the voice assistant function
def speak(text):
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
rate = engine.getProperty('rate')
engine.setProperty('rate', rate-20)
engine.say(text)
engine.runAndWait()
# Define the speech recognition function
def get_audio():
r = sr.Recognizer()
with sr.Microphone() as source:
r.pause_threshold = 1
r.adjust_for_ambient_noise(source, duration=1)
audio = r.listen(source)
said = ""
try:
said = r.recognize_google(audio,language="en-US")
print(said)
match = re.search(email_pattern, said)
if match:
print("email:" + match.group())
except sr.UnknownValueError:
# messagebox.showinfo("Error", "Sorry, I didn't catch that.")
pygame.mixer.music.load("sounds/soryididnotget2.wav")
pygame.mixer.music.play(loops=0)
except sr.RequestError:
# messagebox.showinfo("Error", "Sorry, my speech recognition service is down.")
pygame.mixer.music.load("sounds/spechservicedown2.wav")
pygame.mixer.music.play(loops=0)
#speak("Error", "Sorry, my speech recognition service is down.")
return said.lower()
# Define the Gmail authentication function
def authenticate_gmail():
creds = None
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
try:
service = build('gmail', 'v1', credentials=creds)
return service
except Exception as e:
speak("An error occurred while authenticating Gmail.")
speak(str(e))
return None
# Define the function to open a new window for listing inbox emails
def open_inbox_window():
def fetch_emails():
# Authenticate Gmail
service = authenticate_gmail()
if service is None:
messagebox.showerror("Error", "Failed to authenticate Gmail.")
inbox_window.destroy()
return
# Get inbox emails
try:
results = service.users().messages().list(userId='me', labelIds=['INBOX']).execute()
messages = results.get('messages', [])
if messages:
for message in messages:
msg = service.users().messages().get(userId='me', id=message['id']).execute()
payload = msg['payload']
headers = payload['headers']
for header in headers:
if header['name'] == 'Subject':
subject = header['value']
if header['name'] == 'From':
sender = header['value']
email_listbox.insert(tk.END, f"From: {sender}\nSubject: {subject}\n")
else:
email_listbox.insert(tk.END, "No emails in the inbox.")
except Exception as e:
pygame.mixer.music.load("sounds/inbox-error.wav")
pygame.mixer.music.play(loops=0)
print("error!")
inbox_window.destroy()
return
# Remove the loading text
loading_label.destroy()
def read_email_aloud():
selected_email = email_listbox.get(email_listbox.curselection())
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
rate = engine.getProperty('rate')
engine.setProperty('rate', rate-20)
engine.say(selected_email)
engine.runAndWait()
inbox_window = tk.Toplevel(window)
inbox_window.title("Inbox")
inbox_window.geometry("400x300")
# Create a scrollbar
scrollbar = ttk.Scrollbar(inbox_window)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
# Create a listbox to display the emails
email_listbox = tk.Listbox(inbox_window, yscrollcommand=scrollbar.set, relief="flat",)
email_listbox.pack(fill=tk.BOTH, expand=True)
# Create the function to read email aloud
def read_email_aloud():
selected_email = email_listbox.get(email_listbox.curselection())
engine = pyttsx3.init()
speak(selected_email)
engine.runAndWait()
# Create the button to read email aloud
read_button = tk.Button(inbox_window, text="Read Aloud", command=read_email_aloud)
read_button.pack()
# Configure the scrollbar
scrollbar.config(command=email_listbox.yview)
# Add a loading text
loading_label = tk.Label(inbox_window, text="Loading...")
loading_label.pack()
#check_mails(service)
# Start a new thread to fetch emails
thread = threading.Thread(target=fetch_emails)
thread.start()
pygame.mixer.music.load("sounds/inboxenkuan2.wav")
pygame.mixer.music.play(loops=0)
# Define the function to check unread emails
def check_mails(service):
try:
results = service.users().labels().get(userId='me', id='INBOX').execute()
messages = results.get('messages', [])
unread = results.get('messagesUnread')
speak(f"You have {unread} unread messages in your inbox.")
if messages:
for message in messages[:10]:
msg = service.users().messages().get(userId='me', id=message['id']).execute()
payload = msg['payload']
headers = payload['headers']
for header in headers:
if header['name'] == 'Subject':
subject = header['value']
if header['name'] == 'From':
sender = header['value']
speak(f"From {sender}, subject {subject}")
except Exception as e:
speak("An error occurred while checking emails.")
speak(str(e))
# Define the function to create a message
def create_message(sender, to, subject, message_text):
message = MIMEText(message_text)
message['to'] = to
message['from'] = sender
message['subject'] = subject
raw = base64.urlsafe_b64encode(message.as_bytes())
raw = raw.decode()
body = {'raw': raw}
return body
# Define the function to send a message
def send_message(service, user_id, message):
try:
message = service.users().messages().send(userId=user_id, body=message).execute()
# speak('Your email has been sent.')
pygame.mixer.music.load("sounds/email-success.wav")
pygame.mixer.music.play(loops=0)
except Exception as e:
pygame.mixer.music.load("sounds/email-sending-error.wav")
pygame.mixer.music.play(loops=0)
# Define the function to handle the "Compose" button click event
def compose_email():
to = to_entry.get()
subject = subject_entry.get()
message_text = message_text_entry.get("1.0", tk.END).strip()
message = create_message("me", to, subject, message_text)
send_message(service, "me", message)
# Define the function to handle the voice input for message field
def get_message():
#speak("What is the message of the email?")
pygame.mixer.music.load("sounds/e-message.wav")
pygame.mixer.music.play(loops=0)
message = get_audio()
message_text_entry.delete("1.0", tk.END)
message_text_entry.insert(tk.END, message)
# Define the function to handle the voice input for subject field
def get_subject():
#speak("What is the subject of the email?")
pygame.mixer.music.load("sounds/subject.wav")
pygame.mixer.music.play(loops=0)
subject = get_audio()
subject_entry.delete(0, tk.END)
subject_entry.insert(tk.END, subject)
#get_message()
# Define the function to handle the voice input for recipient field
def get_recipient():
#speak("Who do you want to send the email to?")
pygame.mixer.music.load("sounds/to-address.wav")
pygame.mixer.music.play(loops=0)
recipient = get_audio()
recipient = recipient.replace(" ", "")
#replace the word "at" with the character "@"
recipient = recipient.replace("at", "@")
match = re.search(email_pattern, recipient)
if match:
#speak("The email address is {}".format(match.group()))
to_entry.delete(0, tk.END)
to_entry.insert(tk.END, recipient)
else:
pygame.mixer.music.load("sounds/wrong-email.wav")
pygame.mixer.music.play(loops=0)
#get_subject()
def voice_command(event=None):
# Create a recognizer object
r = sr.Recognizer()
# Listen to the microphone
with sr.Microphone() as source:
pygame.mixer.music.load("fx/blip.wav")
pygame.mixer.music.play(loops=0)
r.pause_threshold = 1
r.adjust_for_ambient_noise(source, duration=1)
audio = r.listen(source)
text = ""
# Try to recognize the speech
try:
text = r.recognize_google(audio, language="en-US")
print("You said:", text)
# Check if the wake word is in the text
if any(word in text.lower() for word in WAKE):
# Print a message
print("Wake word detected")
# Get the recipient of the email
get_recipient()
# Check if the subject keyword is in the text
elif SUBJECT in text.lower():
# Print a message
print("Subject keyword detected")
# Get the subject of the email
get_subject()
# Check if the message keyword is in the text
elif MESSAGE in text.lower():
# Print a message
print("Message keyword detected")
# Get the message of the email
get_message()
# Check if the send keyword is in the text
elif any(word in text.lower() for word in SEND):
# Print a message
print("Send keyword detected")
# Send the email
compose_email()
# Check if the inbox keyword is in the text
elif any(word in text.lower() for word in INBOX):
# Print a message
print("Inbox keyword detected")
# Open the inbox window
open_inbox_window()
# Check if the stop keyword is in the text
elif any(word in text.lower() for word in STOP):
# Print a message
print("Stop keyword detected")
# Speak a farewell
speak("Goodbye")
# Close the window
window.destroy()
except:
# print("Sorry, I could not understand you")
pygame.mixer.music.load("fx/error-main.wav")
pygame.mixer.music.play(loops=0)
pygame.mixer.music.load("sounds/enkuan2.wav")
pygame.mixer.music.play(loops=0)
window.bind("<space>", voice_command)
# Create the Inbox button
inbox_button = tk.Button(window, text="Go to Inbox", command=open_inbox_window, bg="crimson", fg="white", borderwidth="0", cursor="arrow")
inbox_button.grid(row=0, column=0, padx=15, pady=15)
# Create the recipient field
to_label = tk.Label(window, text="To:",bg="#f8f8f8")
to_label.grid(row=1, column=0,)
to_entry = tk.Entry(window, width=50)
to_entry.grid(row=1, column=1,)
# Create the subject field
subject_label = tk.Label(window, text="Subject:",bg="#f8f8f8")
subject_label.grid(row=2, column=0,)
subject_entry = tk.Entry(window, width=50)
subject_entry.grid(row=2, column=1,)
# Create the message field
message_text_label = tk.Label(window, text="Message:",bg="#f8f8f8")
message_text_label.grid(row=3, column=0,)
message_text_entry = tk.Text(window, width=40, height=10)
message_text_entry.grid(row=3, column=1,)
# Create the voice input buttons
recipient_button = tk.Button(window, text="🎙️", command=get_recipient, borderwidth="0", font="Serif",bg="#f8f8f8")
recipient_button.grid(row=1, column=2,padx=5,pady=5)
subject_button = tk.Button(window, text="🎙️", command=get_subject, borderwidth="0", font="Serif", bg="#f8f8f8")
subject_button.grid(row=2, column=2,padx=5,pady=5)
message_button = tk.Button(window, text="🎙️", command=get_message, borderwidth="0", font="Serif",bg="#f8f8f8")
message_button.grid(row=3, column=2,padx=5,pady=5)
# Create the Compose button
compose_button = tk.Button(window, text="Send", command=compose_email, bg="#05f", fg="white", borderwidth="0", font=("Serif", 11), width="7")
compose_button.grid(row=4, column=1,pady=10)
# Authenticate Gmail
service = authenticate_gmail()
# Pack the button
compose_button.grid()
# Start the main event loop
window.after(1200, voice_command)
window.mainloop()