信使webhook设置出错

时间:2016-06-07 05:57:27

标签: node.js facebook-messenger

我正在使用相同的验证令牌,但是它给了我错误https://infinite-dusk-17985.herokuapp.com/webhook/,当我在信使上使用它时,它都没有响应。

'use strict';

const express = require('express')
const bodyParser = require('body-parser')
const request = require('request')
const app = express()

app.set('port', (process.env.PORT || 5000))

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({extended: false}))

// parse application/json
app.use(bodyParser.json())

// index
app.get('/', function (req, res) {
    res.send('hello world i am a secret bot')
})

// for facebook verification
app.get('/webhook/', function (req, res) {
    if (req.query['hub.verify_token'] === 'my_voice_is_my_password_verify_me') {
        res.send(req.query['hub.challenge'])
        res.send('Sucess, Challenge loop crossed')
    }
    res.send('Error, wrong token')
})

// to post data
app.post('/webhook/', function (req, res) {
    let messaging_events = req.body.entry[0].messaging
    for (let i = 0; i < messaging_events.length; i++) {
        let event = req.body.entry[0].messaging[i]
        let sender = event.sender.id
        if (event.message && event.message.text) {
            let text = event.message.text
            if (text === 'Generic') {
                sendGenericMessage(sender)
                continue
            }
            sendTextMessage(sender, "Text received, echo: " + text.substring(0, 200))
        }
        if (event.postback) {
            let text = JSON.stringify(event.postback)
            sendTextMessage(sender, "Postback received: "+text.substring(0, 200), token)
            continue
        }
    }
    res.sendStatus(200)
})


const token = "EAACKS5K1KvkBAASh07gKvgk9LvjCweLqKxKti1ZBzdzArNFPYNX9ZCx9tu35NNWquJZBuZCdZBLdsZBJAPFhvKgMZBDlazgofkbZAAeE6Hgv3gOh8jRd1W42AAZBIBd7EYNJsADepcpIgSlJEH9kHrup49oT5wZBHZBItnQwwDqr96z4wZDZD"

function sendTextMessage(sender, text) {
    let 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 messages: ', error)
        } else if (response.body.error) {
            console.log('Error: ', response.body.error)
        }
    })
}

function sendGenericMessage(sender) {
    let messageData = {
        "attachment": {
            "type": "template",
            "payload": {
                "template_type": "generic",
                "elements": [{
                    "title": "First card",
                    "subtitle": "Element #1 of an hscroll",
                    "image_url": "http://messengerdemo.parseapp.com/img/rift.png",
                    "buttons": [{
                        "type": "web_url",
                        "url": "https://www.messenger.com",
                        "title": "web url"
                    }, {
                        "type": "postback",
                        "title": "Postback",
                        "payload": "Payload for first element in a generic bubble",
                    }],
                }, {
                    "title": "Second card",
                    "subtitle": "Element #2 of an hscroll",
                    "image_url": "http://messengerdemo.parseapp.com/img/gearvr.png",
                    "buttons": [{
                        "type": "postback",
                        "title": "Postback",
                        "payload": "Payload for second element in a generic bubble",
                    }],
                }]
            }
        }
    }
    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 messages: ', error)
        } else if (response.body.error) {
            console.log('Error: ', response.body.error)
        }
    })
}

// spin spin sugar
app.listen(app.get('port'), function() {
    console.log('running on port', app.get('port'))
})

任何帮助都将受到高度赞赏。

2 个答案:

答案 0 :(得分:0)

由于您没有共享错误堆栈跟踪,我不确定原因。但是,您的代码存在问题。

对于以下代码段,

// for facebook verification
app.get('/webhook/', function (req, res) {
   if (req.query['hub.verify_token'] === 'my_voice_is_my_password_verify_me') {
      res.send(req.query['hub.challenge'])
      res.send('Sucess, Challenge loop crossed')
   }
   res.send('Error, wrong token')
})

你肯定会得到Error: Can't set headers after they are sent.

因此,请使用以下内容更新代码。

// for facebook verification
app.get('/webhook/', function (req, res) {
   if (req.query['hub.verify_token'] === 'my_voice_is_my_password_verify_me') {
      res.send(req.query['hub.challenge'])
      console.log('Sucess, Challenge loop crossed')
   } else{ 
     res.send('Error, wrong token')
   }
})

答案 1 :(得分:0)

这是一个有效的解决方案:

// my_server.js
'use strict';

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


const app = express();
app.set('port', process.env.PORT || 5000);
// parse application/json
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

// spin spin sugar
app.listen(app.get('port'), function() {
    console.log('running on port', app.get('port'));
});

/* for facebook verification */
app.get('/webhook', function(req, res) {
  if (req.query['hub.verify_token'] === 'my_voice_is_my_password_verify_me') {
    console.log("Validating webhook");
    res.status(200).send(req.query['hub.challenge']);
  } else {
    console.error("Verification failed. Make sure the validation tokens match.");
    res.status(403).end();
  }
});

很少有事情需要注意:

  • 当您在 developers.facebook.com 设置webhook时,请确保您提供的Verification Token 与上述代码中的字符串完全相同(即&#39; my_voice_is_my_password_verify_me&#39;)
  • 如果您想更改此令牌,请确保在两个地方都更新。这是非常重要
  • 如果您将此代码部署到Heroku,process.env.PORT将成为您的端口。 硬编码的端口号可能无效!
  • 您会注意到app.use(bodyParser.json());在这里使用。这是因为 Facebook在请求正文中发送JSON数据(有效负载)
  • 请注意,您不能写出2 res.send()个语句,它会给您带来错误,如Mukesh所述。一旦发送了标题,就无法修改
  • 最后,作为最佳做法,您可以尝试使用npm run startnode my_server.js在本地运行它,并确保它没有错误,例如缺少节点模块和&amp;等虽然您在本地运行时未获得成功回复