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