Facebook messenger webhooks failing to receive updates

129 Views Asked by At

The same exact code was running until yesterday, I restarted the ngrok and updated the callback URL. But now it isn't responding. Instead, I got a webhook failing alert from Facebook.

Here is my index.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))

app.use(bodyParser.urlencoded({extended: false}))

app.use(bodyParser.json())

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

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

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)
            console.log(sender);
            continue
        }
        sendTextMessage(sender, ": " + text.substring(0, 200))
    }
    if (event.postback) {
        let text = JSON.stringify(event.postback)
        sendTextMessage(sender, "Postback received", token)
        continue
    }
    }
    res.sendStatus(200)
    })


    const token = process.env.PAGE_ACCESS_TOKEN

    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)
    }
    })
    }



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

I get this error:

running on port 5000 Error: Can't set headers after they are sent.

at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:346:11) at ServerResponse.header (/Users/akash/Google Drive/challenge6/messenger- bot/node_modules/express/lib/response.js:719:10) at ServerResponse.send (/Users/akash/Google Drive/challenge6/messenger- bot/node_modules/express/lib/response.js:164:12) at /Users/akash/Google Drive/challenge6/messenger-bot/index.js:26:6 at Layer.handle [as handle_request] (/Users/akash/Google Drive/challenge6/messenger-bot/node_modules/express/lib/router/layer.js:95:5) at next (/Users/akash/Google Drive/challenge6/messenger-bot/node_modules/express/lib/router/route.js:131:13) at Route.dispatch (/Users/akash/Google Drive/challenge6/messenger-bot/node_modules/express/lib/router/route.js:112:3) at Layer.handle [as handle_request] (/Users/akash/Google Drive/challenge6/messenger-bot/node_modules/express/lib/router/layer.js:95:5) at /Users/akash/Google Drive/challenge6/messenger-bot/node_modules/express/lib/router/index.js:277:22 at Function.process_params (/Users/akash/Google Drive/challenge6/messenger-bot/node_modules/express/lib/router/index.js:330:12)

1

There are 1 best solutions below

1
On

Did you added Procfile? like web: node index.js please sure to save it as a file like Procfile only no extensions like .txt