-
Notifications
You must be signed in to change notification settings - Fork 1
/
gif.js
45 lines (43 loc) · 1.01 KB
/
gif.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
let giphy = require('giphy-api')();
const debug = require('debug')('giphy_sms');
const maxGifSize = 1500000;
module.exports.handleGifCommand = function (message) {
return giphy.search(message.command.query)
.then(searchGifResponse)
.then(function (gifUrl) {
return {
media: [gifUrl],
to: message.numbers.to,
from: message.numbers.from
}
})
.catch(function (error) {
debug(error);
return {
text: 'Can\'t find gif for: ' + message.command.query,
to: message.numbers.to,
from: message.numbers.from
};
});
};
const searchGifResponse = function (gifs) {
if (!gifs.data) {
throw new Error('No data in gif response');
}
let gifUrl = '';
gifSearch:
for (let i = 0; i < gifs.data.length; i++) {
let gif = gifs.data[i]
for (key in gif.images) {
let pict = gif.images[key];
debug('Gif Size: %s', pict.size);
if (pict.size < maxGifSize) {
gifUrl = pict.url;
debug('Found Gif!: ', gifUrl)
break gifSearch;
}
}
}
debug('Gif Url: %s', gifUrl);
return gifUrl;
}