Skip to content
This repository has been archived by the owner on Mar 26, 2022. It is now read-only.

Prime and fibonacci fixed #9

Open
wants to merge 42 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
981de22
Adding gitignore file
Feb 22, 2016
929f8ac
Answer the question who made it
Feb 22, 2016
12dbea8
Answer the question who made it
Feb 22, 2016
37798ce
Adding prime feature
Feb 22, 2016
738635f
fibonacci function added. Tested and working
jEnbuska Feb 22, 2016
9d97db1
merged with fibonacci. gitignore changed
jEnbuska Feb 22, 2016
807c24a
Prime fixed and working, but not complete
Feb 22, 2016
0505395
only some code cleanup
jEnbuska Feb 22, 2016
581fbab
Prime is done
Feb 22, 2016
3a9562a
Merge branch 'master' into prime
Feb 22, 2016
80386db
Merging with use of upstream master
Feb 23, 2016
d0716bf
merging
Feb 23, 2016
bf799c4
fibonacci repaired
jEnbuska Feb 23, 2016
b8b3135
Fixed Prime to print results in descending order
Feb 23, 2016
e6f88b9
Merge branch 'fibonacciRepair'
jEnbuska Feb 23, 2016
19e5e1f
weather feature implemented and tested
jEnbuska Feb 23, 2016
d06ad02
Merge pull request #1 from robertstankevich/weatherFeature
jEnbuska Feb 23, 2016
39dcef5
tests pulled
jEnbuska Feb 23, 2016
55ceb30
Merge branch 'master' of https://github.com/robertstankevich/botkit
jEnbuska Feb 23, 2016
5d57def
test included
jEnbuska Feb 23, 2016
a07aecd
Adding travis
Feb 24, 2016
8598d6e
Added link to readme
Feb 24, 2016
8c4bfea
Travis icon uodated with readme
Feb 24, 2016
cc3d97c
Added Procfile
Feb 24, 2016
6b1c838
Installed weather-js npm package
Feb 24, 2016
6a4c2c5
Fixed reference to weather-js
Feb 24, 2016
ada4dc3
One more try for Procfile. Now with token
Feb 24, 2016
77677a4
speedrun feature started but not ready
jEnbuska Feb 24, 2016
0f507e1
added file 123
jEnbuska Feb 25, 2016
12db0b0
speedrun done and working
jEnbuska Feb 25, 2016
0cbdc45
added welcome message
thatsafy Feb 26, 2016
e93b49d
Feature for guys who leave channels
Feb 26, 2016
5dbd7a8
Merge pull request #2 from robertstankevich/testing
thatsafy Feb 26, 2016
bed8f8e
Merge pull request #3 from robertstankevich/strangefeature
Feb 26, 2016
5f4f233
fixed (?) channel_leave
thatsafy Feb 26, 2016
55debdf
Merge pull request #4 from robertstankevich/testing
thatsafy Feb 26, 2016
9034f01
merge
jEnbuska Feb 26, 2016
3caf09d
merging
jEnbuska Feb 26, 2016
f6e1c8d
github issues feature implemented and working
jEnbuska Feb 26, 2016
f53ed67
Merge pull request #5 from robertstankevich/issue/feature
jEnbuska Feb 26, 2016
c571cce
really nervous reaction on typing is added
Feb 26, 2016
dc7db42
Merge pull request #6 from robertstankevich/strangefeature
thatsafy Feb 26, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules
/.idea
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
language: node_js
node_js:
- "4.1"
- "4.0"
- "iojs"
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
worker: token=xoxb-22469172560-FHIXCnTkvGU0fTo1m6WmUnCv node bot.js
176 changes: 167 additions & 9 deletions bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,132 @@ var bot = controller.spawn({
token: process.env.token
}).startRTM();

const weather = require('weather-js');
const request = require('request');

controller.hears(['issues (.*)'],'direct_message,direct_mention,mention',function(bot, message) {
if (message.match[1].indexOf(', ')===-1) {
bot.reply(message,'wrong format', null, 2);
}else {
var input = message.match[1].split(", ");
var options = {
url: "https://api.github.com/repos/"+input[0]+"/"+input[1]+"/issues",
timeout: 15000,
headers: {
'User-Agent': input[0]
}
};
request.get(options, function (err, res, body) {
if (err) {
console.log("ERROR: " + err);
return callback(err);
}
else if (res.statusCode !== 200) {
console.log("res.statusCode !== 200");
return callback('Request failed (' + res.statusCode + ')');
}
else {
var issues = JSON.parse(body);

var issueStr = "";
for(var i = 0; i<issues.length; i++){
var issue = issues[i];
for(var j = 2; j<input.length; j++){
if(issue.hasOwnProperty(input[j])){
issueStr+= input[j]+": " + issue[input[j]] + ", ";
}
issueStr+="\n";
}
issueStr+="\n";
}
bot.reply(message, issueStr, null, 2);
}
});
}

});

controller.hears(['speedrun (.*)'],'direct_message,direct_mention,mention',function(bot, message) {
var gameInfo;
if (message.match[1].indexOf(', ')!==-1) {
gameInfo = message.match[1].split(", ");
}else{
gameInfo=[];
gameInfo.push(message.match[1]);
}
console.log(gameInfo);
var gamename = gameInfo[0];
console.log(gamename);
var difficulty;
if(gameInfo.length>1){
difficulty=gameInfo[1];
}
gamename.replace(' ', '%20');
request.get({url: "http://www.speedrun.com/api_records.php?game="+gamename, timeout: 15000}, function(err, res, body) {
if(err){
console.log("ERROR: " + err);
return callback(err);
}
else if(res.statusCode !== 200) {
console.log("res.statusCode !== 200");
return callback('Request failed (' + res.statusCode + ')');
}
else{
body = JSON.parse(body);
console.log(body);
if(body.hasOwnProperty(gamename)){
var game = body[gamename];
if(difficulty !== undefined && game.hasOwnProperty(difficulty)){
var gameWithDif =game[difficulty];
bot.reply(message,gameWithDif['timeigt'], null, 2);
bot.reply(message,gameWithDif['video'], null, 2);
}else if(difficulty===undefined){
for(key in game){
bot.reply(message,key, null, 2);
}
}
}
}
});

});

controller.on('user_typing',function(bot, message) {
controller.storage.users.get(message.user,function(err, user) {
if (user && user.name) {
bot.reply(message, user.name + ' is typing something. Is it going to be a novel?');
} else {
bot.reply(message,'Someone is typing... STOP IT! DO NOT TYPE! YOU ARE MAKING ME NERVOUS');
}
});
});

controller.on('user_channel_join',function(bot, message) {
controller.storage.users.get(message.user,function(err, user) {
if (user && user.name) {
bot.reply(message,'Welcome to the channel ' + user.name + '!!');
} else {
bot.reply(message,'Welcome to the channel!');
}
});
});

controller.hears(['How is the weather in (.*), (.*)'],'direct_message,direct_mention,mention',function(bot, message) {

var input1 = message.match[1];
var input2 = message.match[2];
console.log(input1);
console.log(input2);
weather.find({search: input1 + " " + input2, degreeType: 'C'}, function (err, result) {
console.log(JSON.stringify(result, null, 2));
if(result !== undefined){
bot.reply(message, JSON.stringify(result[0].current.temperature, null, 2));
}else{
bot.reply(message, "You dont make any sence");
}
});
});


controller.hears(['hello','hi'],'direct_message,direct_mention,mention',function(bot, message) {

Expand All @@ -95,13 +221,14 @@ controller.hears(['hello','hi'],'direct_message,direct_mention,mention',function
bot.botkit.log('Failed to add emoji reaction :(',err);
}
});


var answers = ["Hello", "Hi", "Good day to you", "HELLO-HELLO-HELLO", "Moi", "Salute", "Greetings", "Hallo"]
var rand = Math.floor((Math.random() * answers.length));

controller.storage.users.get(message.user,function(err, user) {
if (user && user.name) {
bot.reply(message,'Hello ' + user.name + '!!');
bot.reply(message,answers[rand] + ", " + user.name + '!!');
} else {
bot.reply(message,'Hello.');
bot.reply(message,answers[rand]);
}
});
});
Expand Down Expand Up @@ -171,7 +298,7 @@ controller.hears(['uptime','identify yourself','who are you','what is your name'

controller.hears(['fibonacci'], 'direct_message,direct_mention,mention', function(bot, message) {
if (message.text === 'fibonacci') {
bot.reply(message, '1, 1, 2, 3, 5, 8, 13, 21, 34, 55');
bot.reply(message, '1, 1, 2, 3, 5');
}
});

Expand All @@ -184,20 +311,33 @@ controller.hears(['fibonacci ([0-9]+)'], 'direct_message,direct_mention,mention'
bot.reply(message, 'That is not a Fibonacci number!');
}
else {
bot.reply(message, fibonacci.slice(fibonacci.length-10,fibonacci.length).join(', '));
var a = fibonacci[fibonacci.length-1];
var b;
if(fibonacci.length>1){
b=fibonacci[fibonacci.length-2];
}else{
b=0;
}
var nextFive = [];
for(var i = 0; i<5; i++){
nextFive.push(a+b);
b = a;
a = nextFive[i];
}
bot.reply(message, nextFive.slice(0,nextFive.length).join(', '));
}
});

function calculateFibonacciUpto(goal) {
var fibonacci = [1, 1];

while (fibonacci[fibonacci.length-1] < goal) {
fibonacci.push(fibonacci[fibonacci.length-2] + fibonacci[fibonacci.length-1]);
}

return fibonacci;
}

// module.exports.calculateFibonacciUpto = calculateFibonacciUpto;

function formatUptime(uptime) {
var unit = 'second';
if (uptime > 60) {
Expand All @@ -216,6 +356,20 @@ function formatUptime(uptime) {
return uptime;
}



controller.on('channel_leave',function(bot,message) {

controller.storage.users.get(message.user, function(err, user) {
if(user && user.name) {
bot.reply(message,"Ok, " + user.name+", go find a new channel of your own with blackjack and hookers!");
}
else {
bot.reply(message,"Someone left the channel");
}
});
});

controller.hears('prime',['direct_message', 'direct_mention', 'mention'],function(bot,message) {
if (message.text === "prime") {
return bot.reply(message, '2, 3, 5, 7, 11, 13, 17, 19, 23, 29');
Expand All @@ -235,8 +389,12 @@ controller.hears('prime (.*)',['direct_message', 'direct_mention', 'mention'],fu
if (MathHelper.isPrime(number)) {
primes.push(number);
}

if(number ==0) {
break;
}

number++;
number--;
}

var reply = "";
Expand Down
16 changes: 16 additions & 0 deletions botmath.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,19 @@ var isPrime = function (n) {

module.exports.isPrime = isPrime;

var sum = function (num1, num2) {
return parseFloat(num1) + parseFloat(num2);
}

module.exports.sum = sum;

function calculateFibonacciUpto(goal) {
var fibonacci = [1, 1];
var next;
while ((next =(fibonacci[fibonacci.length-2] + fibonacci[fibonacci.length-1])) < goal) {
fibonacci.push(next);
}
return fibonacci;
}

module.exports.calculateFibonacciUpto = calculateFibonacciUpto;
14 changes: 7 additions & 7 deletions lib/CoreBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ function Botkit(configuration) {
this.capture = function(response) {
var capture_key = this.sent[this.sent.length - 1].text;

if (this.capture_options.key) {
capture_key = this.capture_options.key;
if (this.capture_options.issueKey) {
capture_key = this.capture_options.issueKey;
}

if (this.capture_options.multiple) {
Expand Down Expand Up @@ -252,13 +252,13 @@ function Botkit(configuration) {

var res = {};
for (var key in this.responses) {
res[key] = this.extractResponse(key);
res[issueKey] = this.extractResponse(issueKey);
}
return res;
};

this.extractResponse = function(key) {
return this.combineMessages(this.responses[key]);
return this.combineMessages(this.responses[issueKey]);
};

this.replaceTokens = function(text) {
Expand Down Expand Up @@ -466,10 +466,10 @@ function Botkit(configuration) {
var convo = this.convos[c];

for (var key in convo.responses) {
if (!answers[key]) {
answers[key] = {};
if (!answers[issueKey]) {
answers[issueKey] = {};
}
answers[key][convo.source_message.user] = convo.extractResponse(key);
answers[issueKey][convo.source_message.user] = convo.extractResponse(issueKey);
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/SlackBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function Slackbot(configuration) {
var message = {};

for (var key in req.body) {
message[key] = req.body[key];
message[issueKey] = req.body[issueKey];
}

// let's normalize some of these fields to match the rtm message format
Expand Down Expand Up @@ -106,7 +106,7 @@ function Slackbot(configuration) {
var message = {};

for (var key in req.body) {
message[key] = req.body[key];
message[issueKey] = req.body[issueKey];
}

// let's normalize some of these fields to match the rtm message format
Expand Down
4 changes: 2 additions & 2 deletions lib/storage/firebase_storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ module.exports = function(config) {
firebaseRef.once('value',
function(records) {
var list = [];
for (key of Object.keys(records.val())) {
list.push(records.val()[key]);
for (issueKey of Object.keys(records.val())) {
list.push(records.val()[issueKey]);
}
cb(undefined, list);
},
Expand Down
2 changes: 1 addition & 1 deletion lib/storage/simple_storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ module.exports = function(config) {
cb(err, data);
} else {
cb(err, Object.keys(data).map(function(key) {
return data[key];
return data[issueKey];
}));
}
};
Expand Down
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
"eventemitter2": "0.4.14",
"express": "^4.13.3",
"jfs": "^0.2.6",
"mocha": "^2.4.5",
"mustache": "^2.2.1",
"request": "^2.67.0",
"ws": "^1.0.0"
"weather-js": "^1.0.2",
"ws": "^1.0.0",
"xml2js": "*"
},
"devDependencies": {
"jscs": "^2.7.0",
Expand All @@ -21,6 +24,9 @@
"tape": "^4.4.0",
"winston": "^2.1.1"
},
"scripts": {
"test": "mocha tests/*.js"
},
"repository": {
"type": "git",
"url": "https://github.com/juhovan/botkit.git"
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# [Botkit](http://howdy.ai/botkit) - Best course ever!

[![Build Status](https://travis-ci.org/robertstankevich/botkit.svg?branch=master)](https://travis-ci.org/robertstankevich/botkit)

Botkit designed to ease the process of designing and running useful, creative or
just plain weird bots (and other types of applications) that live inside [Slack](http://slack.com)!

Expand Down
Loading