Bot Framework Web Chat Documentation

repository·main·Indexed 23 days ago

https://github.com/microsoft/botframework-webchat

A highly customizable web-based client for the Bot Framework v4 SDK. This documentation covers integration via iframes, package bundles (Full, ES5, and Minimal), architecture layers (base, core, component, directlinespeech, embed), and localization workflows. It also includes details on the cldr-data-downloader tool and Vibe-Grep for repository housekeeping.

Tokens
124.6K
Snippets
267
Records
634
Agent score
82%

What's inside Bot Framework Web Chat

  1. Overview of Speech Services in Web Chat

    main

    Web Chat supports speech-to-text (STT) and text-to-speech (TTS) functionality through Azure Cognitive Services.

    Note: Direct Line Speech is the preferred method for providing speech functionality in Web Chat. This guide specifically covers integration using Azure Cognitive Services directly.

  2. Overview of Bot Framework Web Chat

    main

    Bot Framework Web Chat is a highly-customizable web-based client designed for the Bot Framework v4 SDK. It provides a user interface for interacting with conversational AI bots. It is part of the larger Microsoft Bot Framework ecosystem.

    Security Note: Web Chat supports Content Security Policy (CSP). Developers are encouraged to enable CSP to protect conversations and improve security.

  3. Explore Web Chat hosted samples

    main
    The samples directory contains hosted examples of Web Chat in action. These samples are designed to demonstrate various features of the library by connecting to MockBot, a specialized bot used for testing Web Chat capabilities. You can find the source code for these samples in the main repository under the samples folder, and the source code for the MockBot used in these examples is available in a separate repository.
  4. What is Direct Line Speech and when to use it

    main

    Direct Line Speech is a protocol designed for Voice Assistant scenarios, such as smart displays, automotive dashboards, or navigation systems that require low latency in Single-Page Applications (SPA) or Progressive Web Apps (PWA).

    Key characteristics:

    • Optimized for highly-customized UIs that may not show conversation transcripts.
    • Not recommended for traditional, transcript-based websites.
    • Requires modern browser media capabilities (does not support Internet Explorer 11).

    For specific requirements regarding Cognitive Services Speech Services, refer to the SPEECH.md documentation in the repository.

  5. What is polymiddleware and how to use it

    main

    Polymiddleware is Web Chat's unified middleware approach. It allows for deep, cascaded UI customization by enabling you to add, remove, replace, or decorate UI elements. Middleware is passed via a single polymiddleware prop to the ReactWebChat component and operates in a cascading sequence where upstream middleware can influence downstream rendering.

    To use it, create an array of middleware functions (often wrapped in specific creators like createActivityPolymiddleware) and pass them to the polymiddleware prop. Use useMemo to ensure the middleware array is stable across re-renders.

    function MyChatUI() {
      const polymiddleware = useMemo(
        () => [
          createActivityPolymiddleware(
            next => request =>
              request.activity.type === 'event'
                ? // Handle rendering of event activity though <EventActivity activity={request.activity}>
                  reactComponent(EventActivity, { activity: request.activity })
                : // Continue rendering.
                  next(request)
          ),
          createActivityPolymiddleware(...),
          createActivityPolymiddleware(...),
    
          // Handle rendering of error box.
          createErrorBoxPolymiddleware(...)
        ],
        []
      );
    
      return (
        <ReactWebChat
          polymiddleware={polymiddleware}
          // ... other props ...
        />
      );
    }
  6. How livestreaming handles packet loss and late joins

    main

    To ensure reliability during packet loss or when a client joins a conversation after a livestream has already started, interim activities are designed to contain overlapping content.

    Instead of sending incremental fragments, each interim activity provides enough content to allow the client to catch up to the current state of the livestream. This approach also allows the bot to backtrack or erase parts of a response if necessary.

  7. Clear conversation after idle time using custom store middleware

    main

    To automatically clear conversation data and start a new session after a period of inactivity, you can implement a custom store middleware. This pattern involves monitoring specific Web Chat actions to reset an idle timer.

    Implementation Logic

    1. Monitor Actions: Use a middleware function within createStore to listen for DIRECT_LINE/CONNECT_FULFILLED (when the connection is established) or WEB_CHAT/SUBMIT_SEND_BOX (when the user sends a message).
    2. Reset Timer: When these actions occur, update a state variable (e.g., resetAt) to a new timestamp (Date.now() + IDLE_TIMEOUT).
    3. Trigger Reset: When the idle timer expires, call setSession(false) to clear the current session and then re-initialize the session with a new Direct Line token and a new store instance to start a fresh conversation.

    Note: This approach is a proof of concept and should be implemented with production security considerations in mind.

    setSession({
      directLine: createDirectLine({ token }),
      key,
      store: createStore({}, () => next => action => {
        if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED' || action.type === 'WEB_CHAT/SUBMIT_SEND_BOX') {
          // Reset the timer when the connection established, or the user sends an activity
          setResetAt(Date.now() + IDLE_TIMEOUT);
        }
    
        return next(action);
      })
    });
  8. How capability discovery and updates work

    main

    Web Chat uses a dynamic discovery mechanism to sync configuration between the adapter and the UI:

    1. Initial fetch: Upon mounting, Web Chat checks the adapter for capability getter functions and retrieves their initial values.
    2. Event-driven updates: When the adapter dispatches a capabilitieschanged event, Web Chat re-fetches all capabilities.
    3. Optimized re-renders: Only components that use a useCapabilities hook with a selector targeting the changed capability will re-render.
  9. Understand Activity Keys vs Activity IDs

    main

    While Activity ID is a unique service-assigned ID, not every activity is guaranteed to have one. Web Chat uses Activity Keys as an alternative opaque string reference.

    Key characteristics:

    • Assigned when an activity first appears in Web Chat and remains constant until a restart.
    • Multiple activities can share the same key if they are revisions of each other (e.g., different versions of a livestreaming activity).

    Use the following hooks to navigate between these identifiers:

    • useGetActivitiesByKey
    • useGetActivityByKey
    • useGetKeyByActivity
    • useGetKeyByActivityId
  10. Control activity narration using webchat:fallback-text

    main

    To provide custom, accessible narration for message activities (especially when dealing with complex Markdown, HTML, or file attachments), use the webchat:fallback-text field within the channelData object of the activity.

    Web Chat follows this priority logic:

    1. If channelData['webchat:fallback-text'] is present and not empty: Web Chat uses this string for narration. This is the recommended way to ensure consistent results for complex content.
    2. If not present:
      • If textFormat is markdown, Web Chat attempts to strip Markdown syntax from the text field using a best-effort algorithm before narrating.
      • Otherwise, it narrates the text field as-is.

    In both cases, it then appends narration for every attachment rendered through the attachmentForScreenReader middleware.