Web Deep Dive

Into the depths of the Web SDK

Advanced Actions

Asynchronous handling of intent actions

If the intentActionHandler method is asynchronous (such as a network call) then you should return a Promise. If the Promise resolves then Slang checks the return value. If the value is false then it considers the action to have failed. Alternatively if the Promise is rejected then also Slang considers the action to have failed. You can use async/await syntax to write intentActionHandlers in simpler way like:

const intentHandler = async (intent) => {
    switch(intent.name){
        case 'show_balance':
            await showBalance() // showBalance is an async function
            return true
        default:
            return false
    }
};

Customizing Prompts

Completion Prompts

When an intent is detected by Slang, it will call the SlangIntentActionHandler function that is registered with Slang. After the function is done with its handling, Slang considers the action to be successful except when it returns anything other than false (or if it returns a Promise that rejects).

If it returns false ( or if it returns a Promise that rejects ) then Slang will consider the action to have failed.

Slang will speak out the Affirmative Completion prompt associated with this intent if the action succeeds, else it will speak out the Negative Completion prompt.

By default it will use the Completion prompt configured in the Slang Console for that intent. The "action handler" can override the prompt if necessary.

            case "balance":
                // app specific logic to handle this intent
                if (handle_balance(intent)) {
                    // This will override the `Affirmative` Completion prompt of the intent
                    intent.completionStatement.overrideAffirmative("here is your balance");       
                    return true;
                } else {
                    // This will override the `Negative` Completion prompt prompt of the intent
                    intent.completionStatement.overrideAffirmative("Sorry, there was a problem getting your balance");       
                    return false;
                }                
                ...

Setting the Greeting and Clarification prompt

By default Slang will show a prompt on its surface when the Slang Trigger is pressed ("Hi! How may I help you"). Similarly when Slang does not understand what the user is saying, it will ask for the user to retry with a default prompt ("Sorry, I didn't understand that, could you please say it again?). These prompts can be overridden in the Slang Console.

Customizing the UI

Turning on/off Slang UI

Sometimes it might be required to turn off Slang (for example, when in the login page). To do this, you can use the "show/hide" APIs.

    //Hide the UI
    Slang.ui.hide()

    //Show the UI
    Slang.ui.show()

Show/Hide the Slang Trigger

If you just want to hide the trigger and not the entire UI (say you want to trigger Slang programmatically), you can use the following APIs.

    // Hide the Trigger
    Slang.ui.hideTrigger()

    // Show the Trigger
    Slang.ui.showTrigger()

Others

Programmatically Changing the locale of Slang.

If you want to change the locale without the user explicitly selecting a locale from the Slang ui (for example when the user changes global application locale).

    Slang.changeLocale("hi-IN")

Mute/Unmute Slang.

If you want to toggle whether Slang should be mute or not you can use the toggleMute API.

    Slang.toggleMute()

Note: Slang Starts unmuted

Improve Speech Recognition.

Hints tell Slang there is a higher probability of certain words occurring in this application. This helps improve the recognition quality of the end user's utterances for words that Slang's speech recognition might find it hard to recognize. Hints are a map of locale to an array of strings.

    const hints = {
        "en-IN": ["this","is","a","hint"],
        "hi-IN": ["नमस्ते","दुनिया"]
    }

    Slang.setASRHints(hints);

Start a conversation programmatically

If you want to programmatically start a conversation with a given statement instead of the welcome statement configured in the schema you can use the startConversation api.

    Slang.startConversation (startStatement, isSpoken )

isSpoken is a boolean flag denoting if Slang will speak out the statement.

You can even start a new conversation from inside a action handler (in which case Slang queues the new conversation and starts it as soon as the current cycle is completed)

Last updated