Skip to content

Commit

Permalink
Merge pull request #1 from Woodhouse/v2
Browse files Browse the repository at this point in the history
Rewrite for v2
  • Loading branch information
CodeFoodPixels authored Jun 22, 2019
2 parents fd02d2d + 696f72f commit 880bf3a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 63 deletions.
118 changes: 56 additions & 62 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,61 @@
var client,
slack = function(){

this.name = 'slack';
this.displayname = 'Slack Chat';
this.description = 'Send messages to woodhouse via Slack';

this.defaultPrefs = [{
name: 'token',
displayname: 'Token',
type: 'text',
value: ''
}];

}

slack.prototype.init = function(){
var self = this;
client = require('slack-client');
this.getPrefs().done(function(prefs){
self.connection = new client(prefs.token, true, true);

self.connection.login();

self.connection.on('open', function() {
self.userid = self.connection.self.id;
});

self.connection.on('message', function(message) {
self.recieveMessage(message);
});
});

this.addMessageSender(function(message, to){
self.send(message, to);
});
}

slack.prototype.recieveMessage = function(message) {
var channel = this.connection.getChannelGroupOrDMByID(message.channel),
user = this.connection.getUserByID(message.user),
nameRegex = new RegExp('^[<]{0,1}[@]{0,1}' + this.userid + '[>]{0,1}[:]{0,1}'),
mentionRegex = new RegExp('<@([0-9a-z]+)>', 'gi'),
mentions,
mentionUser;

if (message.type === 'message' && message.text) {
message.text = message.text.replace(nameRegex, this.api.name);

while (mention = mentionRegex.exec(message.text)) {
mentionUser = this.connection.getUserByID(mention[1]);
message.text = message.text.replace(mention[0], mentionUser.name);
}
this.messageRecieved(channel, message.text, user.name);
'use strict';

const client = require('@slack/client');
const bluebird = require('bluebird');

class slack {
constructor() {
this.name = 'slack';
this.displayname = 'Slack Chat';
this.description = 'Send messages to woodhouse via Slack';

this.defaultPrefs = {
token: {
displayname: 'Token',
type: 'text',
value: ''
}
};
}
}

slack.prototype.send = function(message, channel) {
channel.send(message);
}

slack.prototype.exit = function(){
if (this.connection) {
this.connection.disconnect();
init() {
bluebird.all([
this.getSystemPref('name'),
this.getPref('token')
]).then(([name, token]) => {
const connection = new client.RtmClient(token, {
dataStore: new client.MemoryDataStore()
});
let botId;

connection.start();

connection.on(client.CLIENT_EVENTS.RTM.AUTHENTICATED, (rtmStartData) => {
botId = rtmStartData.self.id;
});

connection.on(client.RTM_EVENTS.MESSAGE, (message) => {
const user = connection.dataStore.getUserById(message.user);
const nameRegex = new RegExp('^[<]{0,1}[@]{0,1}' + botId + '[>]{0,1}[:]{0,1}');
const mentionRegex = new RegExp('<@([0-9a-z]+)>', 'gi');

if (message.type === 'message' && message.text) {
message.text = message.text.replace(nameRegex, name);

let mention;
while (mention = mentionRegex.exec(message.text)) {
let mentionUser = connection.dataStore.getUserById(mention[1]);
message.text = message.text.replace(mention[0], mentionUser.name);
}

this.messageRecieved(message.channel, message.text, user.name);
}
});

this.addMessageSender(function(to, message){
connection.sendMessage(message, to);
});
});
}
}

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
"homepage": "https://github.com/Woodhouse-bot/woodhouse-interface-slack",
"dependencies": {
"slack-client": "^1.3.1"
"@slack/client": "^3.6.1",
"bluebird": "^3.4.6"
}
}

0 comments on commit 880bf3a

Please sign in to comment.