1

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
Eron Salling
  • 185
  • 2
  • 5

1 Answers1

3

The problem is the following line:

var channel = JSON.stringify(params[0]["id"]);

The value params[0]["id"] would be the channel ID (for example "C123456"). But when you pass it through JSON.stringify(), you'll end up with escaped quotes in the string (for example "\"C123456\""). Once you pass that to web.channels.info(), this channel will not be found.

That would solve the immediate error you see (channel_not_found), but you also need to fix this for member too, since you call web.channels.invite() later.

Ankur
  • 2,792
  • 3
  • 23
  • 26
  • That makes a lot of sense! Though I can't send params[0]["id"] with the API call because slack doesn't take PHP-style array objects (foo[0]), and returns the error "invalid_array_arg" instead. I've been trying to figure out how to get around that, which is why I tried stringifying it. – Eron Salling Jan 19 '18 at 14:50
  • Figured out the (of course) very simple answer, which was to change `JSON.stringify(params[0]["id"]);` to `params[0]["id"].toString()`. Thank you for your help in identifying the problem! – Eron Salling Jan 21 '18 at 14:11