-
Notifications
You must be signed in to change notification settings - Fork 0
/
dlxdd_bot.js
194 lines (163 loc) · 5.01 KB
/
dlxdd_bot.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
module.exports = {
exec : function() {
return require('exec-sync');
},
fs : function() {
return require('fs');
},
py : function(cmd) {
cmd.unshift("python");
var exec = module.exports.exec();
var res = exec(cmd.join(" "), true);
console.log(res.stderr);
if(res.stderr) {
console.log(res.stderr.toString());
return res.stderr.toString();
}
console.error("No result to return");
return null;
},
http : function() {
return require('http');
},
querystring : function() {
return require('querystring');
},
xmpp : function(obj) {
obj = module.exports.querystring().stringify(obj);
var opts = {
host : "localhost",
path : "/communicate",
port : process.env.SLACK_PORT || 8080,
method : 'POST',
headers : {
'Content-Type' : 'application/x-www-form-urlencoded',
'Content-Length' : obj.length
}
};
var callback = function(res) {
var str = '';
res.on('data', function(chunk) {
str += chunk;
});
res.on('end', function() {
console.log(str);
});
};
var req = module.exports.http().request(opts, callback);
req.on('error', function(err) {
console.log("ERROR: " + err);
});
req.write(obj);
req.end();
return req;
},
test_mod: function() {
//return module.exports.create_deepdream("user_id", "user_name");
//return module.exports.iterate_deepdream("abcdefg12345678");
//return module.exports.giffify_deepdream("abcdefg12345678");
//return module.exports.init_deepdream("https://slack-files.com/files-pub/T043P3V33-F07RL3K9T-93ad9997ec/download/1hdwaunxoaaaewie-vinz8uwuskwpqzmo.large.png");
return module.exports.generate_dlxdd_request(null,
{ is_test : true, message : "AYO TECHNOLOGY!" },
"AYO TECHNOLOGY");
},
get_home : function() {
return process.env.HOME || process.env.USERPROFILE;
},
generate_dlxdd_request : function(doc_id, extras, response) {
dlxdd_request = {};
for(var prop in extras) {
dlxdd_request[prop] = extras[prop];
}
if(doc_id) {
dlxdd_request.doc_id = doc_id;
}
var xmpp = module.exports.xmpp(dlxdd_request);
if(xmpp) {
return {
text : response + " image `" + (doc_id ? doc_id : "(none)") + "`! Stand by..."
};
}
return null;
},
create_deepdream : function(user_id, user_name) {
var asset_regex = /^https:\/\/slack\-files\.com/i;
var request_file = module.exports.py(['slack_api.py', 'request_file', user_id]);
if(!request_file || !request_file.match(asset_regex)) {
console.error("could not get any file.");
return null;
}
var send_to_dropbox = module.exports.py(['slack_api.py', 'send_file', request_file]);
var init_deepdream = module.exports.init_deepdream(request_file);
console.log(init_deepdream);
if(send_to_dropbox && init_deepdream) {
return {
text : "...starting Deepdream for <@" + user_id + ">..."
};
} else {
console.error("could not send file to dropbox.");
}
return null;
},
init_deepdream : function(request_file) {
var comp = request_file.split('/');
comp.reverse();
module.exports.generate_dlxdd_request(null, {
file_name : comp[2] + "_" + comp[0],
task_path : "Documents.evaluate_document.evaluateDocument"
}, "Initing Deepdream for file ");
return true;
},
giffify_deepdream : function(doc_id) {
return module.exports.generate_dlxdd_request(doc_id,
{ task_path : "DeepDream.giffify_deepdream.giffify_deepdream" },
"Giffifying Deepdream for");
},
iterate_deepdream : function(doc_id) {
return module.exports.generate_dlxdd_request(doc_id,
{ task_path : "DeepDream.iterate_deepdream.iterate_deepdream" },
"MOAR Deepdream on");
},
respond : function(req, res, next) {
var user_name = req.body.user_name;
if(user_name === 'slackbot') {
return res.status(200).end();
}
console.log(req.body);
var dlxdd_payload = null;
var moar_regex = /^moar\s[a-z0-9]{40}/i;
var giffify_regex = /^gif\s[a-z0-9]{40}/i;
var PAYLOAD_DIRECTION = {
CREATE : 1,
ITERATE : 2,
GIFFIFY : 3
};
var build_payload = function(direction, extras) {
if(direction === PAYLOAD_DIRECTION.CREATE) {
console.log("no text in request body. Let's scrape user " + extras + "'s last image...");
return module.exports.create_deepdream(extras, user_name);
} else if(direction === PAYLOAD_DIRECTION.ITERATE) {
console.log("moar-ing image " + extras);
return module.exports.iterate_deepdream(extras);
} else if(direction === PAYLOAD_DIRECTION.GIFFIFY) {
console.log("giffifying image " + extras);
return module.exports.giffify_deepdream(extras);
}
};
if(!req.body.text) {
dlxdd_payload = build_payload(PAYLOAD_DIRECTION.CREATE, req.body.user_id);
} else {
var moar = req.body.text.match(moar_regex);
var gif = req.body.text.match(giffify_regex);
if(moar) {
dlxdd_payload = build_payload(PAYLOAD_DIRECTION.ITERATE, moar[0].split(" ")[1]);
} else if(gif) {
dlxdd_payload = build_payload(PAYLOAD_DIRECTION.GIFFIFY, gif[0].split(" ")[1]);
}
}
if(dlxdd_payload) {
return res.status(200).json(dlxdd_payload);
}
return res.status(200).end();
}
};