-
Notifications
You must be signed in to change notification settings - Fork 1
/
myReporter.js
executable file
·64 lines (52 loc) · 1.88 KB
/
myReporter.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
/**
* @file Defines Reporter functions for Spark
* @author guillain guillain@gmail.com
* @license GPL-3.0
*/
// Load config
var config = require('./config');
exports.help = function(bot) {
var help = '**Reporter** \n\n';
help += '_Description_ : Provide dump features on **CSV** format for the current Space \n\n';
help += '_Commands_ : @ [reporter|r] [bot] \n\n';
help += '* @ reporter \n\n';
help += '* @ r bot \n\n';
bot.say(help);
};
exports.reporter = function (bot, trigger) {
// Remove the first two args
trigger.args.splice(0,2);
var Spark = require('node-sparky');
var fs = require('fs');
var spark = new Spark({ token: config.token });
var date = new Date().toISOString().replace(/T.*/, '_');
var filename = config.reporter.tmpfile + date + bot.room.id + '.csv';
var filetxt = 'Dump of the space: ' + bot.room.id + '\n';
filetxt += 'id;roomType;text;personId;personEmail;created;\n';
var dumpbottxt = '';
if (/bot/i.test(trigger.args['0'])) {
dumpbottxt = 'Dump of the bot env. in the space: ' + bot.room.id + '\n';
dumpbottxt += bot +'\n';
}
if (/help/i.test(trigger.args['0'])) { module.exports.help(bot); }
else {
spark.messagesGet({roomId: bot.room.id}, 1000)
.then(function(msgs) {
msgs.forEach(function(msg) {
filetxt += msg.id + ';' + msg.roomType + ';' + msg.text + ';' + msg.personId + ';' + msg.personEmail + ';' + msg.created + ';\n';
});
if (dumpbottxt != '') { filetxt += dumpbottxt; }
fs.writeFile(filename, filetxt, function(err) {
if(err) {
bot.say('* Error during file writing');
console.log(err);
}
else {
bot.upload(filename);
fs.unlinkSync(filename);
}
});
})
.catch(function(err) { bot.say('* Error during messages listing'); console.log(err); });
}
};