2

I am trying to use Messenger Platform to create a chatbot. I needed to deploy my Node.js code on a server so I used Heroku.

index.js:

'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 || 1337, () => console.log('webhook is listening'));
app.listen(process.env.PORT || 1337, function(){
  console.log("Express server listening on port %d in %s mode", this.address().port, app.settings.env);
});

// 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 = "simsar-verify-token"

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

When I locally run Heroku (heroku local web), I get the following:

[WARN] No ENV file found
[WARN] EISDIR: illegal operation on a directory, read
[OKAY] package.json file found - trying 'npm start'
15:30:49 web.1   |  > fbwebhook@1.0.0 start F:\fbWebhook
15:30:49 web.1   |  > index.js
15:30:57 web.1   Exited Successfully

so I suppose these warnings should not affect my app. However, when I use (heroku open), I get an "Application Error":

Application error
An error occurred in the application and your page could not be served. If you are the application owner, check your logs for details.

Here are the logs:

2018-01-10T21:55:47.918244+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2018-01-10T21:55:47.911296+00:00 app[web.1]: npm ERR! missing script: start
2018-01-10T21:55:47.917867+00:00 app[web.1]:
2018-01-10T21:55:45.391857+00:00 heroku[web.1]: Starting process with command `npm start`
2018-01-10T21:55:48.039477+00:00 heroku[web.1]: Process exited with status 1
2018-01-10T21:55:42.161256+00:00 heroku[web.1]: State changed from crashed to starting
2018-01-11T03:39:53.875990+00:00 heroku[web.1]: Starting process with command `npm start`
2018-01-11T03:39:56.708942+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2018-01-11T03:39:56.772043+00:00 heroku[web.1]: Process exited with status 1
2018-01-11T03:39:56.697183+00:00 app[web.1]: npm ERR! missing script: start
2018-01-11T03:39:56.708167+00:00 app[web.1]:
2018-01-11T03:39:56.709117+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2018-01-11T03_39_56_699Z-debug.log
2018-01-11T09:32:01.850128+00:00 heroku[web.1]: State changed from crashed to starting
2018-01-11T09:32:06.933938+00:00 heroku[web.1]: Starting process with command `npm start`
2018-01-11T09:32:09.001451+00:00 app[web.1]: npm ERR! missing script: start
2018-01-11T09:32:09.009908+00:00 app[web.1]:
2018-01-11T09:32:09.010475+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2018-01-11T09_32_09_004Z-debug.log
2018-01-11T09:32:09.010262+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2018-01-11T09:32:09.154493+00:00 heroku[web.1]: Process exited with status 1
2018-01-11T09:32:09.168837+00:00 heroku[web.1]: State changed from starting to crashed
2018-01-11T11:53:07.808796+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/robots.txt" host=stark-brushlands-12887.herokuapp.com request_id=735f1156-f647-41b5-84e5-fed9d263853e fwd="84.201.133.9" dyno= connect= service= status=503 bytes= protocol=https
2018-01-11T11:53:12.491453+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=stark-brushlands-12887.herokuapp.com request_id=bdcb1cce-91df-4962-af54-c13ae2ed3765 fwd="77.88.47.9" dyno= connect= service= status=503 bytes= protocol=https
2018-01-11T11:53:20.205754+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=stark-brushlands-12887.herokuapp.com request_id=f236fe35-77f1-45d6-93ca-04534cbc7b59 fwd="84.201.133.3" dyno= connect= service= status=503 bytes= protocol=https
2018-01-11T11:53:16.304878+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=stark-brushlands-12887.herokuapp.com request_id=25e8d044-2d1b-43b9-837c-9dc44d568e98 fwd="77.88.47.8" dyno= connect= service= status=503 bytes= protocol=https
2018-01-11T15:00:25.846672+00:00 heroku[web.1]: Process exited with status 1
2018-01-11T15:00:25.749933+00:00 app[web.1]: npm ERR! missing script: start
2018-01-11T15:00:25.759309+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2018-01-11T15_00_25_751Z-debug.log
2018-01-11T15:00:25.759130+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2018-01-11T15:00:25.758761+00:00 app[web.1]:
2018-01-11T15:00:23.110217+00:00 heroku[web.1]: Starting process with command `npm start`
2018-01-11T15:00:23.110217+00:00 heroku[web.1]: Starting process with command `npm start`
2018-01-11T20:28:36.053317+00:00 app[web.1]: npm ERR! missing script: start
2018-01-11T20:28:36.060101+00:00 app[web.1]:
2018-01-11T20:28:36.060445+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2018-01-11T20:28:36.060618+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2018-01-11T20_28_36_056Z-debug.log
2018-01-11T20:28:36.135240+00:00 heroku[web.1]: Process exited with status 1
2018-01-11T20:28:33.692886+00:00 heroku[web.1]: Starting process with command `npm start`
2018-01-12T01:56:42.471937+00:00 heroku[web.1]: Starting process with command `npm start`
2018-01-12T01:56:44.429428+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2018-01-12T01:56:44.429597+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2018-01-12T01_56_44_422Z-debug.log
2018-01-12T01:56:44.421065+00:00 app[web.1]: npm ERR! missing script: start
2018-01-12T01:56:44.429121+00:00 app[web.1]:
2018-01-12T01:56:44.484876+00:00 heroku[web.1]: Process exited with status 1
2018-01-12T07:24:51.897030+00:00 heroku[web.1]: State changed from crashed to starting
2018-01-12T07:24:54.468890+00:00 heroku[web.1]: Starting process with command `npm start`
2018-01-12T07:24:56.519546+00:00 app[web.1]:
2018-01-12T07:24:56.519843+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2018-01-12T07:24:56.519971+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2018-01-12T07_24_56_513Z-debug.log
2018-01-12T07:24:56.512072+00:00 app[web.1]: npm ERR! missing script: start
2018-01-12T07:24:56.573356+00:00 heroku[web.1]: Process exited with status 1
2018-01-12T07:24:56.590284+00:00 heroku[web.1]: State changed from starting to crashed
2018-01-12T13:09:04.829915+00:00 heroku[web.1]: State changed from crashed to starting
2018-01-12T13:09:07.647910+00:00 heroku[web.1]: Starting process with command `npm start`
2018-01-12T13:09:10.357983+00:00 app[web.1]: npm ERR! missing script: start
2018-01-12T13:09:10.365896+00:00 app[web.1]:
2018-01-12T13:09:10.366193+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2018-01-12T13:09:10.366382+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2018-01-12T13_09_10_360Z-debug.log
2018-01-12T13:09:10.464700+00:00 heroku[web.1]: State changed from starting to crashed
2018-01-12T13:09:10.446451+00:00 heroku[web.1]: Process exited with status 1
2018-01-12T18:53:33.848992+00:00 heroku[web.1]: State changed from crashed to starting
2018-01-12T18:53:36.847218+00:00 heroku[web.1]: Starting process with command `npm start`
2018-01-12T18:53:39.621356+00:00 app[web.1]: npm ERR! missing script: start
2018-01-12T18:53:39.635540+00:00 app[web.1]:
2018-01-12T18:53:39.636218+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2018-01-12T18:53:39.636946+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2018-01-12T18_53_39_623Z-debug.log
2018-01-12T18:53:39.713479+00:00 heroku[web.1]: Process exited with status 1
2018-01-12T18:53:39.728886+00:00 heroku[web.1]: State changed from starting to crashed
2018-01-13T00:30:07.267149+00:00 heroku[web.1]: State changed from crashed to starting
2018-01-13T00:30:22.714411+00:00 heroku[web.1]: Starting process with command `npm start`
2018-01-13T00:30:27.075997+00:00 app[web.1]: npm ERR! missing script: start
2018-01-13T00:30:27.112493+00:00 app[web.1]:
2018-01-13T00:30:27.113007+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2018-01-13T00:30:27.113286+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2018-01-13T00_30_27_085Z-debug.log
2018-01-13T00:30:27.241145+00:00 heroku[web.1]: Process exited with status 1
2018-01-13T00:30:27.262097+00:00 heroku[web.1]: State changed from starting to crashed
2018-01-13T06:22:40.575803+00:00 heroku[web.1]: State changed from crashed to starting
2018-01-13T06:22:47.547488+00:00 heroku[web.1]: Starting process with command `npm start`
2018-01-13T06:22:50.008167+00:00 heroku[web.1]: State changed from starting to crashed
2018-01-13T06:22:49.916148+00:00 app[web.1]: npm ERR! missing script: start
2018-01-13T06:22:49.928182+00:00 app[web.1]:
2018-01-13T06:22:49.928443+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2018-01-13T06:22:49.928558+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2018-01-13T06_22_49_918Z-debug.log
2018-01-13T06:22:49.991343+00:00 heroku[web.1]: Process exited with status 1
2018-01-13T12:31:16.840641+00:00 heroku[web.1]: Starting process with command `npm start`
2018-01-13T12:31:18.994426+00:00 heroku[web.1]: Process exited with status 1
2018-01-13T12:31:18.912930+00:00 app[web.1]: npm ERR! missing script: start
2018-01-13T12:31:18.919786+00:00 app[web.1]:
2018-01-13T12:31:18.920052+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2018-01-13T12:31:18.920165+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2018-01-13T12_31_18_914Z-debug.log
2018-01-13T12:31:19.009480+00:00 heroku[web.1]: State changed from starting to crashed
2018-01-13T12:31:14.460087+00:00 heroku[web.1]: State changed from crashed to starting
2018-01-14T09:38:34.382554+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=stark-brushlands-12887.herokuapp.com request_id=00ce2f80-ecda-4dec-851f-0a34602b13c1 fwd="176.195.92.145" dyno= connect= service= status=503 bytes= protocol=https
2018-01-16T23:41:24.818427+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=stark-brushlands-12887.herokuapp.com request_id=a49b7da5-d8b0-44a1-9dcd-80b2bd5270b6 fwd="46.188.52.227" dyno= connect= service= status=503 bytes= protocol=https
2018-01-19T03:57:00.429131+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=stark-brushlands-12887.herokuapp.com request_id=1c818331-d562-407b-a46e-9187faa6b1cf fwd="109.173.52.156" dyno= connect= service= status=503 bytes= protocol=https
2018-01-19T05:49:54.789035+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=stark-brushlands-12887.herokuapp.com request_id=fddaeb02-09ab-420e-8b98-a51f0b58366f fwd="178.140.62.238" dyno= connect= service= status=503 bytes= protocol=https
2018-01-20T12:37:12.578068+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=stark-brushlands-12887.herokuapp.com request_id=fe92acb8-bf2c-4a63-9e69-1ed5cd2cb7fa fwd="156.222.2.255" dyno= connect= service= status=503 bytes= protocol=https
2018-01-20T12:37:18.021816+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=stark-brushlands-12887.herokuapp.com request_id=52c7b969-d5e4-4082-baac-7140c12f5f30 fwd="156.222.2.255" dyno= connect= service= status=503 bytes= protocol=https
2018-01-20T12:38:38.376525+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=stark-brushlands-12887.herokuapp.com request_id=b7e9473d-76cb-4793-8278-3e58a22022bc fwd="156.222.2.255" dyno= connect= service= status=503 bytes= protocol=https
2018-01-20T12:38:38.932367+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=stark-brushlands-12887.herokuapp.com request_id=5b241b1e-0adc-4433-883c-56e7c77a3d42 fwd="156.222.2.255" dyno= connect= service= status=503 bytes= protocol=https
2018-01-20T13:09:26.003809+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=stark-brushlands-12887.herokuapp.com request_id=27252400-a134-4769-a8e1-6b398eac0e75 fwd="156.222.2.255" dyno= connect= service= status=503 bytes= protocol=https
2018-01-20T13:09:26.932188+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=stark-brushlands-12887.herokuapp.com request_id=e62aed4a-9290-45e8-9c47-17dbb75e779b fwd="156.222.2.255" dyno= connect= service= status=503 bytes= protocol=https
2018-01-20T13:52:15.843438+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=HEAD path="/" host=stark-brushlands-12887.herokuapp.com request_id=f1e5e952-ea5f-405a-8b3f-5a32523a816a fwd="107.22.114.80" dyno= connect= service= status=503 bytes= protocol=http
2018-01-20T14:40:08.055379+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=stark-brushlands-12887.herokuapp.com request_id=fb355f6e-9e63-4c65-949e-23b159129fc8 fwd="41.43.199.44" dyno= connect= service= status=503 bytes= protocol=https
2018-01-20T14:40:10.688721+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=stark-brushlands-12887.herokuapp.com request_id=0c022d4d-5985-4a81-ac00-b34e8ba86b3a fwd="41.43.199.44" dyno= connect= service= status=503 bytes= protocol=https
2018-01-20T14:41:19.679024+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=stark-brushlands-12887.herokuapp.com request_id=a86c152b-ba26-4a49-a8f3-43f69e8de664 fwd="41.43.199.44" dyno= connect= service= status=503 bytes= protocol=https
2018-01-20T14:41:20.936565+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=stark-brushlands-12887.herokuapp.com request_id=a42d929d-d8c1-4745-83df-7c280d9ad1ba fwd="41.43.199.44" dyno= connect= service= status=503 bytes= protocol=https
2018-01-20T14:42:37.088991+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=stark-brushlands-12887.herokuapp.com request_id=2c2c358e-35d0-4295-9752-f570fc673ffe fwd="41.43.199.44" dyno= connect= service= status=503 bytes= protocol=https
2018-01-20T14:42:37.902849+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=stark-brushlands-12887.herokuapp.com request_id=e89a7e3c-c086-4555-a5a4-8d2924e4a3fa fwd="41.43.199.44" dyno= connect= service= status=503 bytes= protocol=https
2018-01-20T15:41:33.000000+00:00 app[api]: Build started by user abeer.ah12.7@gmail.com
2018-01-20T15:41:33.000000+00:00 app[api]: Build failed -- check your build logs
2018-01-20T15:46:29.107092+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=stark-brushlands-12887.herokuapp.com request_id=11b8f7e7-e858-40f9-8468-212ef1078b9a fwd="41.43.199.44" dyno= connect= service= status=503 bytes= protocol=https
2018-01-20T15:46:29.645363+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=stark-brushlands-12887.herokuapp.com request_id=0dbead30-af5b-43e8-b8ad-4aacc96f661c fwd="41.43.199.44" dyno= connect= service= status=503 bytes= protocol=https

I got the webhook setup code (my index.js) from https://developers.facebook.com/docs/messenger-platform/getting-started/webhook-setup

I found that package.json is essential but it's up-to-date in my project:

{
  "name": "fbwebhook",
  "version": "1.0.0",
  "engines": {
    "node": "6.11.0",
    "npm": "3.10.10"
  },
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "index.js"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/Abeer-Ahmed/fbwebhook.git"
  },
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/Abeer-Ahmed/fbwebhook/issues"
  },
  "homepage": "https://github.com/Abeer-Ahmed/fbwebhook#readme",
  "dependencies": {
    "body-parser": "^1.18.2",
    "express": "^4.16.2"
  },
  "devDependencies": {
    "dotenv": "^4.0.0"
  },
  "keywords": [
    "heroku"
  ],
  "description": ""
}

So what am I missing?

Abeer Ahmed
  • 21
  • 1
  • 3

1 Answers1

4

Try to change "start": "index.js" to "start": "node index.js". In scripts section of package.json.

Beside you should define a procfile like this like in the official heroku documentation

FedeMITIC
  • 93
  • 1
  • 5
  • I did so and now heroku local web gives the following: [WARN] No ENV file found 16:45:18 web.1 | Express server listening on port 5000 in development mode But still heroku open gives H10 error! – Abeer Ahmed Jan 20 '18 at 14:46
  • I don't think the problem is related to .env file unless you have used it to work locally: .env files just contains a set of variable that can be read by the application. Can you please edit the log you provided with the new error(s)? – FedeMITIC Jan 20 '18 at 15:36
  • Sure, @FedeMITIC! – Abeer Ahmed Jan 20 '18 at 15:50
  • I tried to deploy your code on my heroku account, and it works without any problem: build succeded and the app doesn't crash. I didn't do any modification to your code, so I suggest you to try again from scratch the set up of heroku. @AbeerAhmed – FedeMITIC Jan 20 '18 at 16:15
  • OK, you rock! Now I get "CANNOT GET/" error. It's a routing problem now, isn't it? Is it about the port number? @FedeMITIC – Abeer Ahmed Jan 20 '18 at 18:07
  • @AbeerAhmed Yep, because you don't have any route for '/' defined. If you want a simple test, you can put in index js near `app.get('/webhook')` this code `app.get('/', (req, res) => { res.status(200).send({ message: "Hello!", }); });` and it will show "Hello" when you visit GET /. – FedeMITIC Jan 20 '18 at 18:10
  • Aha! I added that code but I get H12 (request timeout)! Sorry for keeping asking :") @FedeMITIC. Oh running it on local web now gives an unhandled error (listen EADDRINUSE :::5000) – Abeer Ahmed Jan 20 '18 at 18:47
  • EADDRINUSE :::5000 it's a common error that occurs when you are trying to start a service on a port that is already in use. Just make sure to kill the previous instance of the server (you can issue the command top if you're on Linux and kill the application) before running a new one. About the timeout, if I recall correctly it's set by Heroku and it's default value is 30s. I don't know how to modify that value, so I recommend you to open a new question on SO if you need further help – FedeMITIC Jan 20 '18 at 18:57