Skip to content

Commit

Permalink
Merge pull request #10 from Galavantier/master
Browse files Browse the repository at this point in the history
Fixed a couple of bugs related to the PM functionality
  • Loading branch information
igorantun committed Jul 8, 2015
2 parents 8ce7314 + 4c7059b commit 06d495c
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,17 +234,20 @@ slackAPI.prototype.getUserByEmail = function(term) {
};

slackAPI.prototype.getIM = function(term) {
var im = null;
var im = null, self = this;
for (var i in self.slackData.ims) {
if(self.slackData.ims[i]['user'] === term) {
im = self.slackData.ims[i];
}
}
if (im === null) {
for (var i_ in self.slackData.ims) {
if (self.slackData.ims[i_]['user'] === this.getUser(term).id) {
im = self.slackData.ims[i_];
}
var user = this.getUser(term);
if(user !== null) {
for (var i_ in self.slackData.ims) {
if (self.slackData.ims[i_]['user'] === user.id) {
im = self.slackData.ims[i_];
}
}
}
}
if (im === null) {
Expand All @@ -261,10 +264,24 @@ slackAPI.prototype.sendMsg = function(channel, text) {
this.sendSock({'type': 'message', 'channel': channel, 'text': text});
};

slackAPI.prototype.sendPM = function(user, text) {
this.sendSock({'type': 'message', 'channel': this.getIM(user).id, 'text': text});
slackAPI.prototype.sendPM = function(userID, text) {
var self = this;
channel = self.getIM(userID);
if(channel !== null) {
self.sendSock({'type': 'message', 'channel': channel.id, 'text': text});
} else {
self.reqAPI('im.open', { user : this.getUser(userID).id }, function(data){
if(data.ok === true) {
self.slackData.ims.push(data.channel);
self.sendSock({'type': 'message', 'channel': data.channel.id, 'text': text});
} else {
self.out('error', 'Error. Unable to create an im channel: ' + data);
self.emit("error", data);
}
});
}
};

slackAPI.prototype.events = events;

module.exports = slackAPI;
module.exports = slackAPI;

0 comments on commit 06d495c

Please sign in to comment.