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:
- Cloud Pub/Sub
This guide explains how to launch a Hubot bot in Hangouts Chat, using either type of endpoint.
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
receiving events from Hangouts Chat, both modes create a
HangoutsChatTextMessage object (which extends from Hubot's
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.
To use the Hangouts Chat adapter in HTTP mode, you need to pass a
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
bin/hubot in order to avoid port conflict. Alternatively, if
you wish to keep the HTTP daemon running, you can set
something other than
$> 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' # 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'
To run the Hubot instance locally, run the following command from the
$> 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.
First, create an app.yaml file in your Hubot directory. The contents will resemble the following example:
runtime: nodejs8 env_variables: PORT: 8080
Next, check the instructions on this page to configure environment variables.
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 startto start your application. Setup the scripts property in your package.json to configure npm start to invoke Hubot.
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:
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.