One chatbot’s journey from Apps Script to Cloud Functions
The Aim 🎯
So you might have heard that here at Cobry, we’re interested in Google Workspace. Naturally, we like to keep an eye on any new updates that Google puts out about changes that they’re making to the platform. They have a really nice blog that they post updates to, and with that, there’s a handy XML feed that allows you to grab these posts programmatically. Lightbulb moment 💡 - Let’s make a chatbot for this that messages us whenever there’s a new update! So we assembled the Dev Team over a fresh cafetiere and got started on a new Apps Script project.
Apps Script 🧾
Apps Script, if you haven’t heard, is a scripting platform embedded into Google Workspace. It runs on top of JavaScript, making it extremely versatile, and it links into all the Google Workspace APIs effortlessly, bringing automations, workflows and custom sub-systems to your fingertips.
Originally, we had a super simple Apps Script project which fired off every hour to check for new updates from the XML feed. If it found new blog posts, it would fire them into a google chatspace using a webhook. Doesn’t get much simpler than that! They looked like this:
We found that it worked really well for us internally, but that wasn’t the end of the road for this chatbot. We realised that this chatbot could be loved by others, too, and so we set about preparing it for the big bad world. At first, we tried to keep it in Apps Script alone. It has a decent deployment system which might have worked alright, but it would have required end users to create webhooks and somehow pass it into their instance of the script. Plus, Apps Script limits the maximum number of users at any time, so we were concerned about future-proofing the app. Ultimately, we realised that if we wanted our chatbot dreams to come true, we were going to need a bigger boat.
The Answer 🌞
Once we realised we needed more power, we came up with a small architecture on Google Cloud Platform (GCP), using Cloud Functions, Firebase, and a Cloud Pub/Sub queue. If Apps Script is a bike, then GCP is a bus. Keep reading to see how each part plays its role!
Cloud Functions ⛅
Cloud Functions is a managed service provided as part of the Google Cloud Platform (GCP). It’s sort of similar to Apps Script - it provides a serverless environment to run scripts and small instantaneous programs on. You simply write your code in your favourite language, and choose the trigger method - everything else is taken care of for you, but you’re still free to modify the environment parameters if you feel like it. Cloud Functions is ideal here as it lets us split the script up into three discrete, interoperating parts; a queue publisher, a message sender, and a room handler (as shown in the above diagram).
The Subscriber / Room Handler
This function is responsible for the chatbot’s behaviour when it’s added to a Google Chat Space. It’s triggered when a user adds the bot, and it hooks into Firebase to make an entry for the space, to keep track of when its last update was. It also sends a nice greeting message! When the bot is removed from a space, this function also takes care of cleaning up and deleting information.
The Updater / Queue Publisher
This script is triggered periodically using Cloud Scheduler. We can set the interval to be anything we like, but as a basepoint, we set it to one hour. It checks the XML feed for new blog posts, and if it finds any, it publishes the contents to our Pub/Sub queue. Pub/Sub, by the way, is GCP’s message queue, perfect for passing data around different cloud components in a robust, buffered, replayable fashion (more on than in a moment).
The Chatter / Message Sender
This function is triggered when messages are published to the Pub/Sub queue. You can see now how these functions link together! It checks the date and time of when the blog post was published to the blog, and if the message is new, it sends it to the chatroom for the end users to enjoy.
Cloud Pub/Sub 📪
When we pictured the chatbot in production, we imagined it sending messages to many rooms. We didn’t know how many exactly, but we thought somewhere in the hundreds seemed about right. So we wanted scalability, and we wanted redundancy. Cloud Functions allowed us to split the program into microservices, now it was a case of linking things together. Pub/Sub allowed us to queue up messages for the sender to process instantaneously. If everything goes to plan, we can acknowledge the message and terminate the process. If something fails, though, the message will chill out in the message queue for us to give it another shot. This acts as a buffer, and gives us better control of how the whole app will behave on the cloud. It also gives us visibility for better analysis and debugging. Future proof? Check!
Firebase 🔥
The last component of our chatbot is a very lightweight database stored in Firebase. This allows us to keep track of when each chat space last received a message, so that we don’t accidentally send duplicate messages. This ensures that each space only gets each new update once. Our microservices are able to link into Firebase easily thanks to the Node.js environment they’re running in.
Why you should care ☕
Future proofing, scalability, durability and concurrency are all words used to describe an infrastructure that performs well, and doesn’t break under high load. It’s all super vague, so let’s put it in context. If we were to keep this application on Apps Script, things would start to error out as soon as we went above ~100 spaces subscribed to the bot. If Google released more than 5 updates in a day, that number would go down to 20 spaces. There are also limitations in the services we can invoke and features we can deploy. What would happen if we wanted to make the bot interactive, allowing users to upvote and downvote updates, and making a leaderboard of updates? Yeah… Dream on…
To put the power of GCP into perspective, cloud run can easily scale to more than 9,000,000 requests per minute. For comparison, Google Search gets 3.8 million searches per minute. With pub/sub, we can process 2GB of updates a second, and ping to around 3,000 spaces a second. Talk about using a jackhammer to drive a nail into butter!
Install the Bot 💬
You’ve read this far, don’t you want to see the bot in action? To install the bot for your organisation, click here head to the Workspace Marketplace and follow the steps to get it set up (or just search for ‘Workspace Updates Bot’ on the marketplace).
Hopefully this blog has given you some ideas about what sort of chatbot you’d like to use. Send users forms, images, videos, and anything else you can think of in an instantaneous message - the possibilities are endless! There are loads of chatbots already on the Workspace Marketplace ready for you to use right now. And if you can’t find exactly what you’re looking for, you can create bespoke chatbots using Apps Script or GCP.
We’ve got expertise in making GCP do cool things, so leave your email below and we can help you take advantage of the cool tech Google has to offer 👇