-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
64 lines (53 loc) · 1.67 KB
/
index.js
File metadata and controls
64 lines (53 loc) · 1.67 KB
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
// Request.
const request = require('request')
// Express.
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({
extended: false
}))
app.get('/', function (req, res) {
if (typeof(req.query.text) !== 'undefined') {
var text = req.query.text.substr(7)
giphySynoChat.returnResult(text, res)
}
})
app.post('/', function (req, res) {
if (typeof(req.body.text) !== 'undefined') {
var text = req.body.text.substr(7)
giphySynoChat.returnResult(text, res)
}
})
app.listen(3000, function () {
console.log('Server running on http://localhost:3000')
})
var giphySynoChat = {
returnResult: function (text, res) {
var query = {
api_key: process.env.GIPHY_API_KEY,
q: text
}
request({url: 'http://api.giphy.com/v1/gifs/search', qs: query}, function(err, response, body) {
var data = JSON.parse(body)
if (!err && response.statusCode == 200) {
var rand = Math.floor(Math.random() * 25)
if (typeof(data.data[rand]) !== 'undefined') {
var item = data.data[rand]
res.send({text: item.images.downsized_medium.url})
}
// Error if item isn't present.
else {
res.send('Error, item is missing.')
}
}
else {
// Print error message if present.
if (typeof(data.message) !== 'undefined') {
res.send(data.message)
}
}
})
}
}