Ad

Send Messages In Google Hangouts With Node.js

- 1 answer

I have looked for a long time trying to find a package that allows sending and reading messages in node.js

I have found lots of guides on how to make bots in the G-Suite version of Hangouts, but I'm not a G-suite user and am looking to do it in the regular version.

I am aware of the Jaxbot hangouts-bot: https://github.com/jaxbot/hangouts-bot but this doesn't support sending and reading to group chats, and I'm looking for something that can.

I'm hoping the package can read/send messages in DMs and group chats, so leave an answer if you know of anything that can do it!

Thanks, privrax

Ad

Answer

I've used the HangupsJS package on npm to interact with Hangouts. https://npmjs.com/package/hangupsjs. Before installing, you will need to install the coffeescript package globally to install this package. Just use: npm install coffeescript -g. If installing on a Mac use the sudo prefix.

This is what my bot looked like:

const Hangups = require('hangupsjs');
global.client = new Hangups();

const credsfunc = async function() {
    return {
        auth: async function() {
            return process.env.TOKEN
        }
    }
}
client.on('chat_message', msg => {
    console.log(msg)
    if (msg.chat_message.message_content.segment[0].text == 'Hello') {
        client.sendchatmessage(msg.conversation_id.id, [[0, 'Hi!']]);
    }
});
client.connect(credsfunc).then(() => {
    console.log('Client logged in.');
});

To get the authorization token you will need to follow these instructions: https://github.com/tdryer/hangups/issues/260#issuecomment-246578670 and replace process.env.TOKEN with your OAuth token.

From here on, start your bot with node and then add them to a group chat. They should start to read and respond to messages.

Ad
source: stackoverflow.com
Ad