-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
160 lines (147 loc) · 4.6 KB
/
main.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
const http = require('http'),
req = require('requestretry'),
Promise = require('bluebird'),
default_resp = require('./default_resp.json'),
imgur = require('./sources/imgur.js'),
giphy = require('./sources/giphy.js'),
async = require('async'),
path = require('path'),
fs = require('fs')
var DEBUG = process.env.DEBUG == "true" || false
var port = 3000
if (process.argv.length > 2) {
port = process.argv[2]
}
var sources = initSources()
const requestHandler = function (request, response) {
if(request.method == "POST") {
var body = "";
request.on('data', function (chunk) {
body += chunk;
});
request.on('end', function () {
var contents = getContents(body)
var fullUrl = decodeURIComponent(contents.response_url)
picResponse(decodeURIComponent(contents.text), request).then(function(pic){
req.post(fullUrl, {body:pic, json:true, maxAttempts: 5 },function(err,httpResponse,body){
if(err || httpResponse.statusCode >= 300){
console.log('----')
console.log("Errors replying:")
console.log("Pic: ", pic)
console.log("Reply URL: ", fullUrl)
console.log(err)
console.log(httpResponse.statusCode)
console.log(body)
console.log('----')
}
})
})
})
}
if(request.url.includes("/images/")) {
return sendStaticImages(response, request)
}
console.log(request.method, " on ", request.url, " from ", request.connection.remoteAddress)
response.setHeader('content-type', 'application/json');
response.end(JSON.stringify(default_resp))
}
const server = http.createServer(requestHandler)
server.listen(port, function (err) {
if (err) {
return console.log('something bad happened', err)
}
console.log('server is listening on', port)
})
function getContents(body) {
var parts = body.split('&')
var contents = {}
parts.forEach(function(item) {
var pair = item.split("=")
contents[pair[0]] = pair[1]
}, this);
return contents
}
//NOTE: the request object is here because we need to attribute giphy images... kinda dirty.
function picResponse(terms_concat, request) {
var terms = terms_concat.split('+')
return getOptions(terms).then(function(options){
debug("Got " + options.length + " Options")
var picData = selectRandom(options)
debug("Selected: " + JSON.stringify(picData))
var pic = picData.pic
var response = {
"response_type": "in_channel",
"text":picData.pic
}
debug("Response data: " + JSON.stringify(response))
var attach = {};
if(picData.source == "giphy"){
debug("From giphy")
attach = {
"fallback":"Powered by " + picData.source,
"image_url": "http://" + request.headers.host + "/images/poweredByGiphy.png"
}
} else if(picData.source == "imgur") {
attach = {
"fallback":"Don't relly know what this is.",
"color":"#85bf25",
"text":picData.post
}
}
response.attachments = [attach]
return response
})
}
function getOptions(terms){
return new Promise(function(resolve, reject){
async.concat(sources, function(source, callback){
source.search(terms).then(function(results){
callback(null, results)
}).catch(function(error) {
console.log(error)
console.log('Source: ', source.name, ' had a problem with terms: ', terms)
callback(null, [])
})
}, function(err, results){
if(err) {
console.log("Error getting images from sources:", err)
return reject(err)
}
resolve(results)
})
})
}
function initSources(){
return [
new imgur.Imgur(),
new giphy.Giphy(),
]
}
function selectRandom(options){
var total = options.length
if(total == 0){
return {"source":"static", "pic": "http://i.imgur.com/4AjVHdR.png"}
}
var optionIndex = Math.floor(Math.random() * total)
debug("Choosing index: " + optionIndex + " of Total: " + total)
return options[optionIndex]
}
function sendStaticImages(response, request) {
response.statusCode = 200
var fileLoc = path.resolve("./")
fileLoc = path.join(fileLoc, request.url)
return fs.readFile(fileLoc, function(err, data){
if(err) {
response.writeHead(404, 'Not Found');
response.write('404: File Not Found!');
return response.end();
}
response.write(data)
return response.end();
})
}
function debug(msg) {
if(DEBUG){
console.log(msg)
}
}