-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
74 lines (63 loc) · 2.75 KB
/
bot.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
var Flint = require('node-flint');
var webhook = require('node-flint/webhook');
var express = require('express');
var bodyParser = require('body-parser');
var MongoClient = require('mongodb-bluebird');
var fs = require('fs');
var states = require('./states.js');
var path = require('path');
var config = JSON.parse(fs.readFileSync('config.json', 'utf8'));
config.webhookUrl = config.rootUrl + '/bot';
var app = express();
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
// init flint
var flint = new Flint(config);
flint.debugger = console.log;
flint.start();
flint.messageFormat = 'markdown';
// add flint event listeners
flint.on('message', function(bot, trigger, id) {
// flint.debug('"%s" said "%s" in room "%s"', trigger.personEmail, trigger.text, trigger.roomTitle);
// console.log(trigger);
if(trigger.personEmail != config.email){ //this ignores messages from self
trigger.text = trigger.text.replace(config.botName, "").trim().toLowerCase();
trigger.raw = trigger.raw.replace(config.botName, "").trim().toLowerCase();
trigger.args = trigger.text.split(" ");
var roomState = states.roomStates[bot.room.id]; //Eacg riin gas a roomState
var promise = states[roomState.id](bot, trigger, roomState); //each roomState has an id which indicates the main state is is in
if(promise){ // if a promise is returned by the state function, roomState is saved only after resolving it
promise.then(function(){
console.log("updated by promise");
states.updateState(bot.room.id, roomState);
});
}else if(typeof promise === 'boolean' && promise === false){ //when no change is made to roomState
console.log("not updated");
}else{ //when no asynchronous change is made to the roomState
console.log("updated synchronously");
states.updateState(bot.room.id, roomState);
}
}
});
flint.on('initialized', function() {
flint.debug('initialized %s rooms', flint.bots.length);
states.bots = flint.bots;
});
flint.on('spawn', states.spawn);
// define express path for incoming webhooks
app.post('/bot', webhook(flint));
//define express path for other webpages
app.get('/create/:roomId/:token', states.createSetPage);
app.post('/create', states.createSet);
// start express server
var server = app.listen(config.port, function () {
flint.debug('Flint listening on port %s', config.port);
});
// gracefully shutdown (ctrl-c)
process.on('SIGINT', function() {
flint.debug('stoppping...');
server.close();
flint.stop().then(function() {
process.exit();
});
});