# twilio-webchat-react-app **Repository Path**: mirrors_twilio/twilio-webchat-react-app ## Basic Information - **Project Name**: twilio-webchat-react-app - **Description**: Twilio Webchat React App is an application that demonstrates a website chat widget built with Twilio's Conversations JS SDK, Twilio Paste Design library and Create React App. - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-06-16 - **Last Updated**: 2025-09-14 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Twilio Webchat React App _The Twilio Webchat React app_ is an open-source project built with Twilio's Conversations JS SDK, the Twilio Paste design library, and the Create React app. It provides an example of how to build a basic webchat widget, integrate it with the Flex platform, and use it as a communication channel between your website visitors and your Flex-powered contact center. Before deciding to use the Webchat React app, we recommend reviewing [Webchat 3.x.x](https://www.twilio.com/docs/flex/developer/conversations/webchat), our out-of-the-box webchat solution for Flex that features enhanced security capabilities. Webchat 3.x.x meets most customers’ standard webchat needs. If Webchat 3.x.x’s out-of-the-box functionality doesn't fully meet your requirements and you need extensive customization, consider building with the Webchat React app. However, note that Twilio does not provide support for your implementation of the Webchat React app. --- 1. [Getting started](#Getting-started) 1. [Setup](#Setup) 2. [Work locally](#Work-locally) 2. [Features](#Features) 3. [Project structure](#Project-structure) 1. [React App](#1-react-app) 2. [Local backend server](#2-local-backend-server) 4. [Working in production](#working-in-production) 5. [Browser support](#Browser-support) 6. [Accessibility](#Accessibility) 7. [FAQs](#faqs) --- # Getting Started ## Setup ### 1. Install Dependencies Run the following command ```shell yarn ``` ### 2. Populate Your .env File We provide a handy `bootstrap` script to set up the environment variables required, but you can alternatively copy the `.env.sample` file. ```shell yarn bootstrap \ accountSid=YOUR_ACCOUNT_SID \ authToken=YOUR_AUTH_TOKEN \ apiKey=YOUR_API_KEY_SID \ apiSecret=YOUR_API_SECRET \ addressSid=YOUR_ADDRESS_SID \ conversationsServiceSid=YOUR_CONVERSATIONS_SERVICE_SID ``` You can find your **Account Sid** and **Auth Token** on the main [Twilio Console page](https://console.twilio.com/). For more info on how to create an **API key** and an **API secret**, please check the [documentation](https://www.twilio.com/docs/glossary/what-is-an-api-key#how-can-i-create-api-keys). You can find your **Conversations Service Sid** on the [services page](https://console.twilio.com/us1/develop/conversations/manage/services?frameUrl=%2Fconsole%2Fconversations%2Fservices%3Fx-target-region%3Dus1). Make sure to pick the one linked to your Flex Account — usually it is named `Flex Chat Service` and it starts with `IS` For the Address Sid, click on the edit button of your address and the edit screen will contain Address Sid. Note this Sid starts with `IG`. The environment variables associated with enabling and configuring customer transcripts can be found in the `.env.sample` file and their use will be covered [here](#chat-transcripts). ## Working Locally ### 1. Start the Local Backend Server ```shell yarn server ``` Your server will be served at http://localhost:3001/. ### 2. Start the Local React App Server ```shell yarn start ``` Your app will be served at http://localhost:3000/. # Features ## Core Messaging Features Twilio Webchat React App is built entirely with Twilio Conversations SDK and provides UI for most of its features: - Typing indicator - Read receipt - Attachments - Unread messages This app also makes use of the v2 WebChats endpoint which creates a Conversation, an anonymous user, and configures the conversation as per the Address Sid. ## Ability to Resume Sessions Twilio Webchat React App will persist user session, if the user closes and reopens the tab. The customer will achieve it by potentially storing the JWT token in their user's local storage. This JWT token will expire after an amount of time. ## Connectivity Notification Twilio Webchat React App monitors user internet connectivity and will inform them with a notification if their connection has been lost. Once the connection has been re-established, the user again will be informed and the list of messages will be updated with any missed messages during the connectivity loss. This feature is built using [Conversations SDK ConnectionState](http://media.twiliocdn.com/sdk/js/conversations/releases/2.0.0/docs/modules.html#ConnectionState) events and [Twilio Paste alert component](https://paste.twilio.design/components/alert). See how to re-use Paste alert component to build custom notifications in our [How to guides](https://docs.google.com/document/d/1RWuvvZZWdV3AbuZNIC_IFyOxJaKFagdk3k-FT2VMyEM/edit#heading=h.467mbzcgq98p). ## Pre-engagement Form Twilio Webchat React App comes out-of-the-box with a pre-engagement form. The data gathered on submission will be passed by default to the `initWebchat` endpoint of your server. More info [here](#a-note-about-the-pre-engagement-form-data). ## Chat Transcripts Twilio Webchat React App can provide customers with chat transcripts, if enabled. Chat transcripts can be provided to a customer through a direct download or email, at the end of a chat session. All files attached within the chat will be also be provided if a transcript is requested. This feature is disabled by default, but can be enabled by following the steps below. ### Downloading Transcripts Customers can download chat transcripts as a plain text file. If attachments are present within the chat conversation, a zip file containing the plain text transcript file and the attached files will be downloaded. **Setup** Allowing customers to download transcripts requires no additional setup beyond adding the below entry to the `.env` file. ``` DOWNLOAD_TRANSCRIPT_ENABLED=true ``` ### Emailing Transcripts Customers can email chat transcripts to the email address provided in the pre-engagement form. The transcript will be provided within the body of the email and any associated files will be added as attachments to the email. Emails will be sent using the [SendGrid](https://sendgrid.com/) API. **Setup** 1. Add the following entry to the `.env` file. ``` EMAIL_TRANSCRIPT_ENABLED=true ``` 2. Create a [SendGrid](https://sendgrid.com/) account with a verified domain or email. This will be used to send the email transcripts to customers. 3. Add the SendGrid API key and verified email to the `.env` file as the values for `SENDGRID_API_KEY` and `FROM_EMAIL` respectively. The email subject and content can be customised in the configuration object, as described [here](#configuration). **Customisation** The email subject and HTML email content can be customised using the configuration object, as described [here](#configuration). # Project Structure Twilio Webchat React App is an open source repository that includes: 1. A React App 2. A local backend server ## 1. React App The **React app** is a newer version of the legacy [webchat widget](https://www.twilio.com/docs/flex/developer/messaging/webchat/setup). With this new app, you can clone and customize it to meet your needs. This App is built in React using Typescript, Twilio Paste and Twilio Conversations SDK. You can find the source files in the `src` folder. After being initialized, the widget renders a button in the bottom right part of the page that, once clicked, will show a customisable form for your customers to compile. Once submitted, the App will hit the `initWebchat` endpoint of your server with the form data and get back a token with a conversationSid. At that point, your customer will be able to send messages to your agents. ### Configuration This React app is open-sourced, so you can freely customise every aspect of it to suit your needs. However, to speed up some aspects of the customisations, we are exposing a configuration object on the init function. Here's an example of how to use this config object in your `index.html` template. ```javascript window.addEventListener("DOMContentLoaded", () => { Twilio.initWebchat({ serverUrl: "%YOUR_SERVER_URL%", theme: { isLight: true, overrides: { backgroundColors: { colorBackgroundBody: "#faebd7" // .. other Paste tokens } } }, fileAttachment: { enabled: true, maxFileSize: 16777216, // 16 MB acceptedExtensions: ["jpg", "jpeg", "png", "amr", "mp3", "mp4", "pdf"] }, transcript: { emailSubject: (agentNames) => { let subject = "Transcript of your chat"; if (agentNames.length > 0) { subject = subject.concat(` with ${agentNames[0]}`); agentNames.slice(1).forEach((name) => (subject = subject.concat(` and ${name}`))); } return subject; }, emailContent: (customerName, transcript) => { return `
Hello ${customerName},
Please see below your transcript, with any associated files attached, as requested.
${transcript}