Voice Assistant For E-Mail in Python
Voice Assistant For E-Mail in Python
mail in Python
Installation:
Below is the
implementation in Python.
You can easily copy the code and run it on your
machine
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()
speak("Welcome to mail service")
def get_audio():
r = sr.Recognizer()
with sr.Microphone() as source:
r.pause_threshold = 1
r.adjust_for_ambient_noise(sour
ce, duration=1)
audio = r.listen(source)
said = ""
try:
said = r.recognize_google(audio)
print(said)
except:
speak("Didn't get that")
return said.lower()
def authenticate_gmail():
"""Shows basic usage of the Gmail
API.
Lists the user's Gmail labels.
"""
creds = None
# The file token.pickle stores the
user's
# access and refresh tokens, and is
# created automatically when the
authorization
# flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as
token:
creds = pickle.load(token)
def check_mails(service):
today_main =
today.strftime('%Y/%m/%d')
# Call the Gmail API
results =
service.users().messages().list(userId
='me',
labelIds=
["INBOX", "UNREAD"],
q="after:{0}
and
category:Primary".format(today_main
)).execute()
# The above code will get emails
from primary
# inbox which are unread
messages = results.get('messages',
[])
if not messages:
# if no new emails
print('No messages found.')
speak('No messages found.')
else:
m = ""
# if email found
speak("{} new emails
found".format(len(messages)))
if text == "read":
print(msg['snippet'])
else:
speak("email passed")
SERVICE2 = authenticate_gmail()
check_mails(SERVICE2)