howdyai / botkit

Botkit is an open source developer tool for building chat bots, apps and custom integrations for major messaging platforms.
MIT License
11.49k stars 2.28k forks source link

Cannot read property 'user_id' when dealing with Slack Apps. #590

Closed kevinwritescode closed 6 years ago

kevinwritescode commented 7 years ago

When attempting to configure a private Slack App, I'm running into issues with team.bot.user_id being undefined? Is this a configuration issue with Slack Apps? The team object I get back in that area of code looks like this:

{
  "id": "UNIQID1",
  "createdBy": "UNIQID2",
  "url": "https://COMPANY.slack.com/",
  "name": "COMPANY"
}

I've pasted the error logs below.

TypeError: Cannot read property 'user_id' of undefined
    at /Users/knuut/slack-bot/node_modules/botkit/lib/SlackBot.js:177:33
    at Object.get (/Users/knuut/slack-bot/node_modules/botkit/lib/CoreBot.js:822:17)
    at Object.Slackbot.slack_botkit.findTeamById (/Users/knuut/slack-bot/node_modules/botkit/lib/SlackBot.js:300:36)
    at Object.Slackbot.slack_botkit.handleWebhookPayload (/Users/knuut/slack-bot/node_modules/botkit/lib/SlackBot.js:158:22)
    at /Users/knuut/slack-bot/node_modules/botkit/lib/SlackBot.js:137:26
    at Layer.handle [as handle_request] (/Users/knuut/slack-bot/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/knuut/slack-bot/node_modules/express/lib/router/route.js:131:13)
    at Route.dispatch (/Users/knuut/slack-bot/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/Users/knuut/slack-bot/node_modules/express/lib/router/layer.js:95:5)
    at /Users/knuut/slack-bot/node_modules/express/lib/router/index.js:277:22

I've also added the basic code used for testing.

const Botkit = require('botkit');

const controller = Botkit.slackbot({
  debug: true,
});

controller.configureSlackApp({
  clientId: process.env.CLIENT_ID,
  clientSecret: process.env.CLIENT_SECRET,
  scopes: ['incoming-webhook','team:read','users:read','channels:read','im:read','im:write','groups:read','emoji:read','chat:write:bot'],
});

controller.setupWebserver(process.env.PORT, (err, webserver) => {
  controller.createWebhookEndpoints(webserver);
  controller.createOauthEndpoints(webserver, (err2, req, res) => {
    if (err2) {
      res.status(500).send(`ERROR: ${err2}`);
    } else {
      res.send('Success!');
    }
  });
});

controller.on('slash_command', (bot, message) => {
  // check message.command
  // and maybe message.text...
  // use EITHER replyPrivate or replyPublic...
  bot.replyPrivate(message, `This is a private reply to the ${message.command} slash command!`);

  // and then continue to use replyPublicDelayed or replyPrivateDelayed
  bot.replyPublicDelayed(message, `This is a public reply to the ${message.command} slash command!`);

  bot.replyPrivateDelayed(message, ':dash:');
});
kevinwritescode commented 7 years ago

It looks like it may be a configuration issue after all. I was able to get it to work once I also allowed "bot" during the oauth "Slack Button" creation, but I wasn't using a bot for anything in my scripts, just slash commands.

I'm not sure if there is any advise on how to set it up if you don't need a bot? Maybe I'm configuring the code improperly to handle a no bot situation.

ethan-sonza commented 7 years ago

Had the same issue. After allowing a bot, I'm getting the error:

TypeError: Cannot read property 'replace' of undefined
    at Object.<anonymous> (/home/ethan/node_projects/tripkada-slack/node_modules/botkit/lib/SlackBot.js:648:84)
    at Object.Botkit.botkit.trigger (/home/ethan/node_projects/tripkada-slack/node_modules/botkit/lib/CoreBot.js:986:49)
    at /home/ethan/node_projects/tripkada-slack/node_modules/botkit/lib/CoreBot.js:1090:32
    at Object.bot.findConversation (/home/ethan/node_projects/tripkada-slack/node_modules/botkit/lib/Slackbot_worker.js:751:9)
    at /home/ethan/node_projects/tripkada-slack/node_modules/botkit/lib/CoreBot.js:1086:21
    at next (/home/ethan/node_projects/tripkada-slack/node_modules/ware/lib/index.js:82:27)
    at Ware.run (/home/ethan/node_projects/tripkada-slack/node_modules/ware/lib/index.js:88:3)
    at Object.Botkit.botkit.receiveMessage (/home/ethan/node_projects/tripkada-slack/node_modules/botkit/lib/CoreBot.js:1081:35)
    at handleSlashCommand (/home/ethan/node_projects/tripkada-slack/node_modules/botkit/lib/SlackBot.js:303:22)
    at /home/ethan/node_projects/tripkada-slack/node_modules/botkit/lib/SlackBot.js:226:24

Using botkit 0.4.10. Traced the code: my bot object is undefined.

Should I be spawning a bot? If so, how would I get the bot token if it's an app's bot?

EDIT: Downgraded to 0.4.2, no longer getting the error.

trevor-msgio commented 7 years ago

Hey @peterswimm - over here we ran into the same issue. I think this may be due to some of the changes that Slack has made recently.

Since by default, if you enable slash command on your slack app (without a bot user) - you do not ask for the 'bot' permission. And now with the latest Slack change, you cannot add bot permission without a bot user.

It should still be possible to use slash commands without asking for the bot oauth permission (and subsequently adding a bot user) - as slash commands are independent from bot users.

The call to set the user_id of the bot object (which doesn't exist, because we don't have bot oauth permission, see line 5) is here : https://github.com/howdyai/botkit/blob/master/lib/SlackBot.js#L209 I'm also not sure it's really necessary to spawn a bot for a slash command.

Just wanted you to be aware of this. We are looking at the feasibility of submitting a PR for you guys; I think it may be relatively easy to fix.

ritave commented 6 years ago

@peterswimm The latest version has moved braces and the functionallity is broken again

In the original PR the if was closed here https://github.com/howdyai/botkit/pull/699/files#diff-ffa8ec227ea6e9babdfc11d5a71ee29dR223

While currently it's closed before getting bot id making the problem reappear: https://github.com/howdyai/botkit/blob/master/lib/SlackBot.js#L217

hbakhtiyor commented 6 years ago

i've the same issue with the latest version (0.6.7)

TypeError: Cannot read property 'user_id' of undefined
    at /demo/node_modules/botkit/lib/SlackBot.js:220:34
    at /demo/node_modules/botkit/lib/SlackBot.js:148:17
    at Store.get (/demo/node_modules/botkit/node_modules/jfs/lib/Store.js:203:28)
    at Store.get (/demo/node_modules/botkit/node_modules/jfs/lib/Store.js:345:18)
    at Object.get (/demo/node_modules/botkit/lib/storage/simple_storage.js:54:26)
    at Object.Slackbot.slack_botkit.findTeamById (/demo/node_modules/botkit/lib/SlackBot.js:505:36)
    at Object.Slackbot.slack_botkit.findAppropriateTeam (/demo/node_modules/botkit/lib/SlackBot.js:146:22)
    at Object.Slackbot.slack_botkit.handleWebhookPayload (/demo/node_modules/botkit/lib/SlackBot.js:188:22)
    at /demo/node_modules/botkit/lib/SlackBot.js:134:26
    at Layer.handle [as handle_request] (/demo/node_modules/express/lib/router/layer.js:95:5)
    at next (/demo/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/demo/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/demo/node_modules/express/lib/router/layer.js:95:5)
    at /demo/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/demo/node_modules/express/lib/router/index.js:335:12)
    at next (/demo/node_modules/express/lib/router/index.js:275:10)
hbakhtiyor commented 6 years ago

any updates of the issue? still not resolved in the latest version(0.6.9)

gingeranyhow commented 6 years ago

I'm also facing this issue after following the basic slash commands set-up. Any suggested workaround at this time?

izotos commented 6 years ago

I've been having the same issue when trying to initiate a dialog, I issued a PR https://github.com/howdyai/botkit/pull/1281 with changes according to https://github.com/howdyai/botkit/issues/590#issuecomment-351545096

This is quite important for us, we could not find a npm version that implements dialogs on Slack that does not have this issue.

erodewald commented 6 years ago

This still hasn't been merged? I can't seem to get Slash commands working because of this.

jimmyjames commented 6 years ago

Also seeing this issue; verified #1281 is fixing this for me as well. Do we have an ETA on getting that PR reviewed/merged?

mrbar42 commented 6 years ago

Work around - We are using another workaround from here https://github.com/howdyai/botkit/issues/108 which led me to do this (solves both problems):

const controller = Botkit.slackbot({
    disable_startup_messages: true,
    json_file_store: 'tmp/slack.json'
});

// FIX for: https://github.com/howdyai/botkit/issues/108
var bot = controller.spawn({
    token: SLACK_TOKEN
});
bot.api.team.info({}, function(err, response) {
    if (err) throw new Error(err.stack || JSON.stringify(err));
    // FIX2 this is a workaround for https://github.com/howdyai/botkit/issues/590
    response.team.bot = {
        id: 'boti',
        name: 'boti'
    };
    // END FIX2
    controller.saveTeam(response.team, function() {
        // ignore
    })
});
// END FIX
jmeyers91 commented 6 years ago

I'm getting this issue and after trying @mrbar42's fix, I'm getting this error:

Error: "missing_scope"
    at bot.api.team.info (/Users/james/projects/emberex-fantasy-football/server/hooks/addSlackBot.hook.js:52:25)
    at Request._callback (/Users/james/projects/emberex-fantasy-football/server/node_modules/botkit/lib/Slack_web_api.js:277:24)
    at Request.self.callback (/Users/james/projects/emberex-fantasy-football/server/node_modules/request/request.js:185:22)
    at Request.emit (events.js:159:13)
    at Request.<anonymous> (/Users/james/projects/emberex-fantasy-football/server/node_modules/request/request.js:1157:10)
    at Request.emit (events.js:159:13)
    at IncomingMessage.<anonymous> (/Users/james/projects/emberex-fantasy-football/server/node_modules/request/request.js:1079:12)
    at Object.onceWrapper (events.js:254:19)
    at IncomingMessage.emit (events.js:164:20)
    at endReadableNT (_stream_readable.js:1054:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)

Why is this issue closed? Is that fix supposed to be the right way to get around this user_id issue? It seems #1281 fixes this issue. I'll just fork until it gets merged.

justinemar commented 5 years ago

Is there any fixed for this? Currently encountering this on version 0.4.3 currently using @mrbar42 quick fix.

peterswimm commented 5 years ago

@justinemar that's a very old botkit, you should upgrade to the latest version and tell us if you are still seeing the trouble

joanitad commented 5 years ago

I am experiencing the same issue again while creating a custom slash command. I noticed that the https://github.com/jnv/botkit/commit/f35fe53cbcb9f7ee25e8307cb9e3e6787847b4c4 this fix has been reverted. When I get back this code, it works fine. I am using the latest version of botkit 0.7.4 How do I work around this?

benbrown commented 5 years ago

@joanitad Does your Slack application have a bot associated with it?

Adding one should resolve this.

joanitad commented 5 years ago

@benbrown : I have a bot user setup. Is that what you mean? if not, how do I know if the slack application has a bot associated with it? I am fairly new to create custom slash command.

joanitad commented 5 years ago

Hi @benbrown , could you clarify this, I have a bot user associated with the application and the bot is added to #general.I still get this error.