Skip to content

Commit

Permalink
Beta structure of gerzek.
Browse files Browse the repository at this point in the history
  • Loading branch information
ramesaliyev committed Feb 25, 2015
1 parent 062cbdc commit b113bc4
Show file tree
Hide file tree
Showing 19 changed files with 450 additions and 0 deletions.
14 changes: 14 additions & 0 deletions common/posttoslack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Web hook url.
var webhookUrl = "https://hooks.slack.com/services/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";

// Require modules.
var request = require('request');

module.exports = function(postBody){
request({
method : "POST",
url : webhookUrl,
json : true,
body : postBody
});
};
18 changes: 18 additions & 0 deletions common/readdirectory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = function(dirname, _exports){
// Require modules.
var path = require("path"),
fs = require("fs");

// Get path.
var normalizedPath = path.join(dirname);

// Read directory.
fs.readdirSync(normalizedPath).forEach(function(file) {
// If file starts with underscore.
if(file[0] === "_"){
// Require all files.
file = file.replace("_", "").replace(".js", "");
_exports[file] = require(normalizedPath +"/_"+ file);
}
});
};
23 changes: 23 additions & 0 deletions common/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Server constructor.
var Server = function () {
// Constants.
var express = require('express'),
bodyParser = require('body-parser');

// Private.
this.app = express();

// Parse body.
this.app.use(bodyParser.urlencoded({
extended: true
}));
};

// Listen port.
Server.prototype.listen = function (port, callback) {
this.port = port;
this.app.listen(this.port, callback);
};

// Export module.
module.exports = Server;
6 changes: 6 additions & 0 deletions common/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
generateRandom: function(min, max){
var random = Math.floor(Math.random() * (max - min + 1)) + min;
return Math.min(max, Math.max(min, random));
}
};
16 changes: 16 additions & 0 deletions listener/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Require and create server.
var Server = require("../common/server"),
server = new Server();

// Get modules.
var listener = require("./listener");
utils = require("../common/utils");
modules = require("./modules");

// Assign listener to listen every communication over 'gerzek'.
server.app.post('/gerzek', listener);

// Listen port.
server.listen(3000, function(){
console.log("Listener has initialized.");
});
21 changes: 21 additions & 0 deletions listener/listener.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module.exports = function (req, res, next) {
// Get requested text.
var requestText = req.body.text;

// Process words.
var splittedText = requestText.split(" "),
moduleName = splittedText[1],
moduleItself = global.modules[moduleName] || global.modules["other"],
responsePayload;

// Check if module exist for given command.
if(moduleItself) {
splittedText.splice(0, 1);
responsePayload = moduleItself(req, splittedText);
}else{
return res.status(200).end();
}

// Send response.
return res.status(200).json(responsePayload);
};
3 changes: 3 additions & 0 deletions listener/modules/_google.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function (req, command) {
return "Google henüz çalışmıyor pampa.";
};
66 changes: 66 additions & 0 deletions listener/modules/_hatirlat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
var schedule = require('node-schedule'),
postToSlack = require('../../common/posttoslack'),
moment = require('moment-timezone');

// Hatirlat.
module.exports = function (req, command) {
// Remove "hatirlat" part from command.
command.shift();

// Join command and then parse by ":".
command = command.join(" ").split(":");

// Take first part as remind date and rest as remind message.
var remindDate = command.splice(0, 1).join(""),
remindText = command.join(":");

// Show help if date or text not provided.
if(remindDate.length < 1 || remindText.length < 1){
return {
text: "şöyle kullancan pampa: 'gerzek hatirlat <saat> [dakika] [gun] [ay] [yil] : <hatirlatma metni>'"
}
}

// Parse date
remindDate = remindDate.split(" ");

// Get current date.
var currentMoment = moment().tz("Europe/Istanbul");

// Set provided dates.
currentMoment.hour(remindDate[0]);
if (remindDate[1]) currentMoment.minute(remindDate[1]);
if (remindDate[2]) currentMoment.date(remindDate[2]);
if (remindDate[3]) currentMoment.month(remindDate[3] - 1);
if (remindDate[4]) currentMoment.year(remindDate[4]);

// Convert to server time zone.
var timezoneMoment = currentMoment.clone().tz("America/New_York");

// Create js remind date object.
var jsRemindDate = new Date(
timezoneMoment.year(),
timezoneMoment.month(),
timezoneMoment.date(),
timezoneMoment.hour(),
timezoneMoment.minute(),
0
);

// Correct urls.
remindText = remindText.replace(/<(.*)\|(.*)>/g, "$1");

// Schedule job.
schedule.scheduleJob(jsRemindDate, function(){
postToSlack({
username : "gerzek (hatirlatma)",
text : "@"+ req.body.user_name +" -> "+ remindText,
link_names : 1
});
});

// Send success message.
return {
text: "tamam hatırlatıcam pampa."
}
};
36 changes: 36 additions & 0 deletions listener/modules/_hello.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Add common commands.
global.commonCommands.push({
name : "hello",
commands: [
"selam",
"hi",
"hello",
"selamınaleyküm",
"merhaba",
"slm",
"selams",
"mrb"
]
});

// Answers.
var answers = [
"selam",
"ooo kimler gelmiş selam pampa",
"welcome canım",
"meyaba",
"melaba",
"hi canım",
"ooo bro hoşgeldin",
"slm.",
"Merhaba."
];

module.exports = function (req, command) {
var responseText = answers[utils.generateRandom(0, 5)];

// Return response text.
return {
text: responseText
}
};
28 changes: 28 additions & 0 deletions listener/modules/_other.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module.exports = function (req, command) {
// Response payload.
var responsePayload = null;

// Look out for common commands.
global.commonCommands.forEach(function(module){
// Return if responsePayload attached.
if(responsePayload !== null) return;

// Check if module commands include given command.
var isInclude = module.commands.indexOf(command[0]);

// If include.
if(isInclude !== -1){
// Send command to module.
responsePayload = global.modules[module.name](req, command);
}
});

// If there is no module.
if(responsePayload === null) {
responsePayload = {
text: "Kanka pek anlamadım."
};
}

return responsePayload;
};
27 changes: 27 additions & 0 deletions listener/modules/_random.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Generate random number.
module.exports = function (req, command) {
// Remove "command" part from command.
command.shift();

// Set min and max.
var min = +command[0],
max = +command[1];

// If min and max not provided.
if(!min && !max){
return {
text: "şöyle kullanıcan pampa: 'gerzek random min max' ya da 'gerzek random max'"
};
}

// If only one provied.
if(!max){
max = min;
min = 0;
}

// Send success message.
return {
text: utils.generateRandom(min, max)
};
};
34 changes: 34 additions & 0 deletions listener/modules/_whatsup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Add common commands.
global.commonCommands.push({
name : "whatsup",
commands: [
"naber",
"nasılsın",
"napan",
"nörüyon",
"nettin",
"nbr"
]
});

// Answers.
var answers = [
"eyi senden naber?",
"iyi nolsun, sen?",
"iyiyim sen nassın?",
"eyi napam aynı sen napan?",
"aynı terane, senden?",
"huzur var para yok pampa, sende ne var ne yok?",
"standart moruk, sen?",
"allaha çok şükür mümin kardeşim sen nasılsın inşallah?",
"on numarayım pampa, sen?"
];

module.exports = function (req, command) {
var responseText = answers[utils.generateRandom(0, 5)];

// Return response text.
return {
text: responseText
}
};
62 changes: 62 additions & 0 deletions listener/modules/_youtube.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
var youtube = require('youtube-feeds'),
postToSlack = require('../../common/posttoslack');

// Search on youtube.
module.exports = function (req, command) {
// Remove "command" part from command.
command.shift();

// Get second command.
var toDo = command[0];

// Join rest.
command.shift();
var searchText = command.join(" ");

// If there is no search text.
if (!searchText.length) {
return {
text: "şöyle kullanıcan pampa: 'gerzek youtube <getir|ara> <aranacak şey>'"
}
}

// Search on youtube.
youtube.feeds.videos({q: searchText, 'max-results': 9}, function(err, data){
// Create respond details.
var respondMessage = "@"+ req.body.user_name +" -> '"+ searchText +"' laflarıyla aradım; ",
respondData = {},
youtubeLink = "http://www.youtube.com/watch?v=";

// If error.
if(err){
respondMessage += "ya bi bok bulamadım ya da başka bi sıkıntı çıktı. ("+ err.message +")"
}else{
if(toDo === "getir"){
respondMessage += youtubeLink+data.items[0].id;
}else{
// Do not unfurl link.
respondData.unfurl_links = false;
respondData.parse = "none";

// Break line.
respondMessage += "\n";

data.items.forEach(function(item){
respondMessage += "- <"+ youtubeLink + item.id + "|"+item.title+"> \n";
});
}
}

// Set respond data details.
respondData.username = "gerzek (youtube)";
respondData.text = respondMessage;
respondData.link_names = 1;

// Post to slack.
postToSlack(respondData);
});

return {
text: "arıyorum pampa."
}
};
10 changes: 10 additions & 0 deletions listener/modules/_zar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Generate random number.
module.exports = function (req, command) {
// Remove "command" part from command.
command.shift();

// Send success message.
return {
text: utils.generateRandom(1,6) +" "+ utils.generateRandom(1,6)
};
};
Loading

0 comments on commit b113bc4

Please sign in to comment.