Integrating Hubot bots with Hangouts Chat

Hubot is a great way to build a bot that works on multiple platforms. With the Hangouts Chat Hubot Adapter, you can easily utilize your Hubot bots in Hangouts Chat. The adapter feeds messages into Hubot and serves the replies.

The Hangouts Chat Hubot adapter supports two types of endpoints:

  • HTTP
  • Cloud Pub/Sub

This guide explains how to launch a Hubot bot in Hangouts Chat, using either type of endpoint.

Install

Follow the instructions in Getting Started with Hubot to download and install the necessary tools: Node.js, npm and the Hubot generator for Yeoman.

You can try creating a new instance of Hubot that uses the built-in shell adapter, install Hubot scripts, play with it, and get a feel of the Hubot ecosystem and developing a Hubot bot.

Using the Hubot Hangouts Chat adapter

This section guides you through creating a Hubot bot that uses the Hangouts Chat adapter, deploying it in AppEngine, and publishing the bot in Hangouts Chat. You can also choose to deploy the bot in any system of your choice other than AppEngine.

The Hangouts Chat Hubot adapter supports two modes: HTTP and Cloud Pub/Sub. The HTTP mode spins up an Express web server and listens for events in a user specified port. The Cloud Pub/Sub mode creates a Pub/Sub subscriber and pulls events from a user specified subscription. On receiving events from Hangouts Chat, both modes create a HangoutsChatTextMessage object (which extends from Hubot's TextMessage object) and pass it on to your Hubot script. The response from your Hubot script is posted to the chat room or DM in Hangouts Chat from which the original message event was published to the bot.

Create a Hubot instance

Make sure to set up Node.js, the npm package manager, and the Yeoman generator for Hubot before proceeding.

Suppose you want to create a bot called "myhubot". Start by creating a new directory and then create your Hubot instance it in.

$> mkdir myhubot
$> cd myhubot
$> yo hubot

At this point, Yeoman asks a few questions about who is creating the bot and which adapter to use. Specify google-hangouts-chat for the adapter.

Alternatively, you can install the adapter separately using the following command:

$> npm install --save hubot-google-hangouts-chat

In Hubot the actual bot behaviour is implemented using scripts. Hubot comes with a sample script that you can use for testing. Once you have everything setup, you can customize and add your own scripts to implement the desired bot behaviour.

Configure options for the Hangouts Chat adapter

Options are passed to the Hangouts Chat adapter using environment variables.

Service Account

To use the full functionality of the adapter, you need to setup a service account for your bot. For Pub/Sub bots, a service account is mandatory. For HTTP bots, not having a service account limits you to the reply method for responding to messages.

Follow the Using service accounts guide to create a service account and download the JSON file containing your key. Then, set the path to your key in an environment variable:

# Point to the private key json file of the service account you created.
$> export GOOGLE_APPLICATION_CREDENTIALS='Path to the service account key json file'

HTTP Options

To use the Hangouts Chat adapter in HTTP mode, you need to pass a PORT option with value 8080. The Express server spun up by the adapter listens to events on this port. You must also disable the default HTTP daemon by passing -d to bin/hubot in order to avoid port conflict. Alternatively, if you wish to keep the HTTP daemon running, you can set EXPRESS_PORT to something other than 8080.

$> export PORT=8080

Cloud Pub/Sub Options

See Set up a Pub/Sub endpoint to setup a GCP project, Cloud Pub/Sub topic, subscription, and service account to be used by your bot. Ignore the sample code in the link; in the paragraphs below, you'll create a Hubot bot with the Hangouts Chat adapter.

To use the Hangouts Chat adapter in Pub/Sub mode, you need to set the following options:

# To toggle the adapter to Pub/Sub mode.
$> export IS_PUBSUB=true

# Set the IDs of the project and subscription you created.
$> export PUBSUB_PROJECT_ID='GCP Project ID'
$> export PUBSUB_SUBSCRIPTION_ID='GCP Cloud Pub/Sub Subscription ID'

Run locally

To run the Hubot instance locally, run the following command from the myhubot directory:

$> bin/hubot -d -a google-hangouts-chat

The following message appears in the console if the Hangouts Chat Hubot Adapter was initialized successfully:

Hangouts Chat adapter initialized successfully

Depending on whether you configured a HTTP adapter or a Cloud Pub/Sub adapter, other relevant information can also appear on the screen.

Deploy in AppEngine

Follow the instructions in the AppEngine Quickstart to set up the GCP project and development environment.

Once this is set up, follow the steps below for configuring and deploying your Hubot instance in AppEngine.

Create app.yaml

First, create an app.yaml file in your Hubot directory. The contents will resemble the following example:

runtime: nodejs8
env_variables:
  PORT: 8080
  

Configure environment

Next, check the instructions on this page to configure environment variables.

Create package.json

Finally, create a package.json file that specifies the node.js version and the scripts property used to launch the app.

  • Specify the Node.js version you would like to use with the engines property in your package.json file.
  • AppEngine uses npm start to start your application. Setup the scripts property in your package.json to configure npm start to invoke Hubot.
# Sample snippet of package.json file

"dependencies" : {
  "hubot": "^3.1.1",
  "hubot-google-hangouts-chat": "2",
  # more deps...
}

"scripts": {
  "start": "bin/hubot -d -a google-hangouts-chat"
},
"engines": {
  "node": ">=8"
}

# Once you've set up everything, deploy the app in AppEngine
$> gcloud app deploy

Publish in Hangouts Chat

Follow the instructions in Publish bots page to publish a bot in Hangouts Chat via the Google Cloud Console. Configure the appropriate endpoint (HTTP or Cloud Pub/Sub) in the bot configuration page. If you created a GCP project in the previous step to deploy your Hubot instance in AppEngine, you can use the same GCP project to publish your bot.

Test the bot

Once you publish your bot, you can add it to a room or start a direct message with it in Hangouts Chat. Your bot should respond to messages sent to it. Hubot comes with a sample script in its scripts folder. Uncomment the code in the sample script that responds to messages containing the word "badger" and restart the bot.

If you named your bot "myhubot" and added it to a room, you can message it as shown below:

@myhubot badger

You should see the following response from your bot:

Badgers? BADGERS? WE DON'T NEED NO STINKIN BADGERS

You can now customize or add Hubot scripts to your Hubot instance to implement the desired functionality for your bot. To see example scripts specific to Hangouts Chat, look at the Hangouts Chat Adapter GitHub Repository.

Enviar comentários sobre…

Precisa de ajuda? Acesse nossa página de suporte.