Skip to content

Commit

Permalink
Include groups in channel list
Browse files Browse the repository at this point in the history
  • Loading branch information
mishk0 committed Jun 9, 2015
1 parent 67fab3f commit 4c96b18
Showing 1 changed file with 73 additions and 1 deletion.
74 changes: 73 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Bot.prototype.login = function() {
this.channels = data.channels;
this.users = data.users;
this.ims = data.ims;
this.groups = data.groups;

this.emit('start');

Expand Down Expand Up @@ -85,14 +86,26 @@ Bot.prototype.getUsers = function() {
return this._api('users.list');
};

/**
* Get groups
* @returns {vow.Promise}
*/
Bot.prototype.getGroups = function() {
if (this.groups) {
return Vow.fulfill({ groups: this.groups });
}

return this._api('groups.list');
};

/**
* Get user by name
* @param {string} name
* @returns {object}
*/
Bot.prototype.getUser = function(name) {
return this.getUsers().then(function(data) {
return find(data.members, { name: name});
return find(data.members, { name: name });
});
};

Expand All @@ -107,6 +120,17 @@ Bot.prototype.getChannel = function(name) {
});
};

/**
* Get group by name
* @param {string} name
* @returns {object}
*/
Bot.prototype.getGroup = function(name) {
return this.getGroups().then(function(data) {
return find(data.groups, { name: name });
});
};

/**
* Get channel ID
* @param {string} name
Expand All @@ -118,6 +142,17 @@ Bot.prototype.getChannelId = function(name) {
});
};

/**
* Get group ID
* @param {string} name
* @returns {string}
*/
Bot.prototype.getGroupId = function(name) {
return this.getGroup(name).then(function(group) {
return group.id;
});
};

/**
* Get "direct message" channel ID
* @param {string} name
Expand Down Expand Up @@ -186,6 +221,43 @@ Bot.prototype.postMessageToChannel = function(name, text, params) {
}.bind(this));
};

/**
* Posts a message to group by name
* @param {string} name
* @param {string} text
* @param {object} params
* @returns {vow.Promise}
*/
Bot.prototype.postMessageToGroup = function(name, text, params) {
return this.getGroupId(name).then(function(groupId) {
return this.postMessage(groupId, text, params);
}.bind(this));
};

/**
* Posts a message to group | channel | user
* @param name
* @param text
* @param params
* @returns {vow.Promise}
*/
Bot.prototype.postTo = function(name, text, params) {
return Vow.all([this.getChannels(), this.getUsers(), this.getGroups()]).then(function(data) {
var all = [].concat(data[0].channels, data[1].members, data[2].groups);
var result = find(all, {name: name});

assert(Object.keys(result).length, 'wrong name');

if (result['is_channel']) {
return this.postMessageToChannel(name, text, params);
} else if (result['is_group']) {
return this.postMessageToGroup(name, text, params);
} else {
return this.postMessageToUser(name, text, params);
}
}.bind(this));
};

/**
* Send request to API method
* @param {string} methodName
Expand Down

0 comments on commit 4c96b18

Please sign in to comment.