-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
64 lines (57 loc) · 1.45 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
56
57
58
59
60
61
62
63
64
var CabbageBot = require('./lib/cabbage');
var delay = require('./lib/utils').delay;
var openIdLogin = require('./lib/auth').openIdLogin;
var request = require('./lib/utils').request;
// load configuration
try {
var config = require('./config');
}
catch (e) {
console.error('Please set up the configuration file `config.js` first.')
process.exit(1);
}
// Example handler
function handleMessageEvent (e, cbg) {
if (e.event_type != 1) {
return;
}
if (e.content.indexOf('!!rabbit') > -1) {
cbg.send('I like rabbits!', e.room_id).then(function (msgId) {
if (!msgId) {
throw new Error('Invalid id');
}
console.log('Message sent: ' + msgId);
return delay(5000).then(function () {
return cbg.edit('I *really* like rabbits!', msgId).then(function (msgId) {
console.log('Message edited: ' + msgId);
});
});
}).catch(console.error);
}
}
// start bot
var cbg = new CabbageBot(config.roomId);
openIdLogin(config.emailAddress, config.password)
.then(cbg.connect.bind(cbg))
.then(cbg.listen.bind(cbg))
.catch(console.error);
// SIGINT event handler
process.on('SIGINT', cbg.quit.bind(cbg));
// event handler
cbg.once('open', function () {
console.log('Connection established.');
});
cbg.once('close', function () {
console.log('Connection closed.');
});
cbg.on('error', function (err) {
console.error(err);
})
cbg.on('event', function (evt) {
if (evt.event_type == 1) {
handleMessageEvent(evt, cbg);
}
else {
console.log(evt);
}
});