Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for using events api instead of rtm #68

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions core/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module.exports = class Router {

if (this.server) {
this.setupCallback();
this.setupEventWebhook();
}
}

Expand Down Expand Up @@ -160,6 +161,12 @@ module.exports = class Router {
});
}

setupEventWebhook() {
this.server.initEventsEndpoint((body) => {
this.handle(body.event);
})
}

addExtras(data) {
const matches = data.text.match(cmdPattern);
if (!matches) {
Expand Down
20 changes: 20 additions & 0 deletions core/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,26 @@ module.exports = class Server {
})
}

initEventsEndpoint(callback) {
this.app.post('/events', (req, res) => {
const body = JSON.parse(req.body)
// check that the verification token matches expected value
if (body.token === config.getKey('slack_verification_token')) {
// immediately respond with a empty 200 response to let
// Slack know the command was received
res.send('')

callback(body)
} else {
console.error(
'Sent 500',
'Check that the verification token matches expected value'
)
res.sendStatus(500)
}
})
}

start(callback = () => { }) {
const port = config.getKey('port') || 3000
this.app.listen(port, () => {
Expand Down
78 changes: 47 additions & 31 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,40 +77,56 @@ class SlackCat {
return;
}

const rtm = new RTMClient(config.getKey("slack_access_token"));
let router;

rtm.start();

rtm.on("authenticated", data => {
console.log('Incoming RTM authenticated event');
if (!router) {
const bot = new SlackCatBot(data);
const server = new Server();
const moduleLoader = new MoudleLoader(bot, server, this.pathToModules);
const modules = moduleLoader.getModules();

// Fix me :(((((((((((
bot.setModules(modules);
router = new Router(bot, modules, server);

server.start(() => {
SlackCatEvents.publish(SlackCatEvents.EventTypes.SetupComplete, []);
});
}
});
if (config.getKey("use_events_api") === 'true') {
// TODO: how to get bot data now
const bot = new SlackCatBot({});
const server = new Server();
const moduleLoader = new MoudleLoader(bot, server, this.pathToModules);
const modules = moduleLoader.getModules();

// Fix me :(((((((((((
bot.setModules(modules);
const router = new Router(bot, modules, server);

rtm.on("message", data => {
router.handle(data);
});
server.start(() => {
SlackCatEvents.publish(SlackCatEvents.EventTypes.SetupComplete, []);
});
} else {
const rtm = new RTMClient(config.getKey("slack_access_token"));
let router;

rtm.start();

rtm.on("authenticated", data => {
console.log('Incoming RTM authenticated event');
if (!router) {
const bot = new SlackCatBot(data);
const server = new Server();
const moduleLoader = new MoudleLoader(bot, server, this.pathToModules);
const modules = moduleLoader.getModules();

// Fix me :(((((((((((
bot.setModules(modules);
router = new Router(bot, modules, server);

server.start(() => {
SlackCatEvents.publish(SlackCatEvents.EventTypes.SetupComplete, []);
});
}
});

rtm.on("reaction_added", data => {
router.handle(data);
});
rtm.on("message", data => {
router.handle(data);
});

rtm.on("member_joined_channel", data => {
router.handle(data);
});
rtm.on("reaction_added", data => {
router.handle(data);
});

rtm.on("member_joined_channel", data => {
router.handle(data);
});
}
}

async runDebugCommand() {
Expand Down