The information on this page pertains to the legacy version of Botkit, 0.7 and below and may be out of date! Documentation for the new v4+ version of Botkit can be found here.

Botkit and Google Hangouts

Table of Contents

Getting Started

  1. Install Botkit on your computer

  2. Create a Botkit powered Node app:

  botkit new --platform googlehangouts

Things to note

Hangouts Chat supports team collaboration by providing:

Hangouts Chat communicates with your bot via an HTTP endpoint, you must specify an endpoint in the Chat API configuration tab when you publish your bot.

Create a Controller

To connect Botkit to Google Hangouts, use the constructor method, Botkit.googlehangoutsbot(). This will create a Botkit controller with all core features as well as some additional methods.

Botkit.googlehangoutsbot()

Argument Description
config an object containing configuration options

Returns a new Botkit controller.

The config argument is an object with these properties:

Name Type Description
studio_token String An API token from Botkit Studio
debug Boolean Enable debug logging
endpoint string Bot endpoint link
token string to verify bot authenticity

For example:

var Botkit = require('botkit');
var controller = Botkit.googlehangoutsbot({
    endpoint: 'YOUR_ENDPOINT',
    token: "YOUR_TOKEN"
});

Event List

In addition to the core events that Botkit fires, this connector also fires some platform specific events.

Incoming Events

Event Description
message_received The bot received a direct message from a user
direct_message This event indicates that your bot was added to a DM
bot_room_join This event indicates that your bot was added to a shared room
bot_room_leave This event indicates that your bot was removed from a shared room
bot_dm_join This event indicates that your bot was added to a 1:1
bot_dm_leave This event indicates that your bot was removed from a 1:1
card_clicked The bot received a card click

Here's an example on how to handler incoming events :

controller.hears('hello', 'message_received', function (bot, message) { ... });
controller.hears('hi', 'direct_message', function (bot, message) { ... });
controller.on('bot_room_leave', function (bot, message) { ... });
controller.on('card_clicked', function (bot, message) { ... });

Send simple text messages

These messages are displayed like any other chat message. They can include simple character formatting.

Learn more about simple text messages

bot.reply()

Argument Description
src source message as received from google
reply reply message (string or object)
callback optional callback

This function sends an immediate response that is visible to everyone in the channel:

bot.reply(message, `Hello`);
bot.reply(message, {text : `Hi !`});

`

bot.replyWithThreadKey()

Argument Description
src source message as received from google
reply reply message (string or object)
callback optional callback

To send a message as a reply in an existing thread:

bot.replyWithThreadKey(message, {
    threadKey : "YOUR_THREAD_KEY",
    requestBody : {
        text : `Hi ! this message inside the same thread`
    }
});

`

bot.replyAsNewThread()

Argument Description
src source message as received from google
reply reply message (string or object)
callback optional callback

To send a message into Hangouts Chat as a new thread:

bot.replyAsNewThread(message, `Hello ! this is a new thread`);

`

Send card messages

These messages contain all the details that Hangouts Chat needs to render a card in the chat room. These details include any text, special formatting, widgets, and other details used in the card.

Learn more about card messages

controller.hears('cards', 'message_received', function (bot, message) {
    bot.reply(message, {
        requestBody: {
            cards: [
                {
                    "sections": [
                        {
                            "widgets": [
                                {
                                    "image": { "imageUrl": "https://image.slidesharecdn.com/botkitsignal-160526164159/95/build-a-bot-with-botkit-1-638.jpg?cb=1464280993" }
                                },
                                {
                                    "buttons": [
                                        {
                                            "textButton": {
                                                "text": "Get Started",
                                                "onClick": {
                                                    "openLink": {
                                                        "url": "https://botkit.ai/docs/"
                                                    }
                                                }
                                            }
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    });
});

Is something missing or out of date?

This file is managed on Github. click here to view the source, and send us a pull request with your improvements!

Back to top