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 pathindex.js
166 lines (149 loc) · 5.36 KB
/
index.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
const async = require('async');
const request = require('request');
const _ = require('underscore');
const events = require('events');
const util = require('util');
const http = require('http');
const formidable = require('formidable');
// config { token:groupme token,
// group:the room to connect to,
// name: the bot name,
// url: optional callback,
// avatar_url: optional avatar image
function Bot (config) {
if (! (this instanceof Bot)) return new Bot(config);
for (var key in config) if ( config.hasOwnProperty(key) ) this[key] = config[key];
if (this.token && this.group && this.name) {
console.log("registering the bot");
this.registerBot();
} else {
console.log("pass me config so you can actually be a bot");
}
};
util.inherits(Bot, events.EventEmitter);
// start the web server
// arg: address to serve on
Bot.prototype.serve = function(address) {
var self = this;
var server = http.createServer(function (request, response) {
if (request.url == '/' && request.method == 'GET') {
response.writeHead(200, {"Content-Type": "application/json"});
response.end(JSON.stringify({'name': self.name, 'group': self.group}));
} else if (request.url == '/incoming' && request.method == 'POST') {
var form = new formidable.IncomingForm();
var messageFields = {};
form.parse(request, function(err, fields, files) {
if (err) console.error("bad incoming data " + err);
});
form.on('field', function(name, value) {
messageFields[name] = value;
});
form.on('end', function() {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("THANKS");
self.emit('botMessage', self, { name:messageFields.name, text:messageFields.text });
});
} else {
response.writeHead(404, {"Content-Type": "text/plain"});
response.end("NOT FOUND");
}
}.bind(this));
server.listen(address);
};
// make the bot say something
Bot.prototype.message = function(_message) {
var url = 'https://api.groupme.com/v3/bots/post';
var package = {};
package.text = _message;
package.bot_id = this.botId;
request( { url:url, method:'POST', body: JSON.stringify(package) });
};
// destroys a bot by id, if no bot_id is sent, unregisters itself
Bot.prototype.unRegister = function(bot_id, callback) {
var url = 'https://api.groupme.com/v3/bots/destroy?token=' + this.token;
request( { url : url, method : 'POST', body : JSON.stringify({bot_id:bot_id}) },
function(error, response, body) {
callback();
});
};
// get a list of registered bots
Bot.prototype.allBots = function(callback) {
var url = 'https://api.groupme.com/v3/bots?token=' + this.token;
request( { url : url, method : 'GET' }, function(err, response, body) {
body = JSON.parse(body);
var bots = [];
_.each(body.response, function(bot) {
bots.push(bot);
});
if (callback) callback(bots);
});
};
// kill all bots, this is a big deal. Use with caution
Bot.prototype.killAllBots = function(callback) {
this.allBots(function(listOfBots) {
_.each(listOfBots, function(bot) {
this.unRegister(bot.bot_id);
});
}.bind(this));
if (callback) callback("done");
};
// register the bot with groupme, but first kill any bots in this room with this name.
Bot.prototype.registerBot = function() {
async.waterfall([
// get a list of the bots
function(callback) {
var self = this;
this.allBots(function(bots) {
var existingBots = bots.filter(function(bot) {
return bot.group_id == self.group && bot.name == self.name;
});
var targetCallbackUrl = self.url ? self.url + '/incoming' : null;
if (existingBots.length === 1
&& existingBots[0].callback_url === targetCallbackUrl
&& existingBots[0].avatar_url == self.avatar_url) {
//bot already registered and has same settings. Leave it as is and skip registration
self.botId = existingBots[0].bot_id;
return callback(null, []);
}
callback(null, existingBots.map(function(bot) { return bot.bot_id; }));
});
}.bind(this),
// kill the bots
function(botsToKill, callback) {
async.each(botsToKill, this.unRegister.bind(this));
callback();
}.bind(this),
// register the new bot
function(callback) {
if (this.botId) {
//bot exists already, skip registration
return callback(null, this.botId);
}
var bot = {};
bot.name = this.name;
bot.group_id = this.group;
if (this.url) {
bot.callback_url = this.url + '/incoming';
};
if (this.avatar_url) {
bot.avatar_url = this.avatar_url;
};
var url = 'https://api.groupme.com/v3/bots?token=' + this.token;
request( { url:url, method:'POST', body: JSON.stringify( { bot:bot } ) },
function(error, response, body) {
if (!error) {
var parsedBody = JSON.parse(body).response.bot;
this.emit('botRegistered', this);
callback(null, parsedBody.bot_id);
} else {
callback(error);
}
}
);
}.bind(this)
], function (err, bot_id) {
this.botId = bot_id;
this.emit('botReady', this);
}.bind(this));
};
module.exports = Bot;