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.

Constructors

  • 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' });
    

    Parameters

    Returns AskBotService

Properties

subscribeTo: <
    T extends
        | "loading"
        | "botMessage"
        | "initialized"
        | "loadingError"
        | "localeChanged"
        | "messageHistoryChanged"
        | "streamingError"
        | "streamingReadyState"
        | "streamingMessage"
        | "userCommand"
        | "userMessage"
        | (
            | "loading"
            | "botMessage"
            | "initialized"
            | "loadingError"
            | "localeChanged"
            | "messageHistoryChanged"
            | "streamingError"
            | "streamingReadyState"
            | "streamingMessage"
            | "userCommand"
            | "userMessage"
        )[],
>(
    eventType: T,
    callback: (
        _: T extends (
            | "loading"
            | "botMessage"
            | "initialized"
            | "loadingError"
            | "localeChanged"
            | "messageHistoryChanged"
            | "streamingError"
            | "streamingReadyState"
            | "streamingMessage"
            | "userCommand"
            | "userMessage"
        )[]
            ? Event
            : | Extract<BotMessageEvent, { type: T }>
            | Extract<InitializedEvent, { type: T }>
            | Extract<LoadingErrorEvent, { type: T }>
            | Extract<LoadingEvent, { type: T }>
            | Extract<LocaleChangedEvent, { type: T }>
            | Extract<MessageHistoryChangedEvent, { type: T }>
            | Extract<StreamingErrorEvent, { type: T }>
            | Extract<StreamingReadyStateEvent, { type: T }>
            | Extract<StreamingMessageEvent, { type: T }>
            | Extract<UserCommandEvent, { type: T }>
            | Extract<UserMessageEvent, { type: T }>,
    ) => void,
) => void = ...

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));

Accessors

  • get baseURL(): string

    The API Base url that was defined during initialization.

    Returns string

  • get commands(): { clear: () => Promise<void> }

    Contains the available commands.

    Returns { clear: () => Promise<void> }

  • get lastMessage(): Message

    Returns the last message in the message history, or undefined if there is none.

    Returns Message

  • get lastMessageHasError(): boolean

    Returns whether the last message in the history has an error.

    Returns boolean

  • get messageHistory(): Message[]

    Holds the complete message history of the current session.

    Returns Message[]

  • get settings(): {
        apiEndpoint?: string;
        botID?: number;
        enableDebug?: boolean;
        headers?: Record<string, string>;
        language?: Languages;
        stateIdentifier: string;
        streamMessages?: boolean;
        token?: string;
    }

    Contains the instance settings that were passed on initialization. See types.Settings.

    Returns {
        apiEndpoint?: string;
        botID?: number;
        enableDebug?: boolean;
        headers?: Record<string, string>;
        language?: Languages;
        stateIdentifier: string;
        streamMessages?: boolean;
        token?: string;
    }

Methods

  • 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');

    Parameters

    • key: string

    Returns string

  • 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();
    

    Returns Promise<void>

  • 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:

    If any error occurs, the following event is fired:

    Example usage:

    botService.postChatMessage({ message: 'Hello Bot' });
    

    Parameters

    • args: { message?: string; state?: string } = {}
      • Optionalmessage?: string

        Actual chat message

      • Optionalstate?: string

        Internal message identifier

    • addToHistory: boolean = true

    Returns Promise<void>

  • Resets the chat history and requests a welcome message.

    Example usage:

    botService.reset();
    

    Returns Promise<void>

  • Re-sends the last user message, if any.

    Example usage:

    botService.retryLastMessage();
    

    Returns Promise<void>

  • Cuts 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);
    

    Parameters

    • index: number

    Returns Promise<void>