How I created a Calendar Assistant with Google Apps Script that automates a daily voice summary with Google Speech to Text, ChatGPT and Google Chat

Stéphane Giron
4 min readApr 14, 2023

--

Have you ever wished for a personal assistant who could give you a quick summary of your day’s events while you’re busy getting ready in the morning? I worked on a Google Apps Script that does just that! By collecting events from in Google Calendar, generating a summary using ChatGPT, and creating a voice file with Google Text to Speech, the script will send a daily summary straight to their Google Chat Spaces.

Google Speech To Text / ChatGPT / Google Calendar integration

Prerequisite : You must have a GCP Project with this API activated :
Google Calendar
Google Drive
Google Text to Speech

Collecting events and generate a summary

The first step in creating this automated daily summary is to collect events from Google Calendar. This is really easy with Apps Script and can be done with few line of code :

const now = new Date();
let start = new Date(now.getTime())
start = changeTime(start)
let end = new Date(now.getTime() + (24 * 60 * 60 * 1000))
end = changeTime(end)
const events = CalendarApp.getDefaultCalendar().getEvents(start, end);

Now we need to generate the summary for that we will use ChatGPT from Open AI, a language model that can generate human-like responses to text prompts. The Prompt construction will be :

Prompt example to generate your daily meetings summary

As you can see we instruct ChatGPT to generate the summary as an assistant and not just the summary of the events provided. This is the most important and you can customize it to better answer your needs.
We use the Open AI API through Google Apps Script :

Gpt.query = function(data) {
const url = 'https://api.openai.com/v1/chat/completions';
const params = {
method : "get",
headers : {"Authorization": "Bearer YOUR_API_KEY" },
contentType: 'application/json',
payload : JSON.stringify(data),
muteHttpExceptions :true
};
const req = UrlFetchApp.fetch(url,params);
const txt = req.getContentText();
const json = JSON.parse(txt)
return json;
}

The result was a concise and easy-to-understand summary that included all the important details of the day’s events. This summary was then used to create a voice file using Google Text to Speech, which we’ll discuss in the next section.

Creating a voice file with Google Text to Speech

Once the summary was generated using ChatGPT Open AI, the next step was to create a voice file that could be easily accessed and listened to. This was achieved using Google Text to Speech, a powerful API that can convert text into natural-sounding speech.

It was easy to achieve this using the Google API, we just need to ass the scope “https://www.googleapis.com/auth/cloud-platform” in the appsscript.json file to add the good scope to the token generated with Apps Script and then do the call.

Tts.synthetize = function(txt) {
let params = {
method : "post",
headers : {"Authorization": "Bearer "+ScriptApp.getOAuthToken()},
contentType: 'application/json',
payload : JSON.stringify(this.getParams(txt)),
muteHttpExceptions :true
};

const url = 'https://texttospeech.googleapis.com/v1/text:synthesize';
const req = UrlFetchApp.fetch(url,params);
// console.log(req.getContentText())

return JSON.parse(req.getContentText());
}

One of the specific part of the code was the wav file creation from the API Response.

let bytes =  Utilities.base64Decode(voice.audioContent, Utilities.Charset.UTF_8);
let blob = Utilities.newBlob(bytes, 'audio/wav', 'My Day '+Utilities.formatDate(now,Session.getTimeZone(), 'yyyy-mm-dd')+'.wav');
let file = DriveApp.createFile(blob);

We store the file in Google Drive as it is a convenient way to do that but it was also possible to d save in Google Cloud Storage for example. You can view an example of Cloud Storage API usage in this article.

Overall, the integration of Google Text to Speech with the script was seamless and easy to implement. The result was a high-quality voice file that accurately represented the summary of the day’s events.

Sending the summary to a Google Chat Spaces

The final step in creating this automated daily summary was to send the voice file to a Google Chat Spaces. I selected Google Chat because it offered a more flexible way to interact with the summary, and it was easy to set up using Webhook integration.

First you need to create a space and then create a Webhook, you can follow this help article : link.

The code is really simple, we don’t send a complex card just a text message with the link :

const webhook = 'https://chat.googleapis.com/v1/spaces/xxxxx/messages?key=123_xyz'
const message = { 'text': 'Hello here your daily summary : '+file.getUrl()}
const options = {
'method': 'post',
'contentType': 'application/json',
'payload': JSON.stringify(message)
};
UrlFetchApp.fetch(webhook, options);

Now we just need to create a trigger to run the function each morning in order to receive a daily summary planning by the personnel assistant powered by ChatGPT and Google Text to Speech. Result in Google Chat mobil :

Daily Agenda Summary with ChatGPT, Google Text to Speech and Google Chat.

Conclusion

In just a few hours, I was able to develop an amazing script that automates the process of generating a daily summary of events using AI and sends it to a Google Chat Spaces.

Thanks to the power of Google Workspace and Google Cloud, I was able to quickly and easily integrate with various Google applications and APIs, making the development process a breeze.

But the best part of this script is the fun and cool way it allows us to access our daily planning. With the integration of ChatGPT Open AI and Google Text to Speech, we can now have our own personal assistant that talks back to us! If you’re looking for a fun and convenient way to access your daily planning, give this script a try and see how it can transform your mornings!

--

--