【问题标题】:Meteor Facebook Messenger Bot webhookMeteor Facebook Messenger Bot webhook
【发布时间】:2016-11-09 07:04:16
【问题描述】:

我正在尝试使用 Meteor 构建一个 Facebook Messenger 机器人。设置包括查找验证令牌并以验证 GET 请求中发送的质询进行响应。在 Facebook 的(非 Meteor)示例应用中,使用了以下代码:

app.get('/webhook', function(req, res) {
  if (req.query['hub.mode'] === 'subscribe' &&
      req.query['hub.verify_token'] === VALIDATION_TOKEN) {
    console.log("Validating webhook");
    res.status(200).send(req.query['hub.challenge']);
  } else {
    console.error("Failed validation. Make sure the validation tokens match.");
    res.sendStatus(403);          
  }  
});

当我尝试使用以下 (Meteor) 代码实现相同的功能时,我收到以下错误。

var bodyParser = Meteor.npmRequire( 'body-parser');

// Add two middleware calls. The first attempting to parse the request body as
// JSON data and the second as URL encoded data.
Picker.middleware( bodyParser.json() );
Picker.middleware( bodyParser.urlencoded( { extended: false } ) );

// ------------------------------------------------------------
// HANDLE THE INITIAL HANDSHAKE WITH FACEBOOK VIA A GET REQUEST
// ------------------------------------------------------------
var getRoutes = Picker.filter(function(req, res) {
  // you can write any logic you want.
  // but this callback does not run inside a fiber
  // at the end, you must return either true or false
  return req.method == "GET";
});

getRoutes.route('/webhook', function(params, req, res, next) {
  if (params.query['hub.verify_token'] === '78750') {
        console.log(params.query['hub.verify_token']);
        // res.end();
        res.end(params.query['hub.challenge']);
  }
}); // end getRoutes

错误:

The URL couldn't be validated. Response does not match challenge, expected value = '1127215706', received='<!DOCTYPE html> <htm...

也许这个问题是由于它是在客户端而不是服务器上运行的?如果是这样,我应该把这段代码放在哪里才能让它在服务器上运行?

另外,我的浏览器控制台出现以下错误12次:

Mixed Content: The page at 'https://pfbe.meteorapp.com/' was loaded over HTTPS, but requested an insecure font 'http://themes.googleusercontent.com/static/fonts/inconsolata/v5/BjAYBlHtW3CJxDcjzrnZCIbN6UDyHWBl620a-IRfuBk.woff'. This request has been blocked; the content must be served over HTTPS.

我可以做些什么来解决这个问题?

【问题讨论】:

  • “它在客户端而不是服务器上运行”是什么意思? Facebook 发送请求,所以它这里的客户端,而你的服务器是它被发送到的那个。 // received='&lt;!DOCTYPE html&gt; … 非常明显 - 您的端点返回一个完整的 HTML 文档(无论是默认模板,还是错误文档,都无法从该 sn-p 中得知),而不仅仅是它应该的挑战值到。
  • 你修好了吗?或找到解决方案?

标签: javascript facebook meteor bots facebook-messenger


【解决方案1】:

使用 Restivus - 您需要在正文中以挑战形式响应并将其作为 parsedInt 返回

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-16
    • 2016-08-09
    • 2018-09-03
    • 1970-01-01
    • 2016-08-13
    • 2017-05-26
    相关资源
    最近更新 更多