From 06db41d2a54c371c862a6764fdc8c7114cf0719a Mon Sep 17 00:00:00 2001 From: Kasper Kiiskinen Date: Tue, 9 Feb 2016 14:58:35 +0200 Subject: [PATCH 01/34] 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/34] 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 2bb91b0d9f364b3840d1c095963b4af1b72dec95 Mon Sep 17 00:00:00 2001 From: segeila Date: Mon, 22 Feb 2016 11:57:24 +0200 Subject: [PATCH 03/34] Create .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..07e6e472c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/node_modules From ea63c69c15ce8d8d7e23b88a2951addeea0d5607 Mon Sep 17 00:00:00 2001 From: Polina Timofeeva Date: Mon, 22 Feb 2016 14:06:21 +0200 Subject: [PATCH 04/34] Bot answers who made it --- bot.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/bot.js b/bot.js index 227944a9f..dbe1eea90 100755 --- a/bot.js +++ b/bot.js @@ -132,6 +132,17 @@ controller.hears(['what is my name','who am i'],'direct_message,direct_mention,m }); }); +controller.hears(['Who made you','Who created you'],'direct_message,direct_mention,mention',function(bot, message) { + + controller.storage.users.get(message.user,function(err, user) { + if (user && user.name) { + bot.reply(message,'Polina is my master'); + } else { + bot.reply(message,'I don\'t know yet!'); + } + }); +}); + controller.hears(['shutdown'],'direct_message,direct_mention,mention',function(bot, message) { From da242a1a18a65c17f50243700dc9f16f1a5755a9 Mon Sep 17 00:00:00 2001 From: Polina Timofeeva Date: Mon, 22 Feb 2016 14:15:53 +0200 Subject: [PATCH 05/34] Bot answers who made it --- bot.js | 7 ------- 1 file changed, 7 deletions(-) diff --git a/bot.js b/bot.js index dbe1eea90..1fd292563 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 created you'],'direct_message,direct_mention,mention',function(bot, message) { - - controller.storage.users.get(message.user,function(err, user) { - if (user && user.name) { bot.reply(message,'Polina is my master'); - } else { - bot.reply(message,'I don\'t know yet!'); - } - }); }); From 2ca2f8a5cbc898e41d21c5f6640e581ac198f0c3 Mon Sep 17 00:00:00 2001 From: Polina Timofeeva Date: Mon, 22 Feb 2016 15:02:28 +0200 Subject: [PATCH 06/34] Testing if a number is a Fibonacci number --- bot.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/bot.js b/bot.js index 1fd292563..af7b71a27 100755 --- a/bot.js +++ b/bot.js @@ -121,6 +121,38 @@ controller.hears(['call me (.*)'],'direct_message,direct_mention,mention',functi }); }); +controller.hears(['fibonacci'],'direct_message,direct_mention,mention',function(bot, message) { + if (message.length === 10) { + bot.reply(message,'First ten Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34.'); + } else { + var number = parseInt(message[10:]); + var fib01 = 0; + var fib02 = 2; + do { + var save_fib01 = fib01; + fib01 = fib02; + fib02 = save_fib01+fib02; + } + while (fib02 <= number); + + if (fib02 === number) { + bot.reply (message, 'This message is a Fibonacci number'); + } + else { + bot.reply (message, 'This is not a Fibonacci number'); + } + } +}); + +controller.hears(['fibonacci '],'direct_message,direct_mention,mention',function(bot, message) { + + bot.reply(message,'First ten Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34.'); + +}); + + + + controller.hears(['what is my name','who am i'],'direct_message,direct_mention,mention',function(bot, message) { controller.storage.users.get(message.user,function(err, user) { From 96d1095fe222de74a4fd7305a2aa1ac346f55a52 Mon Sep 17 00:00:00 2001 From: Polina Timofeeva Date: Mon, 22 Feb 2016 15:04:31 +0200 Subject: [PATCH 07/34] Testing if a number is a Fibonacci number --- bot.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot.js b/bot.js index af7b71a27..8dba4208a 100755 --- a/bot.js +++ b/bot.js @@ -125,7 +125,7 @@ controller.hears(['fibonacci'],'direct_message,direct_mention,mention',function( if (message.length === 10) { bot.reply(message,'First ten Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34.'); } else { - var number = parseInt(message[10:]); + var number = parseInt(message.slice(10:)); var fib01 = 0; var fib02 = 2; do { From 3aa4ca0138b2698e7a0a948a874af22d5763053a Mon Sep 17 00:00:00 2001 From: Polina Timofeeva Date: Mon, 22 Feb 2016 15:05:55 +0200 Subject: [PATCH 08/34] Testing if a number is a Fibonacci number --- bot.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot.js b/bot.js index 8dba4208a..19d9058fc 100755 --- a/bot.js +++ b/bot.js @@ -125,7 +125,7 @@ controller.hears(['fibonacci'],'direct_message,direct_mention,mention',function( if (message.length === 10) { bot.reply(message,'First ten Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34.'); } else { - var number = parseInt(message.slice(10:)); + var number = parseInt(message.slice(10)); var fib01 = 0; var fib02 = 2; do { From a9132679609c97e2df2a54a6e2f29da5c0a8e53a Mon Sep 17 00:00:00 2001 From: Polina Timofeeva Date: Mon, 22 Feb 2016 15:08:53 +0200 Subject: [PATCH 09/34] Testing if a number is a Fibonacci number --- bot.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bot.js b/bot.js index 19d9058fc..03e571185 100755 --- a/bot.js +++ b/bot.js @@ -122,9 +122,9 @@ controller.hears(['call me (.*)'],'direct_message,direct_mention,mention',functi }); controller.hears(['fibonacci'],'direct_message,direct_mention,mention',function(bot, message) { - if (message.length === 10) { + /*if (message.length === 10) {*/ bot.reply(message,'First ten Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34.'); - } else { + /*} else { var number = parseInt(message.slice(10)); var fib01 = 0; var fib02 = 2; @@ -141,7 +141,7 @@ controller.hears(['fibonacci'],'direct_message,direct_mention,mention',function( else { bot.reply (message, 'This is not a Fibonacci number'); } - } + }*/ }); controller.hears(['fibonacci '],'direct_message,direct_mention,mention',function(bot, message) { From 7b2667eab2913975fb694c6e176bfbc51159c408 Mon Sep 17 00:00:00 2001 From: Polina Timofeeva Date: Mon, 22 Feb 2016 15:11:05 +0200 Subject: [PATCH 10/34] Testing if a number is a Fibonacci number --- bot.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/bot.js b/bot.js index 03e571185..226c9c7a8 100755 --- a/bot.js +++ b/bot.js @@ -122,10 +122,11 @@ controller.hears(['call me (.*)'],'direct_message,direct_mention,mention',functi }); controller.hears(['fibonacci'],'direct_message,direct_mention,mention',function(bot, message) { - /*if (message.length === 10) {*/ + if (message.length === 10) { bot.reply(message,'First ten Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34.'); - /*} else { - var number = parseInt(message.slice(10)); + } else { + bot.reply(message, 'There is a mistake.'); + /*var number = parseInt(message.slice(10)); var fib01 = 0; var fib02 = 2; do { @@ -140,8 +141,8 @@ controller.hears(['fibonacci'],'direct_message,direct_mention,mention',function( } else { bot.reply (message, 'This is not a Fibonacci number'); - } - }*/ + }*/ + } }); controller.hears(['fibonacci '],'direct_message,direct_mention,mention',function(bot, message) { From 4d362a9c0bf7bd8b3656adde4e0a338e28b60d10 Mon Sep 17 00:00:00 2001 From: Polina Timofeeva Date: Mon, 22 Feb 2016 15:13:32 +0200 Subject: [PATCH 11/34] Testing if a number is a Fibonacci number --- bot.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bot.js b/bot.js index 226c9c7a8..9b3cd61cf 100755 --- a/bot.js +++ b/bot.js @@ -122,12 +122,12 @@ controller.hears(['call me (.*)'],'direct_message,direct_mention,mention',functi }); controller.hears(['fibonacci'],'direct_message,direct_mention,mention',function(bot, message) { - if (message.length === 10) { + if (message.length === 9) { bot.reply(message,'First ten Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34.'); } else { - bot.reply(message, 'There is a mistake.'); - /*var number = parseInt(message.slice(10)); - var fib01 = 0; + var number = parseInt(message.slice(10)); + bot.reply(message, number); + /*var fib01 = 0; var fib02 = 2; do { var save_fib01 = fib01; From a9b936729c4a703abad1b4605b492eca130dac06 Mon Sep 17 00:00:00 2001 From: Polina Timofeeva Date: Mon, 22 Feb 2016 15:18:21 +0200 Subject: [PATCH 12/34] Testing if a number is a Fibonacci number --- bot.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot.js b/bot.js index 9b3cd61cf..eaf737338 100755 --- a/bot.js +++ b/bot.js @@ -121,11 +121,11 @@ controller.hears(['call me (.*)'],'direct_message,direct_mention,mention',functi }); }); -controller.hears(['fibonacci'],'direct_message,direct_mention,mention',function(bot, message) { +controller.hears(['fibonacci (.*)'],'direct_message,direct_mention,mention',function(bot, message) { if (message.length === 9) { bot.reply(message,'First ten Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34.'); } else { - var number = parseInt(message.slice(10)); + var number = message.match[1]; bot.reply(message, number); /*var fib01 = 0; var fib02 = 2; From 366826b0abe45856bcb0f777454f8c62178c4b2c Mon Sep 17 00:00:00 2001 From: Polina Timofeeva Date: Mon, 22 Feb 2016 15:22:10 +0200 Subject: [PATCH 13/34] Testing if a number is a Fibonacci number --- bot.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/bot.js b/bot.js index eaf737338..e48a99ba7 100755 --- a/bot.js +++ b/bot.js @@ -121,12 +121,17 @@ controller.hears(['call me (.*)'],'direct_message,direct_mention,mention',functi }); }); -controller.hears(['fibonacci (.*)'],'direct_message,direct_mention,mention',function(bot, message) { +controller.hears(['fibonacci'],'direct_message,direct_mention,mention',function(bot, message) { if (message.length === 9) { - bot.reply(message,'First ten Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34.'); - } else { - var number = message.match[1]; + bot.reply(message,'First ten Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34.'); + } +}); + +controller.hears(['fibonacci (.*)'],'direct_message,direct_mention,mention',function(bot, message) { + + var number = message.match[1]; bot.reply(message, number); + bot.reply(message,'First ten Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34.'); /*var fib01 = 0; var fib02 = 2; do { From 8e4f15548ea4a38857384982d04091f620c83a60 Mon Sep 17 00:00:00 2001 From: Polina Timofeeva Date: Mon, 22 Feb 2016 15:23:05 +0200 Subject: [PATCH 14/34] Testing if a number is a Fibonacci number --- bot.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/bot.js b/bot.js index e48a99ba7..b64113652 100755 --- a/bot.js +++ b/bot.js @@ -131,7 +131,6 @@ controller.hears(['fibonacci (.*)'],'direct_message,direct_mention,mention',func var number = message.match[1]; bot.reply(message, number); - bot.reply(message,'First ten Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34.'); /*var fib01 = 0; var fib02 = 2; do { @@ -147,7 +146,6 @@ controller.hears(['fibonacci (.*)'],'direct_message,direct_mention,mention',func else { bot.reply (message, 'This is not a Fibonacci number'); }*/ - } }); controller.hears(['fibonacci '],'direct_message,direct_mention,mention',function(bot, message) { From 41527e7d13872f3c62a036ec410d748ff3ee5e15 Mon Sep 17 00:00:00 2001 From: Polina Timofeeva Date: Mon, 22 Feb 2016 15:24:10 +0200 Subject: [PATCH 15/34] Testing if a number is a Fibonacci number --- bot.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/bot.js b/bot.js index b64113652..40c60eb54 100755 --- a/bot.js +++ b/bot.js @@ -122,9 +122,7 @@ controller.hears(['call me (.*)'],'direct_message,direct_mention,mention',functi }); controller.hears(['fibonacci'],'direct_message,direct_mention,mention',function(bot, message) { - if (message.length === 9) { bot.reply(message,'First ten Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34.'); - } }); controller.hears(['fibonacci (.*)'],'direct_message,direct_mention,mention',function(bot, message) { From 587c5d579fb66a9f4729d5ce4edb93b8384c1c69 Mon Sep 17 00:00:00 2001 From: Polina Timofeeva Date: Mon, 22 Feb 2016 15:26:14 +0200 Subject: [PATCH 16/34] Testing if a number is a Fibonacci number --- bot.js | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/bot.js b/bot.js index 40c60eb54..f33a4db60 100755 --- a/bot.js +++ b/bot.js @@ -128,8 +128,7 @@ controller.hears(['fibonacci'],'direct_message,direct_mention,mention',function( controller.hears(['fibonacci (.*)'],'direct_message,direct_mention,mention',function(bot, message) { var number = message.match[1]; - bot.reply(message, number); - /*var fib01 = 0; + var fib01 = 0; var fib02 = 2; do { var save_fib01 = fib01; @@ -143,18 +142,9 @@ controller.hears(['fibonacci (.*)'],'direct_message,direct_mention,mention',func } else { bot.reply (message, 'This is not a Fibonacci number'); - }*/ -}); - -controller.hears(['fibonacci '],'direct_message,direct_mention,mention',function(bot, message) { - - bot.reply(message,'First ten Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34.'); - + } }); - - - controller.hears(['what is my name','who am i'],'direct_message,direct_mention,mention',function(bot, message) { controller.storage.users.get(message.user,function(err, user) { From 2473ede73027f4f48b0c1e1b0e5de01cf32db1da Mon Sep 17 00:00:00 2001 From: Polina Timofeeva Date: Mon, 22 Feb 2016 15:27:30 +0200 Subject: [PATCH 17/34] Testing if a number is a Fibonacci number --- bot.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot.js b/bot.js index f33a4db60..afd7fea83 100755 --- a/bot.js +++ b/bot.js @@ -129,7 +129,7 @@ controller.hears(['fibonacci (.*)'],'direct_message,direct_mention,mention',func var number = message.match[1]; var fib01 = 0; - var fib02 = 2; + var fib02 = 1; do { var save_fib01 = fib01; fib01 = fib02; From 67e946d7a0877799cccc4588ca06e868d458aad5 Mon Sep 17 00:00:00 2001 From: Polina Timofeeva Date: Mon, 22 Feb 2016 15:30:27 +0200 Subject: [PATCH 18/34] Testing if a number is a Fibonacci number --- bot.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bot.js b/bot.js index afd7fea83..153255fa8 100755 --- a/bot.js +++ b/bot.js @@ -122,7 +122,9 @@ controller.hears(['call me (.*)'],'direct_message,direct_mention,mention',functi }); controller.hears(['fibonacci'],'direct_message,direct_mention,mention',function(bot, message) { + if (message.length === 9) { bot.reply(message,'First ten Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34.'); + } }); controller.hears(['fibonacci (.*)'],'direct_message,direct_mention,mention',function(bot, message) { @@ -138,7 +140,7 @@ controller.hears(['fibonacci (.*)'],'direct_message,direct_mention,mention',func while (fib02 <= number); if (fib02 === number) { - bot.reply (message, 'This message is a Fibonacci number'); + bot.reply (message, 'This number is a Fibonacci number'); } else { bot.reply (message, 'This is not a Fibonacci number'); From 8241e4817c41aae772f41050a2ec9bb1ae2ca6e6 Mon Sep 17 00:00:00 2001 From: Polina Timofeeva Date: Mon, 22 Feb 2016 15:41:42 +0200 Subject: [PATCH 19/34] Testing if a number is a Fibonacci number --- bot.js | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/bot.js b/bot.js index 153255fa8..741b8d6ad 100755 --- a/bot.js +++ b/bot.js @@ -122,7 +122,7 @@ controller.hears(['call me (.*)'],'direct_message,direct_mention,mention',functi }); controller.hears(['fibonacci'],'direct_message,direct_mention,mention',function(bot, message) { - if (message.length === 9) { + if (message.text.length === 9) { bot.reply(message,'First ten Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34.'); } }); @@ -130,21 +130,12 @@ controller.hears(['fibonacci'],'direct_message,direct_mention,mention',function( controller.hears(['fibonacci (.*)'],'direct_message,direct_mention,mention',function(bot, message) { var number = message.match[1]; - var fib01 = 0; - var fib02 = 1; - do { - var save_fib01 = fib01; - fib01 = fib02; - fib02 = save_fib01+fib02; - } - while (fib02 <= number); + if (isFib(number)) { + bot.reply(message, 'Yes!'); + } else { + bot.reply(message, 'No!'); + } - if (fib02 === number) { - bot.reply (message, 'This number is a Fibonacci number'); - } - else { - bot.reply (message, 'This is not a Fibonacci number'); - } }); controller.hears(['what is my name','who am i'],'direct_message,direct_mention,mention',function(bot, message) { @@ -199,6 +190,17 @@ controller.hears(['uptime','identify yourself','who are you','what is your name' }); +function isFib(val){ + var prev = 0; + var curr = 1; + while(prev<=val){ + if(prev == val){ + return True; + } + curr = prev + curr; + prev = curr - prev; + } + function formatUptime(uptime) { var unit = 'second'; if (uptime > 60) { From 7dc864170e0d2fc18e839ec2a45f78e2b19e40e7 Mon Sep 17 00:00:00 2001 From: Polina Timofeeva Date: Mon, 22 Feb 2016 15:46:20 +0200 Subject: [PATCH 20/34] Checking if a number is a Fibonacci number --- bot.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/bot.js b/bot.js index 741b8d6ad..2bdda3dc5 100755 --- a/bot.js +++ b/bot.js @@ -130,7 +130,7 @@ controller.hears(['fibonacci'],'direct_message,direct_mention,mention',function( controller.hears(['fibonacci (.*)'],'direct_message,direct_mention,mention',function(bot, message) { var number = message.match[1]; - if (isFib(number)) { + if (isFib(number)===1) { bot.reply(message, 'Yes!'); } else { bot.reply(message, 'No!'); @@ -195,11 +195,12 @@ function isFib(val){ var curr = 1; while(prev<=val){ if(prev == val){ - return True; + return 1; } curr = prev + curr; prev = curr - prev; - } + } +} function formatUptime(uptime) { var unit = 'second'; From c70921da7145ffcf5710d4258c2dd2e8864f4c8f Mon Sep 17 00:00:00 2001 From: segeila Date: Tue, 23 Feb 2016 12:21:06 +0200 Subject: [PATCH 21/34] Fix prime and fibonacci --- bot.js | 297 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 149 insertions(+), 148 deletions(-) diff --git a/bot.js b/bot.js index 4927cc45c..49756a6a3 100755 --- a/bot.js +++ b/bot.js @@ -1,24 +1,3 @@ - Skip to content -This repository - - Pull requests - Issues - Gist - - @segeila - -2 -1 - - 233 - -juhovan/botkit forked from howdyai/botkit -Code Pull requests 2 Pulse Graphs -botkit/bot.js -76e4ff5 an hour ago -@juhovan juhovan Merge branch 'master' of github:/juhovan/botkit -@benbrown @juhovan @Kasperki @MiguelSR @anonrig -executable file 254 lines (186 sloc) 6.77 KB /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ______ ______ ______ __ __ __ ______ /\ == \ /\ __ \ /\__ _\ /\ \/ / /\ \ /\__ _\ @@ -58,8 +37,8 @@ Read all about it here: -> http://howdy.ai/botkit ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ if (!process.env.token) { -console.log('Error: Specify token in environment'); -process.exit(1); + console.log('Error: Specify token in environment'); + process.exit(1); } var MathHelper = require('./botmath.js'); var Botkit = require('./lib/Botkit.js'); @@ -71,153 +50,175 @@ var bot = controller.spawn({ token: process.env.token }).startRTM(); controller.hears(['hello','hi'],'direct_message,direct_mention,mention',function(bot, message) { -bot.api.reactions.add({ + bot.api.reactions.add({ timestamp: message.ts, channel: message.channel, name: 'robot_face', -},function(err, res) { -if (err) { -bot.botkit.log('Failed to add emoji reaction :(',err); -} -}); -controller.storage.users.get(message.user,function(err, user) { -if (user && user.name) { -bot.reply(message,'Hello ' + user.name + '!!'); -} else { -bot.reply(message,'Hello.'); -} -}); + },function(err, res) { + if (err) { + bot.botkit.log('Failed to add emoji reaction :(',err); + } + }); + controller.storage.users.get(message.user,function(err, user) { + if (user && user.name) { + bot.reply(message,'Hello ' + user.name + '!!'); + } else { + bot.reply(message,'Hello.'); + } + }); }); controller.hears(['call me (.*)'],'direct_message,direct_mention,mention',function(bot, message) { -var matches = message.text.match(/call me (.*)/i); -var name = matches[1]; -controller.storage.users.get(message.user,function(err, user) { -if (!user) { -user = { + var matches = message.text.match(/call me (.*)/i); + var name = matches[1]; + controller.storage.users.get(message.user,function(err, user) { + if (!user) { + user = { id: message.user, -}; -} -user.name = name; -controller.storage.users.save(user,function(err, id) { -bot.reply(message,'Got it. I will call you ' + user.name + ' from now on.'); -}); -}); + }; + } + user.name = name; + controller.storage.users.save(user,function(err, id) { + bot.reply(message,'Got it. I will call you ' + user.name + ' from now on.'); + }); + }); }); controller.hears(['what is my name','who am i'],'direct_message,direct_mention,mention',function(bot, message) { -controller.storage.users.get(message.user,function(err, user) { -if (user && user.name) { -bot.reply(message,'Your name is ' + user.name); -} else { -bot.reply(message,'I don\'t know yet!'); -} -}); + controller.storage.users.get(message.user,function(err, user) { + if (user && user.name) { + bot.reply(message,'Your name is ' + user.name); + } 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) { -convo.ask('Are you sure you want me to shutdown?',[ -{ + bot.startConversation(message,function(err, convo) { + convo.ask('Are you sure you want me to shutdown?',[ + { pattern: bot.utterances.yes, callback: function(response, convo) { -convo.say('Bye!'); -convo.next(); -setTimeout(function() { -process.exit(); -},3000); -} -}, -{ + convo.say('Bye!'); + convo.next(); + setTimeout(function() { + process.exit(); + },3000); + } + }, + { pattern: bot.utterances.no, -default: true, + default: true, callback: function(response, convo) { -convo.say('*Phew!*'); -convo.next(); -} -} -]); -}); + convo.say('*Phew!*'); + convo.next(); + } + } + ]); + }); }); controller.hears(['uptime','identify yourself','who are you','what is your name'],'direct_message,direct_mention,mention',function(bot, message) { -var hostname = os.hostname(); -var uptime = formatUptime(process.uptime()); -bot.reply(message,':robot_face: I am a bot named <@' + bot.identity.name + '>. I have been running for ' + uptime + ' on ' + hostname + '.'); + var hostname = os.hostname(); + var uptime = formatUptime(process.uptime()); + bot.reply(message,':robot_face: I am a bot named <@' + bot.identity.name + '>. I have been running for ' + uptime + ' on ' + hostname + '.'); }); 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(', ')); -} + if (message.text === 'fibonacci') { + bot.reply(message, '1, 1, 2, 3, 5'); + } }); -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; + +/*controller.hears(['fibonacci ([0-9]+)'], 'direct_message,direct_mention,mention', function(bot, message) { + var parameter = parseInt(message.match[1]); + var fibonacci = calculateFibonacciDownto(parameter); + if (fibonacci[fibonacci.length - 5] !== parameter) { + bot.reply(message, 'That is not a Fibonacci number!'); + } + else { + bot.reply(message, fibonacci.slice(fibonacci.length-5,fibonacci.length).join(', ')); + } +}); +function calculateFibonacciDownto(goal) { + var fibonacci = [1,1]; + while (fibonacci[fibonacci.length-1] < 15) { + fibonacci.push(fibonacci[fibonacci.length-2] + fibonacci[fibonacci.length-1]); + } + return fibonacci; +}*/ + +controller.hears(['fibonacci (.*)'],'direct_message,mention',function(bot,message) { + var num = message.match[1]; + if (isFibonacci(num)==1) { + var i = 0; + var s = ""; + for (i=0; i<5; i++){ + num=findnextFibonacci(num); + s = s+ ', ' + num; + } + bot.reply(message,'your number is a fibonacci number, the following 5 fibonacci numbers are' + s); + } else { + bot.reply(message, 'your number is not a fibonacci number'); + } + + + +}); +function isFibonacci(number) { + var prev = 0; + var curr = 1; + while(prev<=number){ + if(prev == number){ + return 1; + } + curr = prev + curr; + prev = curr - prev; + } + +}function findnextFibonacci(lowerLimit) { + + for (lowerLimit++; !isFibonacci(lowerLimit);lowerLimit++) + ; + return lowerLimit; } + function formatUptime(uptime) { -var unit = 'second'; -if (uptime > 60) { -uptime = uptime / 60; -unit = 'minute'; -} -if (uptime > 60) { -uptime = uptime / 60; -unit = 'hour'; -} -if (uptime != 1) { -unit = unit + 's'; -} -uptime = uptime + ' ' + unit; -return uptime; + var unit = 'second'; + if (uptime > 60) { + uptime = uptime / 60; + unit = 'minute'; + } + if (uptime > 60) { + uptime = uptime / 60; + unit = 'hour'; + } + if (uptime != 1) { + unit = unit + 's'; + } + uptime = uptime + ' ' + unit; + 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'); -} + if (message.text === "prime") { + return 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 parameter = parseInt(message.match[1]); -if (MathHelper.isPrime(parameter)) { -var primes = new Array(); -var number = parameter + 1; -while (primes.length < 10) { -if (MathHelper.isPrime(number)) { -primes.push(number); -} -number++; -} -var reply = ""; -for (var i = 0; i < primes.length; i++) { -reply += primes[i] + " "; -} -return bot.reply(message, reply); -} -else { -return bot.reply(message, "your parameter: " + parameter + " is not Prime number"); -} -}); - - Status - API - Training - Shop - Blog - About - Pricing - - © 2016 GitHub, Inc. - Terms - Privacy - Security - Contact - Help +controller.hears('prime (.*)',['direct_message', 'direct_mention', 'mention'],function(bot,message) { + var parameter = parseInt(message.match[1]); + if (MathHelper.isPrime(parameter)) { + var primes = new Array(); + var number = parameter - 1; + while (primes.length < 10) { + if (MathHelper.isPrime(number)) { + primes.push(number); + } + number--; + } + var reply = ""; + for (var i = 0; i < primes.length; i++) { + reply += primes[i] + " "; + } + return bot.reply(message, reply); + } + else { + return bot.reply(message, "your parameter: " + parameter + " is not Prime number"); + } +}); \ No newline at end of file From 180848bf867369fedd0bcc55cf561f101789773d Mon Sep 17 00:00:00 2001 From: segeila Date: Tue, 23 Feb 2016 12:34:20 +0200 Subject: [PATCH 22/34] Fix prime and fibonacci --- bot.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot.js b/bot.js index 49756a6a3..1913f10f9 100755 --- a/bot.js +++ b/bot.js @@ -221,4 +221,4 @@ controller.hears('prime (.*)',['direct_message', 'direct_mention', 'mention'],fu else { return bot.reply(message, "your parameter: " + parameter + " is not Prime number"); } -}); \ No newline at end of file +}); From 28dc8510f7c3f9a36200e20a2d7745836dc27009 Mon Sep 17 00:00:00 2001 From: tundet Date: Tue, 23 Feb 2016 14:21:07 +0200 Subject: [PATCH 23/34] Prime, Fibonacci and Weather --- .gitmodules | 3 + bot.js | 394 +++++++++++++++++++++++++++------------------------- weather | 1 + 3 files changed, 209 insertions(+), 189 deletions(-) create mode 100644 .gitmodules create mode 160000 weather diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 000000000..df1465a1c --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "weather"] + path = weather + url = https://github.com/cmfatih/weather diff --git a/bot.js b/bot.js index 1913f10f9..34bd6ff6c 100755 --- a/bot.js +++ b/bot.js @@ -1,224 +1,240 @@ /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -______ ______ ______ __ __ __ ______ -/\ == \ /\ __ \ /\__ _\ /\ \/ / /\ \ /\__ _\ -\ \ __< \ \ \/\ \ \/_/\ \/ \ \ _"-. \ \ \ \/_/\ \/ -\ \_____\ \ \_____\ \ \_\ \ \_\ \_\ \ \_\ \ \_\ -\/_____/ \/_____/ \/_/ \/_/\/_/ \/_/ \/_/ -This is a sample Slack bot built with Botkit. -This bot demonstrates many of the core features of Botkit: -* Connect to Slack using the real time API -* Receive messages based on "spoken" patterns -* Reply to messages -* Use the conversation system to ask questions -* Use the built in storage system to store and retrieve information -for a user. -# RUN THE BOT: -Get a Bot token from Slack: --> http://my.slack.com/services/new/bot -Run your bot from the command line: -set token= -node bot.js -# USE THE BOT: -Find your bot inside Slack to send it a direct message. -Say: "Hello" -The bot will reply "Hello!" -Say: "who are you?" -The bot will tell you its name, where it running, and for how long. -Say: "Call me " -Tell the bot your nickname. Now you are friends. -Say: "who am I?" -The bot will tell you your nickname, if it knows one for you. -Say: "shutdown" -The bot will ask if you are sure, and then shut itself down. -Make sure to invite your bot into other channels using /invite @! -# EXTEND THE BOT: -Botkit is has many features for building cool and useful bots! -Read all about it here: --> http://howdy.ai/botkit -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ + ______ ______ ______ __ __ __ ______ + /\ == \ /\ __ \ /\__ _\ /\ \/ / /\ \ /\__ _\ + \ \ __< \ \ \/\ \ \/_/\ \/ \ \ _"-. \ \ \ \/_/\ \/ + \ \_____\ \ \_____\ \ \_\ \ \_\ \_\ \ \_\ \ \_\ + \/_____/ \/_____/ \/_/ \/_/\/_/ \/_/ \/_/ + This is a sample Slack bot built with Botkit. + This bot demonstrates many of the core features of Botkit: + * Connect to Slack using the real time API + * Receive messages based on "spoken" patterns + * Reply to messages + * Use the conversation system to ask questions + * Use the built in storage system to store and retrieve information + for a user. + # RUN THE BOT: + Get a Bot token from Slack: + -> http://my.slack.com/services/new/bot + Run your bot from the command line: + set token= + node bot.js + # USE THE BOT: + Find your bot inside Slack to send it a direct message. + Say: "Hello" + The bot will reply "Hello!" + Say: "who are you?" + The bot will tell you its name, where it running, and for how long. + Say: "Call me " + Tell the bot your nickname. Now you are friends. + Say: "who am I?" + The bot will tell you your nickname, if it knows one for you. + Say: "shutdown" + The bot will ask if you are sure, and then shut itself down. + Make sure to invite your bot into other channels using /invite @! + # EXTEND THE BOT: + Botkit is has many features for building cool and useful bots! + Read all about it here: + -> http://howdy.ai/botkit + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ if (!process.env.token) { - console.log('Error: Specify token in environment'); - process.exit(1); + console.log('Error: Specify token in environment'); + process.exit(1); } +var weather = require('./weather/lib/weather.js'); var MathHelper = require('./botmath.js'); var Botkit = require('./lib/Botkit.js'); var os = require('os'); var controller = Botkit.slackbot({ -debug: true, + debug: true, }); var bot = controller.spawn({ -token: process.env.token + token: process.env.token }).startRTM(); -controller.hears(['hello','hi'],'direct_message,direct_mention,mention',function(bot, message) { - bot.api.reactions.add({ -timestamp: message.ts, -channel: message.channel, -name: 'robot_face', - },function(err, res) { - if (err) { - bot.botkit.log('Failed to add emoji reaction :(',err); - } - }); - controller.storage.users.get(message.user,function(err, user) { - if (user && user.name) { - bot.reply(message,'Hello ' + user.name + '!!'); - } else { - bot.reply(message,'Hello.'); - } - }); +controller.hears(['hello', 'hi'], 'direct_message,direct_mention,mention', function (bot, message) { + bot.api.reactions.add({ + timestamp: message.ts, + channel: message.channel, + name: 'robot_face', + }, function (err, res) { + if (err) { + bot.botkit.log('Failed to add emoji reaction :(', err); + } + }); + controller.storage.users.get(message.user, function (err, user) { + if (user && user.name) { + bot.reply(message, 'Hello ' + user.name + '!!'); + } else { + bot.reply(message, 'Hello.'); + } + }); }); -controller.hears(['call me (.*)'],'direct_message,direct_mention,mention',function(bot, message) { - var matches = message.text.match(/call me (.*)/i); - var name = matches[1]; - controller.storage.users.get(message.user,function(err, user) { - if (!user) { - user = { -id: message.user, - }; - } - user.name = name; - controller.storage.users.save(user,function(err, id) { - bot.reply(message,'Got it. I will call you ' + user.name + ' from now on.'); - }); - }); +controller.hears(['call me (.*)'], 'direct_message,direct_mention,mention', function (bot, message) { + var matches = message.text.match(/call me (.*)/i); + var name = matches[1]; + controller.storage.users.get(message.user, function (err, user) { + if (!user) { + user = { + id: message.user, + }; + } + user.name = name; + controller.storage.users.save(user, function (err, id) { + bot.reply(message, 'Got it. I will call you ' + user.name + ' from now on.'); + }); + }); }); -controller.hears(['what is my name','who am i'],'direct_message,direct_mention,mention',function(bot, message) { - controller.storage.users.get(message.user,function(err, user) { - if (user && user.name) { - bot.reply(message,'Your name is ' + user.name); - } else { - bot.reply(message,'I don\'t know yet!'); - } - }); +controller.hears(['what is my name', 'who am i'], 'direct_message,direct_mention,mention', function (bot, message) { + controller.storage.users.get(message.user, function (err, user) { + if (user && user.name) { + bot.reply(message, 'Your name is ' + user.name); + } 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) { - convo.ask('Are you sure you want me to shutdown?',[ - { -pattern: bot.utterances.yes, -callback: function(response, convo) { - convo.say('Bye!'); - convo.next(); - setTimeout(function() { - process.exit(); - },3000); - } - }, - { -pattern: bot.utterances.no, - default: true, -callback: function(response, convo) { - convo.say('*Phew!*'); - convo.next(); - } - } - ]); - }); +controller.hears(['shutdown'], 'direct_message,direct_mention,mention', function (bot, message) { + bot.startConversation(message, function (err, convo) { + convo.ask('Are you sure you want me to shutdown?', [ + { + pattern: bot.utterances.yes, + callback: function (response, convo) { + convo.say('Bye!'); + convo.next(); + setTimeout(function () { + process.exit(); + }, 3000); + } + }, + { + pattern: bot.utterances.no, + default: true, + callback: function (response, convo) { + convo.say('*Phew!*'); + convo.next(); + } + } + ]); + }); }); -controller.hears(['uptime','identify yourself','who are you','what is your name'],'direct_message,direct_mention,mention',function(bot, message) { - var hostname = os.hostname(); - var uptime = formatUptime(process.uptime()); - bot.reply(message,':robot_face: I am a bot named <@' + bot.identity.name + '>. I have been running for ' + uptime + ' on ' + hostname + '.'); +controller.hears(['uptime', 'identify yourself', 'who are you', 'what is your name'], 'direct_message,direct_mention,mention', function (bot, message) { + var hostname = os.hostname(); + var uptime = formatUptime(process.uptime()); + bot.reply(message, ':robot_face: I am a bot named <@' + bot.identity.name + '>. I have been running for ' + uptime + ' on ' + hostname + '.'); }); -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) { + if (message.text === 'fibonacci') { + bot.reply(message, '1, 1, 2, 3, 5'); + } }); /*controller.hears(['fibonacci ([0-9]+)'], 'direct_message,direct_mention,mention', function(bot, message) { - var parameter = parseInt(message.match[1]); - var fibonacci = calculateFibonacciDownto(parameter); - if (fibonacci[fibonacci.length - 5] !== parameter) { - bot.reply(message, 'That is not a Fibonacci number!'); - } - else { - bot.reply(message, fibonacci.slice(fibonacci.length-5,fibonacci.length).join(', ')); - } -}); -function calculateFibonacciDownto(goal) { - var fibonacci = [1,1]; - while (fibonacci[fibonacci.length-1] < 15) { - fibonacci.push(fibonacci[fibonacci.length-2] + fibonacci[fibonacci.length-1]); - } - return fibonacci; -}*/ + var parameter = parseInt(message.match[1]); + var fibonacci = calculateFibonacciDownto(parameter); + if (fibonacci[fibonacci.length - 5] !== parameter) { + bot.reply(message, 'That is not a Fibonacci number!'); + } + else { + bot.reply(message, fibonacci.slice(fibonacci.length-5,fibonacci.length).join(', ')); + } + }); + function calculateFibonacciDownto(goal) { + var fibonacci = [1,1]; + while (fibonacci[fibonacci.length-1] < 15) { + fibonacci.push(fibonacci[fibonacci.length-2] + fibonacci[fibonacci.length-1]); + } + return fibonacci; + }*/ -controller.hears(['fibonacci (.*)'],'direct_message,mention',function(bot,message) { - var num = message.match[1]; - if (isFibonacci(num)==1) { - var i = 0; - var s = ""; - for (i=0; i<5; i++){ - num=findnextFibonacci(num); - s = s+ ', ' + num; - } - bot.reply(message,'your number is a fibonacci number, the following 5 fibonacci numbers are' + s); - } else { - bot.reply(message, 'your number is not a fibonacci number'); +controller.hears(['fibonacci (.*)'], 'direct_message,mention', function (bot, message) { + var num = message.match[1]; + if (isFibonacci(num) == 1) { + var i = 0; + var s = ""; + for (i = 0; i < 5; i++) { + num = findnextFibonacci(num); + s = s + ', ' + num; } - - - + bot.reply(message, 'your number is a fibonacci number, the following 5 fibonacci numbers are' + s); + } else { + bot.reply(message, 'your number is not a fibonacci number'); + } + + + }); function isFibonacci(number) { var prev = 0; var curr = 1; - while(prev<=number){ - if(prev == number){ + while (prev <= number) { + if (prev == number) { return 1; - } + } curr = prev + curr; prev = curr - prev; - } - -}function findnextFibonacci(lowerLimit) { - - for (lowerLimit++; !isFibonacci(lowerLimit);lowerLimit++) - ; + } + +} +function findnextFibonacci(lowerLimit) { + + for (lowerLimit++; !isFibonacci(lowerLimit); lowerLimit++) + ; return lowerLimit; } function formatUptime(uptime) { - var unit = 'second'; - if (uptime > 60) { - uptime = uptime / 60; - unit = 'minute'; - } - if (uptime > 60) { - uptime = uptime / 60; - unit = 'hour'; - } - if (uptime != 1) { - unit = unit + 's'; - } - uptime = uptime + ' ' + unit; - return uptime; + var unit = 'second'; + if (uptime > 60) { + uptime = uptime / 60; + unit = 'minute'; + } + if (uptime > 60) { + uptime = uptime / 60; + unit = 'hour'; + } + if (uptime != 1) { + unit = unit + 's'; + } + uptime = uptime + ' ' + unit; + 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'); - } +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'); + } }); -controller.hears('prime (.*)',['direct_message', 'direct_mention', 'mention'],function(bot,message) { - var parameter = parseInt(message.match[1]); - if (MathHelper.isPrime(parameter)) { - var primes = new Array(); - var number = parameter - 1; - while (primes.length < 10) { - if (MathHelper.isPrime(number)) { - primes.push(number); - } - number--; - } - var reply = ""; - for (var i = 0; i < primes.length; i++) { - reply += primes[i] + " "; - } - return bot.reply(message, reply); - } - else { - return bot.reply(message, "your parameter: " + parameter + " is not Prime number"); - } +controller.hears('prime (.*)', ['direct_message', 'direct_mention', 'mention'], function (bot, message) { + var parameter = parseInt(message.match[1]); + if (MathHelper.isPrime(parameter)) { + var primes = new Array(); + var number = parameter - 1; + while (primes.length < 10) { + if (MathHelper.isPrime(number)) { + primes.push(number); + } + number--; + } + var reply = ""; + for (var i = 0; i < primes.length; i++) { + reply += primes[i] + " "; + } + return bot.reply(message, reply); + } else { + return bot.reply(message, "your parameter: " + parameter + " is not Prime number"); + } }); + +controller.hears(['How is the weather in (.*)'], 'direct_message,mention', function (bot, message) { + var city = message.match[1]; + weather.find({search: city, degreeType: 'C'}, function (err, result) { + return bot.reply(message, 'The weather in ' + result[0]["location"]["name"] + " is " + result[0]["current"]["temperature"] + "C and it is " + result[0]["current"]["skytext"]); + + + if (err) + console.log(err); + + console.log(JSON.stringify(result, null, 2)); + + + }); +}); \ No newline at end of file diff --git a/weather b/weather new file mode 160000 index 000000000..cc33373de --- /dev/null +++ b/weather @@ -0,0 +1 @@ +Subproject commit cc33373de4fc850b050efe844c6e5d6089aa44c0 From 69b8441af74308dbf6a69cf83eceecdf7d61b8ef Mon Sep 17 00:00:00 2001 From: MariSyed Date: Wed, 24 Feb 2016 11:57:44 +0200 Subject: [PATCH 24/34] Adding Tuesdays tests --- botmath.js | 26 +++++++++++++++++++++++--- tests/botmathTest.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/botmath.js b/botmath.js index b0400fd8f..1f21af116 100644 --- a/botmath.js +++ b/botmath.js @@ -1,4 +1,3 @@ -<<<<<<< HEAD var isPrime = function (n) { if (isNaN(n) || !isFinite(n) || n%1 || n<2) return false; if (n%2==0) return (n==2); @@ -11,10 +10,31 @@ var isPrime = function (n) { module.exports.isPrime = isPrime; -======= + var sum = function (num1, num2) { return parseFloat(num1) + parseFloat(num2); } module.exports.sum = sum; ->>>>>>> origin/test + +var isFibonacci = function (number) { + var prev = 0; + var curr = 1; + while (prev <= number) { + if (prev == number) { + return 1; + } + curr = prev + curr; + prev = curr - prev; + }} + +module.exports.isFibonacci = isFibonacci; + +var findnextFibonacci = function (lowerLimit) { + for (lowerLimit++; !isFibonacci(lowerLimit); lowerLimit++){ + return lowerLimit; + } +} + +module.exports.findnextFibonacci = findnextFibonacci; + diff --git a/tests/botmathTest.js b/tests/botmathTest.js index 908258c99..0249c5598 100644 --- a/tests/botmathTest.js +++ b/tests/botmathTest.js @@ -13,4 +13,35 @@ describe('botmath', function() { assert.ok(isNaN(bothmath.sum(1335, 'a'))); }); }); + + +}); + +describe('botmath', function() { + describe('isPrime', function() { + it('should return true if the number is Prime', function() { + assert.equal(false, bothmath.isPrime(-2)); + assert.equal(true, bothmath.isPrime(5)); + assert.equal(false, bothmath.isPrime(6)); + }); + + it('should return NaN if number is not numeric', function () { + assert.ok(!isNaN(bothmath.isPrime('a'))); + assert.ok(!isNaN(bothmath.isPrime(14))); + }); + }); +}); + +describe('botmath', function() { + describe('isFibonacci', function () { + it('should return 1 if number is fibonacci', function () { + assert.equal(1, bothmath.isFibonacci(144)); + assert.equal(1, bothmath.isFibonacci(13)); + }) + it('should return NaN if value is string', function () { + assert.ok(isNaN(bothmath.isFibonacci('a'))); + assert.ok(isNaN(bothmath.isFibonacci(14))); + assert.ok(isNaN(bothmath.isFibonacci(-1))); + }); + }); }); From 3f79b876dffff0bb273821454b62dcf4e2aecec2 Mon Sep 17 00:00:00 2001 From: segeila Date: Wed, 24 Feb 2016 11:58:08 +0200 Subject: [PATCH 25/34] Add tests for isFibonacci and isPrime --- botmath.js | 34 +++++++++++++++++++-- foo/calculateFibonacciUptoTest.js | 15 ++++++++++ foo/isPrimeTest.js | 16 ++++++++++ {tests => foo}/test.js | 0 tests/botmathTest.js | 49 ++++++++++++++++++++++++------- 5 files changed, 101 insertions(+), 13 deletions(-) create mode 100644 foo/calculateFibonacciUptoTest.js create mode 100644 foo/isPrimeTest.js rename {tests => foo}/test.js (100%) diff --git a/botmath.js b/botmath.js index b0400fd8f..88551bbf5 100644 --- a/botmath.js +++ b/botmath.js @@ -1,4 +1,4 @@ -<<<<<<< HEAD + var isPrime = function (n) { if (isNaN(n) || !isFinite(n) || n%1 || n<2) return false; if (n%2==0) return (n==2); @@ -11,10 +11,38 @@ var isPrime = function (n) { module.exports.isPrime = isPrime; -======= var sum = function (num1, num2) { return parseFloat(num1) + parseFloat(num2); } module.exports.sum = sum; ->>>>>>> origin/test + +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; +} + +module.exports.calculateFibonacciUpto = calculateFibonacciUpto; + +var isFibonacci = function (number) { + var prev = 0; + var curr = 1; + while (prev <= number) { + if (prev == number) { + return 1; + } + curr = prev + curr; + prev = curr - prev; + }} + +module.exports.isFibonacci = isFibonacci; + +var findnextFibonacci = function (lowerLimit) { + for (lowerLimit++; !isFibonacci(lowerLimit); lowerLimit++); + return lowerLimit; +} + +module.exports.findnextFibonacci = findnextFibonacci; \ No newline at end of file diff --git a/foo/calculateFibonacciUptoTest.js b/foo/calculateFibonacciUptoTest.js new file mode 100644 index 000000000..c9e01713c --- /dev/null +++ b/foo/calculateFibonacciUptoTest.js @@ -0,0 +1,15 @@ +var assert = require('assert'); +var bothmath = require('../botmath.js'); + +describe('botmath', function() { + describe('calculateFibonacciUpto', function () { + it('should find all Fibonacci numbers up to a given number', function () { + assert.equal([1,1], bothmath.calculateFibonacciUpto(0)); + assert.equal([1,1], bothmath.calculateFibonacciUpto(-1)); + assert.equal([0, 1, 1, 2, 3, 5, 8, 13, 21, 34], bothmath.calculateFibonacciUpto(34)); + }) + it('should return NaN if the value is not numeric', function () { + assert.ok(isNaN(bothmath.isPrime('fibonacci'))); + }); + }); +}); \ No newline at end of file diff --git a/foo/isPrimeTest.js b/foo/isPrimeTest.js new file mode 100644 index 000000000..c0cb5a4b7 --- /dev/null +++ b/foo/isPrimeTest.js @@ -0,0 +1,16 @@ +var assert = require('assert'); +var bothmath = require('../botmath.js'); + +describe('botmath', function() { + describe('isPrime', function () { + it('should check if a number is a prime number', function () { + assert.equal(true, bothmath.isPrime(1)); + assert.equal(false, bothmath.isPrime(4)); + assert.equal(false, bothmath.isPrime(-5)); + assert.equal(false, bothmath.isPrime(147876876876)); + }) + it('should return NaN if the value is not numeric', function () { + assert.ok(isNaN(bothmath.isPrime('prime'))); + }); + }); +}); \ No newline at end of file diff --git a/tests/test.js b/foo/test.js similarity index 100% rename from tests/test.js rename to foo/test.js diff --git a/tests/botmathTest.js b/tests/botmathTest.js index 908258c99..02319d9e0 100644 --- a/tests/botmathTest.js +++ b/tests/botmathTest.js @@ -2,15 +2,44 @@ var assert = require('assert'); var bothmath = require('../botmath.js'); describe('botmath', function() { - describe('sum', function () { - it('should return sum of 2 values', function () { - assert.equal(-2, bothmath.sum(-2, 0)); - assert.equal(1, bothmath.sum(-1, 2)); - assert.equal(6.5, bothmath.sum(3.5, 3)); - assert.equal(1337, bothmath.sum(1338.2, -1.2)); - }) - it('should return NaN if both values are not numeric', function () { - assert.ok(isNaN(bothmath.sum(1335, 'a'))); + describe('sum', function () { + it('should return sum of 2 values', function () { + assert.equal(-2, bothmath.sum(-2, 0)); + assert.equal(1, bothmath.sum(-1, 2)); + assert.equal(6.5, bothmath.sum(3.5, 3)); + assert.equal(1337, bothmath.sum(1338.2, -1.2)); + }) + it('should return NaN if both values are not numeric', function () { + assert.ok(isNaN(bothmath.sum(1335, 'a'))); + }); }); - }); }); + + +describe('bothmath', function() { + describe('isPrime', function () { + it('should return true if a number is a prime number', function () { + assert.equal(true, bothmath.isPrime(5)); + assert.equal(false, bothmath.isPrime(4)); + assert.equal(false, bothmath.isPrime(-5)); + assert.equal(false, bothmath.isPrime(147876876876)); + }) + it('should return NaN if the value is not numeric', function () { + assert.ok(!isNaN(bothmath.isPrime('a'))); + }); + }); +}); + +describe('botmath', function() { + describe('isFibonacci', function () { + it('should return 1 if number is fibonacci', function () { + assert.equal(1, bothmath.isFibonacci(144)); + assert.equal(1, bothmath.isFibonacci(13)); + }) + it('should return NaN if value is string', function () { + assert.ok(isNaN(bothmath.isFibonacci('a'))); + assert.ok(isNaN(bothmath.isFibonacci(14))); + assert.ok(isNaN(bothmath.isFibonacci(-1))); + }); + }); +}); \ No newline at end of file From e9c54a7ed7147cf0d57ab629fd1d56f9c7b87ba5 Mon Sep 17 00:00:00 2001 From: segeila Date: Wed, 24 Feb 2016 12:06:47 +0200 Subject: [PATCH 26/34] Add .travis.yml file --- .travis.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 000000000..c9ff5fef8 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,10 @@ +language: node_js +node_js: + - "4.1" + - "4.0" + - "0.12" + - "0.11" + - "0.10" + - "0.8" + - "0.6" + - "iojs" \ No newline at end of file From 9b1aa1d6b5f8765421c960e31e6aed489b75d429 Mon Sep 17 00:00:00 2001 From: segeila Date: Wed, 24 Feb 2016 14:23:26 +0200 Subject: [PATCH 27/34] removed old travis file --- .travis.yml | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index c9ff5fef8..000000000 --- a/.travis.yml +++ /dev/null @@ -1,10 +0,0 @@ -language: node_js -node_js: - - "4.1" - - "4.0" - - "0.12" - - "0.11" - - "0.10" - - "0.8" - - "0.6" - - "iojs" \ No newline at end of file From 8a394ef9da4111a0d0d44cb2a526c84eec46996d Mon Sep 17 00:00:00 2001 From: segeila Date: Wed, 24 Feb 2016 14:25:51 +0200 Subject: [PATCH 28/34] added .travis.yml again --- .travis.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 000000000..c9ff5fef8 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,10 @@ +language: node_js +node_js: + - "4.1" + - "4.0" + - "0.12" + - "0.11" + - "0.10" + - "0.8" + - "0.6" + - "iojs" \ No newline at end of file From ff2481c9aeb5c1cc1c517570af6965485d79314e Mon Sep 17 00:00:00 2001 From: segeila Date: Wed, 24 Feb 2016 14:31:50 +0200 Subject: [PATCH 29/34] Delete old node.js versions --- .travis.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index c9ff5fef8..6160d8270 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,6 +5,4 @@ node_js: - "0.12" - "0.11" - "0.10" - - "0.8" - - "0.6" - "iojs" \ No newline at end of file From 6f874135927c06ebc75cfdb07f26ca1a6863dd9b Mon Sep 17 00:00:00 2001 From: segeila Date: Wed, 24 Feb 2016 14:54:40 +0200 Subject: [PATCH 30/34] Add 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 6e382b4a5de9e7dffc89b836a65d03c457925fa1 Mon Sep 17 00:00:00 2001 From: segeila Date: Wed, 24 Feb 2016 15:16:00 +0200 Subject: [PATCH 31/34] Delete submodule, add weather-js --- .gitmodules | 3 --- bot.js | 27 +-------------------------- package.json | 1 + weather | 1 - 4 files changed, 2 insertions(+), 30 deletions(-) delete mode 100644 .gitmodules delete mode 160000 weather diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index df1465a1c..000000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "weather"] - path = weather - url = https://github.com/cmfatih/weather diff --git a/bot.js b/bot.js index 5a9d2eeaf..3c8d8959c 100755 --- a/bot.js +++ b/bot.js @@ -40,15 +40,12 @@ if (!process.env.token) { console.log('Error: Specify token in environment'); process.exit(1); } -var weather = require('./weather/lib/weather.js'); +var weather = require('weather-js'); var MathHelper = require('./botmath.js'); var Botkit = require('./lib/Botkit.js'); var os = require('os'); -<<<<<<< HEAD -======= var botmath = require('./botmath.js'); ->>>>>>> origin/test var controller = Botkit.slackbot({ debug: true, }); @@ -132,24 +129,6 @@ controller.hears(['fibonacci'], 'direct_message,direct_mention,mention', functio } }); -/*controller.hears(['fibonacci ([0-9]+)'], 'direct_message,direct_mention,mention', function(bot, message) { - var parameter = parseInt(message.match[1]); - var fibonacci = calculateFibonacciDownto(parameter); - if (fibonacci[fibonacci.length - 5] !== parameter) { - bot.reply(message, 'That is not a Fibonacci number!'); - } - else { - bot.reply(message, fibonacci.slice(fibonacci.length-5,fibonacci.length).join(', ')); - } - }); - function calculateFibonacciDownto(goal) { - var fibonacci = [1,1]; - while (fibonacci[fibonacci.length-1] < 15) { - fibonacci.push(fibonacci[fibonacci.length-2] + fibonacci[fibonacci.length-1]); - } - return fibonacci; - }*/ - controller.hears(['fibonacci (.*)'], 'direct_message,mention', function (bot, message) { var num = message.match[1]; if (isFibonacci(num) == 1) { @@ -202,7 +181,6 @@ function formatUptime(uptime) { uptime = uptime + ' ' + unit; return uptime; } -<<<<<<< HEAD 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'); @@ -244,7 +222,6 @@ controller.hears(['How is the weather in (.*)'], 'direct_message,mention', funct }); }); -======= controller.hears('what is (.*) \\+ (.*)',['direct_message', 'direct_mention', 'mention'],function(bot,message) { @@ -255,5 +232,3 @@ controller.hears('what is (.*) \\+ (.*)',['direct_message', 'direct_mention', 'm return bot.reply(message, num1 + ' + ' + num2 + ' = ' + botmath.sum(num1, num2)); } }); - ->>>>>>> origin/test diff --git a/package.json b/package.json index c24644ad1..0aa8a3211 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "mocha": "^2.4.5", "mustache": "^2.2.1", "request": "^2.67.0", + "weather-js": "^1.0.2", "ws": "^1.0.0" }, "devDependencies": { diff --git a/weather b/weather deleted file mode 160000 index cc33373de..000000000 --- a/weather +++ /dev/null @@ -1 +0,0 @@ -Subproject commit cc33373de4fc850b050efe844c6e5d6089aa44c0 From 68cc3cd70636c005a3b550f82ec5aaf5a6d23473 Mon Sep 17 00:00:00 2001 From: segeila Date: Fri, 26 Feb 2016 11:04:31 +0200 Subject: [PATCH 32/34] Add ISSUE_TEMPLATE.md --- ISSUE_TEMPLATE.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 ISSUE_TEMPLATE.md diff --git a/ISSUE_TEMPLATE.md b/ISSUE_TEMPLATE.md new file mode 100644 index 000000000..fe3ec6882 --- /dev/null +++ b/ISSUE_TEMPLATE.md @@ -0,0 +1,9 @@ +### What happened + +### Environment + +### Expected behaviour + +### Steps to reproduce the bag + +### Priority \ No newline at end of file From 53f857728fbc05b8033e000c1e6f1ee5b215104b Mon Sep 17 00:00:00 2001 From: segeila Date: Fri, 26 Feb 2016 11:11:34 +0200 Subject: [PATCH 33/34] Create ISSUE_TEMPLATE.txt --- ISSUE_TEMPLATE.txt | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 ISSUE_TEMPLATE.txt diff --git a/ISSUE_TEMPLATE.txt b/ISSUE_TEMPLATE.txt new file mode 100644 index 000000000..ddd75f650 --- /dev/null +++ b/ISSUE_TEMPLATE.txt @@ -0,0 +1,7 @@ +What happened? + +What OS? + +Steps to reproduce? + +Priority From e69fab905d99319e1ca16ad5151839b64a6df74f Mon Sep 17 00:00:00 2001 From: MariaSyed Date: Fri, 26 Feb 2016 11:18:39 +0200 Subject: [PATCH 34/34] . --- bot.js | 250 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 250 insertions(+) create mode 100644 bot.js diff --git a/bot.js b/bot.js new file mode 100644 index 000000000..9f52de991 --- /dev/null +++ b/bot.js @@ -0,0 +1,250 @@ +/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + ______ ______ ______ __ __ __ ______ + /\ == \ /\ __ \ /\__ _\ /\ \/ / /\ \ /\__ _\ + \ \ __< \ \ \/\ \ \/_/\ \/ \ \ _"-. \ \ \ \/_/\ \/ + \ \_____\ \ \_____\ \ \_\ \ \_\ \_\ \ \_\ \ \_\ + \/_____/ \/_____/ \/_/ \/_/\/_/ \/_/ \/_/ + This is a sample Slack bot built with Botkit. + This bot demonstrates many of the core features of Botkit: + * Connect to Slack using the real time API + * Receive messages based on "spoken" patterns + * Reply to messages + * Use the conversation system to ask questions + * Use the built in storage system to store and retrieve information + for a user. + # RUN THE BOT: + Get a Bot token from Slack: + -> http://my.slack.com/services/new/bot + Run your bot from the command line: + set token= + node bot.js + # USE THE BOT: + Find your bot inside Slack to send it a direct message. + Say: "Hello" + The bot will reply "Hello!" + Say: "who are you?" + The bot will tell you its name, where it running, and for how long. + Say: "Call me " + Tell the bot your nickname. Now you are friends. + Say: "who am I?" + The bot will tell you your nickname, if it knows one for you. + Say: "shutdown" + The bot will ask if you are sure, and then shut itself down. + Make sure to invite your bot into other channels using /invite @! + # EXTEND THE BOT: + Botkit is has many features for building cool and useful bots! + Read all about it here: + -> http://howdy.ai/botkit + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ +if (!process.env.token) { + console.log('Error: Specify token in environment'); + process.exit(1); +} +var weather = require('weather-js'); +var MathHelper = require('./botmath.js'); +var Botkit = require('./lib/Botkit.js'); +var os = require('os'); +<<<<<<< HEAD + +======= +>>>>>>> 53f857728fbc05b8033e000c1e6f1ee5b215104b +var botmath = require('./botmath.js'); + +var controller = Botkit.slackbot({ + debug: true, +}); +var bot = controller.spawn({ + token: process.env.token +}).startRTM(); +controller.hears(['hello', 'hi'], 'direct_message,direct_mention,mention', function (bot, message) { + bot.api.reactions.add({ + timestamp: message.ts, + channel: message.channel, + name: 'robot_face', + }, function (err, res) { + if (err) { + bot.botkit.log('Failed to add emoji reaction :(', err); + } + }); + controller.storage.users.get(message.user, function (err, user) { + if (user && user.name) { + bot.reply(message, 'Hello ' + user.name + '!!'); + } else { + bot.reply(message, 'Hello.'); + } + }); +}); +controller.hears(['call me (.*)'], 'direct_message,direct_mention,mention', function (bot, message) { + var matches = message.text.match(/call me (.*)/i); + var name = matches[1]; + controller.storage.users.get(message.user, function (err, user) { + if (!user) { + user = { + id: message.user, + }; + } + user.name = name; + controller.storage.users.save(user, function (err, id) { + bot.reply(message, 'Got it. I will call you ' + user.name + ' from now on.'); + }); + }); +}); +controller.hears(['what is my name', 'who am i'], 'direct_message,direct_mention,mention', function (bot, message) { + controller.storage.users.get(message.user, function (err, user) { + if (user && user.name) { + bot.reply(message, 'Your name is ' + user.name); + } 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) { + convo.ask('Are you sure you want me to shutdown?', [ + { + pattern: bot.utterances.yes, + callback: function (response, convo) { + convo.say('Bye!'); + convo.next(); + setTimeout(function () { + process.exit(); + }, 3000); + } + }, + { + pattern: bot.utterances.no, + default: true, + callback: function (response, convo) { + convo.say('*Phew!*'); + convo.next(); + } + } + ]); + }); +}); +controller.hears(['uptime', 'identify yourself', 'who are you', 'what is your name'], 'direct_message,direct_mention,mention', function (bot, message) { + var hostname = os.hostname(); + var uptime = formatUptime(process.uptime()); + bot.reply(message, ':robot_face: I am a bot named <@' + bot.identity.name + '>. I have been running for ' + uptime + ' on ' + hostname + '.'); +}); +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,mention', function (bot, message) { + var num = message.match[1]; + if (isFibonacci(num) == 1) { + var i = 0; + var s = ""; + for (i = 0; i < 5; i++) { + num = findnextFibonacci(num); + s = s + ', ' + num; + } + bot.reply(message, 'your number is a fibonacci number, the following 5 fibonacci numbers are' + s); + } else { + bot.reply(message, 'your number is not a fibonacci number'); + } + + + +}); +function isFibonacci(number) { + var prev = 0; + var curr = 1; + while (prev <= number) { + if (prev == number) { + return 1; + } + curr = prev + curr; + prev = curr - prev; + } + +} +function findnextFibonacci(lowerLimit) { + + for (lowerLimit++; !isFibonacci(lowerLimit); lowerLimit++) + ; + return lowerLimit; +} + +function formatUptime(uptime) { + var unit = 'second'; + if (uptime > 60) { + uptime = uptime / 60; + unit = 'minute'; + } + if (uptime > 60) { + uptime = uptime / 60; + unit = 'hour'; + } + if (uptime != 1) { + unit = unit + 's'; + } + uptime = uptime + ' ' + unit; + return uptime; +} +<<<<<<< HEAD + +======= +>>>>>>> 53f857728fbc05b8033e000c1e6f1ee5b215104b +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'); + } +}); + +controller.hears('prime (.*)', ['direct_message', 'direct_mention', 'mention'], function (bot, message) { + var parameter = parseInt(message.match[1]); + if (MathHelper.isPrime(parameter)) { + var primes = new Array(); + var number = parameter - 1; + while (primes.length < 10) { + if (MathHelper.isPrime(number)) { + primes.push(number); + } + number--; + } + var reply = ""; + for (var i = 0; i < primes.length; i++) { + reply += primes[i] + " "; + } + return bot.reply(message, reply); + } else { + return bot.reply(message, "your parameter: " + parameter + " is not Prime number"); + } +}); + +controller.hears(['How is the weather in (.*)'], 'direct_message,mention', function (bot, message) { + var city = message.match[1]; + weather.find({search: city, degreeType: 'C'}, function (err, result) { + return bot.reply(message, 'The weather in ' + result[0]["location"]["name"] + " is " + result[0]["current"]["temperature"] + "C and it is " + result[0]["current"]["skytext"]); + + + if (err) + console.log(err); + + console.log(JSON.stringify(result, null, 2)); + + + }); +}); +<<<<<<< HEAD + +======= +>>>>>>> 53f857728fbc05b8033e000c1e6f1ee5b215104b + +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)); + } +}); +<<<<<<< HEAD + +======= +>>>>>>> 53f857728fbc05b8033e000c1e6f1ee5b215104b