forked from guidone/node-red-contrib-chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatbot-debug.js
68 lines (56 loc) · 2.2 KB
/
chatbot-debug.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
var _ = require('underscore');
var clc = require('cli-color');
var green = clc.greenBright;
var white = clc.white;
var grey = clc.blackBright;
module.exports = function(RED) {
function ChatBotDebug(config) {
RED.nodes.createNode(this, config);
var node = this;
this.chatId = config.chatId;
this.on('input', function(msg) {
//var context = node.context();
//var transport = msg.originalMessage != null && msg.originalMessage.transport != null ? msg.originalMessage.transport : null;
//var chatId = msg.payload.chatId || (originalMessage && originalMessage.chat.id);
if (_.isFunction(msg.chat)) {
var chatContext = msg.chat();
// format a little
// eslint-disable-next-line no-console
console.log('');
// eslint-disable-next-line no-console
console.log(grey('------ ChatBot debug ----------------'));
// eslint-disable-next-line no-console
console.log(green('Transport:'), white(chatContext.get('transport')));
// eslint-disable-next-line no-console
console.log(green('chatId:'), white(chatContext.get('chatId')));
// push out context
if (chatContext != null) {
// eslint-disable-next-line no-console
console.log(grey('------ ChatBot context --------------'));
_(chatContext.all()).each(function (value, key) {
// eslint-disable-next-line no-console
console.log(green(key + ':'), value instanceof Buffer ? '<Buffer>' : white(value));
});
}
} else {
// normal message here
// eslint-disable-next-line no-console
console.log('');
// eslint-disable-next-line no-console
console.log(grey('------ Message ----------------'));
if (_.isString(msg.payload)) {
// eslint-disable-next-line no-console
console.log(msg.payload);
} else {
// eslint-disable-next-line no-console
console.log(JSON.stringify(msg.payload));
}
}
// eslint-disable-next-line no-console
console.log('');
// show on console
node.warn(chatContext.all());
});
}
RED.nodes.registerType('chatbot-debug', ChatBotDebug);
};