Search…
Code Integration - Basic Steps
Integrating the Slang Voice Assistant with your app
By now you must have configured and published your Assistant via the Slang Console. Congratulations! :) If you have not already done that, you can do so by following the instructions here.
While the overall idea is similar across platforms, the specific steps involved vary slightly based on the platform on which your app is built. Supported platforms are:
    Android Native
    React Native for Android
    Web (JS)
Let's start coding!
For testing, we recommend using a physical Android device instead of an emulator because most emulators don't work well with microphones.

1. Configure the build system

The first step is to update the app's build system to include Slang's Travel Assistant SDK.
Android Native
React Native
Web

Add the Slang dependency to your gradle files

Add the path to the Slang maven repository to your top-level gradle file
1
# Add this to your top level gradle file
2
3
allprojects {
4
repositories {
5
6
maven { url "http://maven.slanglabs.in:8080/artifactory/gradle-release" }
7
}
8
}
Copied!
Add the Slang Travel Assistant dependency to your app's gradle file
1
# Add this to your app's gradle file
2
3
dependencies {
4
5
implementation 'in.slanglabs.assistants:slang-travel-assistant:4.0.17'
6
}
Copied!

N/A

N/A

2. Code integration

2.1 Initialization

The next step is to initialize the SDK with the keys you obtained after creating the Assistant in the Slang console.
Android Native
React Native
Web
The recommendation is to perform the initialization in the onCreate method of the Application class. If the app does not use an Application class, the next best place would be the onCreate method of the primary Activity class.
1
// Your application class
2
3
protected void onCreate(Bundle savedInstance) {
4
...
5
AssistantConfiguration configuration = new AssistantConfiguration.Builder()
6
.setAPIKey(<API Key>)
7
.setAssistantId(<AssistantId>)
8
.setEnvironment(STAGING) // Change this to PRODUCTION once you've published the Assistant to production environment
9
.build();
10
SlangTravelAssistant.initialize(this, configuration);
11
}
Copied!
This should ideally be done in the componentDidMount of your main app component
1
import SlangRetailAssistant from '@slanglabs/react-native-slang-retail-assistant';
2
3
SlangRetailAssistant.initialize({
4
requestedLocales: ['en-IN', 'hi-IN'], // The languages to enable
5
assistantId: '<assistant id>', // The Assistant ID from the console
6
apiKey: '<API Key>', // The API key from the console
7
})
Copied!
1
import SlangRetailAssistant from '@slanglabs/slang-retail-assistant';
2
3
SlangRetailAssistant.initialize({
4
requestedLocales: ['en-IN', 'hi-IN'], // The languages to enable
5
assistantID: '<assistant id>', // The Assistant ID from the console
6
apiKey: '<API Key>', // The API key from the console
7
})
Copied!

2.2 Show the Trigger (microphone icon)

Once the Assistant is initialized, the next step is to show the microphone UI element (what we call the Trigger) that the app's users can click on to invoke the Assistant and speak to it.
Android Native
React Native
Web
Add the below line to the onResume method of the Activities where you want the Assistant to be enabled.
1
protected void onResume(Bundle savedInstance) {
2
...
3
SlangRetailAssistant.getUI().showTrigger(this); // There is a corresponding hideTrigger too if needed
4
}
Copied!
One can call "show" and "hide" methods as required to control the visibility of the Assistant
1
SlangRetailAssistant.ui.showTrigger(); // There is a corresponding hideTrigger too if needed
Copied!
One can call "show" and "hide" methods as required to control the visibility of the Assistant
1
SlangRetailAssistant.ui.show(); // There is a corresponding hide too if needed
Copied!
The trigger is sticky, which means that it will show up on all Activities after it is made visible. To prevent the trigger from showing up on specific activities, you will need to call: SlangRetailAssistant.getUI().hideTrigger(this)

2.3 Implement Actions

Refresher: A UserJourney represents a path that a user may take to reach their goal when using a web or mobile app. See Voice Assistant Concepts for details.
Last but not the least, the app needs to implement the Actions associated with the various User Journeys supported by the Assistant. This can be done as shown below
Android Native
React Native
Web
1
SlangRetailAssistant.setAction(new SlangRetailAssistant.Action() {
2
@Override
3
public SearchUserJourney.AppState onSearch(SearchInfo searchInfo, SearchUserJourney searchJourney) {
4
// Handle search requests
5
// …
6
searchJourney.setSuccess(); //Setting app state condition
7
return SearchUserJourney.AppState.SEARCH_RESULTS; //Returning app state.
8
}
9
10
@Override
11
public NavigationUserJourney.AppState onNavigation(
12
NavigationInfo navigationInfo,
13
NavigationUserJourney navigationUserJourney
14
) {
15
// Handle navigation requests
16
// …
17
navigationUserJourney.setSuccess();
18
return NavigationUserJourney.AppState.NAVIGATION;
19
}
20
21
@Override
22
public void onAssistantError(final AssistantError error) {
23
// Handle errors that might have occurred during the Assistant lifecycle
24
25
// Error codes available
26
// FATAL_ERROR, SYSTEM_ERROR, ASSISTANT_DISABLED, INVALID_CREDENTIALS,
27
}
28
}
Copied!
1
N/A
Copied!
1
N/A
Copied!
The following user journeys are currently supported by the Slang Travel Assistant:
    Voice Search
    Voice Navigation
The Action Handler interface has an explicit callback for each of the supported user journeys. Whenever the Assistant detects the user journey the user is interested in (based on what they spoke), it invokes the callback associated with that user journey.
When these callbacks are invoked, the Assistant also passes the parametric data corresponding to the user journey that the Assistant was able to gather. The app is then expected to:
    1.
    Consume the parametric data as needed
    2.
    Optionally launch appropriate UI actions
    3.
    Set appropriate conditions in the Assistant based on the app's internal state
    4.
    Return the AppState that the app transitioned to

2.4 Return the AppState

Refresher: An AppState typically corresponds to a screen that the app transitioned to based on user input. See Voice Assistant Concepts for details.
An AppState indicates which state the app transitioned to, based on the user-journey and parametric data that was passed to the app. The list ofAppStatesthat are supported depends on the user journey.
Android Native
React Native
Web
1
public SearchUserJourney.AppState onSearch(SearchInfo searchInfo, SearchUserJourney searchJourney) {
2
// Handle the Search requests
3
// ...
4
return SearchUserJourney.AppState.SEARCH_RESULTS;
5
}
Copied!
1
onSearch: async (searchInfo, searchUserJourney) => {
2
// Handle the search request
3
// ...
4
return SearchUserJourney.AppState.SEARCH_RESULTS;
5
}
Copied!
1
onSearch: async (searchInfo, searchUserJourney) => {
2
// Handle the search request
3
// ...
4
return SearchUserJourney.AppStates.SEARCH_RESULTS;
5
}
Copied!

2.4.1 Set AppState condition

AppState conditions represent more detailed states of the app within a particular app state. For example, when performing a search, the search might have failed or the items might be out of stock. The app can useAppStateconditions to indicate to the Assistant the correct condition. The condition controls the message that the Assistant speaks up after the call-back returns.
Android Native
React Native
Web
1
public SearchUserJourney.AppState onSearch(SearchInfo searchInfo, SearchUserJourney searchJourney) {
2
// Handle the Search requests
3
// ...
4
5
searchUserJourney.setSearchSuccess(); // Set the condition
6
return SearchUserJourney.AppState.SEARCH_RESULTS;
7
}
Copied!
1
onSearch: async (searchInfo, searchUserJourney) => {
2
// Handle the search request
3
// ...
4
5
searchUserJourney.setSearchSuccess();
6
return SearchUserJourney.AppState.SEARCH_RESULTS;
7
}
Copied!
1
onSearch: async (searchInfo, searchUserJourney) => {
2
// Handle the search request
3
// ...
4
5
searchUserJourney.setSuccess();
6
return SearchUserJourney.AppState.SEARCH_RESULTS;
7
}
Copied!

2.4.2 Assistant Prompts

Based on the AppState returned and the conditions that were set, the Assistant will speak out an appropriate message to the user.
The prompts spoken by the Assistant are customizable. Refer to the Customizing the Assistant section, if you're interested in customization.
That's it! These are the basic set of steps required to add Slang's In-App Voice Assistant into your app.
Beyond this integration, Slang Voice Assistants provide a lot more power and flexibility to cater to the more advanced needs of apps. Please refer to the Advanced Concepts section for more details.

Last modified 25d ago