I'm trying to create and host a webhook via heroku, using this Facebook-messenger-tutorial: https://developers.facebook.com/docs/messenger-platform/getting-started/webhook-setup. Therefore, I'm currently testing cURL-requests to my local host. This request:
curl -H "Content-Type: application/json" -X POST "localhost:3000/" -d "{""object"": ""page"", ""entry"": [{""messaging"": [{""message"": ""TEST_MESSAGE""}]}]}"
should return EVENT RECEIVED in the cmd, and TEST_MESSAGE in a second cmd-window, where I'm executing my application. For executing the application, I used $ node index.js, and I'm receiving "webhook listening" which means to me there is no mistake in the way I execute it.
This is how my file index.js looks like:
'use strict';
// Imports dependencies and set up http server
const
express = require('express'),
bodyParser = require('body-parser'),
app = express().use(bodyParser.json()); // creates express http server
// Sets server port and logs message on success
app.listen(process.env.PORT || 3000, () => console.log('webhook is listening'));
// Creates the endpoint for our webhook
app.post('/webhook', (req, res) => {
let body = req.body;
// Checks this is an event from a page subscription
if (body.object === 'page') {
// Iterates over each entry - there may be multiple if batched
body.entry.forEach(function(entry) {
// Gets the message. entry.messaging is an array, but
// will only ever contain one message, so we get index 0
let webhook_event = entry.messaging[0];
console.log(webhook_event);
});
// Returns a '200 OK' response to all requests
res.status(200).send('EVENT_RECEIVED');
} else {
// Returns a '404 Not Found' if event is not from a page subscription
res.sendStatus(404);
}
});
// Adds support for GET requests to our webhook
app.get('/webhook', (req, res) => {
// Your verify token. Should be a random string.
let VERIFY_TOKEN = "somepassword"
// Parse the query params
let mode = req.query['hub.mode'];
let token = req.query['hub.verify_token'];
let challenge = req.query['hub.challenge'];
// Checks if a token and mode is in the query string of the request
if (mode && token) {
// Checks the mode and token sent is correct
if (mode === 'subscribe' && token === VERIFY_TOKEN) {
// Responds with the challenge token from the request
console.log('WEBHOOK_VERIFIED');
res.status(200).send(challenge);
} else {
// Responds with '403 Forbidden' if verify tokens do not match
res.sendStatus(403);
}
}
});
And this is the respond I'm getting (some html-code):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot POST /</pre>
</body>
</html>
I do not receive an error, just the "wrong" return.
Does anyone have an idea on how to fix this and receive "EVENT RECEIVED" and "TEST_MESSAGE"?
Note: I have seen the follwoing articles, Curl command no return, Prometheus API returning HTML instead of JSON and I did change something of the code from the tutorial relating to Facebook messenger bot error Unexpected token ' in JSON at position 0, but I am still not able to fix this.
I also read How do I POST JSON data with Curl from a terminal/commandline to Test Spring REST?, but it didn't solve my problem, too.