1

I've launched a Heroku application using the following files:-

app.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))

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

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

// Index route
app.get('/', function (req, res) {
    res.send("Hello world, I seem to be working")

})

// for Facebook verification
app.get('/webhook', function (req, res) {
    if (req.query['hub.verify_token'] === 'test-token') {
      res.send(req.query['hub.challenge']);
   } else {
      res.send('Error, wrong validation token');    
   }
})

// Spin up the server
app.listen(app.get('port'), function() {
    console.log('running on port', app.get('port'))
})

.gitignore

node_modules

package.json

{
  "name": "heroku-node-practice",
  "version": "1.0.0",
  "description": "New bot",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node app.js"
  },
  "author": "Paigal",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.17.1",
    "express": "^4.15.2",
    "foobar": "^1.1.0",
    "mongoose": "^4.9.8",
    "request": "^2.81.0"
  }
}

Procfile

web: node app.js

I installed node.js dependencies using the command: npm install express request body-parser --save

After git push heroku master, the application launches correctly.

However, when trying to set up a webhook in fb developer, the error is 'URL couldn't be validated. Response does not match expected challenge' then gives the different responses to the challenge. That is, my URL responds with "Hello world, I seem to be working" instead of numerical key.

Would greatly appreciate your help!

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
Piallen
  • 221
  • 2
  • 6

1 Answers1

2

You need to point FB to the route that has the verify function within it. You are currently pointing FB to the site root index '/' it seems.

Change the FB webhook url in the app settings to https://YOUR_DOMAIN.com/webhook and the verification will be complete. FB will then send whatever events you subscribe to to your '/webhook' route.

If this doesn't work, notice you have hardcoded your verify token here as test-token:

...

if (req.query['hub.verify_token'] === 'test-token') { ...

This will only complete the challenge if 'test-token' is what you set your webhook verification token to. A better way to do this imo would be the following:

...

if (req.query['hub.verify_token'] === process.env.VERIFY_TOKEN ) { ...

In the above example you must pass in the verify token you choose for your webhook when running your server, before you try to verify the webhook.

In your heroku dashboard, add VERIFY_TOKEN with your token as the value to your config variables. This will make the verify token available without hardcoding.

Jon Church
  • 2,320
  • 13
  • 23