1.0.0 • Published 5 years ago

megazord-javascript v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
5 years ago

Megazord bindings for javascript

Megazord is a very good fit to embed an assistant in any app: a game or an email app written in Electron, or even a console app written in NodeJS.

Because the Megazord library can run the entire platform, it means that the users of your app do not need to have the Snips platform preinstalled on their device to use your app. It also means that multiple apps running different assistants can all work at the same time.

You can look at the full Megazord and NodeJS documentation on https://docs.snips.ai

Megazord bindings

This library exposes low-level javascript bindings for Megazord, and a helper high-level SnipsPlatform class which makes it easy to load an assistant.

You can compile the bindings using

yarn install
yarn build

To run the code, you need to have libsnips_megazord in your LD_LIBRARY_PATH or DYLD_LIBRARY_PATH for osX

You can install the libraries on osX using brew tap snipsco/homebrew-snips && brew install libsnips_megazord.

You can see where it has been installed using

brew list libsnips_megazord # on osX

Compile with the electron FFI

If you want to use megazord-javascript in an electron app, you need to use the same ffi version as that used by electron

yarn add --dev electron-rebuild
./node_modules/.bin/electron-rebuild

or

HOME=~/.electron-gyp npm rebuild \
    --runtime=electron \
    --target=3.0.8 \
    --disturl=https://atom.io/download/atom-shell \
    --build-from-source \
    --arch=x64 \
    --target-arch=x64

Run tests:

wget https://resources.snips.ai/assistants/assistant-weather-EN-0.19.0-dyn-heysnipsv4.zip
unzip assistant-weather-EN-0.19.0-dyn-heysnipsv4.zip
yarn test

Quickstart

You can find quickstart code in quickstart

const {SnipsPlatform} = require('megazord-javascript');
const mic = require('mic');

const assistant = new SnipsPlatform('assistant', {
    enableSnipsWatch: true,
    enableLogs: false,
});

var _currentSessionId = null;
assistant
    .onIntentDetected((msg, user_data) => {
        console.log('-- intent detected: ' + msg.intent.intent_name);
        console.log(' - input: ' + msg.input);
        _currentSessionId && assistant.endSession(_currentSessionId);
    })
    .onIntentNotRecognized((msg, user_data) => {
        console.log('-- intent not recognized: ' + msg.input);
        _currentSessionId && assistant.endSession(_currentSessionId);
    })
    .onSessionStarted((msg, user_data) => {
        _currentSessionId = msg.session_id;
    })
    .onSessionEnded((msg, user_data) => {
        _currentSessionId = null;
    })
    .onListeningStateChanged((isListening, user_data) => {
        console.log('-- is_listening: ' + isListening);
    })
    .onSnipsWatch((msg, user_data) => {
        console.log('-- snips_watch', msg);
    });

assistant.start();

// stream audio

var micInstance = mic({rate: '16000', channels: '1'});
var micInputStream = micInstance.getAudioStream();

micInputStream.on('data', function(data) {
    const buf = Buffer.from(data);
    assistant.appendBuffer(buf);
});

micInputStream.on('error', function(err) {
    console.error("Error: " + err);
});

micInstance.start();