Send Messages In Google Hangouts With Node.js
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
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.
Related Questions
- → Maximum call stack exceeded when instantiating class inside of a module
- → Browserify api: how to pass advanced option to script
- → Node.js Passing object from server.js to external modules?
- → gulp-rename makes copies, but does not replace
- → requiring RX.js in node.js
- → Remove an ObjectId from an array of objectId
- → Can not connect to Redis
- → React: How to publish page on server using React-starter-kit
- → Express - better pattern for passing data between middleware functions
- → Can't get plotly + node.js to stream data coming through POST requests
- → IsGenerator implementation
- → Async/Await not waiting
- → (Socket.io on nodejs) Updating div with mysql data stops without showing error