Set Up Google Dialogflow
- Go to the Dialogflow Console and sign in with your Google account.
- Click on "Create Agent". Provide a name and select your preferred language and time zone.
- After creating the agent, you'll be redirected to the agent's overview page.
- Under the "Integrations" section on the left, find and click on the "Enable" button for the Google Assistant integration.
Create Gmail API Credentials
- Go to the Google Developers Console and ensure you're logged into the same Google Account.
- Create a new project or select an existing one.
- Navigate to "Library" and search for "Gmail API". Click on it and enable it for your project.
- Go to the "Credentials" tab and click on "Create credentials". Select "OAuth Client ID" as the credentials type.
- Follow the on-screen instructions to configure the consent screen and set up the OAuth Client ID.
- Download the JSON file containing your credentials, as you'll need this later.
Integrate Dialogflow with Gmail
- In Dialogflow, go to "Fulfillment" and toggle on the "Enable Webhook" option.
- Set up a backend server that will read emails through Gmail API and respond accordingly.
from googleapiclient.discovery import build
from google.oauth2 import service_account
# Authentication
SCOPES = ['https://www.googleapis.com/auth/gmail.readonly']
SERVICE_ACCOUNT_FILE = 'path/to/your/service-account-file.json'
creds = None
creds = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
# Gmail API service
service = build('gmail', 'v1', credentials=creds)
# List Messages
def list_messages(service):
results = service.users().messages().list(userId='me').execute()
return results.get('messages', [])
# Fetch and process new emails
messages = list_messages(service)
for message in messages:
# Process each email (your logic here)
pass
Deploy and Test
- Deploy your backend to a platform like Google Cloud Functions, AWS Lambda, or any suitable hosting service.
- Set your Webhook URL in Dialogflow to point to your deployed backend service.
- Test the integration by sending an email and checking if it gets fetched and processed correctly by the webhook.
Additional Configuration
- Consider implementing additional features such as parsing email contents or sending automatic replies using the Gmail API.
- You can also link this integration to other Dialogflow features, such as Google Assistant, for voice-activated email interaction.
pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib