-
Notifications
You must be signed in to change notification settings - Fork 1
/
myServiceDesk.js
executable file
·104 lines (92 loc) · 3.2 KB
/
myServiceDesk.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
/**
* @file Defines ServiceDesk functions for Spark
* @author guillain guillain@gmail.com
* @license GPL-3.0
*/
// Load config
var config = require('./config');
var redis = require("redis");
var client = redis.createClient({detect_buffers: true});
var Search = require('redis-search');
var search = Search.createSearch(config.SD.storage);
var fs = require('fs');
exports.help = function(bot) {
var help = '**Service Desk** \n\n';
help += '_Description_ : Provide Q&A feature. Source can come from web motor engine, hub, CSV, SQL/NoSQL \n\n';
help += '_Commands_ : @ [servicedesk|sd] [*|loadcsv|testcsv|help] \n\n';
help += '* @ servicedesk WebEx meeting \n\n';
help += '* @ sd WebEx delegation \n\n';
help += '_Admin tools_ : \n\n';
help += '* @ sd loadcsv \n\n';
help += '* @ sd testcsv \n\n';
bot.say(help);
};
exports.servicedesk = function (bot, trigger) {
// Remove the first two args
trigger.args.splice(0,2);
if (/help/i.test(trigger.args['0'])) { module.exports.help(bot); }
else if (/^loadcsv$/i.test(trigger.args['0'])) { loadcsv(bot, trigger); }
else if (/^testcsv$/i.test(trigger.args['0'])) { testcsv(bot, trigger); }
else { mysearch(bot, trigger); }
};
mysearch = function(bot, trigger) {
j = 0;
var phrase = '';
var tosay = 'Search result \n';
for (i = 0; i < trigger.args.length; i++) {
if(i == 0) { phrase = trigger.args[i]; }
else { phrase += ' '+trigger.args[i]; }
}
client.hgetall(config.SD.storage, function(err,kms){
if (err) throw err;
for (var i in kms) {
//console.log('>>> i:'+i+', kms[i]:'+kms[i]);
var re = new RegExp('\\b'+ phrase + '\\b','i');
if(re.exec(kms[i])) {
console.log('Found :'+kms[i]);
if (j < config.SD.searchlimit) { tosay += '- '+kms[i]+'\n'; }
j++;
}
}
tosay += '\nOn '+j+' result found, limited to '+config.SD.searchlimit+' entry\n';
bot.say(tosay);
});
/*
search.query(phrase, function(err, ids){
if (err) throw err;
console.log('Search results for "%s":', phrase);
console.log(ids);
if (ids.length != 0) {
tosay = 'Found \n';
for(var i in ids) { tosay += '- '+ids[i]+'\n'; }
bot.say(tosay);
} else { bot.say('Not found'); }
});
*/
}
loadcsv = function(bot, trigger){
// Parse CSV file and set value in redis
fs.readFile(config.SD.csv, function(err, data) {
if(err) throw err;
var strs = [];
var array = data.toString().split("\n");
for(i = 0; i < array.length - 1; i++) {
lineArr = array[i].split(';');
strs.push(lineArr[0], lineArr[1]);
//console.log('>>> i:'+i+', key:'+lineArr[0]+', txt:'+lineArr[1]);
}
client.del(config.SD.storage, redis.print);
client.hmset(config.SD.storage, strs, redis.print);
bot.say('Nb of row imported:'+i);
});
}
testcsv = function(bot, trigger) {
client.get('13121', function (err, km) {
if (km) { bot.say('* km 13121 found:'+km); }
else { bot.say('* km 13121 not found'); }
});
client.hget(config.SD.storage, '13121', function (err, kms) {
if (kms) { bot.say('* km '+config.SD.storage+' found:'+kms); }
else { bot.say('* km '+config.SD.storage+' not found'); }
});
}