3

I'm trying to create a Facebook chatbot with NodeJS, Express, and a Heroku server.

I created my webhook on heroku and had it verified and saved by facebook. I then started adding code that would reply to the incoming messages and I can't seem to get it connected. It keeps saying "Error, wrong validation token" when I try to load my webhook in my browser. And when I try to send my bot a message I get no response. Even though I already had it verified and didn't change the code.

Here is my code:

var express = require('express');
var bodyParser = require('body-parser');

var app = express();
var port = process.env.PORT || 3000;

// body parser middleware
app.use(bodyParser.urlencoded({ extended: true }));

// test route
//app.get('/', function (req, res) { res.status(200).send('Hello world!') });

app.get('/', function (req, res) {
  if (req.query['hub.verify_token'] === '8FKU9XWeSjnZN4ae') {
    res.send(req.query['hub.challenge']);
  }
  res.send('Error, wrong validation token');
})

app.post('/', function (req, res) {
  messaging_events = req.body.entry[0].messaging;
  for (i = 0; i < messaging_events.length; i++) {
    event = req.body.entry[0].messaging[i];
    sender = event.sender.id;
    if (event.message && event.message.text) {
      text = event.message.text;
      sendTextMessage(sender, "Text received, echo: "+ text.substring(0, 200));
    }
  }
  res.sendStatus(200);
});

// error handler
app.use(function (err, req, res, next) {
  console.error(err.stack);
  res.status(400).send(err.message);
});

app.listen(port, function () {
  console.log('Listening on port ' + port);
});

var token = <myToken>;

function sendTextMessage(sender, text) {
  messageData = {
    text:text
  }
  request({
    url: 'https://graph.facebook.com/v2.6/me/messages',
    qs: {access_token:token},
    method: 'POST',
    json: {
      recipient: {id:sender},
      message: messageData,
    }
  }, function(error, response, body) {
    if (error) {
      console.log('Error sending message: ', error);
    } else if (response.body.error) {
      console.log('Error: ', response.body.error);
    }
  });
}

So I'm confused as to why nothing is happening and why I'm getting that error. I feel like I'm missing a whole step. I am following this tutorial by the way: https://developers.facebook.com/docs/messenger-platform/quickstart

Any help is appreciated. Thanks!

Edit: Here are my heroku logs

Heroku Logs

Gaurav Dave
  • 6,838
  • 9
  • 25
  • 39
Eric Walier
  • 331
  • 2
  • 18

2 Answers2

1
  1. Do not post your full access tokens here!
  2. Have you tested the output of the challenge? Since it's just a GET and you know all values you can try it yourself: your-app-domain.com/your-callback-url?hub_mode=subscribe&hub_verify_token=the_token_you_set_in_your_app_config&hub_challenge=ping which sould print 'ping' if everything work fine.
  3. Make sure you add sendStatus(200) to the hub challenge response, too.
  4. You need to subscribe your page to the app first. To do so make a POST request to /your-page-id/subscribed_apps which should return "success". You can make a GET request to the same endpoint afterwards to double check your app is subscribed to your page
  5. You did not mention which events you subscribed to (needs to be message_deliveries, messages, messaging_optins, messaging_postbacks)
  6. Make sure the webhooks tab in your app dashboard now says "complete"
  7. Test again
lars.schwarz
  • 1,276
  • 1
  • 8
  • 12
  • Hey, thanks for the help. Quick question though. Would "https://graph.facebook.com/v2.6/me" be the call-back-url for step 2 or am I way off on that – Eric Walier Apr 18 '16 at 12:43
  • The callback URL is the URL you specified in your app under Webhooks > Page Subscription. That /me endpoint is the URL to retrieve the user's first name from so you can use it for a "Hello xy" message – lars.schwarz Apr 18 '16 at 13:32
0

You are actually using "request" but you are never importing it anywhere. Here's how to fix it:

var request = require("request")

Once you have added that to your index.js or app.js file (basically whatever this file is), make sure you do:

npm install request --save

This should fix it. Unfortunately, Heroku doesn't error out and say that it does not know what "request" is and that's why it was so hard to figure this out in the first place!

helicopter
  • 151
  • 5