forked from howdyai/botkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConsoleBot.js
107 lines (79 loc) · 2.72 KB
/
ConsoleBot.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
var Botkit = require(__dirname + '/CoreBot.js');
var readline = require('readline');
function TextBot(configuration) {
// Create a core botkit bot
var text_botkit = Botkit(configuration || {});
text_botkit.middleware.spawn.use(function(bot, next) {
text_botkit.listenStdIn(bot);
next();
});
text_botkit.middleware.format.use(function(bot, message, platform_message, next) {
// clone the incoming message
for (var k in message) {
platform_message[k] = message[k];
}
next();
});
text_botkit.defineBot(function(botkit, config) {
var bot = {
botkit: botkit,
config: config || {},
utterances: botkit.utterances,
};
bot.createConversation = function(message, cb) {
botkit.createConversation(this, message, cb);
};
bot.startConversation = function(message, cb) {
botkit.startConversation(this, message, cb);
};
bot.send = function(message, cb) {
// eslint-disable-next-line no-console
console.log('BOT:', message.text);
if (cb) {
cb();
}
};
bot.reply = function(src, resp, cb) {
var msg = {};
if (typeof(resp) == 'string') {
msg.text = resp;
} else {
msg = resp;
}
msg.channel = src.channel;
bot.say(msg, cb);
};
bot.findConversation = function(message, cb) {
botkit.debug('CUSTOM FIND CONVO', message.user, message.channel);
for (var t = 0; t < botkit.tasks.length; t++) {
for (var c = 0; c < botkit.tasks[t].convos.length; c++) {
if (
botkit.tasks[t].convos[c].isActive() &&
botkit.tasks[t].convos[c].source_message.user == message.user
) {
botkit.debug('FOUND EXISTING CONVO!');
cb(botkit.tasks[t].convos[c]);
return;
}
}
}
cb();
};
return bot;
});
text_botkit.listenStdIn = function(bot) {
text_botkit.startTicking();
var rl = readline.createInterface({ input: process.stdin, output: process.stdout, terminal: false });
rl.on('line', function(line) {
var message = {
text: line,
user: 'user',
channel: 'text',
timestamp: Date.now()
};
text_botkit.ingest(bot, message, null);
});
};
return text_botkit;
}
module.exports = TextBot;