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

Commit

Permalink
v1.2.0 (#2)
Browse files Browse the repository at this point in the history
* Switch to [BootBot](https://github.com/Charca/bootbot)
* Use rawgit.com for GitHub raw files
* Update ESLint to v3.3.1
  • Loading branch information
sergejmueller authored Aug 17, 2016
1 parent a92d203 commit 78e3f70
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 97 deletions.
Binary file modified config.json.cast5
Binary file not shown.
37 changes: 6 additions & 31 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,18 @@
*/

const os = require('os');
const http = require( 'http' )
const cluster = require('cluster')
const ip = require( './lib/ip' )
const BootBot = require( 'bootbot' )
const app = require( './lib/app' )
const Bot = require( 'messenger-bot' )
const bot = new Bot( require( './config.json' ) )
const config = require( './config.json' )
const bot = new BootBot( config )


bot.on( 'message', ( payload, reply ) => {

const msg = payload.message.text

if ( ! msg ) {
return
}

// Invalid IP
if ( ! ip.isValid( msg ) ) {
return app.handleInvalidIP( msg, reply )
}

// Private IP
if ( ip.isPrivate( msg ) ) {
return app.handlePrivateIP( msg, reply )
}

// Lookup IP
return app.lookupValidIP( msg, reply )

} )


bot.on( 'error', ( error ) => console.log( error.message ) )
bot.module( app );


if ( cluster.isMaster ) {
os.cpus().forEach( _ => cluster.fork( _ ) )
os.cpus().forEach( () => cluster.fork() )
} else {
http.createServer( bot.middleware() ).listen( 3000 )
bot.start()
}
124 changes: 63 additions & 61 deletions lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,89 +3,91 @@
* Required modules
*/

const ip = require( './ip' )
const ip2countrify = require( 'ip2countrify' )


module.exports = {
/**
* Handle incoming messages
*
* @param {Function} bot Bot instance function
* @return mixed
*/

module.exports = ( bot ) => {

/**
* Handle requests with a invalid IP address
*
* @param {String} msg Incoming message
* @param {Function} reply Bot instance function
* @return void
*/
// Greetings
bot.setGreetingText( 'Hey there! Tell me a IP, I will map the right country.' );

handleInvalidIP( msg, reply ) {

if ( msg.toLowerCase().includes( 'thank' ) ) {
return reply( {
'text': "👍\n\nYou are welcome."
} )
}
// Hear, hear
bot.hear( [ 'hello', 'hi', 'hey', 'help' ], ( payload, chat ) => {

return reply( {
'text': "👋\n\nTell me a valid IP,\nI will map the right country."
} )
return chat.say( "👋\n\nHey there! Tell me a IP,\nI will map the right country." );

},
} );
bot.hear( [ 'thanks', 'thank you', 'bye', 'cu' ], ( payload, chat ) => {

return chat.say( "👍\n\nYou are welcome." );

/**
* 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."
} )
// Incomming messages
bot.on( 'message', ( payload, chat, data ) => {

},
const text = payload.message.text

if ( ! text || data.captured ) {
return
}

/**
* Start lookup for a valid IP address
*
* @param {String} ip Incoming IP address
* @param {Function} reply Bot instance function
* @return void
*/
// Invalid IP
if ( ! ip.isValid( text ) ) {
return chat.say( "😢\n\nTell me a valid IP (IPv4 or IPv6),\nI will map the right country." )
}

lookupValidIP( ip, reply ) {
// Private IP
if ( ip.isPrivate( text ) ) {
return chat.say( "😝\n\nReserved IP address?\nYou are kidding me." )
}

ip2countrify.lookup( ip, ( ip, result, error ) => {
// Lookup IP
return lookupValidIP( text, chat )

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`
} ]
}
}
} )
}

} )

}
/**
* Start country lookup for a valid IP address
*
* @param {String} text Incoming IP address
* @param {Function} chat Bot instance function
* @return void
*/

const lookupValidIP = ( text, chat ) => {

ip2countrify.lookup( text, ( ip, result, error ) => {

if ( error ) {
return chat.say( "😢\n\nNothing found for this IP." )
}

const countryName = result.countryName
const countryCode = result.countryCode.toLowerCase()

return chat.sendGenericTemplate( [
{
'title': countryName,
'image_url': `https://cdn.rawgit.com/hjnilsson/country-flags/master/png1000px/${countryCode}.png`
}
], {
typing: true
} )

} )

}
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ip2country-bot",
"version": "1.1.0",
"version": "1.2.0",
"description": "Messenger Bot for IP 2 Country",
"main": "index.js",
"private": true,
Expand All @@ -10,16 +10,16 @@
"lib"
],
"dependencies": {
"ip2countrify": "0.0.4",
"messenger-bot": "^2.3.0"
"bootbot": "^1.0.6",
"ip2countrify": "0.0.4"
},
"devDependencies": {
"config-leaf": "^0.3.0",
"eslint": "^3.3.0"
"eslint": "^3.3.1"
},
"scripts": {
"test": "npm outdated && eslint .",
"start": "node index.js --use_strict",
"start": "node index.js",
"encrypt": "encrypt config.json config.json.cast5",
"decrypt": "decrypt config.json.cast5 config.json"
},
Expand Down

0 comments on commit 78e3f70

Please sign in to comment.