Search in sources :

Example 1 with SessionStartedMessage

use of ai.snips.hermes.SessionStartedMessage in project snips-platform-android-demo by snipsco.

the class MainActivity method startMegazordService.

private void startMegazordService() {
    if (client == null) {
        // a dir where the assistant models was unziped. it should contain the folders asr dialogue hotword and nlu
        File assistantDir = new File(Environment.getExternalStorageDirectory().toString(), "snips_android_assistant");
        client = new SnipsPlatformClient.Builder(assistantDir).enableDialogue(// defaults to true
        true).enableHotword(// defaults to true
        true).enableSnipsWatchHtml(// defaults to false
        true).enableLogs(// defaults to false
        true).withHotwordSensitivity(// defaults to 0.5
        0.5f).enableStreaming(// defaults to false
        true).build();
        client.setOnPlatformReady(new Function0<Unit>() {

            @Override
            public Unit invoke() {
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        findViewById(R.id.loadingPanel).setVisibility(View.GONE);
                        findViewById(R.id.scrollView).setVisibility(View.VISIBLE);
                        final Button button = findViewById(R.id.start);
                        button.setEnabled(true);
                        button.setText(R.string.start_dialog_session);
                        button.setOnClickListener(new OnClickListener() {

                            @Override
                            public void onClick(View view) {
                                // programmatically start a dialogue session
                                client.startSession(null, new ArrayList<String>(), false, null);
                            }
                        });
                    }
                });
                return null;
            }
        });
        client.setOnHotwordDetectedListener(new Function0<Unit>() {

            @Override
            public Unit invoke() {
                Log.d(TAG, "an hotword was detected !");
                // Do your magic here :D
                return null;
            }
        });
        client.setOnIntentDetectedListener(new Function1<IntentMessage, Unit>() {

            @Override
            public Unit invoke(IntentMessage intentMessage) {
                Log.d(TAG, "received an intent: " + intentMessage);
                // Do your magic here :D
                // For now, lets just use a random sentence to tell the user we understood but don't know what to do
                List<String> answers = Arrays.asList("This is only a demo app. I understood you but I don't know how to do that", "Can you teach me how to do that?", "Oops! This action has not be coded yet!", "Yes Master! ... hum, ..., er, ... imagine this as been done", "Let's pretend I've done it! OK?");
                client.endSession(intentMessage.getSessionId(), answers.get(Math.abs(ThreadLocalRandom.current().nextInt()) % answers.size()));
                return null;
            }
        });
        client.setOnListeningStateChangedListener(new Function1<Boolean, Unit>() {

            @Override
            public Unit invoke(Boolean isListening) {
                Log.d(TAG, "asr listening state: " + isListening);
                // Do you magic here :D
                return null;
            }
        });
        client.setOnSessionStartedListener(new Function1<SessionStartedMessage, Unit>() {

            @Override
            public Unit invoke(SessionStartedMessage sessionStartedMessage) {
                Log.d(TAG, "dialogue session started: " + sessionStartedMessage);
                return null;
            }
        });
        client.setOnSessionQueuedListener(new Function1<SessionQueuedMessage, Unit>() {

            @Override
            public Unit invoke(SessionQueuedMessage sessionQueuedMessage) {
                Log.d(TAG, "dialogue session queued: " + sessionQueuedMessage);
                return null;
            }
        });
        client.setOnSessionEndedListener(new Function1<SessionEndedMessage, Unit>() {

            @Override
            public Unit invoke(SessionEndedMessage sessionEndedMessage) {
                Log.d(TAG, "dialogue session ended: " + sessionEndedMessage);
                return null;
            }
        });
        // This api is really for debugging purposes and you should not have features depending on its output
        // If you need us to expose more APIs please do ask !
        client.setOnSnipsWatchListener(new Function1<String, Unit>() {

            public Unit invoke(final String s) {
                runOnUiThread(new Runnable() {

                    public void run() {
                        // We enabled html logs in the builder, hence the fromHtml. If you only log to the console,
                        // or don't want colors to be displayed, do not enable the option
                        ((EditText) findViewById(R.id.text)).append(Html.fromHtml(s + "<br />"));
                        findViewById(R.id.scrollView).post(new Runnable() {

                            @Override
                            public void run() {
                                ((ScrollView) findViewById(R.id.scrollView)).fullScroll(View.FOCUS_DOWN);
                            }
                        });
                    }
                });
                return null;
            }
        });
        // We enabled steaming in the builder, so we need to provide the platform an audio stream. If you don't want
        // to manage the audio stream do no enable the option, and the snips platform will grab the mic by itself
        startStreaming();
        client.connect(this.getApplicationContext());
    }
}
Also used : EditText(android.widget.EditText) SessionEndedMessage(ai.snips.hermes.SessionEndedMessage) ArrayList(java.util.ArrayList) IntentMessage(ai.snips.hermes.IntentMessage) SessionQueuedMessage(ai.snips.hermes.SessionQueuedMessage) Unit(kotlin.Unit) View(android.view.View) ScrollView(android.widget.ScrollView) Button(android.widget.Button) OnClickListener(android.view.View.OnClickListener) SessionStartedMessage(ai.snips.hermes.SessionStartedMessage) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File)

Aggregations

IntentMessage (ai.snips.hermes.IntentMessage)1 SessionEndedMessage (ai.snips.hermes.SessionEndedMessage)1 SessionQueuedMessage (ai.snips.hermes.SessionQueuedMessage)1 SessionStartedMessage (ai.snips.hermes.SessionStartedMessage)1 View (android.view.View)1 OnClickListener (android.view.View.OnClickListener)1 Button (android.widget.Button)1 EditText (android.widget.EditText)1 ScrollView (android.widget.ScrollView)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Unit (kotlin.Unit)1