Assistant Lifecycle Events
This page describes how developers can register for some interesting life-cycle events from the Assistant
The Slang Assistant handles most of the heavy lifting of interacting with the end-users and notifies the app when there is some action to be performed by the app. But in some cases, apps may want to be notified of low-level events that the Assistant is aware of. For example, whenever a user clicks on the trigger (the microphone button) or when the user cancels the Slang surface.
Access to low-level Assistant events is available through the Assistant's Lifecycle Events API.
The app can register with the Assistant to be notified of all interesting life-cycle events via the
setLifeCycleObserver
methodJava
iOS
React Native
Flutter
Web
SlangRetailAssistant.setLifecycleObserver(observer);
import SlangRetailAssistant
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
SlangRetailAssistant.setLifecycleObserver(with: self)
}
}
extension ViewController: LifecycleObserver {
func onAssistantInitSuccess() {
}
func onAssistantInitFailure(with description: String) {
//description represents the reason for the failure
}
func onAssistantInvoked() {
}
func onAssistantClosed(with cancelled: Bool) {
//cancelled is a boolean that informs whether
//the assistant was closed because of a user cancellation.
}
func onAssistantLocaleChanged(for locale: Locale) {
//locale is a Locale object that represents the current language.
}
func onUtteranceDetected(for utterance: String?) {
//utterance is a string that represents the current utterance spoken
//by the user.
}
func onUnrecognisedUtterance(for utterance: String?) {
//utterance is a string that represents the current utterance spoken
//by the user that was unrecognized.
}
func onOnboardingSuccess() {
}
func onOnboardingFailure() {
}
}
SlangRetailAssistant.setLifecycleObserver({
onAssistantInitSuccess: () => {
console.log('onAssistantInitSuccess')
},
onAssistantInitFailure: (error) => {
console.error('onAssistantInitFailure ' + error)
},
onAssistantInvoked: () => {
console.log('onAssistantInvoked')
},
onAssistantClosed: (isCancelled) => {
console.log('onAssistantClosed ' + isCancelled)
},
onAssistantLocaleChanged: (locale) => {
console.log('onAssistantLocaleChanged ' + JSON.stringify(locale, null, '\t'))
},
onUnrecognisedUtterance: (utterance) => {
console.log('onUnrecognisedUtterance ' + utterance)
},
onUtteranceDetected: (utterance) => {
console.log('onUtteranceDetected ' + utterance)
},
onOnboardingSuccess: () => {
console.log('onOnboardingSuccess')
},
onOnboardingFailure: () => {
console.log('onOnboardingFailure')
},
onMicPermissionDenied: () => {
console.log('onMicPermissionDenied')
},
onMicPermissionGranted: () => {
console.log('onMicPermissionGranted');
},
onCoachmarkAction: coachmarkInfo => {
console.log(
'onCoachmarkAction ' + JSON.stringify(coachmarkInfo, null, '\t'),
)
}
});
SlangRetailAssistant.setLifecycleObserver(observer);
As part of the Lifecycle Events API, an observer will be notified of the following events:
onAssistantInitSuccess
- Called as soon as the Assistant has initialized successfully and is ready to serve the app
onAssistantInitFailure
- Called when the Assistant failed to initialize successfully. The reason is passed as a parameter to this callback
onAssistantInvoked
- Called whenever the Assistant has been launched (this can be either as a result of the user clicking on the trigger or the app invoking the Assistant via the
startConversation
API)
onAssistantClosed
- Called whenever the Assistant has been dismissed. A boolean parameter
isCanceled
is passed to indicate whether this happened because the user canceled the session or if the Assistant was done with its job
onAssistantLocaleChanged
- Called whenever the user changes the locale of the Assistant. A locale dictionary parameter is passed to indicate the current locale.
locale
is a dictionary that contains the following fields :country
: field that represents the country in 2 characters. Example: "IN", "US"language
: field that represents the locale in 2 characters. Example: "en", "hi", "ta"
onUnrecognisedUtterance
- Called whenever the Assistant is not able to understand what the user spoke. The utterance that the user spoke is passed as a parameter.
onUtteranceDetected
- Called whenever the Assistant has detected an utterance that was spoken by the user. The utterance that the user spoke is passed as a parameter.
onOnboardingSuccess
- Called whenever the Assistant has completed the entire onboarding process for the given user.
onOnboardingFailure
- Called whenever the Assistant onboarding process has been canceled by the user.
onMicPermissionGranted
- Called whenever the microphone permission that is required by the assistant has been granted by the user.
onMicPermissionDenied
- Called whenever the microphone permission that is required by the assistant has been denied by the user.
onCoachmarkAction
- Called whenever the coachmark UI that is provided by the assistant has been interacted with by the user. It additionally provides an
CoachmarkInfo
object that describes the action. CoachmarkInfo
is a dictionary that contains the following fields :coachmarkType
:"ONBOARDING"
,"USER_HINT"
,"LOCALE_CHANGE_HINT"
coachmarkAction
:"VIEW"
,"CANCEL"
,"CLICK",
"UNKNOWN"
Last modified 29d ago