UNPKG

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

43 lines (35 loc) 1.34 kB
const _ = require('underscore'); const utils = require('../lib/helpers/utils'); const RegisterType = require('../lib/node-installer'); const GlobalContextHelper = require('../lib/helpers/global-context-helper'); const fs = require('fs'); module.exports = function(RED) { const registerType = RegisterType(RED); const globalContextHelper = GlobalContextHelper(RED); function ChatBotNLPSave(config) { RED.nodes.createNode(this, config); const node = this; globalContextHelper.init(this.context().global); this.filename = config.filename; this.on('input', 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) }; const filename = utils.extractValue('string', 'filename', node, msg, false); const manager = msg.payload; if (manager != null && _.isFunction(manager.export)) { const json = manager.export(false); fs.writeFile(filename, json, (err) => { if (err) { done(err); return; } done(); }); return; } done('Message payload is not a NLP manager class'); }); } registerType('chatbot-nlpjs-save', ChatBotNLPSave); };