diff --git a/index.js b/index.js index 69165a7..0188519 100644 --- a/index.js +++ b/index.js @@ -3,74 +3,44 @@ * Required modules */ +const os = require('os'); const http = require( 'http' ) +const cluster = require('cluster') const ip = require( './lib/ip' ) +const app = require( './lib/app' ) const Bot = require( 'messenger-bot' ) -const ip2countrify = require( 'ip2countrify' ) const bot = new Bot( require( './config.json' ) ) -bot.on( 'error', ( error ) => { - console.log( error.message ) -} ) - - bot.on( 'message', ( payload, reply ) => { - const text = payload.message.text - if ( ! text ) { + const msg = payload.message.text + + if ( ! msg ) { return } // Invalid IP - if ( ! ip.isValid( text ) ) { - - if ( text.toLowerCase().includes( 'thank' ) ) { - return reply( { - 'text': "👍\n\nYou are welcome." - } ) - } - - return reply( { - 'text': "👋\n\nTell me a valid IP,\ni will map the right country." - } ) + if ( ! ip.isValid( msg ) ) { + return app.handleInvalidIP( msg, reply ) } - // Reply private IP - if ( ip.isPrivate( text ) ) { - return reply( { - 'text': "😝\n\nReserved IP address?\nYou are kidding me." - } ) + // Private IP + if ( ip.isPrivate( msg ) ) { + return app.handlePrivateIP( msg, reply ) } // Lookup IP - ip2countrify.lookup( text, ( ip, result, error ) => { - - if ( error ) { - return reply( { - 'text': "😢\n\nNothing found for this IP." - } ) - } - - const countryName = result.countryName - const countryCode = result.countryCode.toLowerCase() + return app.lookupValidIP( msg, reply ) - reply( { - 'attachment': { - 'type': 'template', - 'payload': { - 'template_type': 'generic', - 'elements': [ { - 'title': countryName, - 'image_url': `https://raw.githubusercontent.com/hjnilsson/country-flags/master/png1000px/${countryCode}.png` - } ] - } - } - } ) +} ) - } ) -} ) +bot.on( 'error', ( error ) => console.log( error.message ) ) -http.createServer( bot.middleware() ).listen( 3000 ) +if ( cluster.isMaster ) { + os.cpus().forEach( _ => cluster.fork( _ ) ) +} else { + http.createServer( bot.middleware() ).listen( 3000 ) +} diff --git a/lib/app.js b/lib/app.js new file mode 100644 index 0000000..3c327d9 --- /dev/null +++ b/lib/app.js @@ -0,0 +1,91 @@ + +/** + * Required modules + */ + +const ip2countrify = require( 'ip2countrify' ) + + +module.exports = { + + + /** + * Handle requests with a invalid IP address + * + * @param {String} msg Incoming message + * @param {Function} reply Bot instance function + * @return void + */ + + handleInvalidIP( msg, reply ) { + + if ( msg.toLowerCase().includes( 'thank' ) ) { + return reply( { + 'text': "👍\n\nYou are welcome." + } ) + } + + return reply( { + 'text': "👋\n\nTell me a valid IP,\nI will map the right country." + } ) + + }, + + + /** + * Handle requests with a private IP address + * + * @param {String} ip Incoming IP address + * @param {Function} reply Bot instance function + * @return void + */ + + handlePrivateIP( ip, reply ) { + + return reply( { + 'text': "😝\n\nReserved IP address?\nYou are kidding me." + } ) + + }, + + + /** + * Start lookup for a valid IP address + * + * @param {String} ip Incoming IP address + * @param {Function} reply Bot instance function + * @return void + */ + + lookupValidIP( ip, reply ) { + + ip2countrify.lookup( ip, ( ip, result, error ) => { + + if ( error ) { + return reply( { + 'text': "😢\n\nNothing found for this IP." + } ) + } + + const countryName = result.countryName + const countryCode = result.countryCode.toLowerCase() + + return reply( { + 'attachment': { + 'type': 'template', + 'payload': { + 'template_type': 'generic', + 'elements': [ { + 'title': countryName, + 'image_url': `https://raw.githubusercontent.com/hjnilsson/country-flags/master/png1000px/${countryCode}.png` + } ] + } + } + } ) + + } ) + + } + + +} diff --git a/package.json b/package.json index 901f475..6151f87 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ip2country-bot", - "version": "1.0.0", + "version": "1.1.0", "description": "Messenger Bot for IP 2 Country", "main": "index.js", "private": true, @@ -14,8 +14,8 @@ "messenger-bot": "^2.3.0" }, "devDependencies": { - "config-leaf": "^0.2.0", - "eslint": "^3.1.1" + "config-leaf": "^0.3.0", + "eslint": "^3.3.0" }, "scripts": { "test": "npm outdated && eslint .",