From 78e3f70a00527a56db21325cb3971d85a07452ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergej=20M=C3=BCller?= Date: Wed, 17 Aug 2016 21:00:56 +0200 Subject: [PATCH] v1.2.0 (#2) * Switch to [BootBot](https://github.com/Charca/bootbot) * Use rawgit.com for GitHub raw files * Update ESLint to v3.3.1 --- config.json.cast5 | Bin 232 -> 296 bytes index.js | 37 +++----------- lib/app.js | 124 +++++++++++++++++++++++----------------------- package.json | 10 ++-- 4 files changed, 74 insertions(+), 97 deletions(-) diff --git a/config.json.cast5 b/config.json.cast5 index 21d3fbf1e8f92bfdd44eb20b3b4e363389903041..f875dcb1981561bf7ccbb9a5d665a498cde741e5 100644 GIT binary patch literal 296 zcmV+@0oVSf?)r=E6@iX(vWf=#^u96XAi@I44~Gu!h?<+;nF&P_`eN#qEv?fOoOJuu zi*j<&s7b|H3y5_C`dYnNsYR#zdxNpmFuo(0rTgM88B%AIs1Uzwv3GwK-8ua+vWW8m zfN~hc$G+?ewp?m`Q&jk^X7{a>NwG(kO7-eep?6H<0a~1^PLG<`wiGbi6%HzEn=lke zDxe4jGKh^`D=}i+$@*}2kvhr}bkIBY-L56DhvjXVI-*r{%wJE&9gT1=jAsq<$bst* zz60fJ8WWHL$$LAQEuMpnB%g>f}jDugTz_&_1vVJoF diff --git a/index.js b/index.js index 0188519..dfb7795 100644 --- a/index.js +++ b/index.js @@ -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() } diff --git a/lib/app.js b/lib/app.js index 3c327d9..eb5d216 100644 --- a/lib/app.js +++ b/lib/app.js @@ -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 + } ) + } ) } diff --git a/package.json b/package.json index 6151f87..3e8a6a2 100644 --- a/package.json +++ b/package.json @@ -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, @@ -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" },