I am building a slack bot with botkit and the nodejs slack developer kit. I have a series of api calls, the first to find the users (globalMembers), the second to create a channel based on an array of channels, and a final call to add each user to each of the newly created channels.
To do this, I have several promises in order to loop through the channels and members. The channels are created successfully, and I then send unique arrays with each member and channel ([[member1, channel1], [member1, channel2]....]
) to a promise that will invite members to the respective channel.
No matter what (I am using the channel ID) I am given the "channel_not_found" error when I try to get the channel info or invite members to that channel. I am new to promises and likely have an error there somewhere, and appreciate any help.
Below is my code:
var channels = ["gameplay", "gamelog", "map"];
var channelData = channels.map(channelCreate);
var channelResults = Promise.all(channelData);
return channelResults.then(created => {
var channelMembers = [];
_.each(created, function(channel) {
// globalMembers is the user list
_.each(globalMembers, function(member) {
var array = [channel, member];
// prints array of arrays [channel, member]
channelMembers.push(array);
});
});
var memberData = channelMembers.map(channelJoin);
var memberResults = Promise.all(memberData);
return memberResults.then(joined => {
console.log(joined, "is the joined data");
});
});
var channelCreate = function channelCreate(name) {
// Join the channels
return web.channels.create(name).then((res) => {
console.log("created labyrinth channel: " + JSON.stringify(res.channel));
return res.channel;
}).catch((err) => { console.log(err) }); // End channels.join call
}; // End channel create
var channelJoin = function channelJoin(params) {
var member = JSON.stringify(params[1]["id"]);
var channel = JSON.stringify(params[0]["id"]);
console.log(member, "is the member that will join " + channel);
web.channels.info(channel).then(channelData => {
console.log(channelData);
if (channelData) {
// Invite each user to the labyrinth chat channel
return web.channels.invite(channel, member)
.then(res => {
return res;
}).catch((err) => { console.log(err) });
}
}).catch(err => console.log(err));
}; // End channel Join