From 981de22f320b067794797704e9c385e8c54945e4 Mon Sep 17 00:00:00 2001 From: joonaen Date: Mon, 22 Feb 2016 12:27:33 +0200 Subject: [PATCH 01/30] Adding gitignore file --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..2db5c5c32 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +{code}/node_modules{code} \ No newline at end of file From 929f8acf8dc6af09738f09775f8e8cbf1cc55453 Mon Sep 17 00:00:00 2001 From: Robert Stankevich Date: Mon, 22 Feb 2016 13:15:51 +0200 Subject: [PATCH 02/30] Answer the question who made it --- bot.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/bot.js b/bot.js index 227944a9f..a3eaffcb5 100755 --- a/bot.js +++ b/bot.js @@ -132,6 +132,16 @@ controller.hears(['what is my name','who am i'],'direct_message,direct_mention,m }); }); +controller.hears(['Who made you','who is your daddy'],'direct_message,direct_mention,mention',function(bot, message) { + + controller.storage.users.get(message.user,function(err, user) { + if (user && user.name) { + bot.reply(message, user.name+' made me.'); + } else { + bot.reply(message,'I have never heard his name'); + } + }); +}); controller.hears(['shutdown'],'direct_message,direct_mention,mention',function(bot, message) { From 12dbea86299f6182d0811d280a858a558dfc442e Mon Sep 17 00:00:00 2001 From: Robert Stankevich Date: Mon, 22 Feb 2016 13:27:41 +0200 Subject: [PATCH 03/30] Answer the question who made it --- bot.js | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/bot.js b/bot.js index a3eaffcb5..2252502e1 100755 --- a/bot.js +++ b/bot.js @@ -133,14 +133,7 @@ controller.hears(['what is my name','who am i'],'direct_message,direct_mention,m }); controller.hears(['Who made you','who is your daddy'],'direct_message,direct_mention,mention',function(bot, message) { - - controller.storage.users.get(message.user,function(err, user) { - if (user && user.name) { - bot.reply(message, user.name+' made me.'); - } else { - bot.reply(message,'I have never heard his name'); - } - }); + bot.reply(message, 'Robert made me.'); }); controller.hears(['shutdown'],'direct_message,direct_mention,mention',function(bot, message) { From 37798ce8348a2d3399249a0e8e8c17bb2913b3de Mon Sep 17 00:00:00 2001 From: Robert Stankevich Date: Mon, 22 Feb 2016 14:03:30 +0200 Subject: [PATCH 04/30] Adding prime feature --- bot.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/bot.js b/bot.js index 2252502e1..75275d742 100755 --- a/bot.js +++ b/bot.js @@ -136,6 +136,33 @@ controller.hears(['Who made you','who is your daddy'],'direct_message,direct_men bot.reply(message, 'Robert made me.'); }); +controller.hears(['prime'],'direct_message,direct_mention,mention',function(bot, message) { + bot.reply(message "First ten prime numbers are: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29"); +}); + +controller.hears(['prime (.*)'],'direct_message,direct_mention,mention',function(bot, message) { + + var input = message.text.match(/prime (.*)/i); + var x = parseInt(input); + var check = new Boolean("true"); + + for(int i = 2; i<=x; i++) { + if(x%i!=0){ + check= false; + break; + } + } + + if(check) { + bot.reply(message "The number you gave is prime"); + } + else { + bot.reply(message "The number you gave is not prime"); + } +}); + + + controller.hears(['shutdown'],'direct_message,direct_mention,mention',function(bot, message) { bot.startConversation(message,function(err, convo) { From 738635f5de59677ca650e7669d03f113419cd601 Mon Sep 17 00:00:00 2001 From: jEnbuska Date: Mon, 22 Feb 2016 14:50:53 +0200 Subject: [PATCH 05/30] fibonacci function added. Tested and working --- bot.js | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/bot.js b/bot.js index 227944a9f..08e1cc05e 100755 --- a/bot.js +++ b/bot.js @@ -169,6 +169,60 @@ controller.hears(['uptime','identify yourself','who are you','what is your name' }); +controller.hears(['fibonacci'],'direct_message,direct_mention,mention',function(bot, message) { + console.log(JSON.stringify(message)); + var param = message.text.replace('fibonacci','').trim(); + console.log(param); + + if(param.length==0){ + var a = 0, b = 1; + var fiboStr = ""; + for (var i = 1; i <= 10; i++) { + a = a + b; + b = a - b; + fiboStr+=""+a; + if(i!==param){ + fiboStr+=", " + } + } + bot.reply(message, fiboStr); + }else if(!isNaN(param) && Number(param) % 1 === 0) { + var couldBeFibo = true; + var isSurelyFibo = false; + + var a = 0, b = 1; + var fibos = []; + var i = 1; + for (; couldBeFibo && !isSurelyFibo; i++) { + a = a + b; + b = a - b; + if (a === Number(param)) { + isSurelyFibo = true; + } else if (a > param) { + couldBeFibo = false; + } else { + fibos.push(a); + } + } + if (isSurelyFibo) { + i = i <= 10 ? 0 : fibos.length - 10; + var fiboStr = ""; + for (; i < fibos.length; i++) { + fiboStr+=fibos[i]; + if ((fibos.length - 1) !== i) { + fiboStr += ", "; + } + } + bot.reply(message, fiboStr); + }else{ + bot.reply(message, "not fibonacci number"); + } + }else{ + bot.reply(message, "Give me a number parameter"); + } +}); + + function formatUptime(uptime) { var unit = 'second'; if (uptime > 60) { From 9d97db1b6e10605e035c5291d36ee732b74d4b9a Mon Sep 17 00:00:00 2001 From: jEnbuska Date: Mon, 22 Feb 2016 14:57:33 +0200 Subject: [PATCH 06/30] merged with fibonacci. gitignore changed --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 2db5c5c32..479880f4f 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -{code}/node_modules{code} \ No newline at end of file +/node_modules +/.idea \ No newline at end of file From 807c24a88ff98e63dd41a90d4974c8fedf3676cc Mon Sep 17 00:00:00 2001 From: Robert Stankevich Date: Mon, 22 Feb 2016 15:15:49 +0200 Subject: [PATCH 07/30] Prime fixed and working, but not complete --- bot.js | 49 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/bot.js b/bot.js index 75275d742..285aee75f 100755 --- a/bot.js +++ b/bot.js @@ -137,28 +137,47 @@ controller.hears(['Who made you','who is your daddy'],'direct_message,direct_men }); controller.hears(['prime'],'direct_message,direct_mention,mention',function(bot, message) { - bot.reply(message "First ten prime numbers are: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29"); + bot.reply(message, 'First ten prime numbers are: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29'); }); controller.hears(['prime (.*)'],'direct_message,direct_mention,mention',function(bot, message) { - - var input = message.text.match(/prime (.*)/i); + + var input = message.match[1]; var x = parseInt(input); - var check = new Boolean("true"); + console.log("X is"+x); + var check = true; - for(int i = 2; i<=x; i++) { - if(x%i!=0){ - check= false; - break; - } - } - if(check) { - bot.reply(message "The number you gave is prime"); - } - else { - bot.reply(message "The number you gave is not prime"); + //if(x>3 && (x%2 ==0 || x%3==0)) { + +// var start = 2; +// while(start <=Math.sqrt(x)) { +// if(x%start++<1) { +// //bot.reply(message, 'The number you gave is not prime'); +// check = false; +// break; +// }else{ +// check = true; +// } +// } + + + var d = x-1; + + while(d>1) { + console.log("X is"+x+" D is"+d); + if((x%d)==0) { + check=false; + break; } + d--; + } + + if(check) { + bot.reply(message, 'The number you gave is prime!'); + }else{ + bot.reply(message, 'The number you gave is not prime!'); + } }); From 0505395d383a6692de1b05e9684a462d4df36bc5 Mon Sep 17 00:00:00 2001 From: jEnbuska Date: Mon, 22 Feb 2016 15:39:50 +0200 Subject: [PATCH 08/30] only some code cleanup --- bot.js | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/bot.js b/bot.js index 08e1cc05e..53cad7f09 100755 --- a/bot.js +++ b/bot.js @@ -169,27 +169,20 @@ controller.hears(['uptime','identify yourself','who are you','what is your name' }); -controller.hears(['fibonacci'],'direct_message,direct_mention,mention',function(bot, message) { - console.log(JSON.stringify(message)); - var param = message.text.replace('fibonacci','').trim(); - console.log(param); - +controller.hears(['fibonacci(.*)'],'direct_message,direct_mention,mention',function(bot, message) { + var param = message.match[1].trim(); if(param.length==0){ var a = 0, b = 1; - var fiboStr = ""; + var fibos = []; for (var i = 1; i <= 10; i++) { a = a + b; b = a - b; - fiboStr+=""+a; - if(i!==param){ - fiboStr+=", " - } + fibos.push(a); } - bot.reply(message, fiboStr); + bot.reply(message, fibos.toString()); }else if(!isNaN(param) && Number(param) % 1 === 0) { var couldBeFibo = true; var isSurelyFibo = false; - var a = 0, b = 1; var fibos = []; var i = 1; @@ -205,15 +198,9 @@ controller.hears(['fibonacci'],'direct_message,direct_mention,mention',function( } } if (isSurelyFibo) { - i = i <= 10 ? 0 : fibos.length - 10; - var fiboStr = ""; - for (; i < fibos.length; i++) { - fiboStr+=fibos[i]; - if ((fibos.length - 1) !== i) { - fiboStr += ", "; - } - } - bot.reply(message, fiboStr); + start = i <= 10 ? 0 : fibos.length - 10; + var tenLastFibos = fibos.slice(start, fibos.length); + bot.reply(message, tenLastFibos.toString()); }else{ bot.reply(message, "not fibonacci number"); } @@ -222,7 +209,6 @@ controller.hears(['fibonacci'],'direct_message,direct_mention,mention',function( } }); - function formatUptime(uptime) { var unit = 'second'; if (uptime > 60) { From 581fbab382edf416b5cb338c16025fc695c479fc Mon Sep 17 00:00:00 2001 From: Robert Stankevich Date: Mon, 22 Feb 2016 15:41:04 +0200 Subject: [PATCH 09/30] Prime is done --- bot.js | 56 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/bot.js b/bot.js index 285aee75f..789b4d274 100755 --- a/bot.js +++ b/bot.js @@ -145,39 +145,41 @@ controller.hears(['prime (.*)'],'direct_message,direct_mention,mention',function var input = message.match[1]; var x = parseInt(input); console.log("X is"+x); - var check = true; - - - //if(x>3 && (x%2 ==0 || x%3==0)) { - -// var start = 2; -// while(start <=Math.sqrt(x)) { -// if(x%start++<1) { -// //bot.reply(message, 'The number you gave is not prime'); -// check = false; -// break; -// }else{ -// check = true; -// } -// } - - - var d = x-1; - - while(d>1) { - console.log("X is"+x+" D is"+d); - if((x%d)==0) { - check=false; - break; + + function isPrime(x) { + var d = x-1; + while(d>1) { + console.log("X is"+x+" D is"+d); + if((x%d)==0) { + return false; + } + d--; } - d--; + return true; } - if(check) { + if(x>1 && x%1!=0 && x!=null) { + if(isPrime(x)) { bot.reply(message, 'The number you gave is prime!'); - }else{ + bot.reply(message, 'Next ten prime numberare: '); + var count = 0; + while(count <10){ + x++; + if(isPrime(x)) { + bot.reply(message, x+', '); + count++; + } + } + } + + else{ + bot.reply(message, 'The number you gave is not prime!'); } + } + else { + bot.reply(message, 'Input a proper number, damn it!'); + } }); From 80386db2fcb802610adb500d315c02a96f6005ab Mon Sep 17 00:00:00 2001 From: Robert Date: Tue, 23 Feb 2016 10:49:23 +0200 Subject: [PATCH 10/30] Merging with use of upstream master --- bot.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/bot.js b/bot.js index a416413aa..ae45b6dad 100755 --- a/bot.js +++ b/bot.js @@ -144,12 +144,12 @@ controller.hears(['prime (.*)'],'direct_message,direct_mention,mention',function var input = message.match[1]; var x = parseInt(input); - console.log("X is"+x); + console.log("X is "+x); function isPrime(x) { var d = x-1; while(d>1) { - console.log("X is"+x+" D is"+d); + console.log("X is "+x+" D is"+d); if((x%d)==0) { return false; } @@ -158,8 +158,8 @@ controller.hears(['prime (.*)'],'direct_message,direct_mention,mention',function return true; } - if(x>1 && x%1!=0 && x!=null) { - if(isPrime(x)) { + if(x>1 && x%1==0 && x!=null) { + if(isPrime(x)) { bot.reply(message, 'The number you gave is prime!'); bot.reply(message, 'Next ten prime numberare: '); var count = 0; @@ -170,13 +170,13 @@ controller.hears(['prime (.*)'],'direct_message,direct_mention,mention',function count++; } } - } + } - else{ - + else{ bot.reply(message, 'The number you gave is not prime!'); + } } - } + else { bot.reply(message, 'Input a proper number, damn it!'); } From bf799c494941c69299f25f9f089cc91aef08480e Mon Sep 17 00:00:00 2001 From: jEnbuska Date: Tue, 23 Feb 2016 11:32:26 +0200 Subject: [PATCH 11/30] fibonacci repaired --- bot.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/bot.js b/bot.js index 00eb7b9bd..fda753340 100755 --- a/bot.js +++ b/bot.js @@ -171,7 +171,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,17 +184,28 @@ 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; } From b8b3135ef4f7d75cd03151a94c21f836bd267301 Mon Sep 17 00:00:00 2001 From: Robert Date: Tue, 23 Feb 2016 11:32:40 +0200 Subject: [PATCH 12/30] Fixed Prime to print results in descending order --- bot.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/bot.js b/bot.js index 00eb7b9bd..4f649a47f 100755 --- a/bot.js +++ b/bot.js @@ -235,8 +235,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 = ""; From 19e5e1fba86a10b777f03f53bcaaf9799c974b94 Mon Sep 17 00:00:00 2001 From: jEnbuska Date: Tue, 23 Feb 2016 13:16:18 +0200 Subject: [PATCH 13/30] weather feature implemented and tested --- .gitmodules | 3 +++ bot.js | 17 +++++++++++++++++ package.json | 4 +++- weather | 1 + 4 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 .gitmodules create mode 160000 weather diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 000000000..613b22fd7 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "weather"] + path = weather + url = https://github.com/cmfatih/weather.git diff --git a/bot.js b/bot.js index b9e5a7ded..774a1ce66 100755 --- a/bot.js +++ b/bot.js @@ -83,6 +83,23 @@ var bot = controller.spawn({ token: process.env.token }).startRTM(); +const weather = require('./weather/lib/weather.js'); + +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) { diff --git a/package.json b/package.json index 69a5959a8..b8ab9aeae 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,8 @@ "jfs": "^0.2.6", "mustache": "^2.2.1", "request": "^2.67.0", - "ws": "^1.0.0" + "ws": "^1.0.0", + "xml2js": "*" }, "devDependencies": { "jscs": "^2.7.0", @@ -20,6 +21,7 @@ "tap-spec": "^4.1.1", "tape": "^4.4.0", "winston": "^2.1.1" + }, "repository": { "type": "git", diff --git a/weather b/weather new file mode 160000 index 000000000..cc33373de --- /dev/null +++ b/weather @@ -0,0 +1 @@ +Subproject commit cc33373de4fc850b050efe844c6e5d6089aa44c0 From 39dcef5fa67a14e0effce274f555b3464d89eab8 Mon Sep 17 00:00:00 2001 From: jEnbuska Date: Tue, 23 Feb 2016 13:42:24 +0200 Subject: [PATCH 14/30] tests pulled --- botmath.js | 5 +++++ package.json | 4 ++++ tests/botmathTest.js | 16 ++++++++++++++++ tests/test.js | 9 +++++++++ 4 files changed, 34 insertions(+) create mode 100644 tests/botmathTest.js create mode 100644 tests/test.js diff --git a/botmath.js b/botmath.js index 20c4242a3..025a7b505 100644 --- a/botmath.js +++ b/botmath.js @@ -10,3 +10,8 @@ var isPrime = function (n) { module.exports.isPrime = isPrime; +var sum = function (num1, num2) { + return parseFloat(num1) + parseFloat(num2); +} + +module.exports.sum = sum; diff --git a/package.json b/package.json index b8ab9aeae..76201bf23 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "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", @@ -23,6 +24,9 @@ "winston": "^2.1.1" }, + "scripts": { + "test": "mocha tests/*.js" + }, "repository": { "type": "git", "url": "https://github.com/juhovan/botkit.git" diff --git a/tests/botmathTest.js b/tests/botmathTest.js new file mode 100644 index 000000000..908258c99 --- /dev/null +++ b/tests/botmathTest.js @@ -0,0 +1,16 @@ +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'))); + }); + }); +}); 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)); + }); + }); +}); From 5d57def108fd8a857e70812a814a6e2ceb3f60a1 Mon Sep 17 00:00:00 2001 From: jEnbuska Date: Tue, 23 Feb 2016 15:04:49 +0200 Subject: [PATCH 15/30] test included --- bot.js | 2 ++ botmath.js | 11 +++++++++++ tests/botmathTest.js | 32 +++++++++++++++++++++++--------- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/bot.js b/bot.js index 774a1ce66..fdb1757cd 100755 --- a/bot.js +++ b/bot.js @@ -226,6 +226,8 @@ function calculateFibonacciUpto(goal) { return fibonacci; } +//module.exports.calculateFibonacciUpto = calculateFibonacciUpto; + function formatUptime(uptime) { var unit = 'second'; if (uptime > 60) { diff --git a/botmath.js b/botmath.js index 025a7b505..dd36f3262 100644 --- a/botmath.js +++ b/botmath.js @@ -15,3 +15,14 @@ var sum = function (num1, 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/tests/botmathTest.js b/tests/botmathTest.js index 908258c99..f7d3d01fd 100644 --- a/tests/botmathTest.js +++ b/tests/botmathTest.js @@ -3,14 +3,28 @@ 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'))); - }); + 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)); + }); }); }); From a07aecdb6976f03ccc21c8279b53d397f8c595f9 Mon Sep 17 00:00:00 2001 From: Robert Date: Wed, 24 Feb 2016 11:32:27 +0200 Subject: [PATCH 16/30] Adding travis --- .travis.yml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .travis.yml 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 From 8598d6ebb58f0819cf4ed58cac5eead6eea91a81 Mon Sep 17 00:00:00 2001 From: Robert Date: Wed, 24 Feb 2016 11:42:27 +0200 Subject: [PATCH 17/30] Added link to readme --- readme.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/readme.md b/readme.md index 60fa8dc18..96ec4fd75 100755 --- a/readme.md +++ b/readme.md @@ -1,5 +1,7 @@ # [Botkit](http://howdy.ai/botkit) - Best course ever! +![alt tag](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)! From 8c4bfea7b8b097ff143b4b56c089d6bfb1542fef Mon Sep 17 00:00:00 2001 From: Robert Date: Wed, 24 Feb 2016 13:30:44 +0200 Subject: [PATCH 18/30] Travis icon uodated with readme --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 96ec4fd75..40f977c23 100755 --- a/readme.md +++ b/readme.md @@ -1,6 +1,6 @@ # [Botkit](http://howdy.ai/botkit) - Best course ever! -![alt tag](https://travis-ci.org/robertstankevich/botkit.svg?branch=master!:https://travis-ci.org/robertstankevich/botkit) +[![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)! From cc3d97c4469f3902f26813b3308e1046ab7205d1 Mon Sep 17 00:00:00 2001 From: Robert Date: Wed, 24 Feb 2016 13:34:10 +0200 Subject: [PATCH 19/30] Added Procfile --- Procfile | 1 + 1 file changed, 1 insertion(+) create mode 100644 Procfile diff --git a/Procfile b/Procfile new file mode 100644 index 000000000..87537474d --- /dev/null +++ b/Procfile @@ -0,0 +1 @@ +worker: node bot.js \ No newline at end of file From 6b1c83838afd5b179d37e466367246c9b393a8a5 Mon Sep 17 00:00:00 2001 From: Robert Date: Wed, 24 Feb 2016 14:02:50 +0200 Subject: [PATCH 20/30] Installed weather-js npm package --- .gitmodules | 3 --- bot.js | 3 ++- package.json | 4 ++-- weather | 1 - 4 files changed, 4 insertions(+), 7 deletions(-) delete mode 100644 .gitmodules delete mode 160000 weather diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index 613b22fd7..000000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "weather"] - path = weather - url = https://github.com/cmfatih/weather.git diff --git a/bot.js b/bot.js index fdb1757cd..8b976b56d 100755 --- a/bot.js +++ b/bot.js @@ -83,7 +83,7 @@ var bot = controller.spawn({ token: process.env.token }).startRTM(); -const weather = require('./weather/lib/weather.js'); +const weather = require('weather.js'); controller.hears(['How is the weather in (.*), (.*)'],'direct_message,direct_mention,mention',function(bot, message) { @@ -101,6 +101,7 @@ controller.hears(['How is the weather in (.*), (.*)'],'direct_message,direct_men }); }); + controller.hears(['hello','hi'],'direct_message,direct_mention,mention',function(bot, message) { bot.api.reactions.add({ diff --git a/package.json b/package.json index 76201bf23..2b587703d 100644 --- a/package.json +++ b/package.json @@ -12,8 +12,9 @@ "mocha": "^2.4.5", "mustache": "^2.2.1", "request": "^2.67.0", + "weather-js": "^1.0.2", "ws": "^1.0.0", - "xml2js": "*" + "xml2js": "*" }, "devDependencies": { "jscs": "^2.7.0", @@ -22,7 +23,6 @@ "tap-spec": "^4.1.1", "tape": "^4.4.0", "winston": "^2.1.1" - }, "scripts": { "test": "mocha tests/*.js" diff --git a/weather b/weather deleted file mode 160000 index cc33373de..000000000 --- a/weather +++ /dev/null @@ -1 +0,0 @@ -Subproject commit cc33373de4fc850b050efe844c6e5d6089aa44c0 From 6a4c2c5c19533c9d5515736fc01764a5b1f934f0 Mon Sep 17 00:00:00 2001 From: Robert Date: Wed, 24 Feb 2016 14:17:50 +0200 Subject: [PATCH 21/30] Fixed reference to weather-js --- bot.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot.js b/bot.js index 8b976b56d..d7c820d3b 100755 --- a/bot.js +++ b/bot.js @@ -83,7 +83,7 @@ var bot = controller.spawn({ token: process.env.token }).startRTM(); -const weather = require('weather.js'); +const weather = require('weather-js'); controller.hears(['How is the weather in (.*), (.*)'],'direct_message,direct_mention,mention',function(bot, message) { From ada4dc34e42d4ee3ea118909737d41fae0013d4f Mon Sep 17 00:00:00 2001 From: Robert Date: Wed, 24 Feb 2016 14:38:51 +0200 Subject: [PATCH 22/30] One more try for Procfile. Now with token --- Procfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Procfile b/Procfile index 87537474d..a954b8944 100644 --- a/Procfile +++ b/Procfile @@ -1 +1 @@ -worker: node bot.js \ No newline at end of file +worker: token=xoxb-22469172560-FHIXCnTkvGU0fTo1m6WmUnCv node bot.js \ No newline at end of file From 77677a465e50ea8a9279743e78594e60cf5a3521 Mon Sep 17 00:00:00 2001 From: jEnbuska Date: Wed, 24 Feb 2016 15:58:59 +0200 Subject: [PATCH 23/30] speedrun feature started but not ready --- bot.js | 30 ++++++++++++++++++++++++++++++ weather | 1 + 2 files changed, 31 insertions(+) create mode 160000 weather diff --git a/bot.js b/bot.js index d7c820d3b..7503353b7 100755 --- a/bot.js +++ b/bot.js @@ -84,6 +84,36 @@ var bot = controller.spawn({ }).startRTM(); const weather = require('weather-js'); +const request = require('request'); + +controller.hears(['speedrun (.*) (.*)'],'direct_message,direct_mention,mention',function(bot, message) { + + if(message.match[1]!==undefined && message.match[2]!==undefined) { + var gamename = message.match[1]; + console.log(gamename); + var difficulty=message.match[2]; + + request.get({url: "http://www.speedrun.com/api_records.php?series="+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{ + if(body.hasOwnProperty(gamename)){ + var game = body[gamename]; + if(game.hasOwnProperty(difficulty)){ + var gameWithDif =game[difficulty]; + bot.reply(message,gameWithDif['timeigt'], null, 2); + } + } + } + }); + } +}); controller.hears(['How is the weather in (.*), (.*)'],'direct_message,direct_mention,mention',function(bot, message) { diff --git a/weather b/weather new file mode 160000 index 000000000..cc33373de --- /dev/null +++ b/weather @@ -0,0 +1 @@ +Subproject commit cc33373de4fc850b050efe844c6e5d6089aa44c0 From 0f507e14be2148567eabc9014936aa7d080f593c Mon Sep 17 00:00:00 2001 From: jEnbuska Date: Thu, 25 Feb 2016 13:03:16 +0200 Subject: [PATCH 24/30] added file 123 --- bot.js | 2 +- somefile123 | 0 2 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 somefile123 diff --git a/bot.js b/bot.js index 7503353b7..20bd250e6 100755 --- a/bot.js +++ b/bot.js @@ -86,7 +86,7 @@ var bot = controller.spawn({ const weather = require('weather-js'); const request = require('request'); -controller.hears(['speedrun (.*) (.*)'],'direct_message,direct_mention,mention',function(bot, message) { +controller.hears(['speedrun (.*), (.*)'],'direct_message,direct_mention,mention',function(bot, message) { if(message.match[1]!==undefined && message.match[2]!==undefined) { var gamename = message.match[1]; diff --git a/somefile123 b/somefile123 new file mode 100644 index 000000000..e69de29bb From 12db0b008fdb71cc0943919a740da6bd0b73929d Mon Sep 17 00:00:00 2001 From: jEnbuska Date: Thu, 25 Feb 2016 14:55:34 +0200 Subject: [PATCH 25/30] speedrun done and working --- bot.js | 64 +++++++++++++++++++++++++++++++++-------------------- somefile123 | 0 2 files changed, 40 insertions(+), 24 deletions(-) delete mode 100644 somefile123 diff --git a/bot.js b/bot.js index 20bd250e6..059313e07 100755 --- a/bot.js +++ b/bot.js @@ -86,33 +86,49 @@ var bot = controller.spawn({ const weather = require('weather-js'); const request = require('request'); -controller.hears(['speedrun (.*), (.*)'],'direct_message,direct_mention,mention',function(bot, message) { - - if(message.match[1]!==undefined && message.match[2]!==undefined) { - var gamename = message.match[1]; - console.log(gamename); - var difficulty=message.match[2]; - - request.get({url: "http://www.speedrun.com/api_records.php?series="+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{ - if(body.hasOwnProperty(gamename)){ - var game = body[gamename]; - if(game.hasOwnProperty(difficulty)){ - var gameWithDif =game[difficulty]; - bot.reply(message,gameWithDif['timeigt'], null, 2); +controller.hears(['speedrun (.*)'],'direct_message,direct_mention,mention',function(bot, message) { + var gameInfo; + if (message.match[1].indexOf(', ')!==-1) { + gameInfo = message.match[1].split(", "); + }else{ + gameInfo=[]; + gameInfo.push(message.match[1]); + } + console.log(gameInfo); + var gamename = gameInfo[0]; + console.log(gamename); + var difficulty; + if(gameInfo.length>1){ + 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.hears(['How is the weather in (.*), (.*)'],'direct_message,direct_mention,mention',function(bot, message) { diff --git a/somefile123 b/somefile123 deleted file mode 100644 index e69de29bb..000000000 From 0cbdc45e63779e85e3a8951cb9afbebe119101fa Mon Sep 17 00:00:00 2001 From: Antti Alasalmi Date: Fri, 26 Feb 2016 11:40:48 +0200 Subject: [PATCH 26/30] added welcome message --- bot.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/bot.js b/bot.js index 059313e07..f1d9c957e 100755 --- a/bot.js +++ b/bot.js @@ -131,6 +131,18 @@ controller.hears(['speedrun (.*)'],'direct_message,direct_mention,mention',funct }); +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]; @@ -273,7 +285,7 @@ function calculateFibonacciUpto(goal) { return fibonacci; } -//module.exports.calculateFibonacciUpto = calculateFibonacciUpto; +// module.exports.calculateFibonacciUpto = calculateFibonacciUpto; function formatUptime(uptime) { var unit = 'second'; From e93b49db42e32280e7c4a627aa17b98b4c4c6cb6 Mon Sep 17 00:00:00 2001 From: robertstankevich Date: Fri, 26 Feb 2016 11:56:46 +0200 Subject: [PATCH 27/30] Feature for guys who leave channels --- bot.js | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/bot.js b/bot.js index 059313e07..addea8ef6 100755 --- a/bot.js +++ b/bot.js @@ -86,6 +86,8 @@ var bot = controller.spawn({ const weather = require('weather-js'); const request = require('request'); + + controller.hears(['speedrun (.*)'],'direct_message,direct_mention,mention',function(bot, message) { var gameInfo; if (message.match[1].indexOf(', ')!==-1) { @@ -159,13 +161,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]); } }); }); @@ -293,6 +296,20 @@ function formatUptime(uptime) { return uptime; } + + +controller.on('channel_leave',function(bot,message) { + + controller.storage.users.get(message.user, function(err, user) { + if(user.name!==undefined) { + 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'); From 5f4f233e1789f0ca487a0aa91a9c4abd81d9b18a Mon Sep 17 00:00:00 2001 From: Antti Alasalmi Date: Fri, 26 Feb 2016 12:31:23 +0200 Subject: [PATCH 28/30] fixed (?) channel_leave --- bot.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot.js b/bot.js index 2be394493..6f29e83ae 100755 --- a/bot.js +++ b/bot.js @@ -313,7 +313,7 @@ function formatUptime(uptime) { controller.on('channel_leave',function(bot,message) { controller.storage.users.get(message.user, function(err, user) { - if(user.name!==undefined) { + if(user && user.name) { bot.reply(message,"Ok, " + user.name+", go find a new channel of your own with blackjack and hookers!"); } else { From 9034f01bddab45adf3a6e673f784e55ac7927984 Mon Sep 17 00:00:00 2001 From: jEnbuska Date: Fri, 26 Feb 2016 12:38:36 +0200 Subject: [PATCH 29/30] merge --- bot.js | 75 ++++++++++++++++++++++++++++++--- lib/CoreBot.js | 14 +++--- lib/SlackBot.js | 4 +- lib/storage/firebase_storage.js | 4 +- lib/storage/simple_storage.js | 2 +- 5 files changed, 82 insertions(+), 17 deletions(-) diff --git a/bot.js b/bot.js index 059313e07..bb0f79c85 100755 --- a/bot.js +++ b/bot.js @@ -86,6 +86,44 @@ var bot = controller.spawn({ 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] + } + }; + gamename.replace(' ', '%20'); + 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); + for(var i = 0; i Date: Fri, 26 Feb 2016 12:50:56 +0200 Subject: [PATCH 30/30] github issues feature implemented and working --- bot.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/bot.js b/bot.js index 13584f038..52682f1b5 100755 --- a/bot.js +++ b/bot.js @@ -98,7 +98,6 @@ controller.hears(['issues (.*)'],'direct_message,direct_mention,mention',functio 'User-Agent': input[0] } }; - gamename.replace(' ', '%20'); request.get(options, function (err, res, body) { if (err) { console.log("ERROR: " + err); @@ -110,14 +109,19 @@ controller.hears(['issues (.*)'],'direct_message,direct_mention,mention',functio } else { var issues = JSON.parse(body); + + var issueStr = ""; for(var i = 0; i