September 28th, 2022
How to Connect Your AI Assistant with Google Calendar
Sonam Pankaj
In this blog post we will learn,step by step, how to connect your Rasa App with Google Calendar API and add an event directly from your AI Assistant. The repository has been provided.
How to connect your chatbot with google calendar API in steps:
Create a project in Google console, then go to the dashboard search for google calendar API and enable it for your project.
To Generate and download credentials go to OAuth Consent Screen tab, fill in the consent screen, along with scopes
Now go to the credentials tab, download json file from OAuth 2.0 Client IDs
Rename it to credentials.json and include it in the Source folder
Start using Google OAuth SDK for Python using pip command
pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib
Copy all the requirements in action.py file
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
import pickle
Then add Scopes, credentials and get service function in action.py file
def get_calendar_service():
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_FILE, 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('calendar', 'v3', credentials=creds)
return service
Also Add Event Action function, remember the name of the add action function i.e, action_add_event
def add_event(event_name, time):
# creates one hour event tomorrow 10 AM IST
service = get_calendar_service()
d = datetime.now().date()
tomorrow = datetime(d.year, d.month, d.day, 10)+timedelta(days=1)
start = tomorrow.isoformat()
end = (time + timedelta(hours=1)).isoformat()
event_result = service.events().insert(calendarId='primary',
body={
"summary": event_name,
"description": 'This is a tutorial example of automating google calendar with python',
"start": {"dateTime": time.isoformat(), "timeZone": 'Asia/Kolkata'},
"end": {"dateTime": end, "timeZone": 'Asia/Kolkata'},
}
).execute()
Now for Data folder
- NLU file
Add new intent
- Add intents, entities, slots, forms and add_action_event in domain.yml
- Add Activate form and submit form in rules.yml file, also add add_action_event to submit the form.
In stories.yml file add active form
Run Rasa Action server
Run Rasa Shell
Output on google Calendar api
The summary should be visible on run actions command line
For code-repo you can visit https://github.com/RasaHQ/HandsOn_with_rasa