diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..479880f4f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/node_modules +/.idea \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 000000000..205af32f5 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,5 @@ +language: node_js +node_js: + - "4.1" + - "4.0" + - "iojs" \ No newline at end of file diff --git a/Procfile b/Procfile new file mode 100644 index 000000000..a954b8944 --- /dev/null +++ b/Procfile @@ -0,0 +1 @@ +worker: token=xoxb-22469172560-FHIXCnTkvGU0fTo1m6WmUnCv node bot.js \ No newline at end of file diff --git a/bot.js b/bot.js index 00eb7b9bd..2e35b9627 100755 --- a/bot.js +++ b/bot.js @@ -83,6 +83,132 @@ var bot = controller.spawn({ token: process.env.token }).startRTM(); +const weather = require('weather-js'); +const request = require('request'); + +controller.hears(['issues (.*)'],'direct_message,direct_mention,mention',function(bot, message) { + if (message.match[1].indexOf(', ')===-1) { + bot.reply(message,'wrong format', null, 2); + }else { + var input = message.match[1].split(", "); + var options = { + url: "https://api.github.com/repos/"+input[0]+"/"+input[1]+"/issues", + timeout: 15000, + headers: { + 'User-Agent': input[0] + } + }; + request.get(options, function (err, res, body) { + if (err) { + console.log("ERROR: " + err); + return callback(err); + } + else if (res.statusCode !== 200) { + console.log("res.statusCode !== 200"); + return callback('Request failed (' + res.statusCode + ')'); + } + else { + var issues = JSON.parse(body); + + var issueStr = ""; + for(var i = 0; i1){ + difficulty=gameInfo[1]; + } + gamename.replace(' ', '%20'); + request.get({url: "http://www.speedrun.com/api_records.php?game="+gamename, timeout: 15000}, function(err, res, body) { + if(err){ + console.log("ERROR: " + err); + return callback(err); + } + else if(res.statusCode !== 200) { + console.log("res.statusCode !== 200"); + return callback('Request failed (' + res.statusCode + ')'); + } + else{ + body = JSON.parse(body); + console.log(body); + if(body.hasOwnProperty(gamename)){ + var game = body[gamename]; + if(difficulty !== undefined && game.hasOwnProperty(difficulty)){ + var gameWithDif =game[difficulty]; + bot.reply(message,gameWithDif['timeigt'], null, 2); + bot.reply(message,gameWithDif['video'], null, 2); + }else if(difficulty===undefined){ + for(key in game){ + bot.reply(message,key, null, 2); + } + } + } + } + }); + +}); + +controller.on('user_typing',function(bot, message) { + controller.storage.users.get(message.user,function(err, user) { + if (user && user.name) { + bot.reply(message, user.name + ' is typing something. Is it going to be a novel?'); + } else { + bot.reply(message,'Someone is typing... STOP IT! DO NOT TYPE! YOU ARE MAKING ME NERVOUS'); + } + }); +}); + +controller.on('user_channel_join',function(bot, message) { + controller.storage.users.get(message.user,function(err, user) { + if (user && user.name) { + bot.reply(message,'Welcome to the channel ' + user.name + '!!'); + } else { + bot.reply(message,'Welcome to the channel!'); + } + }); +}); + +controller.hears(['How is the weather in (.*), (.*)'],'direct_message,direct_mention,mention',function(bot, message) { + + var input1 = message.match[1]; + var input2 = message.match[2]; + console.log(input1); + console.log(input2); + weather.find({search: input1 + " " + input2, degreeType: 'C'}, function (err, result) { + console.log(JSON.stringify(result, null, 2)); + if(result !== undefined){ + bot.reply(message, JSON.stringify(result[0].current.temperature, null, 2)); + }else{ + bot.reply(message, "You dont make any sence"); + } + }); +}); + controller.hears(['hello','hi'],'direct_message,direct_mention,mention',function(bot, message) { @@ -95,13 +221,14 @@ controller.hears(['hello','hi'],'direct_message,direct_mention,mention',function bot.botkit.log('Failed to add emoji reaction :(',err); } }); - - + var answers = ["Hello", "Hi", "Good day to you", "HELLO-HELLO-HELLO", "Moi", "Salute", "Greetings", "Hallo"] + var rand = Math.floor((Math.random() * answers.length)); + controller.storage.users.get(message.user,function(err, user) { if (user && user.name) { - bot.reply(message,'Hello ' + user.name + '!!'); + bot.reply(message,answers[rand] + ", " + user.name + '!!'); } else { - bot.reply(message,'Hello.'); + bot.reply(message,answers[rand]); } }); }); @@ -171,7 +298,7 @@ controller.hears(['uptime','identify yourself','who are you','what is your name' controller.hears(['fibonacci'], 'direct_message,direct_mention,mention', function(bot, message) { if (message.text === 'fibonacci') { - bot.reply(message, '1, 1, 2, 3, 5, 8, 13, 21, 34, 55'); + bot.reply(message, '1, 1, 2, 3, 5'); } }); @@ -184,20 +311,33 @@ controller.hears(['fibonacci ([0-9]+)'], 'direct_message,direct_mention,mention' bot.reply(message, 'That is not a Fibonacci number!'); } else { - bot.reply(message, fibonacci.slice(fibonacci.length-10,fibonacci.length).join(', ')); + var a = fibonacci[fibonacci.length-1]; + var b; + if(fibonacci.length>1){ + b=fibonacci[fibonacci.length-2]; + }else{ + b=0; + } + var nextFive = []; + for(var i = 0; i<5; i++){ + nextFive.push(a+b); + b = a; + a = nextFive[i]; + } + bot.reply(message, nextFive.slice(0,nextFive.length).join(', ')); } }); function calculateFibonacciUpto(goal) { var fibonacci = [1, 1]; - while (fibonacci[fibonacci.length-1] < goal) { fibonacci.push(fibonacci[fibonacci.length-2] + fibonacci[fibonacci.length-1]); } - return fibonacci; } +// module.exports.calculateFibonacciUpto = calculateFibonacciUpto; + function formatUptime(uptime) { var unit = 'second'; if (uptime > 60) { @@ -216,6 +356,20 @@ function formatUptime(uptime) { return uptime; } + + +controller.on('channel_leave',function(bot,message) { + + controller.storage.users.get(message.user, function(err, user) { + if(user && user.name) { + bot.reply(message,"Ok, " + user.name+", go find a new channel of your own with blackjack and hookers!"); + } + else { + bot.reply(message,"Someone left the channel"); + } + }); +}); + controller.hears('prime',['direct_message', 'direct_mention', 'mention'],function(bot,message) { if (message.text === "prime") { return bot.reply(message, '2, 3, 5, 7, 11, 13, 17, 19, 23, 29'); @@ -235,8 +389,12 @@ controller.hears('prime (.*)',['direct_message', 'direct_mention', 'mention'],fu if (MathHelper.isPrime(number)) { primes.push(number); } + + if(number ==0) { + break; + } - number++; + number--; } var reply = ""; diff --git a/botmath.js b/botmath.js index 20c4242a3..dd36f3262 100644 --- a/botmath.js +++ b/botmath.js @@ -10,3 +10,19 @@ var isPrime = function (n) { module.exports.isPrime = isPrime; +var sum = function (num1, num2) { + return parseFloat(num1) + parseFloat(num2); +} + +module.exports.sum = sum; + +function calculateFibonacciUpto(goal) { + var fibonacci = [1, 1]; + var next; + while ((next =(fibonacci[fibonacci.length-2] + fibonacci[fibonacci.length-1])) < goal) { + fibonacci.push(next); + } + return fibonacci; +} + +module.exports.calculateFibonacciUpto = calculateFibonacciUpto; diff --git a/lib/CoreBot.js b/lib/CoreBot.js index 090e4a293..3974c3179 100755 --- a/lib/CoreBot.js +++ b/lib/CoreBot.js @@ -51,8 +51,8 @@ function Botkit(configuration) { this.capture = function(response) { var capture_key = this.sent[this.sent.length - 1].text; - if (this.capture_options.key) { - capture_key = this.capture_options.key; + if (this.capture_options.issueKey) { + capture_key = this.capture_options.issueKey; } if (this.capture_options.multiple) { @@ -252,13 +252,13 @@ function Botkit(configuration) { var res = {}; for (var key in this.responses) { - res[key] = this.extractResponse(key); + res[issueKey] = this.extractResponse(issueKey); } return res; }; this.extractResponse = function(key) { - return this.combineMessages(this.responses[key]); + return this.combineMessages(this.responses[issueKey]); }; this.replaceTokens = function(text) { @@ -466,10 +466,10 @@ function Botkit(configuration) { var convo = this.convos[c]; for (var key in convo.responses) { - if (!answers[key]) { - answers[key] = {}; + if (!answers[issueKey]) { + answers[issueKey] = {}; } - answers[key][convo.source_message.user] = convo.extractResponse(key); + answers[issueKey][convo.source_message.user] = convo.extractResponse(issueKey); } } diff --git a/lib/SlackBot.js b/lib/SlackBot.js index 33e34044c..88d57247a 100755 --- a/lib/SlackBot.js +++ b/lib/SlackBot.js @@ -68,7 +68,7 @@ function Slackbot(configuration) { var message = {}; for (var key in req.body) { - message[key] = req.body[key]; + message[issueKey] = req.body[issueKey]; } // let's normalize some of these fields to match the rtm message format @@ -106,7 +106,7 @@ function Slackbot(configuration) { var message = {}; for (var key in req.body) { - message[key] = req.body[key]; + message[issueKey] = req.body[issueKey]; } // let's normalize some of these fields to match the rtm message format diff --git a/lib/storage/firebase_storage.js b/lib/storage/firebase_storage.js index 7bf320c98..a07abf612 100644 --- a/lib/storage/firebase_storage.js +++ b/lib/storage/firebase_storage.js @@ -65,8 +65,8 @@ module.exports = function(config) { firebaseRef.once('value', function(records) { var list = []; - for (key of Object.keys(records.val())) { - list.push(records.val()[key]); + for (issueKey of Object.keys(records.val())) { + list.push(records.val()[issueKey]); } cb(undefined, list); }, diff --git a/lib/storage/simple_storage.js b/lib/storage/simple_storage.js index f91063e33..4ebe32c6e 100755 --- a/lib/storage/simple_storage.js +++ b/lib/storage/simple_storage.js @@ -42,7 +42,7 @@ module.exports = function(config) { cb(err, data); } else { cb(err, Object.keys(data).map(function(key) { - return data[key]; + return data[issueKey]; })); } }; diff --git a/package.json b/package.json index 69a5959a8..2b587703d 100644 --- a/package.json +++ b/package.json @@ -9,9 +9,12 @@ "eventemitter2": "0.4.14", "express": "^4.13.3", "jfs": "^0.2.6", + "mocha": "^2.4.5", "mustache": "^2.2.1", "request": "^2.67.0", - "ws": "^1.0.0" + "weather-js": "^1.0.2", + "ws": "^1.0.0", + "xml2js": "*" }, "devDependencies": { "jscs": "^2.7.0", @@ -21,6 +24,9 @@ "tape": "^4.4.0", "winston": "^2.1.1" }, + "scripts": { + "test": "mocha tests/*.js" + }, "repository": { "type": "git", "url": "https://github.com/juhovan/botkit.git" diff --git a/readme.md b/readme.md index 60fa8dc18..40f977c23 100755 --- a/readme.md +++ b/readme.md @@ -1,5 +1,7 @@ # [Botkit](http://howdy.ai/botkit) - Best course ever! +[![Build Status](https://travis-ci.org/robertstankevich/botkit.svg?branch=master)](https://travis-ci.org/robertstankevich/botkit) + Botkit designed to ease the process of designing and running useful, creative or just plain weird bots (and other types of applications) that live inside [Slack](http://slack.com)! diff --git a/tests/botmathTest.js b/tests/botmathTest.js new file mode 100644 index 000000000..f7d3d01fd --- /dev/null +++ b/tests/botmathTest.js @@ -0,0 +1,30 @@ +var assert = require('assert'); +var bothmath = require('../botmath.js'); + +describe('botmath', function() { + describe('sum', function () { + it('should return sum of 2 values', function () { + assert.equal(-2, bothmath.sum(-2, 0)); + assert.equal(1, bothmath.sum(-1, 2)); + assert.equal(6.5, bothmath.sum(3.5, 3)); + assert.equal(1337, bothmath.sum(1338.2, -1.2)); + }); + it('should return NaN if both values are not numeric', function () { + assert.ok(isNaN(bothmath.sum(1335, 'a'))); + }); + }), + describe('isPrime', function () { + it('should return true', function () { + assert(bothmath.isPrime(2)); + assert(bothmath.isPrime(3)); + assert(bothmath.isPrime(5)); + assert(bothmath.isPrime(7)); + assert(!bothmath.isPrime(22)); + }); + }), + describe('calculateFibonacciUpto', function () { + it('Tests calculateFibonacciUpto', function () { + assert.deepEqual([1,1,2,3,5], bothmath.calculateFibonacciUpto(8)); + }); + }); +}); diff --git a/tests/test.js b/tests/test.js new file mode 100644 index 000000000..b2d23698f --- /dev/null +++ b/tests/test.js @@ -0,0 +1,9 @@ +var assert = require('assert'); +describe('Array', function() { + describe('#indexOf()', function () { + it('should return -1 when the value is not present', function () { + assert.equal(-1, [1,2,3].indexOf(5)); + assert.equal(-1, [1,2,3].indexOf(0)); + }); + }); +}); diff --git a/weather b/weather new file mode 160000 index 000000000..cc33373de --- /dev/null +++ b/weather @@ -0,0 +1 @@ +Subproject commit cc33373de4fc850b050efe844c6e5d6089aa44c0