Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
maxmert committed Oct 25, 2016
0 parents commit 111f096
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["es2015", "stage-0"]
}
5 changes: 5 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": [
"eslint-config-evilai"
]
}
26 changes: 26 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Logs and databases #
######################
*.log
*.sql
*.sqlite
npm-debug*

# OS generated files #
######################
.DS_Store?
.DS_Store
Icon?

# NPM Installed stuff #
#######################

**/node_modules/
__
/.idea/
.sass-cache
/dump.rdb
**/*.map
*.gypi

# Crypt files #
#######################
32 changes: 32 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});

exports.default = function (req, res, next) {
if ((0, _isEmpty2.default)(req.body.entry) || !(0, _isArray2.default)(req.body.entry)) {
throw new Error('Data coming from messenger is corrupted. Please, check for all fields.');
}

if (!req.bot) {
throw new Error('Field \'bot\' should be initialized in request object.');
}

req.bot.normalized = req.body.entry.reduce(function (acc, entry) {
acc = acc.concat(entry.messaging);
return acc;
}, []);

next();
};

var _isEmpty = require('lodash/isEmpty');

var _isEmpty2 = _interopRequireDefault(_isEmpty);

var _isArray = require('lodash/isArray');

var _isArray2 = _interopRequireDefault(_isArray);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
32 changes: 32 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "nbp-normaliser-fb-messenger",
"version": "0.0.1",
"description": "Node.js Bot Platform normaliser for requests from Facebook Messenger",
"main": "dist/index.js",
"scripts": {
"build": "babel ./src --preset babel-preset-es2015 --out-dir ./dist",
"build:watch": "babel --watch ./src --preset babel-preset-es2015 --out-dir ./dist",
"prepublish": "npm run build"
},
"repository": {
"type": "git",
"url": "git+https://github.com/evilai/nbp-normaliser-fb-messenger.git"
},
"author": "Maxim Vetrenko <[email protected]> (https://github.com/maxmert)",
"license": "ISC",
"bugs": {
"url": "https://github.com/evilai/nbp-normaliser-fb-messenger/issues"
},
"homepage": "https://github.com/evilai/nbp-normaliser-fb-messenger#readme",
"devDependencies": {
"babel-core": "^6.18.0",
"babel-loader": "^6.2.5",
"babel-polyfill": "^6.16.0",
"babel-preset-es2015": "^6.18.0",
"babel-preset-stage-0": "^6.16.0",
"eslint-config-evilai": "0.0.1"
},
"dependencies": {
"lodash": "^4.16.4"
}
}
34 changes: 34 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import isEmpty from 'lodash/isEmpty';
import isArray from 'lodash/isArray';

/**
* [
* {
* sender: { id: '823746283746' },
* recipient: { id: '98234982347' },
* timestamp: 1475519849260,
* message: {
* mid: 'mid.239849h238fh98',
* seq: 8,
* text: 'hi'
* }
* }
* ]
*/

export default function(req, res, next) {
if (isEmpty(req.body.entry) || !isArray(req.body.entry)) {
throw new Error('Data coming from messenger is corrupted. Please, check for all fields.');
}

if (!req.bot) {
throw new Error('Field \'bot\' should be initialized in request object.');
}

req.bot.normalized = req.body.entry.reduce((acc, entry) => {
acc = acc.concat(entry.messaging);
return acc;
}, []);

next();
}

0 comments on commit 111f096

Please sign in to comment.