Skip to content
This repository was archived by the owner on Mar 26, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
Empty file added .gitmodules
Empty file.
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
language: node_js
node_js:
- "4.1"
- "4.0"
- "0.12"
- "0.11"
- "0.10"
- "iojs"
notifications:
slack: sdtwarriors:lcO6puoKPxDSLQu9MzfFbtbF
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
worker: node bot.js
87 changes: 79 additions & 8 deletions bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ This bot demonstrates many of the core features of Botkit:
Run your bot from the command line:

set token=<MY TOKEN>

node bot.js

# USE THE BOT:
Expand Down Expand Up @@ -65,7 +65,6 @@ This bot demonstrates many of the core features of Botkit:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/


if (!process.env.token) {
console.log('Error: Specify token in environment');
process.exit(1);
Expand All @@ -74,6 +73,8 @@ if (!process.env.token) {
var MathHelper = require('./botmath.js');
var Botkit = require('./lib/Botkit.js');
var os = require('os');
var botmath = require('./botmath.js');
var weather = require('weather-js');

var controller = Botkit.slackbot({
debug: true,
Expand All @@ -83,6 +84,32 @@ var bot = controller.spawn({
token: process.env.token
}).startRTM();

// Weather
controller.hears(['weather (.*)'],'direct_message,direct_mention,mention',function(bot, message) {
var location = message.text.match(/weather (.*)/i);
var array= location[1].split(" ");
if(array[0]=='in'){
location=array[1];
}
else{
location=array[0];
}
weather.find({search: location+', FI', degreeType: 'C'}, function(err, result) {
try{
var array = result;
var temp = array[0].current.temperature;
var feeltemp = array[0].current.feelslike;
var place = array[0].current.observationpoint;
var skytext = array[0].current.skytext;
bot.reply(message, "Right now in "+place+" the sky is "+skytext+". It's "+temp+'C but it feels like '+feeltemp+'C to be honest.');
}
catch(err){
bot.reply(message,'I don\'t understand.');
console.log(err);
}
});
});


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

Expand Down Expand Up @@ -122,6 +149,34 @@ controller.hears(['call me (.*)'],'direct_message,direct_mention,mention',functi
});
});

controller.on('user_channel_join',function(bot,message) {
var rnd = (process.uptime()*1000)%4;
switch(rnd){
case 0:
bot.reply(message,'Starved of living');
bot.reply(message,'a life beleaguered');
bot.reply(message,'some welcome death');
break;
case 1:
bot.reply(message,'wedding cake');
bot.reply(message,'filled with fruits and nuts');
bot.reply(message,'welcome family');
break;
case 2:
bot.reply(message,'Is your song of spring');
bot.reply(message,'a welcoming?');
bot.reply(message,'for you are welcome, so sing.');
break;
case 3:
bot.reply(message,'Submarines sinking ships');
bot.reply(message,'and Red Baron in the skies');
bot.reply(message,'welcome to the war');
break;
default:
bot.reply(message,'welcome to the channel.');
}
});

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) {
Expand Down Expand Up @@ -169,6 +224,8 @@ controller.hears(['uptime','identify yourself','who are you','what is your name'

});

// Fibonacci

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');
Expand All @@ -177,9 +234,9 @@ 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 = calculateFibonacciUpto(parameter);

if (fibonacci[fibonacci.length-1] !== parameter) {
bot.reply(message, 'That is not a Fibonacci number!');
}
Expand All @@ -190,11 +247,11 @@ controller.hears(['fibonacci ([0-9]+)'], 'direct_message,direct_mention,mention'

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;
}

Expand All @@ -216,6 +273,18 @@ function formatUptime(uptime) {
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));
}
});

// Prime

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 @@ -231,12 +300,15 @@ controller.hears('prime (.*)',['direct_message', 'direct_mention', 'mention'],fu
var number = parameter + 1;

while (primes.length < 10) {
if(number < 2)
break;

if (MathHelper.isPrime(number)) {
primes.push(number);
}

number++;
number--;
console.log(number);
}

var reply = "";
Expand All @@ -250,4 +322,3 @@ controller.hears('prime (.*)',['direct_message', 'direct_mention', 'mention'],fu
return bot.reply(message, "your parameter: " + parameter + " is not Prime number");
}
});

19 changes: 18 additions & 1 deletion botmath.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
var sum = function (num1, num2) {
return parseFloat(num1) + parseFloat(num2);
}

module.exports.sum = sum;

var isPrime = function (n) {
if (isNaN(n) || !isFinite(n) || n%1 || n<2) return false;
if (isNaN(n) || !isFinite(n) || n%1 || n<2) return false;
if (n%2==0) return (n==2);
var m=Math.sqrt(n);
for (var i=3;i<=m;i+=2) {
Expand All @@ -10,3 +16,14 @@ var isPrime = function (n) {

module.exports.isPrime = isPrime;

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;
Loading