The problem
You probably know the Google Drive Chatbot letting you receive notifications about new comments, access requests, and files shared with you, to Google Chat.
But is there something similar with Google Calendar in order to have all notifications in one place ?
But is there something similar with Google Calendar in order to have all notifications in one place ?
Unfortunately not (or I didn't find it at least) so let's see how to fill the gap here
Constraints
First of all, We need to create a user-friendly Chatbot like the Google Drive Chatbot. Therefore, we will need the Card service to render a nice interface.
Then I need to manage Google calendar Notifications. It could be done with the Calendar Advanced Service (i.e. Calendar API) and Push notifications. But when I look into it, it seems to me a bit complicated : HTTPS server for callback receiver, notification channel for each resource endpoint,.... So after taking a step back, I remembered those notifications are received by email. Why not use those emails to get informations and create our Chat notification. Even more, why not delete those emails after using them ? It would reduce the inbox volume and do a bit of green IT. So my choice felt into using a calendar event trigger scanning Gmail inbox with GmailApp AppsScript Service
Finally, in order to create a ChatBot that scans Gmail and Calendar, we need to have a dedicated GCP project with the right APIs activated. This GCP project will also have a service account because we are sending a message asynchronously with the Chat REST API and not with a webHook. This last point means we need to create a OAuth2 management (i.e. to authenticate the script as the user). By chance a very kind person already create a library for that : apps-script-oauth2
Solution architecture

1. OnInstall : when the addon is installed, a Gmail filter is created that will applied the incoming calendar mail notification in a specific label. It also create a onEventUpdate trigger for the user calendar 2.An event is create, deleted, or updated : an email notification is sent to the user inbox and the trigger function is launched3.In the Trigger function : a scan is made into the specific Gmail label to retrieve the emails 4.From the emails ics attached file : we retrieve the event id which allows to retrieve all event informations needed5.With the event informations : we create the card message depending on the email notification/event status (update, invitation or cancelation) and the event organizer (if it's you, no notification needed) 6.Card sending : We send the card message to the bot asynchronously
Code & settings
The code can be separated by applications managed : Gmail, Calendar & Chat
Gmail :Get the event properties from notification emailCreate the Gmail filter using the Advanced Gmail API Delete the filter when ChatBot is remove or uninstalled
Calendar :Get the event properties from an event id . Those properties will be used for the card creation a doGet(e) function for the onEventUpdate trigger creation. Unfortunately, the trigger could not have been created directly by the code with CalendarTriggerBuilder . As we do not know the exact reason, we overcome this issue with a WebApp doing this task. The webApp deployment is setup with the executed as option as the script owner in order to create a trigger in the current script.
Chat :onAddToSpace(e) function to setup the Gmail filter, the onEventUpdate trigger and the space name to know where to post the messageonMessage(e) function to answer to a direct message . This will give the standard informations about the chatBotpostMessage(spaceId, message) function that actually sends the message using the OAuth2 library createCard(subject, eventId) function creating the card with event informations thanks to the eventID. The subjectcomes from the email : event deleted, updated or inviteonCardClick(event) function to update the user invite answer (YES,NO,MAYBE) to the event Chalendar function as the main function setup in the OnEventUpdated Trigger. This function get the emails in the dedicated label and send a card message to the ChatBot. We also have a 5 minutes waiting in case the email comes after the event update information received by the trigger. This will prevent to run the function without any email while a calendar notification is on the way. If the email takes more than 5 minutes to get to Gmail, a standard information message card is sent to inform the user.
Apart from application management, there are also .gs files containing :
The OAuth2 library functions appsscript.json : because the chatBot is still an addon, the script json should be defined with the chat application parameter Constants : service account informations (private key, email account)
addToSpaceCard send when the chatBot is installed or a message is sent to it
informationMessage card to inform the user if the notification email is not yet in Gmail
colors json object for button attendance management (i.e. button for the user to answer YES,MAYBE or NO to the event)
TIMELIMIT to decide how many milliseconds we should wait for the email
All the code is here : Chalendar open souce code
After that, you can deploy the script as an Addon. There will be 2 deployments : one for the trigger creation webApp and one for the chatBot
Finally, the chatBot should be published. In this case a dedicated Google Cloud Platform (GCP) project should be created. Then on this project :
Define the API used : Gmail, Calendar, Chat , Google Workspace MarketplaceFor Gmail & Calendar, just enable themFor Google Workspace Marketplace , define the addOn type,the scopes and the play store informations ( logo, help email & website). More information hereFor Chat, you'll have to defined the configuration with the Addon deployment ID from your script and the main logo
This last part might be a bit confusing the first time but try and test without worrying because it won't break your script. Comment below if you need some help !
Conclusion
We've been using this script for 6 month now and it works well. However some improvement can be done :
Code & settings
The code can be separated by applications managed : Gmail, Calendar & Chat
Gmail :Get the event properties from notification emailCreate the Gmail filter using the Advanced Gmail API Delete the filter when ChatBot is remove or uninstalled
Calendar :Get the event properties from an event id . Those properties will be used for the card creation a doGet(e) function for the onEventUpdate trigger creation. Unfortunately, the trigger could not have been created directly by the code with CalendarTriggerBuilder . As we do not know the exact reason, we overcome this issue with a WebApp doing this task. The webApp deployment is setup with the executed as option as the script owner in order to create a trigger in the current script.
Chat :onAddToSpace(e) function to setup the Gmail filter, the onEventUpdate trigger and the space name to know where to post the messageonMessage(e) function to answer to a direct message . This will give the standard informations about the chatBotpostMessage(spaceId, message) function that actually sends the message using the OAuth2 library createCard(subject, eventId) function creating the card with event informations thanks to the eventID. The subjectcomes from the email : event deleted, updated or inviteonCardClick(event) function to update the user invite answer (YES,NO,MAYBE) to the event Chalendar function as the main function setup in the OnEventUpdated Trigger. This function get the emails in the dedicated label and send a card message to the ChatBot. We also have a 5 minutes waiting in case the email comes after the event update information received by the trigger. This will prevent to run the function without any email while a calendar notification is on the way. If the email takes more than 5 minutes to get to Gmail, a standard information message card is sent to inform the user.
Apart from application management, there are also .gs files containing :
The OAuth2 library functions appsscript.json : because the chatBot is still an addon, the script json should be defined with the chat application parameter Constants : service account informations (private key, email account)
addToSpaceCard send when the chatBot is installed or a message is sent to it
informationMessage card to inform the user if the notification email is not yet in Gmail
colors json object for button attendance management (i.e. button for the user to answer YES,MAYBE or NO to the event)
TIMELIMIT to decide how many milliseconds we should wait for the email
All the code is here : Chalendar open souce code
After that, you can deploy the script as an Addon. There will be 2 deployments : one for the trigger creation webApp and one for the chatBot
Finally, the chatBot should be published. In this case a dedicated Google Cloud Platform (GCP) project should be created. Then on this project :
Define the API used : Gmail, Calendar, Chat , Google Workspace MarketplaceFor Gmail & Calendar, just enable themFor Google Workspace Marketplace , define the addOn type,the scopes and the play store informations ( logo, help email & website). More information hereFor Chat, you'll have to defined the configuration with the Addon deployment ID from your script and the main logo
This last part might be a bit confusing the first time but try and test without worrying because it won't break your script. Comment below if you need some help !
Conclusion
We've been using this script for 6 month now and it works well. However some improvement can be done :
- Better notification management : if the email does not comes before the 5 minutes wait, we do not receive the notification. We could use the real Push notifications tool to have a proper events analysis
- The limits might have not been reached for bigger company and users number
The code could be improved with you !
That's why its open source and you can comment it directly in this document : Chalendar open souce code
Comments