Skip to content

Commit

Permalink
Add slack bot api
Browse files Browse the repository at this point in the history
  • Loading branch information
aek committed Mar 18, 2017
1 parent 56cbd41 commit deb8fd5
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 10 deletions.
23 changes: 13 additions & 10 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,20 @@ try {
process.exit(1);
}

var RtmClient = require('@slack/client').RtmClient;
var RTM_EVENTS = require('@slack/client').RTM_EVENTS;
var bot_token = process.env.SLACK_API_TOKEN || '';

var rtm = new RtmClient(bot_token);

rtm.on(RTM_EVENTS.MESSAGE, function handleRtmMessage(message) {
console.log('Message:', message); //this is no doubt the lamest possible message handler, but you get the idea
});
//Loading routes
process.stdout.write('Loading Slack bot....');
try {
var slackBot = require('./slack/bot');
slackBot.listChannels();
slackBot.listUsers();
slackBot.listen()
process.stdout.write(colors.green('OK\n'));
} catch (exception) {
console.log(exception);
process.stdout.write(colors.red('NOK\n'));
process.exit(1);
}

rtm.start();

process.stdout.write(colors.yellow('Listening on port ' + config.port + '...\n'));
server.listen(config.port);
65 changes: 65 additions & 0 deletions slack/bot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
module.exports = function() {

}

var token = process.env.SLACK_API_TOKEN || '';

var RtmClient = require('@slack/client').RtmClient;
var rtm = new RtmClient(token);
rtm.start();

var WebClient = require('@slack/client').WebClient;
var web = new WebClient(token);

var channels = [];
var users = [];

module.exports.listChannels = function() {
var _this = this;
web.channels.list(function(err, info) {
if (err) {
console.log('Error:', err);
} else {
for(var i in info.channels) {
channels.push({id: info.channels[i].id, name: info.channels[i].name});
_this.channelHistory(info.channels[i].id);
}
console.log(channels);
}
});
}

module.exports.listUsers = function() {
web.users.list(function(err, info) {
if (err) {
console.log('Error:', err);
} else {
for(var i in info.members) {
users.push({id: info.members[i].id, name: info.members[i].name})
}
console.log(users);
}
});
}

module.exports.channelHistory = function(channel) {
web.channels.history(channel, function(err, info) {
console.log(info);
});
}

module.exports.listen = function() {
var RTM_EVENTS = require('@slack/client').RTM_EVENTS;
rtm.on(RTM_EVENTS.MESSAGE, function handleRtmMessage(message) {
console.log('Message:', message); //this is no doubt the lamest possible message handler, but you get the idea
});
}

module.exports.send = function() {

var RTM_CLIENT_EVENTS = require('@slack/client').CLIENT_EVENTS.RTM;
var channel = "#general";
rtm.on(RTM_CLIENT_EVENTS.RTM_CONNECTION_OPENED, function () {
rtm.sendMessage("Hello!", channel);
});
}

0 comments on commit deb8fd5

Please sign in to comment.