How to make a voice assistant for E-mail in Python? - GeeksforGeeks

05 Apr, 2021
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 module
speech_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.
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.
Python3
from __future__ import print_functionimport datetimeimport pickleimport os.pathfrom googleapiclient.discovery import buildfrom google_auth_oauthlib.flow import InstalledAppFlowfrom google.auth.transport.requests import Requestimport osimport pyttsx3import speech_recognition as srfrom datetime import date  # If modifying these scopes, delete the file token.pickle .# if you run this for the firs# t time it will take you to gmail to choose your accountSCOPES = ["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(source, 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 = message['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)
Output:
Python-projects
python-utility
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Selecting rows in pandas DataFrame based on conditions
Defaultdict in Python
Python | Get unique values from a list
Python | os.path.join() method
Create a directory in Python
Python | Split string into list of characters