Venkatesh Potluri
Venkatesh Potluri

Reputation: 199

How do I link a node.js webhook to the messenger API of my Facebook app?

I am unable to link my node.js server as a messenger web hook to my Facebook app. If I try to validate the request in the '/webhook' call, Facebook gives me a page not found error while trying to verify and save the web hook. If I place the code in the '/' call, that is, app.get('/',...) call, I get an internal server error while saving the web hook. My app is hosted on Heroku. On checking the heroic logs, I see this error.

Reference error:

hub is not defined

and it points to this line:

res.send(hub.query['hub.challenge'])

My code is:

var express = require('express')
var bodyParser = require('body-parser')
var request = require('request')
//the imports

app = express()
app.set('port', (process.env.PORT || 5000))
app.use(bodyParser.urlencoded({extended: false}))

// Process application/json
app.use(bodyParser.json())
app.get('/',function(req,res){
    if (req.query['hub.verify_token'] === 'my_voice_is_my_password_verify_me') {
    res.send(hub.query['hub.challenge'])
    }
    res.send('wrong token,error')
})


app.get('/webhook',function(req,res){
    if (req.query['hub.verify_token'] === 'my_voice_is_my_password_verify_me') {
    res.send(hub.query['hub.challenge'])
    }
    res.send('wrong token,error')
})


app.listen(app.get('port'), function(req,res) {
    console.log('server running on port',app.get('port'))
})

Upvotes: 1

Views: 1501

Answers (1)

Rich
Rich

Reputation: 154

Replace hub.query with req.query you're trying to access an object that doesn't exist.

Upvotes: 3

Related Questions