The options
will be parsed and validated against types.Settings.
The services
should be untouched unless you want to explicitly pass custom services, e.g.
for testing reasons.
Example usage:
const askBotService = new AskBotService({ botID: 1, token: 'token' });
Registration function for listening to certain events. Listeners can be added for multiple events. For event details, see types.Event.
Example usage:
// Single event
botService.subscribeTo('userMessage', message => console.log('User message:', message));
// Multiple events
botService.subscribeTo(['botMessage', 'userMessage'], message => console.log('User message:', message));
List of available locale records.
The API Base url that was defined during initialization.
Contains the available commands.
Calls AskBotService.reset
Returns a date time format function.
Returns a function to format a date distance to now. Uses https://date-fns.org/v4.1.0/docs/formatDistanceToNow under the hood
Contains the currently selected language.
Returns the last message in the message history, or undefined
if there is none.
Returns whether the last message in the history has an error.
Holds the complete message history of the current session.
Contains the instance settings that were passed on initialization. See types.Settings.
Creates a settings key using the state identifier. This can be used if some host application wants to store settings along the service settings, using the same state identifier schema.
Example usage:
const settingsKey = botService.createSettingsKey('my-key');
localStorage.setItem(settingsKey, 'my-value');
Initializes the chatbot if not already done. If the message history is empty, it posts an empty chat message to the backend to obtain an initial greeting.
At the function end, fires types.InitializedEvent. Example usage:
botService.init();
Posts a chat message to the backend and waits for the response.
If args.state
is not set, the function sets it to the state of the last message
in the message history.
If the message starts with a /
, it will be treated as a command (see AskBotService.commands), and fire the following event:
Otherwise, the underlying ApiService fires events in the following order:
addToHistory
is true
true
) before the requestfalse
) after the requestIf any error occurs, the following event is fired:
Example usage:
botService.postChatMessage({ message: 'Hello Bot' });
Optional
message?: stringActual chat message
Optional
state?: stringInternal message identifier
Resets the chat history and requests a welcome message.
Example usage:
botService.reset();
Re-sends the last user message, if any.
Example usage:
botService.retryLastMessage();
Sets the language for the chatbot. See AskBotService#availableLanguages. The underlying LocaleService fires a types.LocaleChangedEvent (payload: new language) after the language was set.
Example usage:
botService.setLanguage('de');
Optional
language: LanguagesCuts the chat history off one item before a given index. If the new last message is a user message, it will be re-sent.
Example usage:
botService.setStateToPreviousIndex(2);
The
AskBotService
provides functionality for interaction with an ASK Backend. It's contains all communication and state logic that is used by our<ask-chat>
and<ask-button>
components.For basic usage examples, see the index file.