# botframework-webchat-adapter-azure-communication-chat **Repository Path**: mirrors_microsoft/botframework-webchat-adapter-azure-communication-chat ## Basic Information - **Project Name**: botframework-webchat-adapter-azure-communication-chat - **Description**: WebChat Adapter to use Azure Communication Services Chat with WebChat - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-02-16 - **Last Updated**: 2026-01-24 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Introduction The Azure Communication Services WebChat Adapter is a project for connecting the Bot Framework WebChat Component to an Azure Communication Services Chat thread. It provides a way to translate DirectLine activities to ACS Chat messages and vice versa, allowing for a seamless integration between the two services. ## Creating an adapter ``` js createACSAdapter = ( token: string, id: string, threadId: string, environmentUrl: string, fileManager: IFileManager, pollingInterval: number, eventSubscriber: IErrorEventSubscriber, displayName?: string, chatClient?: ChatClient, logger?: ILogger, adapterOptions?: AdapterOptions ): SealedAdapter ``` **`token`** An ACS user access token **`id`** The ACS user's id **`threadId`** Id of the chat thread to join **`environmentUrl`** ACS resource endpoint **`fileManager`** IFileManager instance for file sharing **`pollingInterval`** Interval in milliseconds that the adapter will poll for messages, polling is only done when notifications are not enabled. The minimum value is 5 seconds. Default value is 30 seconds. **`eventSubscriber`** IErrorEventSubscriber instance to send error events to the caller. **`displayName`** User's displayname **`chatClient`** Chat Client **`logger`** Logger instance **`adapterOptions`** Adapter options, see [Feature Options](#feature-options) for more detail ## Example Application This project includes a sample application that demonstrates how to use the Azure Communication Services WebChat Adapter. The sample application is a simple web app that allows two users to chat with each other using the adapter. ### Before You Start The Azure Communication Services WebChat Adapter requires a `ResourceConnectionString` and optionally a `OneDriveToken` to use file sharing. Ensure that the test application has access via environment variables to the following: * `ResourceConnectionString`: The connection string for the Azure Communication Service resource. * `OneDriveToken`: The OneDrive token for file sharing, if you want to use file sharing. ## Getting Started 1. Ensure you have Node.js LTS installed on your machine. You can download them from [Node.js official website](https://nodejs.org/). 2. `npm install` 3. `npm run start` 4. Open in your browser 5. Copy & paste the address bar content to another tab to enable another chat client 6. The two clients can now chat to each other ## Project structure ``` txt src ├── egress │ └── createEgress[ActivityType]ActivityMiddleware.ts ├── ingress │ └── subscribe[Event].ts └── sdk index.html ``` `ingress` contains the event subscribe middleware which listens to incoming events, translates them to DirectLine Activities and dispatches those to Webchat `egress` contains the middleware which translates DirectLine activities to Azure Communication Services Chat calls, made to the backend service. `sdk` contains API wrappers for zincluding authentication `index.[*].html` is the entry to a sample application, run by `npm run start`. ## How it works Install VSCode mermaid extension to view this diagram in VSCode There are 2 token types: 1. User Token is what the web app uses to validate a login user 2. ACS Token is what ACS Adapter use to communicate with Azure Communication Service ## Handshake process ```mermaid sequenceDiagram participant ACS Token Service participant App Web Server participant Web App participant WebChat participant ACS Adapter participant ACS Chat SDK participant Azure Communication Service Web App-->>App Web Server: Send User Token for authentication and ask for an ACS token App Web Server -->> App Web Server: Validate the user using User Token App Web Server -->> ACS Token Service: Create an ACS token ACS Token Service -->> App Web Server: Return an ACS token App Web Server -->> App Web Server: Bind User Identity with ACS token App Web Server -->> Web App: Return an ACS token Web App -->> ACS Adapter: Use ACS token to create adapter Web App -->> WebChat: Render Webchat and bind with adapter ACS Adapter -->> ACS Chat SDK: Create ChatClient using ACS token ACS Chat SDK -->> Azure Communication Service: Network request to service ``` Notes: 1. All authentications depend on the original app authentication, and the application server is responsible for assigning a token after user validation (and binding the token with user info) 2. Every time the token service is asked to create a new user token, a new communications services user is created and token binding happens again 3. Thread creation could happen either in the backend service or in the adapter; it is a preference of the developer ## Message sending and receiving ```mermaid sequenceDiagram participant WebChat participant ACS Adapter participant ACS Chat SDK participant Azure Communication Service WebChat -->> WebChat: User sends messages WebChat -->> ACS Adapter: Pass message as an Activity to ACS Adapter ACS Adapter -->> ACS Chat SDK: Call chatClient.sendMessage(message) ACS Chat SDK -->> Azure Communication Service: Send post message network request Azure Communication Service -->> ACS Chat SDK: Receive message from network ACS Chat SDK -->> ACS Adapter: Trigger receive message event ACS Adapter -->> WebChat: Dispatch IncomingActivity ``` ## Details for waiting queue ```mermaid sequenceDiagram participant ACS Adapter participant ChatWidget participant Edge Server participant Azure Communication Service Edge Server -->> Azure Communication Service: Join Conversation (using ACS Chat SDK) Edge Server -->> Azure Communication Service: Create token and add customer into thread Edge Server -->> ChatWidget: token & threadId ChatWidget -->> ACS Adapter: token & threadId Edge Server -->> Azure Communication Service: Send queue information (using chatClient.sendMessage) Azure Communication Service -->> ACS Adapter: Dispatch queue messgae ACS Adapter -->> ChatWidget: Dispatch Activity with ChannelData Edge Server -->> Azure Communication Service: Agent ready, create token and join agent to thread Edge Server -->> Azure Communication Service: Agent ready, create token and join the agent to the thread Azure Communication Service -->> ACS Adapter: trigger threadUpdate ``` ## Details for idle status ```mermaid sequenceDiagram participant ACS Adapter participant ChatWidget participant Edge Server participant Azure Communication Service ACS Adapter -->> Edge Server: Send heartbeat ACS Adapter -->> Edge Server: Stop heartbeat Edge Server -->> Edge Server: no heartbeat received for 90s Edge Server -->> Azure Communication Service: kick user out of the thread Azure Communication Service -->> ACS Adapter: Notify thread update ACS Adapter -->> ChatWidget: User left the chat ``` ## Build and Test ## Build a dev test JavaScript file For developer testing, a JavaScript file can work without server logic - check `Before You Start` above for more details. 1. Run `npm run build:dev` 2. Use `webchat-adapter-dev.js` from the `dist` directory 3. Add `webchat-adapter-dev.js` as `