-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdirecto.js
132 lines (107 loc) · 4.17 KB
/
directo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
var config = require('./config'),
credentials = require('./config').twitter,
eventConfig = require('./config').event,
SlackConfig = require('./config').slack,
Scheduled = require("scheduled"),
twitter = require('twitter'),
hangoutsBot = require("hangouts-bot"),
Slack = require('slack-node');
var twitterStream = new twitter(credentials),
hashtags = eventConfig.hashtags,
slack = new Slack(),
masterUserRegex = new RegExp(config.hangouts.usuarioId),
masterUser = config.hangouts.usuarioAutorizado,
bot = new hangoutsBot(config.hangouts.botEmail, config.hangouts.botPassword),
slackRegex = /^[Ss]lack /;
// Slack
slack.setWebhook(SlackConfig.webhookUri);
function slackNotify(msg, details) {
details = details || {};
slack.webhook({
channel: details.channel || SlackConfig.channel,
text: msg || SlackConfig.defaultMessage,
username: details.username || SlackConfig.username,
icon_emoji: details.icon || SlackConfig.icon
}, function(err, response) {
if (err) {
bot.sendMessage(masterUser, "[Error] SLACK: " + err);
}
});
}
// Google Hangouts
bot.on('online', function() {
bot.sendMessage(masterUser, "Hola de nuevo, Jefe!");
});
bot.on('message', function(from, message) {
if (message === "Quien soy?") {
bot.sendMessage(from, "Yo te conozco como " + from);
} else if (slackRegex.test(message)) {
if (masterUserRegex.test(from)) {
message = message.replace(slackRegex, '');
slackNotify(message);
} else {
bot.sendMessage(from, 'Buen intento.. pero con esas zapatillas no envío mensajes!');
bot.sendMessage(masterUser, 'Intento de lanzar un mensaje por parte de ' + from + '\n Contenido: ' + message);
}
} else {
bot.sendMessage(from, "No te entiendo... Explicate mejor, Humano!");
}
});
// Twitter
twitterStream.stream('statuses/filter', {
track: hashtags.join(',')
}, function(stream) {
stream.on('data', function(tweet) {
if (tweet.text !== undefined) {
var data = {
username: "Twitter",
icon: "https://cdn3.iconfinder.com/data/icons/social-icons-5/607/Twitterbird.png"
};
slackNotify('https://twitter.com/' + tweet.user.screen_name + '/status/' + tweet.id_str, data);
}
});
stream.on('error', function(error, code) {
bot.sendMessage(masterUser, "[ERROR][Twitter-Stream] Código: " + code + ". Detalle: " + error + ". CC: @ulisesgascon");
});
});
// Bienvenida
slackNotify("Ya estoy Aquí! Se abren las notificaciones automáticas.\n¡Salvemos el mundo haciendo Software Libre!");
// Temporización via config.js
var messageManager = [];
for (var messages in eventConfig.messagesByPriority){
setTimer(messages);
}
function setTimer (key) {
if(eventConfig.timerPriority[key] && eventConfig.messagesByPriority[key]){
setInterval(function(){
slackNotify(eventConfig.messagesByPriority[key][Math.floor(Math.random() * eventConfig.messagesByPriority[key].length)]);
}, eventConfig.timerPriority[key]);
}
}
for (var item in eventConfig.messagesScheduled){
if(item.cron && item.job && item.msg){
messageManager.push(new Scheduled({
id: item.job,
pattern: item.cron,
task: function(){
slackNotify(item.msg);
}
}).start());
}
}
// Eventos
process.on('SIGINT', function() {
slackNotify("Me expulsan del canal! Ya no me quereis como antes... \ncc: @ulisesgascon te portas mal conmigo.");
bot.sendMessage(masterUser, "Me piro! Ya no me quieres como antes...");
process.exit();
});
process.on('exit', function() {
slackNotify("Salgo del canal! Pasarlo bien amigos.... \n¡Paz, amor y Open Source! \ncc: @ulisesgascon");
bot.sendMessage(masterUser, "Me piro! Vaciones por fín!");
process.exit();
});
process.on('uncaughtException', function() {
slackNotify("Salgo del canal! Cierre inesperado... \n¡Paz, amor y Open Source!\ncc: @ulisesgascon");
bot.sendMessage(masterUser, "Me piro! Cierre inesperado...");
process.exit();
});