forked from guidone/node-red-contrib-chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatbot-ask.js
56 lines (46 loc) · 1.63 KB
/
chatbot-ask.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
var _ = require('underscore');
var utils = require('./lib/helpers/utils');
var MessageTemplate = require('./lib/message-template.js');
var emoji = require('node-emoji');
module.exports = function(RED) {
function ChatBotAsk(config) {
RED.nodes.createNode(this, config);
var node = this;
this.answers = config.answers;
this.message = config.message;
this.inline = config.inline;
this.transports = ['telegram'];
this.on('input', function(msg) {
var chatId = utils.getChatId(msg);
var messageId = utils.getMessageId(msg);
var template = MessageTemplate(msg, node);
var answers = null;
var message = null;
// check transport compatibility
if (!utils.matchTransport(node, msg)) {
return;
}
// get from config or payload
if (_.isArray(node.answers) && !_.isEmpty(node.answers)) {
answers = node.answers;
} else if (_.isObject(msg.payload) && _.isArray(msg.payload.buttons) && !_.isEmpty(msg.payload.buttons)) {
answers = msg.payload.buttons;
}
// get from config or payload
if (_.isString(node.message) && !_.isEmpty(node.message)) {
message = node.message;
} else if (_.isObject(msg.payload) && _.isString(msg.payload.message) && !_.isEmpty(msg.payload.message)) {
message = msg.payload.message;
}
msg.payload = {
type: 'buttons',
content: message != null ? emoji.emojify(template(message)) : null,
chatId: chatId,
messageId: messageId,
buttons: answers
};
node.send(msg);
});
}
RED.nodes.registerType('chatbot-ask', ChatBotAsk);
};