简体   繁体   English

无法将 webhook 连接到 Facebook Messenger bot

[英]Trouble connecting webhook to Facebook messenger bot

I'm trying to create a Facebook chatbot using NodeJS, Express, and a heroku server.我正在尝试使用 NodeJS、Express 和 Heroku 服务器创建 Facebook 聊天机器人。

I created my webhook on heroku and had it verified and saved by Facebook.我在 heroku 上创建了我的 webhook,并由 Facebook 验证和保存。 I then ran this code to connect my webhook to Facebook.然后我运行此代码将我的 webhook 连接到 Facebook。

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

this returned {success:true}.这返回了 {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.我一直在尝试调试它并意识到 app.post 函数根本没有运行。 I have also been getting errors saying my webhook has been disabled.我也收到错误消息,说我的 webhook 已被禁用。

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());使用 app.use(bodyParser.json()); as facebook is sending JSON data in request body.因为 facebook 在请求正文中发送 JSON 数据。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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