Building your first Hangouts Chat Bot in Apps Script

Christian Schalk
Mar 11 · 5 min read

Building a chatbot is easy in Apps Script

The task of building a Google Hangouts chatbot can often seem daunting due to the diverse technology options and approaches. What is often not always known is how relatively easy it is to build chatbots by simply using Google Apps Script.

Here’s a step by step guide on how to build your first Google Hangouts chatbot using Apps Script.

What is Apps Script? (in about 100 words)

Apps Script is a cloud based scripting language and runtime environment based on JavaScript. It offers direct code access to a variety of Google products and APIs via its extensive library of services.

Apps Script is typically used to enhance the functionality of G Suite products (Google Sheets, Docs, Slides, Drive, Gmail ..) by offering a streamlined “low code” approach. For example to send an email in Apps Script, you can simply use:

MailApp.SendMail(“recipient@acme.com”,“My Subject”,“Hello from Apps Script!”);

Apps Script code development can also be done entirely in a browser, so no need to install a local software development environment..

For more on Apps Script see this excellent Apps Script Overview.

Pre-Reqs

Before stepping through this guide, please ensure that you meet the following prerequisites:

  • You must be able to create projects on the Google Cloud Platform (GCP).
  • Basic familiarity with Apps Script and/or JavaScript.
  • For domain wide publishing later on, you’ll also need G Suite Admin authority.
    For simple testing described in this walkthrough, G Suite Admin is not needed.

Steps

Here are the minimal steps needed to build and test your own chatbot in Apps Script.

Create and configure Apps Script Project

Build a new Apps Script project from scratch.

  • Go to script.google.com
  • Click “Create Project” button
  • Name the Project “Hello ChatBot”, click Save.

Configure the project for chat.

  • Access the Apps Script project’s Manifest file. View -> Show Manifest File
  • This opens the file appsscript.json (manifest) file in the script editor. You can now customize this file.
  • To add chat capabilities to this project, simply add ”chat”: { } to the manifest. For example:
{
“timeZone”: “America/Los_Angeles”,
“dependencies”: {
},“exceptionLogging”: “STACKDRIVER”,
“runtimeVersion”: “V8”,
“chat”: { }
}
  • Save the project.
  • In the Code.gs file, add the following function to handle the incoming chat message:
function onMessage(event) {  var response = ‘Hello, ‘ + event.user.displayName + ‘!\nYou entered: \”’ + event.message.text + ‘\”.’;  return { “text”: response };
}
  • Save the project.

That’s it for all the Apps Script code needed to run the chatbot!

Our next steps are to associate the bot’s Apps Script project with a GCP project by getting the Apps Script project’s Deployment ID and saving it for later when configuring a GCP project.

Get Deployment ID from the Manifest.

  • Publish > Deploy from manifest.
  • In the Deployments dialog box next to Latest Version (HEAD), click Get ID.
  • In the Deployment ID dialog box, copy the value listed for the Deployment ID. It will look something like:
AKfycbydf5S1N2v-_ZVNldWGBuY4azxodE06PTJGXKBU9hV3m
  • Copy and save the Deployment ID for later.
  • Click Close and Close to dismiss the dialog boxes.

Create and configure your GCP Project

The next steps will guide you how to create and configure your Google GCP project so that it can serve as the chatbot backend.

Create a new GCP project:

  • Goto https://console.cloud.google.com
  • Create a ‘New Project’.
  • You can name it ‘My First Chatbot’.
  • Select your associated Billing account.
  • Accept the defaults for Organization and Location.
  • Click CREATE
  • After project creation, select the new project in the console.

Enable the Chat API in project.

  • From the main GCP Dashboard, click “goto APIs overview” to open the “APIs and Services” Dashboard.
  • Click:
  • Search for ‘‘Hangouts Chat API”.
  • Once located, click ENABLE.
  • Once enabled, Click Credentials on the left side.
  • On the Credentials screen, click + CREATE CREDENTIALS and select Service account.

Enter the following:

  • Service account name: “My First ChatBot Service Account
  • An account id will be generated automatically.. (my-first-chat-bot-service-accou)
  • Optional: Add a service account description.
  • Click CREATE.

After the account is created, in the Service account permissions section, in the Role dropdown, select Project -> Owner.

  • Click CONTINUE and DONE.
  • Configure the Chat API.
  • Click on Configuration on the left menu.
  • Add the following options:
  • Bot name: enter ‘My First Bot’.
  • Avatar URL:
    https://www.gstatic.com/images/branding/product/1x/chat_48dp.png
    (You can provide your own publicly accessible image here if you want)
  • Description:A simple first Hello Bot’.
  • Functionality: select Bot works in direct messages.
  • Connection settings: select Apps Script project and paste the Deployment ID from the Apps Script project into the field.
  • Under Permissions, select Specific people and groups in your domain, and enter your own email (from within the G Suite domain).
  • Click SAVE.
  • Note: The SAVE button will remain active after saving, but the Bot status at the top of the page will change to “LIVE — available to users”.

Test your new bot!

  • In a new browser window, open chat.google.com in the same domain and user specified as a chatbot user that you specified in the previous step.
  • Next to BOTS click + to add your new bot. Search for your new bot.
  • Once located, click Add (and Message) to start a chat session with the new bot.
  • Enter “Hello!” and see the response!
  • Bonus step! Now that your bot is working, go back to the Apps Script code and add a change to the code so that it translates the message to French!

Hint, use:

LanguageApp.translate(event.message.text,’en’,’fr’)
  • As you save the project, you’ll notice that you can immediately test the bot with your latest code.

Stay tuned for more fun chatbot examples!

If there’s further interest in this subject, I’ll add some more posts with with some fun examples of what you can do with chatbots, such as linking them to APIs and services, and even tapping into Google’s AI & Machine Learning platform!

Christian Schalk

Written by

Google Cloud Developer Advocate who’s passionate about Developer Technologies.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade