This repository has been archived by the owner on Feb 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathapp.js
55 lines (46 loc) · 1.62 KB
/
app.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
const GROUPMETOKEN = process.env['GROUPMETOKEN'];
const GIPHYTOKEN = process.env['GIPHYTOKEN'];
const GROUP = process.env['GROUP'];
const URL = process.env['URL'];
const natural = require('natural');
const _ = require('underscore');
const util = require('util');
const request = require('request');
var tokenizer = new natural.WordTokenizer();
var config = { token:GROUPMETOKEN,
name: "giphybot",
group: GROUP,
url: URL
};
const AVATAR = process.env['AVATAR'];
if (AVATAR) {
config.avatar_url = AVATAR;
}
var giphy = require('giphy-wrapper')(GIPHYTOKEN);
var bot = require('fancy-groupme-bot')(config);
bot.on('botRegistered', function() {
console.log("online");
});
bot.on('botMessage', function(bot, message) {
console.log('incoming');
if (message.name != 'giphybot') {
var tokens = tokenizer.tokenize(message.text);
tokens = _.map(tokens, function(t) { return t.toLowerCase(); });
if (tokens.indexOf('giphybot') >= 0 || tokens.indexOf('g') >= 0) {
tokens = _.without(tokens, 'giphybot', 'g');
console.log("searching for " + tokens);
giphy.search(escape(tokens.join('+')), 20, 0, function(err, data) {
if (err) console.error(err);
console.log("giphy returned " + util.inspect(data));
if (data.data.length) {
data = _.shuffle(data.data);
var id = data[0].id;
var imageUrl = "http://media3.giphy.com/media/" + id + "/giphy.gif";
console.log("sending a message " + imageUrl);
bot.message(imageUrl);
}
});
}
}
});
bot.serve(process.env['PORT'] || 3000);