Last active
November 29, 2018 21:01
-
-
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'.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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