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 and also maybe customized it as required. 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
  • Flutter 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 Retail Assistant SDK.
Android Native
React Native
Flutter
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 Retails 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-retail-assistant:5.0.5'
6
}
Copied!

Install the Slang Retail Assistant package

The next step is to install the required packages inside your code repo

yarn setup

If you use yarn for install packages, run the below command
1
yarn add @slanglabs/slang-conva-react-native-retail-assistant
Copied!
The default package is built and packaged with androidx framework, if your app does not support androidx, please use the below
1
yarn add @slanglabs/[email protected]
Copied!

npm setup

If you use npm for managing your packages, run the below command
1
npm install @slanglabs/slang-conva-react-native-retail-assistant --save
Copied!
Because Slang uses native libraries, you need to link the package to your codebase to run the automatic linking steps
1
react-native link @slanglabs/slang-conva-react-native-retail-assistant
Copied!
Finally, add the path to the Slang maven repository (to download native library dependencies) 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!

Install the Slang Retail Assistant package

Run the below command to install the required packages inside your code repo.
1
$ flutter pub add slang_retail_assistant
Copied!
Once done, run the command 'dart pub get' and ensure Slang assistant is added to the dependencies.
1
2
dependencies:
3
slang_retail_assistant: ^4.0.7
Copied!

Next is to import Slang Retail Assistant in your dart code.

1
import 'package:slang_retail_assistant/slang_retail_assistant.dart';
Copied!

Install the Slang Retail Assistant package

The next step is to install the required packages inside your code repo

yarn setup

If you use yarn for install packages, run the below command
1
$ yarn add @slanglabs/[email protected]
Copied!

npm setup

If you use npm for managing your packages, run the below command
1
$ npm install @slanglabs/[email protected] --save
Copied!

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
Flutter
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
SlangRetailAssistant.initialize(this, configuration);
11
}
Copied!
This should ideally be done in the componentDidMount of your main app component
1
import SlangRetailAssistant from '@slanglabs/slang-conva-react-native-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!
This should ideally be done inside the main method.
1
import 'package:slang_retail_assistant/slang_retail_assistant.dart';
2
3
var assistantConfig = new AssistantConfiguration()
4
..assistantId = "<AssistantId>"
5
..apiKey = "<APIKey>"
6
..requestedLocales = ['en-IN', 'hi-IN'];
7
8
SlangRetailAssistant.initialize(assistantConfig);
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
Flutter
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!
Use "showTrigger" and "hideTrigger" APIs to control the visibility of the Assistant as shown below.
1
SlangRetailAssistant.getUI().showTrigger();
2
SlangRetailAssistant.getUI().hideTrigger();
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:
1
SlangRetailAssistant.getUI().hideTrigger(this)
Copied!

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.
Refresher: The Actions for the various User Journeys can also be specified directly in the console. Refer to the "Define Actions for various User Journeys" section for details
Now if the actions (basically the visual change that the app should do) corresponding to the various User Journeys have not been already defined in the console, the app needs to do that via code and implement the Actions associated with the various User Journeys supported by the Assistant. This can be done as shown below
Android Native
React Native
Flutter
Web
Android (Deprecated)
1
SlangRetailAssistant.setAppAction(new SlangRetailAssistant.AppAction() {
2
@Override
3
public SearchAppState onSearch(SearchInfo searchInfo, SearchUserJourney searchJourney) {
4
// Handle search requests
5
// ...
6
7
return new SearchResultsAppState(OrderViewAppState.SUCCESS);
8
}
9
10
public OrderManagementAppState onOrderManagement(OrderInfo orderInfo, OrderManagementUserJourney orderourney) {
11
// Handle order management requests
12
// ...
13
14
return new OrderViewAppState(OrderViewAppState.SUCCESS);
15
}
16
17
public OfferAppState onOfferManagement(OfferInfo offerInfo, OfferManagementUserJourney offerManagementUserJourney) {
18
// Handle offers requests
19
// ...
20
21
return new ViewOfferAppState(ViewOfferAppState.SUCCESS);
22
}
23
24
public CheckoutAppState onCheckOut(CheckoutInfo checkoutInfo, CheckoutUserJourney checkoutUserJourney) {
25
// Handle checkout requests
26
// ...
27
28
return new CheckoutCompleteAppState(CheckoutCompleteAppState.SUCCESS);
29
}
30
31
public NavigationAppState onNavigation(NavigationInfo navInfo, NavigationUserJourney navJourney) {
32
// Handle navigation requests
33
// ...
34
35
return new NavigationCompleteAppState(SUCCESS);
36
}
37
38
@Override
39
public void onAssistantError(final AssistantError error) {
40
// Handle errors that might have occurred during the Assistant lifecycle
41
42
// Error codes available
43
// FATAL_ERROR, SYSTEM_ERROR, ASSISTANT_DISABLED, INVALID_CREDENTIALS,
44
}
45
}
Copied!
1
const actionHandler = {
2
onSearch: (searchInfo, searchUserJourney) => {
3
// Handle the search request
4
// ...
5
6
searchUserJourney.setSuccess();
7
return SearchUserJourney.AppState.SEARCH_RESULTS;
8
},
9
10
onOrderManagement: (orderInfo, orderManagementUserJourney) => {
11
// Handle the order request
12
// ...
13
14
orderManagementUserJourney.setViewSuccess();
15
return OrderManagementUserJourney.AppState.VIEW_ORDER;
16
},
17
18
onNavigation: (navigationInfo, navigationUserJourney) => {
19
// Handle the navigation request
20
// ...
21
22
navigationUserJourney.setNavigationSuccess();
23
return NavigationUserJourney.AppState.NAVIGATION;
24
},
25
26
onAssistantError: errorCode => {
27
// Handle errors that might have happened during the processing of the
28
// Assistant
29
30
// Error codes available
31
// FATAL_ERROR, SYSTEM_ERROR, ASSISTANT_DISABLED, INVALID_CREDENTIALS,
32
},
33
};
34
35
SlangRetailAssistant.setAction(retailAssistantListener)
Copied!
1
//Set the action handler via the setAction method
2
class AppAction implements RetailAssistantAction {
3
4
@override
5
void initState() {
6
super.initState();
7
SlangRetailAssistant.setAction(this);
8
}
9
10
@override
11
void onAssistantError(Map<String, String> assistantError) {
12
// Handle errors that might have occurred during the Assistant lifecycle
13
}
14
15
@override
16
SearchAppState onSearch(SearchInfo searchInfo, SearchUserJourney searchUserJourney) {
17
// Handle search requests
18
// ...
19
return SearchUserJourney.AppState.SEARCH_RESULTS;
20
}
21
}
22
23
var action = new AppAction();
24
SlangRetailAssistant.setAction(action);
25
Copied!
1
const actionHandler = {
2
onSearch: (searchInfo, searchUserJourney) => {
3
// Handle the search request
4
// ...
5
6
searchUserJourney.setSuccess();
7
return SearchUserJourney.AppStates.SEARCH_RESULTS;
8
},
9
10
onManageOrder: (orderInfo, orderManagementUserJourney) => {
11
// Handle the order request
12
// ...
13
14
orderManagementUserJourney.setViewSuccess();
15
return OrderManagementUserJourney.AppStates.VIEW_ORDER;
16
},
17
18
onNavigation: (navigationInfo, navigationUserJourney) => {
19
// Handle the navigation request
20
// ...
21
22
navigationUserJourney.setNavigationSuccess();
23
return NavigationUserJourney.AppStates.NAVIGATION;
24
},
25
26
};
27
28
SlangRetailAssistant.setAction(retailAssistantListener)
Copied!
1
SlangRetailAssistant.setAction(new SlangRetailAssistant.Action() {
2
@Override
3
public SearchUserJourney.AppState onSearch(SearchInfo searchInfo, SearchUserJourney searchJourney) {
4
// Handle search requests
5
// ...
6
7
userJourney.setSuccess();
8
return SearchUserJourney.AppState.SEARCH_RESULTS;
9
}
10
11
public NavigationUserJourney.AppState onNavigation(NavigationInfo navInfo, NavigationUserJourney navJourney) {
12
// Handle navigation requests
13
// ...
14
15
userJourney.setNavigationSuccess();
16
return NavigationUserJourney.AppState.NAVIGATION;
17
}
18
19
public OrderManagementUserJourney.AppState onOrderManagement(OrderInfo orderInfo, OrderManagementUserJourney orderourney) {
20
// Handle order management requests
21
// ...
22
23
userJourney.setViewSuccess();
24
return OrderManagementUserJourney.AppState.VIEW_ORDER
25
}
26
27
@Override
28
public void onAssistantError(final AssistantError error) {
29
// Handle errors that might have occurred during the Assistant lifecycle
30
31
// Error codes available
32
// FATAL_ERROR, SYSTEM_ERROR, ASSISTANT_DISABLED, INVALID_CREDENTIALS,
33
}
34
}
Copied!
The following user journeys are currently supported by the Slang Retail Assistant:
  • Voice Search
  • Voice Order Management
  • Voice Offers
  • Voice Checkout
  • Voice Navigation
Backward compatibility note: Earlier offers and checkout were targets inside Navigation Journey. Now they have become their own full-blown user journeys
The Action Handler interface has an explicit callback for each of the supported user journeys. Whenever the Assistant detects the user's journey (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. 1.
    Consume the parametric data as needed
  2. 2.
    Optionally launch appropriate UI actions
  3. 3.
    Set appropriate conditions in the Assistant based on the app's internal state
  4. 4.
    Return the AppState that the app transitioned to

2.4 Return the AppState and Condition

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 ofAppStates that are supported depends on the user journey.
Conditions represent more detailed states of the app within a particular app state. For example, the search might have failed when performing the search or the items might be out of stock. The app can use Conditions to indicate the correct condition to the Assistant. The condition controls the message that the Assistant speaks up after the call-back returns.
Android Native
React Native
Flutter
Web
Android (Deprecated)
1
public SearchUserJourney.AppState onSearch(SearchInfo searchInfo, SearchUserJourney searchJourney) {
2
// Handle the Search requests
3
// ...
4
5
return new SearchResultsAppState(SUCCESS);
6
}
Copied!
1
onSearch: async (searchInfo, searchUserJourney) => {
2
// Handle the search request
3
// ...
4
searchUserJourney.setSearchSuccess();
5
return SearchUserJourney.AppState.SEARCH_RESULTS;
6
}
Copied!
1
@override
2
SearchAppState onSearch(SearchInfo searchInfo, SearchUserJourney searchUserJourney) {
3
// Handle search requests
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
searchUserJourney.setSuccess();
5
return SearchUserJourney.AppStates.SEARCH_RESULTS;
6
}
Copied!
1
public SearchUserJourney.AppState onSearch(SearchInfo searchInfo, SearchUserJourney searchJourney) {
2
// Handle the Search requests
3
// ...
4
searchUserJourney.setSearchSuccess(); // Set the condition
5
return SearchUserJourney.AppState.SEARCH_RESULTS;
6
}
Copied!

2.4.1 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 the apps. Please refer to the Advanced Concepts section for more details.

Last modified 8d ago