UNPKG

btnexus-hook

Version:
103 lines (84 loc) 3.09 kB
/** * btNexus Hook, Core functionality * @author Marc Fiedler * @copyright 2020 Blackout Technologies */ // Use Strict mode ECMA Script 5+ "use_strict"; // sys imports const fs = require('fs'); const findRoot = require('find-root'); // 3rd party imports const {Node, Peer, Captions} = require('btnexus-node'); // local imports const HookUtility = require('./utility'); module.exports = class Hook extends HookUtility { constructor(){ super(); // root folder of current workspace var root = findRoot(process.cwd()); // avoid double discollects this.exiting = false; try{ this.package = JSON.parse(fs.readFileSync(__dirname+"/../package.json", "utf-8")); this.info("Using btnexus-hook "+this.package.version); }catch(e){ this.error({errorCode: 12, path: __dirname+"/../package.json"}, "Unable to load package.json"); process.exit(); } try{ var captions = JSON.parse(fs.readFileSync(root+"/captions.json", "utf-8")); var languages = Object.keys(captions); this.info("Loaded captions: "+languages.join(", ")); this.captions = new Captions(captions) }catch(e){ this.warning("Loaded hook without captions!"); // console.dir(e); } // if the user wants to, he can implement part of the construct if( this.onInit != undefined ){ this.onInit(); } try{ this.package = JSON.parse(fs.readFileSync(root+'/package.json', 'utf-8')); }catch(e){ this.error({errorCode: 4}, "Unable to load package information!"); process.exit(); } this.connectToBtNexus(); } connectToBtNexus(){ this.node = new Node({ onConnected: () => { // communicat success this.info("Successfully authenticated to btNexus"); // execute connect signal if( this.onConnected != undefined ){ this.onConnected(); } this.node.join(this.node.settings.id); console.log("Joining: "+this.node.settings.id); this.node.subscribe(this.node.settings.id, 'hookChat', (message) => { console.dir(message); var payload = message.payload.hookRequest; if( this.onMessage != undefined ){ this.onMessage( payload, payload.sessionId ); } }); }, onClose: () => { // send disconnect signal if( this.onDisconnect != undefined ){ this.onDisconnect(); } } }); } // the function say is meant to trigger say(peer, message){ this.node.publish(this.node.settings.id, peer, {hookResponse: message}); } }