0% found this document useful (0 votes)
21 views19 pages

Voice Assistant For E-Mail in Python

Hj

Uploaded by

sachinnishad16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views19 pages

Voice Assistant For E-Mail in Python

Hj

Uploaded by

sachinnishad16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

voice assistant for E-

mail in Python

As we know, emails are very


important for communication as
each professional communication
can be done by emails and the
best service for sending and
receiving mails is as we all know
GMAIL. Gmail is a free email service
developed by Google. Users can
access Gmail on the web and using
third-party programs that
synchronize email content through
POP or IMAP protocols.
To connect Gmail with Voice
Assistant we have to use Gmail API.
In this article, we will see how to
send and check emails by giving
voice commands. The Gmail API
lets you view and manage Gmail
mailbox data like threads,
messages, and labels. More
information about Gmail API can be
found here- Gmail API.
Requirements:
Python(3 or greater)
A Gmail Account
Google API Client
pyttsx3
modulespeech_recognition
module

Installation:

Install the libraries by running the


following commands:
pip install google-api-python-
client google-auth-httplib2
google-auth-oauthlib
Run this or pyttsx3:
pip install pyttsx3
Run this for speech recognition:
pip install SpeechRecognition
Now there is a need to set up a
Google Cloud console to interact
with the Gmail API. This can be done
very easily. You can find
information on how to set up a
Google cloud console.
After setting up the Google Cloud
console, we just need to make an
approach for building a voice
assistant to speak up emails.
Approach:
Import required modules.
Create the below functions:
speak( ): This function will help
our assistant to speak up.
get_audio( ): This function will
help the assistant to get the
input by the user.
check_mails( ): This function is
used to check unread emails in
the primary section of the inbox.
Call the above-created function.

Below is the
implementation in Python.
You can easily copy the code and run it on your
machine

from __future__ import


print_function
import datetime
import pickle
import os.path
from googleapiclient.discovery import
build
from google_auth_oauthlib.flow
import InstalledAppFlow
from google.auth.transport.requests
import Request
import os
import pyttsx3
import speech_recognition as sr
from datetime import date

# If modifying these scopes, delete


the file token.pickle .
# if you run this for the first
# t time it will take you to gmail to
choose your account
SCOPES =
["https://www.googleapis.com/auth/
gmail.readonly"]

from __future__ import


print_function
import datetime
import pickle
import os.path
from googleapiclient.discovery import
build
from google_auth_oauthlib.flow
import InstalledAppFlow
from google.auth.transport.requests
import Request
import os
import pyttsx3
import speech_recognition as sr
from datetime import date

# If modifying these scopes, delete


the file token.pickle .
# if you run this for the first
# t time it will take you to gmail to
choose your account
SCOPES =
["https://www.googleapis.com/auth/
gmail.readonly"]

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)

# If there are no (valid) credentials


available,
# let the user log in.
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)

# Save the credentials for the


next run
with open('token.pickle', 'wb') as
token:
pickle.dump(creds, token)

service = build('gmail', 'v1',


credentials=creds)
return service

def check_mails(service):

# fetching emails of today's date


today = (date.today())

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)))

speak("if you want to read any


particular email just type read ")
speak("and for not reading type
leave ")
for message in messages:
msg =
service.users().messages().get(userId
='me',
id=messag
e['id'], format='metadata').execute()

for add in msg['payload']


['headers']:
if add['name'] == "From":

# fetching sender's email


name
a = str(add['value'].split("<")
[0])
print(a)
speak("email from"+a)
text = input()

if text == "read":

print(msg['snippet'])

# speak up the mail


speak(msg['snippet'])

else:
speak("email passed")
SERVICE2 = authenticate_gmail()
check_mails(SERVICE2)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy