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
50 lines (44 loc) • 1.6 kB
JavaScript
const _ = require('underscore');
const { Language } = require('node-nlp');
const RegisterType = require('../lib/node-installer');
const { isCommand } = require('../lib/helpers/regexps');
const { isValidMessage } = require('../lib/helpers/utils');
const GlobalContextHelper = require('../lib/helpers/global-context-helper');
module.exports = function(RED) {
const registerType = RegisterType(RED);
const globalContextHelper = GlobalContextHelper(RED);
function ChatBotLanguage(config) {
RED.nodes.createNode(this, config);
const node = this;
globalContextHelper.init(this.context().global);
this.on('input', async function(msg, send, done) {
// send/done compatibility for node-red < 1.0
send = send || function() { node.send.apply(node, arguments) };
done = done || function(error) { node.error.call(node, error, msg) };
// check if valid message
if (!isValidMessage(msg, node)) {
done();
return;
}
// exit if not string
if (_.isString(msg.payload.content)) {
// if it's a command, then don't care about the language
if (isCommand(msg.payload.content)) {
send(msg);
done();
return;
}
// now detect
const languageGuesser = new Language();
const guess = languageGuesser.guess(msg.payload.content);
if (!_.isEmpty(guess)) {
await msg.chat().set('language', guess[0].alpha2);
}
}
// go through
node.send(msg);
done();
});
}
registerType('chatbot-language', ChatBotLanguage);
};