简体   繁体   中英

Trouble connecting webhook to Facebook messenger bot

I'm trying to create a Facebook chatbot using NodeJS, Express, and a heroku server.

I created my webhook on heroku and had it verified and saved by Facebook. I then ran this code to connect my webhook to Facebook.

curl -ik -X POST "https://graph.facebook.com/v2.6/me/subscribed_apps?access_token=<token>"

this returned {success:true}.

So then I started adding code that would reply to incoming messages but I can't seem to get it to connect. Whenever I send a message I get no reply.

Here is my code:

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

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

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

app.get('/', function (req, res) {
  if (req.query['hub.verify_token'] === '8FKU9XWeSjnZN4ae') {
    res.send(req.query['hub.challenge']);
    console.log("app.get ran")
    res.sendStatus(200)
  }

  console.log("Error: wrong validation token")
})

app.post('wyrdbot.herokuapp.com', function (req, res) {
  messaging_events = req.body.entry[0].messaging;
  console.log("app.post ran")
  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);
});

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);
    }
  });
}

I have been trying to debug it and have realized the app.post function isn't running at all. I have also been getting errors saying my webhook has been disabled.

When I check my logs I find this printed out a bunch of times:

2016-04-20T14:13:31.487873+00:00 heroku[router]: at=info method=POST path="/" host=wyrdbot.herokuapp.com request_id=fa1e5270-5038-4e67-b7a6-c6852c7c3000 fwd="173.252.90.101" dyno=web.1 connect=0ms service=16ms status=404 bytes=212

I'm completely out of ideas about what to try. Anyone know what I'm missing?

Any help would be appreciated. Thanks!

Use app.use(bodyParser.json()); as facebook is sending JSON data in request body.

试试 app.post('/', function (req, res) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM