Developing bots with Apps Script

Apps Script is one of the quickest ways to create a chat bot for Google Chat.

  • You can get a bot up and running in just a few minutes directly in your browser.
  • You don't need to worry about running and managing servers, ongoing maintenance or operational costs, or even downloading and setting up a development environment.
  • It's very easy to call Google APIs—especially Google Workspace APIs—because with Apps Script there's no download or setup, auth is handled automatically, and Google API calls are just like native function calls.

This guide explains how to create and register a bot using Apps Script.

Getting started

This section shows you how to quickly create a chat bot using Apps Script.

Step 1: Copy the Apps Script template

The easiest way to get started creating an Apps Script bot is to use the Google Chat bot template. This creates a new Apps Script project with the methods that you need. After that, populate the methods as required to implement your bot logic, or to integrate with a bot that you've built. The following code shows an example of the template populated for a simple bot.

 * Responds to a MESSAGE event in Google Chat.
 * @param {Object} event the event object from Google Chat
function onMessage(event) {
  var name = "";

  if ( == "DM") {
    name = "You";
  } else {
    name = event.user.displayName;
  var message = name + " said \"" + event.message.text + "\"";

  return { "text": message };

 * Responds to an ADDED_TO_SPACE event in Google Chat.
 * @param {Object} event the event object from Google Chat
function onAddToSpace(event) {
  var message = "";

  if ( {
    message = "Thank you for adding me to a DM, " + event.user.displayName + "!";
  } else {
    message = "Thank you for adding me to " +
        ( ? : "this chat");

  if (event.message) {
    // Bot added through @mention.
    message = message + " and you said: \"" + event.message.text + "\"";

  return { "text": message };

 * Responds to a REMOVED_FROM_SPACE event in Google Chat.
 * @param {Object} event the event object from Google Chat
function onRemoveFromSpace(event) {"Bot removed from ",
      ( ? : "this chat"));

Step 2: Edit the onMessage function

By default, the onMessage function in the template returns a Message object that contains text and a simple card. You can edit this function to return text or specific card widgets.

function onMessage(e) {
  return { 'text': 'You said: \`' + e.message.text + '\`' };

Step 3: Get deployment ID

Before you can register your bot, you'll need to get the deploymentID for this specific bot.

New editor

  1. Click Deploy > New deployment.
  2. Under Select type, click Add-on.
  3. Fill out the options and click Deploy.
  4. Under "Deployment ID," click Copy.

Legacy editor

  1. Choose Publish > Deploy from manifest.
  2. Click Get ID from a deployment.

See the Release Management guide to learn about recommended practices for using deployment IDs.

Step 4: Register the bot

You can register your bot from the Google Cloud developer console. On the bot registration screen, you will tell us the bot name, avatar URL and description. For testing, you can enable 'Bot works in DMs' and 'Bot works in Spaces'. Under connection settings, choose Apps Script and paste the deployment ID you copied in the previous step above.

Step 5: Test your bot

To test your bot, you can start a DM with it or @mention it into a Space. When you talk to it, it will respond to whatever the Message response is in Step 2. That's it!

Apps Script bot concepts


Apps Script supports several event handler functions that your bot can implement. Each function handles a different event type and each function receives a single argument e, which is an Event object.

This function is executed when your bot is added to a space. Your bot can either be directly added to the space or added through an @mention. In the second case, the event e will also have a message property. This function should return a Message object, which is usually a welcome message to tell end users about your bot or a response to the @mention.
This function is called when your bot is already in the space and the user @mentions your bot. It should return a Message object containing your bot's response.
This function is called when the user removes your bot from their DM list or from a space. The return value of this function is ignored since your bot has been removed and cannot post any more messages.

The following example implements onAddToSpace and onRemoveFromSpace:

// onAddToSpace() is invoked when bot is added to a space or when
// a user initiates / re-initiates a direct message with the bot.
function onAddToSpace(e) {
  if (! {
    return { 'text': 'Thanks for adding me to "' +
        ( ? : "this chat") + '"!' };
  } else {
    return { 'text': 'Thanks for adding me to a DM!' };
// onRemoveFromSpace() is invoked when bot is removed from a space
// or when a user removes a direct message with the bot.
function onRemoveFromSpace(e) {}

Please note that might not be present in direct messages between humans.

Interactive cards

Like other bots, Apps Script bots can display cards that are interactive. To learn about how to make cards interactive, check out that documentation on interactive cards. The main difference for Apps Script bots is that they can specify a specific method to call when the onClick event is fired by using action.actionMethodName and action.parameters key/value pairs as method parameters.


Apps Script bots that access protected resources need to ask users to authorize this access the first time it runs for each user. This doesn't require any work from you, but you should be aware that users may see an authorization dialog the first time they use your bot.

Apps Script handles authorization at the script level. Bots that require authorization cannot perform any actions until the end user authorizes the bot. If you would like your bot to show a welcome message when it has not been authorized and is directly added to a space, you can specify a fallback message in the manifest. If your bot requires initialization logic, you may need to duplicate this logic in the onMessage action. For example:

function onMessage(event) {
  var userProperties = PropertiesService.getUserProperties();
  if (!userProperties.getProperty('initialized')) {
    // handle the onAddToSpace initialization logic here.
    userProperties.setProperties({initialized: 'true'});
  // Handle normal onMessage logic.

Async messages

Some bots might need to send messages into Google Chat based on an external trigger, such as a Time-driven trigger in Apps Script. We plan to natively integrate the Google Chat API into Apps Script for this use case. In the meantime, the only way to achieve this currently is via the external HTTP API (see documentation). This requires the use of a Cloud service account (see documentation) via the OAuth2 for Apps Script library. A full example bot which posts a message every minute to every space it's in:

// Example bot for Google Chat that demonstrates bot-initiated messages
// by spamming the user every minute.
// This bot makes use of the Apps Script OAuth2 library at:
// Follow the instructions there to add the library to your script.

// When added to a space, we store the space's ID in ScriptProperties.
function onAddToSpace(e) {
      .setProperty(, '');
  return {
    'text': 'Hi! I\'ll post a message here every minute. ' +
            'Please remove me after testing or I\'ll keep spamming you!'

// When removed from a space, we remove the space's ID from ScriptProperties.
function onRemoveFromSpace(e) {

// Add a trigger that invokes this function every minute via the
// "Edit > Current Project's Triggers" menu. When it runs, it will
// post in each space the bot was added to.
function onTrigger() {
  var spaceIds = PropertiesService.getScriptProperties()
  var message = { 'text': 'Hi! It\'s now ' + (new Date()) };
  for (var i = 0; i < spaceIds.length; ++i) {
    postMessage(spaceIds[i], message);
var SCOPE = '';
// The values below are copied from the JSON file downloaded upon
// service account creation.
// For SERVICE_ACCOUNT_PRIVATE_KEY, remember to include the BEGIN and END lines of the private key

// Posts a message into the given space ID via the API, using
// service account authentication.
function postMessage(spaceId, message) {
  var service = OAuth2.createService('chat')
  if (!service.hasAccess()) {
    Logger.log('Authentication error: %s', service.getLastError());
  var url = '' + spaceId + '/messages';
  UrlFetchApp.fetch(url, {
    method: 'post',
    headers: { 'Authorization': 'Bearer ' + service.getAccessToken() },
    contentType: 'application/json',
    payload: JSON.stringify(message),

Manifest file

The following is an example of the Apps Script manifest file that must accompany your script. If you did not start your project from the Google Chat bot template, then you need to edit the manifest file to add the "chat": {} object to it. You should also ensure that "runtimeVersion": "V8" is not in the manifest, as bots do not currently support the V8 runtime.

  "timeZone": "America/Los_Angeles",
  "dependencies": {},
  "chat": {
    "addToSpaceFallbackMessage": "Thank you for adding me!"
  "exceptionLogging": "STACKDRIVER"

It's possible for a user to add your bot to a space before authorizing your bot. In this case, your bot is unable to respond to the add-to-space event. You can provide a welcome message to display if this occurs, using the addToSpaceFallbackMessage field.

The manifest file is named appsscript.json and is part of your Apps Script project. To view the manifest file in the Apps Script editor, select View > Show manifest file.