forked from guidone/node-red-contrib-chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatbot-inline-buttons.js
60 lines (49 loc) · 1.81 KB
/
chatbot-inline-buttons.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
var _ = require('underscore');
var MessageTemplate = require('./lib/message-template.js');
var emoji = require('node-emoji');
var utils = require('./lib/helpers/utils');
module.exports = function(RED) {
function ChatBotInlineButtons(config) {
RED.nodes.createNode(this, config);
var node = this;
this.answers = config.answers;
this.message = config.message;
this.transports = ['telegram', 'facebook', 'smooch'];
this.on('input', function(msg) {
// check transport compatibility
if (!utils.matchTransport(node, msg)) {
return;
}
var chatId = utils.getChatId(msg);
var messageId = utils.getMessageId(msg);
var template = MessageTemplate(msg, node);
// check transport compatibility
if (!utils.matchTransport(node, msg)) {
return;
}
// prepare answers, first the config, then payload
var answers = null;
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;
}
// prepare the message, first the config, then payload
var message = null;
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: 'inline-buttons',
content: message != null ? emoji.emojify(template(message)) : null,
chatId: chatId,
messageId: messageId,
buttons: answers
};
node.send(msg);
});
}
RED.nodes.registerType('chatbot-inline-buttons', ChatBotInlineButtons);
};