# Amazon Connect ChatJS
## Table of contents
- [About](#about)
- [Getting Started](#getting-started)
- [A note about the AWS-SDK and ChatJS](#a-note-about-the-aws-sdk-and-chatjs)
- [Usage](#usage)
- [Building](#building)
- [React Native Support](#react-native-support)
- [API](#api)
- [`connect.ChatSession` API](#connectchatsession-api)
- [ChatSession API](#chatsession-api)
- [Amazon Connect Participant Service API wrappers](#amazon-connect-participant-service-api-wrappers)
- [Events](#events)
- [Client side metric](#client-side-metric)
- [Other](#other)
## About
The Amazon Connect Chat javascript library (ChatJS) gives you the power to build your own chat widget to customize the chat experience. This can be used for both the agent user interface, in conjunction with [Amazon Connect Streams](https://github.com/aws/amazon-connect-streams), and for the customer chat interface.
There is a [Chat UI reference implementation](https://github.com/amazon-connect/amazon-connect-chat-ui-examples) here. This will help you deploy an API Gateway and Lambda function for initiating chat from your webpage. From there you can use the ChatJS library to build a custom widget.
### Learn More
To learn more about Amazon Connect and its capabilities, please check out
the [Amazon Connect User Guide](https://docs.aws.amazon.com/connect/latest/userguide/).
## Getting Started
### A note about the AWS-SDK and ChatJS
The AWS-SDK is, by default, included in ChatJS as a "baked-in" dependency. You can view it at `./client/aws-sdk-connectparticipant.js`. In `./client/client.js` we import `ConnectParticipant` from this file. This file and import can be removed while using the AWS SDK imported through a script in the page file of your application, assuming that version of the AWS SDK has the `ConnectParticipant` service included.
Incidentally, Amazon Connect Streams also contains a "baked-in" AWS SDK. This SDK cannot be removed, as it contains unreleased APIs that will not be available in the SDK you include as a script in the page file.
Therefore, there are several occasions where implementations can run into AWS SDK issues.
#### **Scenario 1:** Streams and ChatJS are used. You are not importing the AWS SDK
Ensure you import ChatJS after Streams.
#### **Scenario 2:** Streams and ChatJS are used. You are importing the AWS SDK
Import Streams, then ChatJS, then the SDK.
Ensure that your AWS SDK includes the ConnectParticipant Service (it is relatively new, so make sure you have an up-to-date AWS SDK version [^2.597.0]).
#### **Scenario 3:** ChatJS only, no AWS SDK import
No need to worry here, this will always work.
#### **Scenario 4:** ChatJS only, with AWS SDK import
Import ChatJS before the AWS SDK, and ensure the AWS SDK version you are using contains the ConnectParticipant Service.
#### A note for Scenarios 2 and 4
When using the SDK and ChatJS, you may remove the SDK from ChatJS to ensure lack of import conflicts. However, this should not be relevant if the order in which you are importing these libraries is the order reflected above.
### Usage
#### Using ChatJS from npm
`npm install amazon-connect-chatjs`
#### Using ChatJS from CDN Link
`amazon-connect-chat.js` bundle file is also available over a CDN.
```html
```
#### Importing using npm and ES6
`import "amazon-connect-chatjs"`
Note: this will apply the global `connect` variable to your current scope.
#### TypeScript Support
`amazon-connect-chatjs` is compatible with TypeScript. You'll need to use version `3.0.1` or higher:
```ts
import "amazon-connect-streams";
connect.ChatSession.create({ /* ... */ });
```
#### Using ChatJS from Github
```sh
git clone https://github.com/amazon-connect/amazon-connect-chatjs
```
### Building
1. Install latest LTS version of [NodeJS](https://nodejs.org)
2. Checkout this package into workspace and navigate to root folder
3. `npm install`
4. To build (non-minified):
1. `npm run devo` for a non-minified build.
2. Find build artifacts in **dist** directory.
5. To build (minified):
1. `npm run release` for a minified build.
2. Find build artifacts in **dist** directory.
6. To run unit tests:
1. `npm run test`
7. To clean node_modules:
1. `npm run clean`
8. To make webpack watch all files:
1. `npm run watch`
Find build artifacts in **dist** directory - This will generate a file called `amazon-connect-chat.js` - this is the full Connect ChatJS API which you will want to include in your page.
## React Native Support
Additional configuration is required to support ChatJS in React Native applications. Use `amazon-connect-chatjs@^1.5.0` and follow the documenation: [ReactNativeSupport.md](./.github/docs/ReactNativeSupport.md)
A demo application implementing basic ChatJS functionality is also available in the ui-examples repository: [connectReactNativeChat](https://github.com/amazon-connect/amazon-connect-chat-ui-examples/tree/master/connectReactNativeChat)
## API
### `connect.ChatSession` API
This is the main entry point to `amazon-connect-chatjs`.
All your interactions with the library start here.
#### `connect.ChatSession.setGlobalConfig()`
```js
connect.ChatSession.setGlobalConfig({
loggerConfig: { // optional, the logging configuration. If omitted, no logging occurs
// You can provide your own logger here, otherwise this property is optional
customizedLogger: {
debug: (...msg) => console.debug(...msg), // REQUIRED, can be any function
info: (...msg) => console.info(...msg), // REQUIRED, can be any function
warn: (...msg) => console.warn(...msg), // REQUIRED, can be any function
error: (...msg) => console.error(...msg) // REQUIRED, can be any function
},
// There are five levels available - DEBUG, INFO, WARN, ERROR, ADVANCED_LOG. Default is INFO
level: connect.LogLevel.INFO,
// Choose if you want to use the default logger
useDefaultLogger: true
},
region: "us-east-1", // optional, defaults to: "us-west-2"
//Control switch for enabling/disabling message-receipts (Read/Delivered) for messages
//message receipts use sendEvent API for sending Read/Delivered events https://docs.aws.amazon.com/connect-participant/latest/APIReference/API_SendEvent.html
features: {
messageReceipts: {
shouldSendMessageReceipts: true, // DEFAULT: true, set to false to disable Read/Delivered receipts
throttleTime: 5000 //default throttle time - time to wait before sending Read/Delivered receipt.
}
}
});
```
Set the global configuration to use. If this method is not called, the defaults of `loggerConfig` and `region` are used.
This method should be called before `connect.ChatSession.create()`.
Customizing `loggerConfig` for ChatJS:
- If you don't want to use any logger, you can skip this field.
- There are five log levels available - DEBUG, INFO, WARN, ERROR, ADVANCED_LOG.
- If you want to use your own logger, you can add them into `customizedLogger`, and add `customizedLogger` object as the value of `loggerConfig.customizedLogger`, then set the lowest logger level. `globalConfig.loggerConfig.useDefaultLogger` is not required.
- If you want to use the default logger provided by ChatJS, you can set the logger level, and set `useDefaultLogger` to true. `loggerConfig.customizedLogger` is not required.
- If you not only provide your own logger, but also set `useDefaultLogger` to true, your own logger will be overwritten by the default logger.
- `amazon-connect-chatjs/src/log.js` - has the logic to select LogLevel. Default value is INFO - which cause all logs with higher priority than INFO to be logged. eg: by default info, warn, error and advancedLog messages will be logged.
- Priority of logs:
10: "DEBUG"
20: "INFO"
30: "WARN"
40: "ERROR"
50: "ADVANCED_LOG"
#### `connect.ChatSession.create()`
```js
const customerChatSession = connect.ChatSession.create({
chatDetails: { // REQUIRED
contactId: "...", // REQUIRED
participantId: "...", // REQUIRED
participantToken: "...", // REQUIRED
},
options: { // optional
region: "us-east-1", // optional, defaults to `region` set in `connect.ChatSession.setGlobalConfig()`
},
type: "CUSTOMER", // REQUIRED
});
```
Creates an instance of `AgentChatSession` or `CustomerChatSession`, depending on the specified `type`.
If you're creating a `CustomerChatSession`, the `chatDetails` field should be populated with the response of the [StartChatContact](https://docs.aws.amazon.com/connect/latest/APIReference/API_StartChatContact.html) API.
If you're creating an `AgentChatSession`, you must also include [`amazon-connect-streams`](https://github.com/amazon-connect/amazon-connect-streams). For example:
```js
// order is important, alternatively use