claudiajs / claudia-bot-builder

Create chat bots for Facebook Messenger, Slack, Amazon Alexa, Skype, Telegram, Viber, Line, GroupMe, Kik and Twilio and deploy to AWS Lambda in minutes
https://claudiajs.com/claudia-bot-builder.html
MIT License
1.84k stars 254 forks source link

Cannot read property 'token' of undefined #12

Closed chiefy closed 8 years ago

chiefy commented 8 years ago

I can't seem to get even the simplest example up and running, so I am not sure if this is a bug or I am just not sending the correct headers / data.

Here's an example project that I am trying to use

Even with the sample bot.js example I am getting:

POST /latest/slack/slash-command HTTP/1.1
Host: xxxx.execute-api.us-east-1.amazonaws.com
Content-Type: application/json
token: asldfjklsdfkjasdf0s9809f8
slackToken: asldfjklsdfkjasdf0s9809f8
Cache-Control: no-cache

{
    "token": "asldfjklsdfkjasdf0s9809f8",
    "command": "bot",
    "text": "hi",
    "team_id": "T0001",
    "channel_id": "C2147483705",
    "channel_name": "test",
    "user_id": "U2147483697",
    "user_name": "steve"
}
{
    "errorMessage": "Cannot read property 'token' of undefined",
    "errorType": "TypeError",
    "stackTrace": [
        "/var/task/node_modules/claudia-bot-builder/lib/slack/setup.js:14:21",
        "ApiBuilder.self.router (/var/task/node_modules/claudia-bot-builder/node_modules/claudia-api-builder/src/api-builder.js:98:15)"
    ]
}
stojanovic commented 8 years ago

Hey @chiefy,

I tried your example and it works for me except the request part because you overwrite it on line 5:

const lodash = require('lodash');
const request = require('request-promise'); // <----
const botBuilder = require('claudia-bot-builder');
const base_url = 'https://www.omdbapi.com';

module.exports = botBuilder(request => { // <----

And here are the steps I did to run the example (after fixing request name collision):

  1. Run claudia create --region us-east-1 --api-module movie to create the Lambda function for bot builder. --api-module is movie because the name of your file is movie.js.
  2. After it finish, go to Slack to create a new slash command (url should be https://[your team].slack.com/apps/build/custom-integration), than select Slash command. You'll see your token there.
  3. Run claudia update --configure-slack-slash-command and when it prompts you for the token, copy the token from the previous page and paste it there, then copy webhook url from the terminal to the Slack page. Press enter and wait for claudia to finish.
  4. Go to Slack page from step 2, fill the remaining data and save.
  5. Go to Slack app and test your slash command.

Here's the screenshot:

Setup the command:

setup-command

How it works:

command-test
chiefy commented 8 years ago

@stojanovic derp, ok I will try again without overwriting request

stojanovic commented 8 years ago

Also check if your slackToken variable is set as a stage variable on API Gateway. You are saw that issue with missing token because of that variable.

When you follow the steps I described you should see slackToken and lambdaVersion stage variables for the latest API Gateway version.

chiefy commented 8 years ago

@stojanovic thanks for the tip, I finally got it working! Not sure why I kept seeing that issue before re: request.post.token missing.