Google Cloud Functions bot

Complete the following steps to create a Google Chat bot. This page demonstrates how to build a bot using Google Cloud Functions, where the bot responds with a simple message to a textual trigger in a Google Chat room.


To run this quickstart, you need:

  • Access to the internet and a web browser.
  • A Google account.
  • A Google Cloud account with billing enabled.

Step 1: Create the Google Cloud Function

For more information about how to create Google Cloud Functions using the Google Cloud Console, see the Cloud Functions quickstart.

  1. Open the Google Cloud Console.
  2. Create a new project.
  3. In the left navigation, go to APIs & services > Dashboard.
  4. Enable the Cloud Functions API. You may need to search for the Cloud Functions API in the console. To find the Cloud Functions API, do the following:

    • In the Google Cloud Console, in the left navigation click APIs & services > Library.
    • On the API Library page, in the search box, type 'Cloud Functions'.
    • In the results list, select Google Cloud Functions API.
  5. After the Google Cloud Function API has been enabled, in the search box type and select Cloud Functions and then click Create function.

  6. On the Create function page, do the following:

    • In the Name box, type 'HangoutsChatBot'.
    • Under Trigger, select HTTP trigger.
    • Under Source code, select Inline editor.
    • Under Runtime, select Node.js 8.
    • Under Function to execute, type 'helloHangoutsChat'.
  7. Also on the Create function page, replace the contents of the text editor for index.js with the following code:

     * Google Cloud Function that responds to messages sent from a
     * Hangouts Chat room.
     * @param {Object} req Request sent from Hangouts Chat room
     * @param {Object} res Response to send back
    exports.helloHangoutsChat = function helloHangoutsChat(req, res) {
      if (req.method === 'GET' || !req.body.message) {
        res.send('Hello! This function is meant to be used in a Hangouts Chat ' +
      const sender = req.body.message.sender.displayName;
      const image = req.body.message.sender.avatarUrl;
      const data = createMessage(sender, image);
     * Creates a card with two widgets.
     * @param {string} displayName the sender's display name
     * @param {string} imageURL the URL for the sender's avatar
     * @return {Object} a card with the user's avatar.
    function createMessage(displayName, imageURL) {
      const cardHeader = {
        'title': 'Hello ' + displayName + '!',
      const avatarWidget = {
        'textParagraph': {'text': 'Your avatar picture: '},
      const avatarImageWidget = {
        image: {'imageUrl': imageURL},
      const avatarSection = {
        'widgets': [
      return {
        'cards': [{
          'name': 'Avatar Card',
          'header': cardHeader,
          'sections': [avatarSection],

  8. Click Create.

On the Cloud Functions page, a progress indicator appears next to the Cloud Function that you just created. When the progress indicator disappears and a checkmark appears, your new Cloud Function has completed provisioning.

Step 2: Publish the bot to Google Chat

After the Cloud Function finishes provisioning, do the following to get the URL to trigger the function:

  1. On the Cloud Functions Overview page, click HangoutsChatBot in the list.
  2. On the details page for the Cloud Function, click the Trigger tab.
  3. Copy the URL under URL.
  4. In the Google API Console, enable the Google Chat API by doing the following:

    • In the navigation, click APIs & Services > Dashboard.
    • In the Dashboard, click Enable APIs and Services.
    • Search for "Hangouts Chat API" and enable the API.
    1. Once the API is enabled, click the Configuration tab. In the Configuration pane, do the following:

      • In the Bot name field, enter 'Quickstart bot'.
      • In the Avatar URL field, enter ''.
      • In the Description field, enter 'Quickstart bot'.
      • Under Functionality, select Bot works in direct messages.
      • Under Connection settings, select Bot URL and paste the URL for the Cloud Function trigger into the box
      • Set the Permissions for the bot.
    2. When you've finished configuring your bot, click Save Changes.

Step 3: Run the sample

To run the sample, do the following:

  1. Create a new direct message with the bot by clicking Find people, rooms, bots > Message a Bot.
  2. On the Add bot page, under Find a bot, type 'Quickstart Bot'.
  3. Select the Quickstart Bot.
  4. In the new direct message with the bot, type 'hello'.

The bot returns a card with a greeting and your avatar picture.

Further reading