node-red-contrib-chatbot
Version:
REDBot a Chat bot for a full featured chat bot for Telegram, Facebook Messenger and Slack. Almost no coding skills required
48 lines (40 loc) • 1.45 kB
JavaScript
const utils = require('../lib/helpers/utils');
const MessageTemplate = require('../lib/message-template-async');
const emoji = require('node-emoji');
const _ = require('underscore');
const RegisterType = require('../lib/node-installer');
module.exports = function(RED) {
const registerType = RegisterType(RED);
function ChatBotAsk(config) {
RED.nodes.createNode(this, config);
var node = this;
this.buttons = config.buttons;
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);
// check transport compatibility
if (!utils.matchTransport(node, msg)) {
return;
}
var buttons = utils.extractValue('buttons', 'buttons', node, msg, true);
var message = utils.extractValue('string', 'message', node, msg, false);
// template then send
template(message)
.then(function(translated) {
msg.payload = {
type: _.isEmpty(buttons) ? 'reset-buttons': 'buttons',
content: message != null ? emoji.emojify(translated) : null,
chatId: chatId,
messageId: messageId,
buttons: buttons
};
node.send(msg);
});
});
}
registerType('chatbot-ask', ChatBotAsk);
};