From 06db41d2a54c371c862a6764fdc8c7114cf0719a Mon Sep 17 00:00:00 2001 From: Kasper Kiiskinen Date: Tue, 9 Feb 2016 14:58:35 +0200 Subject: [PATCH 01/36] lets write tests --- package.json | 4 ++++ tests/test.js | 9 +++++++++ 2 files changed, 13 insertions(+) create mode 100644 tests/test.js diff --git a/package.json b/package.json index 69a5959a8..c24644ad1 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" @@ -21,6 +22,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/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 245946530a279b0d5894c1c11719d7c833c853f1 Mon Sep 17 00:00:00 2001 From: Kasper Kiiskinen Date: Mon, 15 Feb 2016 15:14:50 +0200 Subject: [PATCH 02/36] tests example --- bot.js | 12 ++++++++++++ botmath.js | 5 +++++ tests/botmathTest.js | 16 ++++++++++++++++ 3 files changed, 33 insertions(+) create mode 100644 botmath.js create mode 100644 tests/botmathTest.js diff --git a/bot.js b/bot.js index 227944a9f..17c918009 100755 --- a/bot.js +++ b/bot.js @@ -73,6 +73,7 @@ if (!process.env.token) { var Botkit = require('./lib/Botkit.js'); var os = require('os'); +var botmath = require('./botmath.js'); var controller = Botkit.slackbot({ debug: true, @@ -186,3 +187,14 @@ function formatUptime(uptime) { uptime = uptime + ' ' + unit; return uptime; } + +controller.hears('what is (.*) \\+ (.*)',['direct_message', 'direct_mention', 'mention'],function(bot,message) { + + var num1 = message.match[1]; + var num2 = message.match[2]; + + if (num1 != null && num2 != null) { + return bot.reply(message, num1 + ' + ' + num2 + ' = ' + botmath.sum(num1, num2)); + } +}); + diff --git a/botmath.js b/botmath.js new file mode 100644 index 000000000..ec4c913da --- /dev/null +++ b/botmath.js @@ -0,0 +1,5 @@ +var sum = function (num1, num2) { + return parseFloat(num1) + parseFloat(num2); +} + +module.exports.sum = sum; \ No newline at end of file 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'))); + }); + }); +}); From b3e5837eccb4b25b6127c6764d0f2ea7b0c2cd19 Mon Sep 17 00:00:00 2001 From: Onni Aaltonen Date: Mon, 22 Feb 2016 12:04:10 +0200 Subject: [PATCH 03/36] add Gitignore file --- .gitignore | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..084bf9056 --- /dev/null +++ b/.gitignore @@ -0,0 +1,34 @@ +# Logs +logs +*.log +npm-debug.log* + +# Runtime data +pids +*.pid +*.seed + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (http://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules +jspm_packages + +# Optional npm cache directory +.npm + +# Optional REPL history +.node_repl_history \ No newline at end of file From f75d2595bf41bc6f88fac49ef422ea1c222c3a79 Mon Sep 17 00:00:00 2001 From: Tero Anttila Date: Mon, 22 Feb 2016 13:59:36 +0200 Subject: [PATCH 04/36] prime numbers --- bot.js | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/bot.js b/bot.js index 227944a9f..f117c8c98 100755 --- a/bot.js +++ b/bot.js @@ -132,7 +132,16 @@ controller.hears(['what is my name','who am i'],'direct_message,direct_mention,m }); }); +controller.hears(['who made me'],'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 don\'t know yet!'); + } + }); +}); controller.hears(['shutdown'],'direct_message,direct_mention,mention',function(bot, message) { bot.startConversation(message,function(err, convo) { @@ -168,6 +177,58 @@ controller.hears(['uptime','identify yourself','who are you','what is your name' bot.reply(message,':robot_face: I am a bot named <@' + bot.identity.name + '>. I have been running for ' + uptime + ' on ' + hostname + '.'); }); +controller.hears(['prime'],'direct_message,direct_mention,mention',function(bot, message) { + + controller.storage.users.get(message.user,function(err, user) { + bot.reply(message,'2, 3, 5, 7, 11, 13, 17, 19, 23, 29'); + + }); +}); + +controller.hears(['prime (.*)'],'direct_message,direct_mention,mention',function(bot, message) { + + var numbers = message.text.match(/prime (.*)/i); + var number = numbers[1]; + + var test = number / number; + var sectest = number / 2; + + + controller.storage.users.get(message.user,function(err, user) { + + if(test == 1 && sectest % 1 != 0) + { + + bot.reply(message,number +' is prime number'); + + } else + { + bot.reply(message,number +' is not prime number'); + + var count = 0; + var primenumbers = []; + var jako + var jako2 + + while(count < 10) + { + number++; + + jako = number / number; + jako2 = number / 2; + + if(jako == 1 && jako2 % 1 != 0) + { + primenumbers.push(number); + count++; + + } + + } + bot.reply(message,'next 10 prime numbers are: ' + primenumbers); + } + }); +}); function formatUptime(uptime) { var unit = 'second'; From 90f2f540f88bd349f8ab35598d6bad9937008798 Mon Sep 17 00:00:00 2001 From: Tero Anttila Date: Mon, 22 Feb 2016 14:09:02 +0200 Subject: [PATCH 05/36] prime numbers and 2 --- bot.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/bot.js b/bot.js index f117c8c98..8d5fbfaab 100755 --- a/bot.js +++ b/bot.js @@ -196,13 +196,11 @@ controller.hears(['prime (.*)'],'direct_message,direct_mention,mention',function controller.storage.users.get(message.user,function(err, user) { - if(test == 1 && sectest % 1 != 0) - { - - bot.reply(message,number +' is prime number'); - - } else - { + if(test == 1 && sectest % 1 != 0) { + bot.reply(message,number +' is prime number'); + } else if (number == 2) { + bot.reply(message,number +' is prime number'); + } else { bot.reply(message,number +' is not prime number'); var count = 0; From 10179b6e9994fce9914305410f25f691e6117327 Mon Sep 17 00:00:00 2001 From: Tero Anttila Date: Mon, 22 Feb 2016 15:31:53 +0200 Subject: [PATCH 06/36] fibonacci numbers --- bot.js | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/bot.js b/bot.js index 8d5fbfaab..d9ad4143c 100755 --- a/bot.js +++ b/bot.js @@ -227,7 +227,88 @@ controller.hears(['prime (.*)'],'direct_message,direct_mention,mention',function } }); }); +controller.hears(['fibonacci'],'direct_message,direct_mention,mention',function(bot, message) { + controller.storage.users.get(message.user,function(err, user) { + bot.reply(message,'1, 1, 2, 3, 5, 8, 13, 21, 34, 55'); + + }); +}); +controller.hears(['fibonacci (.*)'],'direct_message,direct_mention,mention',function(bot, message) { + +controller.storage.users.get(message.user,function(err, user) { + + var matches = message.text.match(/fibonacci (.*)/i); + var number = matches[1]; + + bot.reply(message,number ); + + function listoffibo(n) { + var a = 0, b = 1, f = 1; + for(var i = 2; i <= n; i++) { + f = a + b; + a = b; + b = f; + } + return f; + }; + + function isFib(val){ + var prev = 0; + var curr = 1; + while(prev<=val){ + if(prev == val){ + + bot.reply(message,' yes ' + val ); + var isnum = true; + + var count = 0; + var fibolist = []; + + while(count < 10) { + + val++ + + var prev = 0; + var curr = 1; + + while(prev<=val){ + if(prev == val){ + + count++; + fibolist.push(val); + var isnum = true; + + } else { + var isnum = false; + } + curr = prev + curr; + prev = curr - prev; + } + + } + bot.reply(message,'next numbers: ' + fibolist ); + + + return; + } else { + var isnum = false; + } + curr = prev + curr; + prev = curr - prev; + } + + + + bot.reply(message,'not fibonacci. '); + + }; + isFib(number); + + + }); + +}); function formatUptime(uptime) { var unit = 'second'; if (uptime > 60) { From 2f72013edc3fed4e992dbcf9265b3541f2efb735 Mon Sep 17 00:00:00 2001 From: Tero Anttila Date: Mon, 22 Feb 2016 15:42:09 +0200 Subject: [PATCH 07/36] fibo2 --- bot.js | 141 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) diff --git a/bot.js b/bot.js index 227944a9f..7dcd38e97 100755 --- a/bot.js +++ b/bot.js @@ -132,7 +132,16 @@ controller.hears(['what is my name','who am i'],'direct_message,direct_mention,m }); }); +controller.hears(['who made me'],'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 don\'t know yet!'); + } + }); +}); controller.hears(['shutdown'],'direct_message,direct_mention,mention',function(bot, message) { bot.startConversation(message,function(err, convo) { @@ -168,7 +177,139 @@ controller.hears(['uptime','identify yourself','who are you','what is your name' bot.reply(message,':robot_face: I am a bot named <@' + bot.identity.name + '>. I have been running for ' + uptime + ' on ' + hostname + '.'); }); +controller.hears(['prime'],'direct_message,direct_mention,mention',function(bot, message) { + + controller.storage.users.get(message.user,function(err, user) { + bot.reply(message,'2, 3, 5, 7, 11, 13, 17, 19, 23, 29'); + + }); +}); + + +controller.hears(['prime (.*)'],'direct_message,direct_mention,mention',function(bot, message) { + + var numbers = message.text.match(/prime (.*)/i); + var number = numbers[1]; + + var test = number / number; + var sectest = number / 2; + + + controller.storage.users.get(message.user,function(err, user) { + + if(test == 1 && sectest % 1 != 0) { + bot.reply(message,number +' is prime number'); + } else if (number == 2) { + bot.reply(message,number +' is prime number'); + } else { + bot.reply(message,number +' is not prime number'); + + var count = 0; + var primenumbers = []; + var jako + var jako2 + + while(count < 10) + { + number++; + + jako = number / number; + jako2 = number / 2; + + if(jako == 1 && jako2 % 1 != 0) + { + primenumbers.push(number); + count++; + + } + + } + bot.reply(message,'next 10 prime numbers are: ' + primenumbers); + } + }); +}); +controller.hears(['fibonacci'],'direct_message,direct_mention,mention',function(bot, message) { + + controller.storage.users.get(message.user,function(err, user) { + bot.reply(message,'1, 1, 2, 3, 5, 8, 13, 21, 34, 55'); + + }); +}); +controller.hears(['fibonacci (.*)'],'direct_message,direct_mention,mention',function(bot, message) { + +controller.storage.users.get(message.user,function(err, user) { + + var matches = message.text.match(/fibonacci (.*)/i); + var number = matches[1]; + + bot.reply(message,number ); + + function listoffibo(n) { + var a = 0, b = 1, f = 1; + for(var i = 2; i <= n; i++) { + f = a + b; + a = b; + b = f; + } + return f; + }; + + function isFib(val){ + var prev = 0; + var curr = 1; + while(prev<=val){ + if(prev == val){ + + bot.reply(message,' yes ' + val ); + var isnum = true; + + var count = 0; + var fibolist = []; + + while(count < 10) { + + val++ + + var prev = 0; + var curr = 1; + + while(prev<=val){ + if(prev == val){ + + count++; + fibolist.push(val); + var isnum = true; + + } else { + var isnum = false; + } + curr = prev + curr; + prev = curr - prev; + } + + } + bot.reply(message,'next numbers: ' + fibolist ); + + + return; + } else { + var isnum = false; + } + curr = prev + curr; + prev = curr - prev; + } + + + + bot.reply(message,'not fibonacci. '); + + }; + isFib(number); + + }); + +}); function formatUptime(uptime) { var unit = 'second'; if (uptime > 60) { From ffc9f7c00bb3f6fc14f0c069b491a5263024949d Mon Sep 17 00:00:00 2001 From: Tero Anttila Date: Tue, 23 Feb 2016 11:25:56 +0200 Subject: [PATCH 08/36] Made changes --- bot.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/bot.js b/bot.js index 6a271bf4c..54065cafc 100755 --- a/bot.js +++ b/bot.js @@ -210,23 +210,28 @@ controller.hears(['prime (.*)'],'direct_message,direct_mention,mention',function while(count < 10) { - number++; + number--; jako = number / number; jako2 = number / 2; - if(jako == 1 && jako2 % 1 != 0) + if(jako == 1 && jako2 % 1 !== 0 || number == 2 || number == -2 || number == 0) { + primenumbers.push(number); count++; } } - bot.reply(message,'next 10 prime numbers are: ' + primenumbers); + bot.reply(message,'last 10 prime numbers are: ' + primenumbers); } }); }); + + + + controller.hears(['fibonacci'],'direct_message,direct_mention,mention',function(bot, message) { controller.storage.users.get(message.user,function(err, user) { From b2d04f0ebec37eae3ad6451f00e5e6b772d640e4 Mon Sep 17 00:00:00 2001 From: Onni Aaltonen Date: Tue, 23 Feb 2016 11:34:21 +0200 Subject: [PATCH 09/36] Day 2 changes to fibonacci functions --- bot.js | 94 +++++++++++++++++++++------------------------------------- 1 file changed, 33 insertions(+), 61 deletions(-) diff --git a/bot.js b/bot.js index ff098e353..13c2f9683 100755 --- a/bot.js +++ b/bot.js @@ -227,13 +227,21 @@ controller.hears(['prime (.*)'],'direct_message,direct_mention,mention',function } }); }); -controller.hears(['fibonacci'],'direct_message,direct_mention,mention',function(bot, message) { + /* controller.hears(['fibonacci'],'direct_message,direct_mention,mention',function(bot, message) { controller.storage.users.get(message.user,function(err, user) { bot.reply(message,'1, 1, 2, 3, 5, 8, 13, 21, 34, 55'); }); +}); */ + + +controller.hears(['fibonacci'], 'direct_message,direct_mention,mention', function(bot, message) { + if (message.text === 'fibonacci') { + bot.reply(message, '1, 1, 2, 3, 5'); + } }); + controller.hears(['fibonacci (.*)'],'direct_message,direct_mention,mention',function(bot, message) { controller.storage.users.get(message.user,function(err, user) { @@ -241,8 +249,6 @@ controller.storage.users.get(message.user,function(err, user) { var matches = message.text.match(/fibonacci (.*)/i); var number = matches[1]; - bot.reply(message,number ); - function listoffibo(n) { var a = 0, b = 1, f = 1; for(var i = 2; i <= n; i++) { @@ -259,36 +265,34 @@ controller.storage.users.get(message.user,function(err, user) { while(prev<=val){ if(prev == val){ - bot.reply(message,' yes ' + val ); var isnum = true; - var count = 0; - var fibolist = []; - - while(count < 10) { - - val++ - - var prev = 0; - var curr = 1; + var count = 0; + var fibolist = []; + + while(count < 5) { + + val++ + + var prev = 0; + var curr = 1; - while(prev<=val){ - if(prev == val){ - - count++; - fibolist.push(val); - var isnum = true; - - } else { - var isnum = false; + while(prev<=val){ + if(prev == val){ + + count++; + fibolist.push(val); + var isnum = true; + + } else { + var isnum = false; + } + curr = prev + curr; + prev = curr - prev; } - curr = prev + curr; - prev = curr - prev; - } - - } - bot.reply(message,'next numbers: ' + fibolist ); + } + bot.reply(message,'next numbers: ' + fibolist ); return; } else { @@ -298,47 +302,15 @@ controller.storage.users.get(message.user,function(err, user) { prev = curr - prev; } - - bot.reply(message,'not fibonacci. '); }; isFib(number); - }); -}); - - -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'); - } -}); +}); -controller.hears(['fibonacci ([0-9]+)'], 'direct_message,direct_mention,mention', function(bot, message) { - var parameter = parseInt(message.match[1]); - - var fibonacci = calculateFibonacciUpto(parameter); - - if (fibonacci[fibonacci.length-1] !== parameter) { - bot.reply(message, 'That is not a Fibonacci number!'); - } - else { - bot.reply(message, fibonacci.slice(fibonacci.length-10,fibonacci.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; -} function formatUptime(uptime) { var unit = 'second'; From b0ba5293e02725e4346d88ac3baf821b616cb738 Mon Sep 17 00:00:00 2001 From: Onni Aaltonen Date: Tue, 23 Feb 2016 11:51:17 +0200 Subject: [PATCH 10/36] Add submodule weather --- .gitmodules | 3 +++ weather | 1 + 2 files changed, 4 insertions(+) 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/weather b/weather new file mode 160000 index 000000000..cc33373de --- /dev/null +++ b/weather @@ -0,0 +1 @@ +Subproject commit cc33373de4fc850b050efe844c6e5d6089aa44c0 From 77f8379b4232f33b9bb47ea5f71bfdc10a6a83f5 Mon Sep 17 00:00:00 2001 From: Totte Partanen Date: Tue, 23 Feb 2016 11:51:22 +0200 Subject: [PATCH 11/36] changes --- bot.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/bot.js b/bot.js index ff098e353..6c381607e 100755 --- a/bot.js +++ b/bot.js @@ -210,13 +210,14 @@ controller.hears(['prime (.*)'],'direct_message,direct_mention,mention',function while(count < 10) { - number++; + number--; jako = number / number; jako2 = number / 2; - if(jako == 1 && jako2 % 1 != 0) + if(jako == 1 && jako2 % 1 !== 0 || number == 2 || number == -2 || number == 0) { + primenumbers.push(number); count++; @@ -358,6 +359,7 @@ function formatUptime(uptime) { return uptime; } +/* 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'); @@ -392,4 +394,5 @@ controller.hears('prime (.*)',['direct_message', 'direct_mention', 'mention'],fu return bot.reply(message, "your parameter: " + parameter + " is not Prime number"); } }); +*/ From 038053eeac84c354015b068d337e522a03edd5b0 Mon Sep 17 00:00:00 2001 From: Migiz Date: Tue, 23 Feb 2016 12:39:11 +0200 Subject: [PATCH 12/36] Fix Fibonacci module --- bot.js | 94 +++++++++------------------------------------------- fibonacci.js | 30 +++++++++++++++++ 2 files changed, 46 insertions(+), 78 deletions(-) create mode 100644 fibonacci.js diff --git a/bot.js b/bot.js index 42a01bbee..9758b2dc6 100755 --- a/bot.js +++ b/bot.js @@ -75,6 +75,8 @@ var MathHelper = require('./botmath.js'); var Botkit = require('./lib/Botkit.js'); var os = require('os'); var botmath = require('./botmath.js'); +var fibo = require('./fibonacci.js'); + var controller = Botkit.slackbot({ debug: true, @@ -228,90 +230,26 @@ controller.hears(['prime (.*)'],'direct_message,direct_mention,mention',function } }); }); - /* controller.hears(['fibonacci'],'direct_message,direct_mention,mention',function(bot, message) { - - controller.storage.users.get(message.user,function(err, user) { - bot.reply(message,'1, 1, 2, 3, 5, 8, 13, 21, 34, 55'); - - }); -}); */ - controller.hears(['fibonacci'], 'direct_message,direct_mention,mention', function(bot, message) { if (message.text === 'fibonacci') { - bot.reply(message, '1, 1, 2, 3, 5'); + bot.reply(message, 'First five fibonacci numbers: 1, 1, 2, 3, 5'); } }); -controller.hears(['fibonacci (.*)'],'direct_message,direct_mention,mention',function(bot, message) { - -controller.storage.users.get(message.user,function(err, user) { - - var matches = message.text.match(/fibonacci (.*)/i); - var number = matches[1]; - - function listoffibo(n) { - var a = 0, b = 1, f = 1; - for(var i = 2; i <= n; i++) { - f = a + b; - a = b; - b = f; - } - return f; - }; - - function isFib(val){ - var prev = 0; - var curr = 1; - while(prev<=val){ - if(prev == val){ - - var isnum = true; - - var count = 0; - var fibolist = []; - - while(count < 5) { - - val++ - - var prev = 0; - var curr = 1; - - while(prev<=val){ - if(prev == val){ - - count++; - fibolist.push(val); - var isnum = true; - - } else { - var isnum = false; - } - curr = prev + curr; - prev = curr - prev; - } - - } - bot.reply(message,'next numbers: ' + fibolist ); - - - return; - } else { - var isnum = false; - } - curr = prev + curr; - prev = curr - prev; - } - - bot.reply(message,'not fibonacci. '); - - }; - isFib(number); - - }); - -}); +controller.hears(['fibonacci ([0-9]+)'], 'direct_message,direct_mention,mention', function(bot, message) { + var parameter = parseInt(message.match[1]); + + var fibonacci = fibo.calculateFibonacciUpto(parameter); + + if (fibonacci[fibonacci.length-1] !== parameter) { + bot.reply(message, 'That is not a Fibonacci number!'); + } + else { + var nextFibs = fibo.calculateNextFiveFibonacci(fibonacci); + bot.reply(message,"That is a Fibonacci number! Here are the next 5: " + nextFibs); + } +}); function formatUptime(uptime) { diff --git a/fibonacci.js b/fibonacci.js new file mode 100644 index 000000000..0bfda90d9 --- /dev/null +++ b/fibonacci.js @@ -0,0 +1,30 @@ +/** + * Created by mikko on 23.2.2016. + */ +/** + * Created by mikko on 23.2.2016. + */ + +var calculateFibonacciUpto = function (goal) { + var fibonacci = [1, 1]; + + while (fibonacci[fibonacci.length-1] < goal) { + fibonacci.push(fibonacci[fibonacci.length-2] + fibonacci[fibonacci.length-1]); + } + + return fibonacci; +}; +var calculateNextFiveFibonacci =function (arr) { + var fib = arr; + var count = 0; + while ( count < 5) { + fib.push(fib[fib.length-2] + fib[fib.length-1]); + count++; + } + fib = fib.slice(fib.length-5,fib.length).join(', '); + return fib; +}; + +module.exports.calculateFibonacciUpto = calculateFibonacciUpto; +module.exports.calculateNextFiveFibonacci = calculateNextFiveFibonacci; + From 1ced376ed41bdd692d5caf2c5b5c8ddf87a9928c Mon Sep 17 00:00:00 2001 From: Totte Partanen Date: Tue, 23 Feb 2016 12:57:45 +0200 Subject: [PATCH 13/36] weather-js --- bot.js | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/bot.js b/bot.js index 299c81c4c..c7f66b788 100755 --- a/bot.js +++ b/bot.js @@ -64,7 +64,7 @@ This bot demonstrates many of the core features of Botkit: -> http://howdy.ai/botkit ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ - +var weather = require('weather-js'); if (!process.env.token) { console.log('Error: Specify token in environment'); @@ -143,6 +143,46 @@ controller.hears(['who made me'],'direct_message,direct_mention,mention',functio } }); }); + +controller.hears(['how is the weather in (.*)'],'direct_message,direct_mention,mention',function(bot, message) { + + var matches = message.text.match(/how is the weather in (.*)/i); + var city = matches[1]; + + controller.storage.users.get(message.user,function(err, user) { + if (city) { + + var weather = getWeather(city); + + bot.reply(message,'The weather in city: ' + city + '' +weather); + getWeather(city); + + + } else { + bot.reply(message,'Please give city.'); + } + }); +}); + +function getWeather(city) { + + + weather.find({search: ''+city+', CA', degreeType: 'F'}, function(err, result) { + if(err) console.log(err); + /* + console.log(JSON.stringify(result, null, 2)); + */ + var info = JSON.stringify(result, null, 2); + console.log("info muuttujan debuggi." + info); + + return info; +}); + +} + + + + controller.hears(['shutdown'],'direct_message,direct_mention,mention',function(bot, message) { bot.startConversation(message,function(err, convo) { From 8dda16912332dc0f16d121413192ebd6273e66b2 Mon Sep 17 00:00:00 2001 From: Onni Aaltonen Date: Tue, 23 Feb 2016 13:07:00 +0200 Subject: [PATCH 14/36] add weather js file --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index c24644ad1..0b9faee28 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "description": "Building blocks for Building Bots", "main": "lib/Botkit.js", "dependencies": { + "weather-js": "", "body-parser": "^1.14.2", "debug": "2.2.0", "eventemitter2": "0.4.14", From 9330ab196f2c45535d876708c65414fa433567e9 Mon Sep 17 00:00:00 2001 From: Migiz Date: Tue, 23 Feb 2016 13:59:17 +0200 Subject: [PATCH 15/36] add tests for fibonacci --- tests/fibonacciTest.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 tests/fibonacciTest.js diff --git a/tests/fibonacciTest.js b/tests/fibonacciTest.js new file mode 100644 index 000000000..36dffac09 --- /dev/null +++ b/tests/fibonacciTest.js @@ -0,0 +1,26 @@ +/** + * Created by mikko on 23.2.2016. + */ +var assert = require('assert'); +var fibonacci = require('../fibonacci.js'); + +describe('fibonacci', function() { + describe('calculateFibonacciUpto', function () { + it('should return array of fibonacci numbers up to a goal', function () { + assert.deepEqual([1,1,2,3,5,8], fibonacci.calculateFibonacciUpto(8)); + assert.deepEqual([1,1,2,3,5,8,13], fibonacci.calculateFibonacciUpto(13)); + assert.deepEqual([1,1], fibonacci.calculateFibonacciUpto(1)); + assert.deepEqual([1,1,2,3,5,8,13,21,34,55], fibonacci.calculateFibonacciUpto(55)); + }) + + }); + describe('calculateNextFiveFibonacci', function () { + it('should return the next 5 fibonacci numbers', function () { + //assert.deepEqual('13, 21, 34, 55, 89', fibonacci.calculateNextFiveFibonacci([1,1,2,3,5,8])); + assert.deepEqual('13, 21, 34, 55, 89', fibonacci.calculateNextFiveFibonacci(fibonacci.calculateFibonacciUpto(8))); + assert.deepEqual('233, 377, 610, 987, 1597', fibonacci.calculateNextFiveFibonacci(fibonacci.calculateFibonacciUpto(144))); + assert.deepEqual('2584, 4181, 6765, 10946, 17711', fibonacci.calculateNextFiveFibonacci(fibonacci.calculateFibonacciUpto(1597))); + }) + + }) +}); From d962c1e5c1b20c7d787fde18c27b6eb04d777a5d Mon Sep 17 00:00:00 2001 From: Totte Partanen Date: Tue, 23 Feb 2016 14:46:32 +0200 Subject: [PATCH 16/36] weather function --- bot.js | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/bot.js b/bot.js index 00e961bb0..e4b1c9bfb 100755 --- a/bot.js +++ b/bot.js @@ -155,30 +155,45 @@ controller.hears(['how is the weather in (.*)'],'direct_message,direct_mention,m controller.storage.users.get(message.user,function(err, user) { if (city) { - var weather = getWeather(city); + weather.find({search: ''+city+', CA', degreeType: 'F'}, function(err, result) { + if(err) console.log(err); - bot.reply(message,'The weather in city: ' + city + '' +weather); - getWeather(city); + console.log("LOCATION" + JSON.stringify(result[0].location.name, null, 2)); + var cityname = JSON.stringify(result[0].location.name, null, 2); + var temperature = JSON.stringify(result[0].current.temperature, null, 2); + var weather = JSON.stringify(result[0].current.skytext, null, 2); + + bot.reply(message,'The weather in ' + cityname + ' ,' +' Temperature: ' + temperature + ' ,' + "Weather: " + weather); + + }); - } else { + } if(!city) { bot.reply(message,'Please give city.'); } + }); }); +function replacer(key,value) +{ + if (key=="current") return value; + else if (key=="location") return value; + else return undefined; +} + function getWeather(city) { - weather.find({search: ''+city+', CA', degreeType: 'F'}, function(err, result) { + weather.find({search: ''+city+', CA', degreeType: 'F'}, function(err, result) { if(err) console.log(err); /* console.log(JSON.stringify(result, null, 2)); */ var info = JSON.stringify(result, null, 2); - console.log("info muuttujan debuggi." + info); - return info; + + return result; }); } From bc9a2007170b031f4e2941ab7e3a894958a9f704 Mon Sep 17 00:00:00 2001 From: Totte Partanen Date: Tue, 23 Feb 2016 14:55:05 +0200 Subject: [PATCH 17/36] =?UTF-8?q?turhat=20funktiot=20pois=20s=C3=A4=C3=A4t?= =?UTF-8?q?oiminnosta?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot.js | 29 +++-------------------------- 1 file changed, 3 insertions(+), 26 deletions(-) diff --git a/bot.js b/bot.js index e4b1c9bfb..7e420d0c8 100755 --- a/bot.js +++ b/bot.js @@ -158,7 +158,9 @@ controller.hears(['how is the weather in (.*)'],'direct_message,direct_mention,m weather.find({search: ''+city+', CA', degreeType: 'F'}, function(err, result) { if(err) console.log(err); - console.log("LOCATION" + JSON.stringify(result[0].location.name, null, 2)); + /* + console.log("LOCATION " + JSON.stringify(result[0].location.name, null, 2)); + */ var cityname = JSON.stringify(result[0].location.name, null, 2); var temperature = JSON.stringify(result[0].current.temperature, null, 2); @@ -175,31 +177,6 @@ controller.hears(['how is the weather in (.*)'],'direct_message,direct_mention,m }); }); -function replacer(key,value) -{ - if (key=="current") return value; - else if (key=="location") return value; - else return undefined; -} - -function getWeather(city) { - - - weather.find({search: ''+city+', CA', degreeType: 'F'}, function(err, result) { - if(err) console.log(err); - /* - console.log(JSON.stringify(result, null, 2)); - */ - var info = JSON.stringify(result, null, 2); - - - return result; -}); - -} - - - controller.hears(['shutdown'],'direct_message,direct_mention,mention',function(bot, message) { From 0d637833109ea8484091614c1e27756d1cea3c4a Mon Sep 17 00:00:00 2001 From: Totte Partanen Date: Tue, 23 Feb 2016 15:06:56 +0200 Subject: [PATCH 18/36] pikku korjaus --- bot.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot.js b/bot.js index 7e420d0c8..08b6ac80a 100755 --- a/bot.js +++ b/bot.js @@ -155,7 +155,7 @@ controller.hears(['how is the weather in (.*)'],'direct_message,direct_mention,m controller.storage.users.get(message.user,function(err, user) { if (city) { - weather.find({search: ''+city+', CA', degreeType: 'F'}, function(err, result) { + weather.find({search: ''+city+'', degreeType: 'C'}, function(err, result) { if(err) console.log(err); /* @@ -166,7 +166,7 @@ controller.hears(['how is the weather in (.*)'],'direct_message,direct_mention,m var temperature = JSON.stringify(result[0].current.temperature, null, 2); var weather = JSON.stringify(result[0].current.skytext, null, 2); - bot.reply(message,'The weather in ' + cityname + ' ,' +' Temperature: ' + temperature + ' ,' + "Weather: " + weather); + bot.reply(message,'The weather in ' + cityname + ' ,' +' Temperature: ' + temperature + 'C ,' + " Weather: " + weather); }); From eee97cc80e3ed2f9023b72193393d0b3399fc304 Mon Sep 17 00:00:00 2001 From: Onni Aaltonen Date: Wed, 24 Feb 2016 10:34:05 +0200 Subject: [PATCH 19/36] ignore netbeans project files --- .gitignore | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 084bf9056..a97110c50 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ logs *.log npm-debug.log* +nbproject # Runtime data pids *.pid @@ -31,4 +32,5 @@ jspm_packages .npm # Optional REPL history -.node_repl_history \ No newline at end of file +.node_repl_history +/nbproject/private/ \ No newline at end of file From 954afeaf521f4a44c2be397d9861689b38afd6ff Mon Sep 17 00:00:00 2001 From: "tero.anttila@metropolia.fi" Date: Wed, 24 Feb 2016 10:42:21 +0200 Subject: [PATCH 20/36] .travis.yml --- .travis.yml | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 000000000..08fb01049 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,3 @@ +language: node_js +node_js: + - "5.4.0" \ No newline at end of file From 0c3d7c31bc1cbe1d853ab0a56a2247f964b562b7 Mon Sep 17 00:00:00 2001 From: "tero.anttila@metropolia.fi" Date: Wed, 24 Feb 2016 10:49:37 +0200 Subject: [PATCH 21/36] pic --- .travis.yml | 3 ++- readme.md | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 08fb01049..579c256d0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,4 @@ language: node_js node_js: - - "5.4.0" \ No newline at end of file + - "5.4.0" + \ No newline at end of file diff --git a/readme.md b/readme.md index 60fa8dc18..1e13ca50d 100755 --- a/readme.md +++ b/readme.md @@ -1,3 +1,5 @@ +[![Build Status](https://travis-ci.org/teroanttila/botkit.svg?branch=master)](https://travis-ci.org/teroanttila/botkit.svg?branch=master) + # [Botkit](http://howdy.ai/botkit) - Best course ever! Botkit designed to ease the process of designing and running useful, creative or From 7c6d4fc7dcb9de18468488e61f6103780a06ee78 Mon Sep 17 00:00:00 2001 From: "tero.anttila@metropolia.fi" Date: Wed, 24 Feb 2016 11:08:26 +0200 Subject: [PATCH 22/36] 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 7647432348c383da93ac6813f08cab8dc6b596cc Mon Sep 17 00:00:00 2001 From: Onni Aaltonen Date: Wed, 24 Feb 2016 13:18:38 +0200 Subject: [PATCH 23/36] Add Poem generator --- bot.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/bot.js b/bot.js index 08b6ac80a..5c27e8db9 100755 --- a/bot.js +++ b/bot.js @@ -147,6 +147,20 @@ controller.hears(['who made me'],'direct_message,direct_mention,mention',functio }); }); +controller.on('user_channel_join',function(bot, message) { + var poembase = [ + "Roses are red, my cat eat dogs for breakfast", + "Gangters are like pets", + "Roses are red Violets are blue Rhyming is hard Like I am for you", + "Roses are okay Violets are fine You be the 6 And I'll be the 9", + "Roses are red Violets are violet Here is my number Why don't you dial it?", + ]; + var selecteditem = parseInt(poembase.length * Math.random(), 10); + var thispoem = poembase[selecteditem]; + + bot.reply(message, 'My poem is here: ' + thispoem); +}); + controller.hears(['how is the weather in (.*)'],'direct_message,direct_mention,mention',function(bot, message) { var matches = message.text.match(/how is the weather in (.*)/i); From 55eae6e251c66934a66dfafa8cd6928d5a2edd26 Mon Sep 17 00:00:00 2001 From: Migiz Date: Wed, 24 Feb 2016 14:43:21 +0200 Subject: [PATCH 24/36] initial commit for speedruns --- .idea/.name | 1 + .idea/botkit.iml | 9 + .idea/encodings.xml | 6 + .idea/jsLibraryMappings.xml | 6 + .idea/libraries/botkit_node_modules.xml | 14 ++ .idea/misc.xml | 13 + .idea/modules.xml | 8 + .idea/vcs.xml | 6 + .idea/workspace.xml | 303 ++++++++++++++++++++++++ bot.js | 77 ++++++ getSpeedrun.js | 39 +++ 11 files changed, 482 insertions(+) create mode 100644 .idea/.name create mode 100644 .idea/botkit.iml create mode 100644 .idea/encodings.xml create mode 100644 .idea/jsLibraryMappings.xml create mode 100644 .idea/libraries/botkit_node_modules.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml create mode 100644 getSpeedrun.js diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 000000000..5fa5cacbc --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +botkit \ No newline at end of file diff --git a/.idea/botkit.iml b/.idea/botkit.iml new file mode 100644 index 000000000..e96c0571f --- /dev/null +++ b/.idea/botkit.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 000000000..97626ba45 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/jsLibraryMappings.xml b/.idea/jsLibraryMappings.xml new file mode 100644 index 000000000..d4bebd32e --- /dev/null +++ b/.idea/jsLibraryMappings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/libraries/botkit_node_modules.xml b/.idea/libraries/botkit_node_modules.xml new file mode 100644 index 000000000..9c430a8f5 --- /dev/null +++ b/.idea/libraries/botkit_node_modules.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 000000000..72abef0a7 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 000000000..ab24e7918 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 000000000..94a25f7f4 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 000000000..13d043d54 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,303 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + $PROJECT_DIR$ + true + + bdd + + DIRECTORY + + false + + + + + + + + + + + 1456306378216 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/bot.js b/bot.js index 08b6ac80a..dadb70744 100755 --- a/bot.js +++ b/bot.js @@ -76,6 +76,10 @@ var Botkit = require('./lib/Botkit.js'); var os = require('os'); var botmath = require('./botmath.js'); var fibo = require('./fibonacci.js'); +var http = require('http'); +var request = require('request'); + + var controller = Botkit.slackbot({ @@ -352,3 +356,76 @@ controller.hears('what is (.*) \\+ (.*)',['direct_message', 'direct_mention', 'm }); +/*controller.hears(['speedrun (.*)'],'direct_message,direct_mention,mention',function(bot, message) { + var matches = message.text.match(/speedrun (.*)/i); + var name = matches[1]; + + var gameArray = new Object(); + + gameArray = getGameInfo(name); + console.log(gameArray); + + //var parsed = JSON.parse(gameInfo); + + /*var gameArray = []; + + for(var x in parsed){ + gameArray.push(parsed[x]); + } + + if(gameArray) { + bot.reply(message, 'Here are some speedruns that I found!'); + + for(var key in gameArray) { + bot.reply(message, key); + } + } else { + bot.reply(message,'Oops! I didnt find any speedruns for ' + name ); + + } + +}); */ + +controller.hears(['speedrun (.*)'],'direct_message,direct_mention,mention',function(bot, message) { + + var matches = message.text.match(/speedrun (.*)/i); + var name = matches[1]; + + const apiUrl = 'http://www.speedrun.com/api_records.php?series='; + var url = apiUrl.concat(name); + + request(url, function (error, response, body) { + if (!error && response.statusCode == 200) { + //console.log(body); + + var parsed = JSON.parse(body); + var gameArray = []; + + for(var x in parsed){ + gameArray.push(parsed[x]); + } + console.log(gameArray) + bot.reply(message, 'Here are some speedruns that I found!'); + + function logArrayElements(element, index, array) { + + var name = JSON.stringify(element); + + bot.reply(message, 'Name: ' + name); + } + + var showDetails = function(name , player, video, len) { + bot.reply(message, 'Name: ' + name); + } + gameArray.forEach(logArrayElements); + + + } + if(error) { + console.log(error); + } + }) + +}); + + diff --git a/getSpeedrun.js b/getSpeedrun.js new file mode 100644 index 000000000..1b7ad9695 --- /dev/null +++ b/getSpeedrun.js @@ -0,0 +1,39 @@ +/** + * Created by mikko on 24.2.2016. + */ +var http = require('http'); + +var getGameInfo = function (name) { + + var options = { + hostname: 'www.speedrun.com', + path: '/api_records.php?series=', + method: 'GET', + json:true + } + options.path.concat(name); + + request(options, function(error, response, body){ + if(error) console.log(error); + else console.log(body); + }); + + + var callback = function(response) { + var str = ''; + + //another chunk of data has been recieved, so append it to `str` + response.on('data', function (chunk) { + str += chunk; + }); + + //the whole response has been recieved, so we just print it out here + response.on('end', function () { + console.log(str); + }); + } + + http.request(options, callback).end(); + + +}; From a5dfa578e330037b65948d2d83468f51594c6e2a Mon Sep 17 00:00:00 2001 From: Migiz Date: Thu, 25 Feb 2016 13:40:18 +0200 Subject: [PATCH 25/36] improve speedrun replies --- .idea/workspace.xml | 31 +++++--------------------- bot.js | 53 ++++++--------------------------------------- getSpeedrun.js | 39 --------------------------------- 3 files changed, 12 insertions(+), 111 deletions(-) delete mode 100644 getSpeedrun.js diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 13d043d54..0cfa7b749 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,6 +2,7 @@ + @@ -26,8 +27,8 @@ - - + + @@ -57,18 +58,6 @@ - - - - - - - - - - - - @@ -269,16 +258,6 @@ - - - - - - - - - - @@ -291,8 +270,8 @@ - - + + diff --git a/bot.js b/bot.js index dadb70744..916e9d21c 100755 --- a/bot.js +++ b/bot.js @@ -356,36 +356,6 @@ controller.hears('what is (.*) \\+ (.*)',['direct_message', 'direct_mention', 'm }); -/*controller.hears(['speedrun (.*)'],'direct_message,direct_mention,mention',function(bot, message) { - var matches = message.text.match(/speedrun (.*)/i); - var name = matches[1]; - - var gameArray = new Object(); - - gameArray = getGameInfo(name); - console.log(gameArray); - - //var parsed = JSON.parse(gameInfo); - - /*var gameArray = []; - - for(var x in parsed){ - gameArray.push(parsed[x]); - } - - if(gameArray) { - bot.reply(message, 'Here are some speedruns that I found!'); - - for(var key in gameArray) { - bot.reply(message, key); - } - } else { - bot.reply(message,'Oops! I didnt find any speedruns for ' + name ); - - } - -}); */ - controller.hears(['speedrun (.*)'],'direct_message,direct_mention,mention',function(bot, message) { var matches = message.text.match(/speedrun (.*)/i); @@ -396,36 +366,27 @@ controller.hears(['speedrun (.*)'],'direct_message,direct_mention,mention',funct request(url, function (error, response, body) { if (!error && response.statusCode == 200) { - //console.log(body); - var parsed = JSON.parse(body); var gameArray = []; for(var x in parsed){ gameArray.push(parsed[x]); } - console.log(gameArray) - bot.reply(message, 'Here are some speedruns that I found!'); - - function logArrayElements(element, index, array) { - var name = JSON.stringify(element); + bot.reply(message,'Here are some speedruns that I found!'); - bot.reply(message, 'Name: ' + name); + for (var item in parsed) { + bot.reply(message,'Game: ' + item); + for (var subItem in parsed[item]) { + var reply = JSON.stringify(parsed[item][subItem], null, 4); + bot.reply(message,reply); + } } - - var showDetails = function(name , player, video, len) { - bot.reply(message, 'Name: ' + name); - } - gameArray.forEach(logArrayElements); - - } if(error) { console.log(error); } }) - }); diff --git a/getSpeedrun.js b/getSpeedrun.js deleted file mode 100644 index 1b7ad9695..000000000 --- a/getSpeedrun.js +++ /dev/null @@ -1,39 +0,0 @@ -/** - * Created by mikko on 24.2.2016. - */ -var http = require('http'); - -var getGameInfo = function (name) { - - var options = { - hostname: 'www.speedrun.com', - path: '/api_records.php?series=', - method: 'GET', - json:true - } - options.path.concat(name); - - request(options, function(error, response, body){ - if(error) console.log(error); - else console.log(body); - }); - - - var callback = function(response) { - var str = ''; - - //another chunk of data has been recieved, so append it to `str` - response.on('data', function (chunk) { - str += chunk; - }); - - //the whole response has been recieved, so we just print it out here - response.on('end', function () { - console.log(str); - }); - } - - http.request(options, callback).end(); - - -}; From 2b73e632220bdcdc0db0f8391a15545a85f96ca7 Mon Sep 17 00:00:00 2001 From: Onni Aaltonen Date: Fri, 26 Feb 2016 10:15:10 +0200 Subject: [PATCH 26/36] Add function for google link --- bot.js | 15 +++++++++++++++ botmath.js | 11 +++++++++++ 2 files changed, 26 insertions(+) diff --git a/bot.js b/bot.js index 5c27e8db9..402071cfc 100755 --- a/bot.js +++ b/bot.js @@ -136,6 +136,21 @@ controller.hears(['what is my name','who am i'],'direct_message,direct_mention,m }); }); + +controller.hears(['google','googleta'],'direct_message,direct_mention,mention',function(bot, message) { + + controller.storage.users.get(message.user,function(err, user) { + + if(err) console.log(err); + + var link = String(botmath.googlefunc('kala ravintola')); + console.log('link in callback' + link); + + bot.reply(message,'Hi, I found a anwser for you. Check this out: ' + link); + + }); +}); + controller.hears(['who made me'],'direct_message,direct_mention,mention',function(bot, message) { controller.storage.users.get(message.user,function(err, user) { diff --git a/botmath.js b/botmath.js index 563cea883..e47ca987e 100644 --- a/botmath.js +++ b/botmath.js @@ -16,3 +16,14 @@ var sum = function (num1, num2) { module.exports.sum = sum; +var googlefunc = function (searchstring) { + //var searchstring = searchstring; + + var link = 'http://lmgtfy.com/?q='+ searchstring; + console.log('Link' + link); + + return link; +} + +module.exports.googlefunc = googlefunc; + From 5f98512fac813175400b12ec61f226245d00e2bb Mon Sep 17 00:00:00 2001 From: "tero.anttila@metropolia.fi" Date: Fri, 26 Feb 2016 10:42:16 +0200 Subject: [PATCH 27/36] Whitespaces replaced with + --- botmath.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/botmath.js b/botmath.js index e47ca987e..b4680239a 100644 --- a/botmath.js +++ b/botmath.js @@ -17,9 +17,10 @@ var sum = function (num1, num2) { module.exports.sum = sum; var googlefunc = function (searchstring) { - //var searchstring = searchstring; - var link = 'http://lmgtfy.com/?q='+ searchstring; + var tosearch = searchstring.replace(/ /g, '+'); + + var link = 'http://lmgtfy.com/?q='+ tosearch; console.log('Link' + link); return link; From 9e128e68d716f2ddebd660854fc010d5d34a9e22 Mon Sep 17 00:00:00 2001 From: Onni Aaltonen Date: Fri, 26 Feb 2016 11:16:33 +0200 Subject: [PATCH 28/36] add a function for random replies --- botmath.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/botmath.js b/botmath.js index e47ca987e..c5bec8161 100644 --- a/botmath.js +++ b/botmath.js @@ -27,3 +27,20 @@ var googlefunc = function (searchstring) { module.exports.googlefunc = googlefunc; +var randomanswer = function () { + var listofresults = [ + 'I found a anwser for you. Check this out:', + 'Hmmm, Have you tried this:', + 'Hey That sound odd could it be this: ', + 'Check the first results here: ', + 'That sounds intrestings! Check this out: ', + 'Maybe you should ask help from here: ', + 'Sorry to hear that you have troubles:( Maybe this would help? ' + ]; + var answers = listofresults[Math.floor(Math.random() * listofresults.length)]; + console.log('result ' + answers); + + return answers; +} + +module.exports.randomanswer = randomanswer; From ab1dbf86c84dad8c30e668ffb995e0e3a2577dd0 Mon Sep 17 00:00:00 2001 From: Tero Anttila Date: Fri, 26 Feb 2016 11:24:55 +0200 Subject: [PATCH 29/36] Create ISSUE_TEMPLATE --- ISSUE_TEMPLATE | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 ISSUE_TEMPLATE diff --git a/ISSUE_TEMPLATE b/ISSUE_TEMPLATE new file mode 100644 index 000000000..461067dcb --- /dev/null +++ b/ISSUE_TEMPLATE @@ -0,0 +1,7 @@ +### Expected behaviuor + + +### Actual behaviour + + +### Steps to reproduce the behaviour From 3d9bbe4d637fa66c0eea300def582197dda3dfa3 Mon Sep 17 00:00:00 2001 From: Tero Anttila Date: Fri, 26 Feb 2016 11:27:32 +0200 Subject: [PATCH 30/36] Update ISSUE_TEMPLATE --- ISSUE_TEMPLATE | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ISSUE_TEMPLATE b/ISSUE_TEMPLATE index 461067dcb..6748cca7c 100644 --- a/ISSUE_TEMPLATE +++ b/ISSUE_TEMPLATE @@ -1,7 +1,7 @@ -### Expected behaviuor +### Expected behavior -### Actual behaviour +### Actual behavior -### Steps to reproduce the behaviour +### Steps to reproduce the behavior From 017bacebe82ef6fb55c4b1c7086abcde0f510d4e Mon Sep 17 00:00:00 2001 From: Totte Partanen Date: Fri, 26 Feb 2016 11:32:33 +0200 Subject: [PATCH 31/36] google --- bot.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/bot.js b/bot.js index 402071cfc..dd603d15c 100755 --- a/bot.js +++ b/bot.js @@ -137,16 +137,27 @@ controller.hears(['what is my name','who am i'],'direct_message,direct_mention,m }); -controller.hears(['google','googleta'],'direct_message,direct_mention,mention',function(bot, message) { +controller.hears(['google (.*)','googleta (.*)'],'direct_message,direct_mention,mention',function(bot, message) { + + + var matches = message.text.match(/google (.*)/i); + + + var sentence = matches[1]; + + console.log(sentence); + controller.storage.users.get(message.user,function(err, user) { if(err) console.log(err); - var link = String(botmath.googlefunc('kala ravintola')); + var link = String(botmath.googlefunc(sentence)); console.log('link in callback' + link); + + var randomfunc = botmath.randomanswer(); - bot.reply(message,'Hi, I found a anwser for you. Check this out: ' + link); + bot.reply(message, randomfunc + ' ' + link); }); }); From 0be79a8ff96c08564026269237ecbe923df7b2d8 Mon Sep 17 00:00:00 2001 From: Totte Partanen Date: Fri, 26 Feb 2016 11:39:34 +0200 Subject: [PATCH 32/36] update --- bot.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/bot.js b/bot.js index dd603d15c..26ad6fa8d 100755 --- a/bot.js +++ b/bot.js @@ -141,10 +141,9 @@ controller.hears(['google (.*)','googleta (.*)'],'direct_message,direct_mention, - var matches = message.text.match(/google (.*)/i); + var matches = message.text.match(/(google|googleta) (.*)/i); - - var sentence = matches[1]; + var sentence = matches[2]; console.log(sentence); From ca5d2524e38a5411d86970412a70a3aa1beeef8d Mon Sep 17 00:00:00 2001 From: Onni Aaltonen Date: Fri, 26 Feb 2016 11:55:31 +0200 Subject: [PATCH 33/36] Add tinyurl address to message --- bot.js | 13 +++++++------ package.json | 1 + 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/bot.js b/bot.js index dd603d15c..4f0c7c313 100755 --- a/bot.js +++ b/bot.js @@ -65,6 +65,7 @@ This bot demonstrates many of the core features of Botkit: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ var weather = require('weather-js'); +var TinyURL = require('tinyurl'); if (!process.env.token) { console.log('Error: Specify token in environment'); @@ -152,13 +153,13 @@ controller.hears(['google (.*)','googleta (.*)'],'direct_message,direct_mention, if(err) console.log(err); - var link = String(botmath.googlefunc(sentence)); - console.log('link in callback' + link); - - var randomfunc = botmath.randomanswer(); + var link = String(botmath.googlefunc(sentence)); - bot.reply(message, randomfunc + ' ' + link); - + var randomfunc = botmath.randomanswer(); + + TinyURL.shorten(link, function(res) { + bot.reply(message, randomfunc + ' ' + res); + }); }); }); diff --git a/package.json b/package.json index 0b9faee28..1ce6b7998 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "main": "lib/Botkit.js", "dependencies": { "weather-js": "", + "tinyurl": "", "body-parser": "^1.14.2", "debug": "2.2.0", "eventemitter2": "0.4.14", From a7415ceb9c0cd755304a3c43e13f2e2c5526896d Mon Sep 17 00:00:00 2001 From: Onni Aaltonen Date: Fri, 26 Feb 2016 12:23:57 +0200 Subject: [PATCH 34/36] Add mention method to trigger func --- bot.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/bot.js b/bot.js index 070c9504f..d8313c8df 100755 --- a/bot.js +++ b/bot.js @@ -138,9 +138,7 @@ controller.hears(['what is my name','who am i'],'direct_message,direct_mention,m }); -controller.hears(['google (.*)','googleta (.*)'],'direct_message,direct_mention,mention',function(bot, message) { - - +controller.hears(['google (.*)','googleta (.*)'],'direct_message,direct_mention,mention,message_received',function(bot, message) { var matches = message.text.match(/(google|googleta) (.*)/i); From 549e5eca8c414bbd945958d2050157de8d23a082 Mon Sep 17 00:00:00 2001 From: Migiz Date: Fri, 26 Feb 2016 12:28:07 +0200 Subject: [PATCH 35/36] Add xkcd comics --- .idea/workspace.xml | 31 ++++++++++++++++++++++++------- bot.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 7 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 0cfa7b749..d22a7cd69 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,7 +2,6 @@ - @@ -27,8 +26,8 @@ - - + + @@ -36,6 +35,16 @@ + + + + + + + + + + @@ -50,7 +59,7 @@ - + @@ -261,17 +270,25 @@ - + + + + + + + + + - - + + diff --git a/bot.js b/bot.js index 916e9d21c..108ad8df9 100755 --- a/bot.js +++ b/bot.js @@ -389,4 +389,37 @@ controller.hears(['speedrun (.*)'],'direct_message,direct_mention,mention',funct }) }); +controller.hears(['xkcd new'],'direct_message,direct_mention,mention',function(bot, message) { + + const url = 'https://xkcd.com/info.0.json'; + + request(url, function (error, response, body) { + if (!error && response.statusCode == 200) { + var parsed = JSON.parse(body); + bot.reply(message,'Here is the latest XKCD comic: ' + parsed['img']); + } + if(error) { + console.log(error); + } + }) +}); + +controller.hears(['xkcd random'],'direct_message,direct_mention,mention',function(bot, message) { + + const host = 'https://xkcd.com/'; + const end = '/info.0.json'; + var num = Math.floor(Math.random() * 1648); + var url = host + num + end; + request(url, function (error, response, body) { + if (!error && response.statusCode == 200) { + var parsed = JSON.parse(body); + console.log('PARSED: ' + parsed); + bot.reply(message,'Here is a random XKCD comic: ' + parsed['img']); + } + if(error) { + console.log(error); + } + }) +}); + From 98eb5c7c70b7191c440bcf8f689a29cd00fa742e Mon Sep 17 00:00:00 2001 From: Migiz Date: Fri, 26 Feb 2016 12:53:36 +0200 Subject: [PATCH 36/36] add gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index a97110c50..0a17ab610 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,7 @@ lib-cov # Coverage directory used by tools like istanbul coverage +.idea/* # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) .grunt @@ -33,4 +34,4 @@ jspm_packages # Optional REPL history .node_repl_history -/nbproject/private/ \ No newline at end of file +/nbproject/private/