Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1 from sergejmueller/1.1.0
Browse files Browse the repository at this point in the history
v1.1.0
  • Loading branch information
sergejmueller authored Aug 15, 2016
2 parents 9a96718 + c7eba07 commit a92d203
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 52 deletions.
68 changes: 19 additions & 49 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
}
91 changes: 91 additions & 0 deletions lib/app.js
Original file line number Diff line number Diff line change
@@ -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`
} ]
}
}
} )

} )

}


}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 .",
Expand Down

0 comments on commit a92d203

Please sign in to comment.