Google Cloud Functions bot

Complete the following steps to create a Hangouts 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 Hangouts Chat room.

Prerequisites

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 navigation click 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 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) {
    
      var sender = req.body.message.sender.displayName;
      var image = req.body.message.sender.avatarUrl;
    
      var data = createMessage(sender, image);
    
      res.send(data);
    };
    
    /**
     * Creates a card with two widgets.
     * @param displayName the sender's display name
     * @param imageURL the URL for the sender's avatar
     */
    function createMessage(displayName, imageURL){
      var HEADER = {
        "title": "Hello " + displayName + "!"
      };
    
      var SENDER_IMAGE_WIDGET = {
        "imageUrl": imageURL
      };
    
      return {
        "cards": [{
          "header": HEADER,
          "sections": [{
             "widgets": [{
               "textParagraph": {
                 "text": "Your avatar picture:"
               }
             }, {
               "image": SENDER_IMAGE_WIDGET
             }]
          }]
        }]
      };
    }
    
  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 Hangouts 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 Hangouts 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 'https://goo.gl/yKKjbw'.
      • 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

Enviar comentarios sobre...

Si necesitas ayuda, visita nuestra página de asistencia.