Skip to content

Instantly share code, notes, and snippets.

@fdstevex
Last active November 29, 2018 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fdstevex/106d8623c64a383a3a043fad43a6a6df to your computer and use it in GitHub Desktop.
Save fdstevex/106d8623c64a383a3a043fad43a6a6df to your computer and use it in GitHub Desktop.
JS code that connects to Slack as a bot and answers 'ping' with 'pong'.
const { RTMClient, WebClient } = require('@slack/client');
var CLIENT_EVENTS = require('@slack/client').CLIENT_EVENTS;
const token = 'xoxb-token-here';
async function runBot() {
const rtm = new RTMClient(token);
const web = new WebClient(token);
const r = await rtm.start();
console.log("Connected", r);
const lookupResponse = await web.users.lookupByEmail({email: 'steve@example.com'});
const user = lookupResponse.user;
console.log("Got user", user);
const groups = await web.groups.list();
console.log("Found Groups", groups);
const devGroup = groups.groups.find((g)=>(g.name === 'dev'));
async function handleMessage(message) {
console.log("on.message", message);
if (message.text === 'ping') {
rtm.sendMessage('pong', devGroup.id);
}
}
rtm.on('message', (message)=>{
handleMessage(message);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment