Microsoft Bot Framework Web Chat
Embeddable web chat control for the Microsoft Bot Framework using the DirectLine API.
Used by the Bot Framework developer portal, Emulator, Web Chat channel, and Azure Bot Service
Web Chat is available both as a React component and as a self-contained control easily usable by any non-React website. Under the covers, Web Chat is built with TypeScript using Redux for state management and RxJS for wrangling async.
You can easily play with the most recent release using botchattest.
How to add Web Chat to your website
If you haven't already, start by registering your bot.
Now decide how you'd like to use Web Chat.
Easiest: In any website, IFRAME the standard Web Chat channel
Add a Web Chat channel to your registered bot, and paste in the supplied <iframe>
code, which points at a Web Chat instance hosted by Microsoft. That was easy, you're done! Please be aware that the version of its Web Chat instance may lag behind the latest release.
- Want more options, or to run the latest release, or a custom build? Read on.
Easy: In your non-React website, run Web Chat inline
Add a DirectLine (not Web Chat) channel, and generate a Direct Line Secret. Make sure to enable Direct Line 3.0.
Include botchat.css
and botchat.js
in your website, for example,
<!DOCTYPE html>
<html>
<head>
<link href="https://cdn.botframework.com/botframework-webchat/latest/botchat.css" rel="stylesheet" />
</head>
<body>
<div id="bot"/>
<script src="https://cdn.botframework.com/botframework-webchat/latest/botchat.js"></script>
<script>
BotChat.App({
directLine: { secret: direct_line_secret },
user: { id: 'userid' },
bot: { id: 'botid' },
resize: 'detect'
}, document.getElementById("bot"));
</script>
</body>
</html>
Starting from 0.12.0
, we no longer package polyfills in botchat.js
. If you are on an older browser, you can use the bundle botchat-es5.js
, which include polyfills for browsers which support up to ES5.
/samples/standalone
has a slightly more sophisticated version of this code, great for testing- You can reference to latest release like this, https://cdn.botframework.com/botframework-webchat/latest/botchat.js. Make sure you use the same version for both
botchat.css
and botchat.js
.
- Don't want to depend on a CDN? Download the files and serve them up from your own website.
- Want to run a custom build of Web Chat? Clone this repo, alter it, build it, and reference your built
botchat.css
and botchat.js
files. - Go to the next level with Advanced Web Chat
- Running Web Chat inline may not work for some web pages. Read on for a solution.
Easyish: In any website, IFRAME your Web Chat instance
You can isolate your instance of Web Chat by running it inside an IFRAME. This involves creating two web pages:
- Your Web Chat instance, as shown above.
- The hosting page, adding
<iframe src="/path/to/your/webchat/instance" height="height" width="width" />
Medium: In your React website, incorporate the Web Chat React component
Add a DirectLine (not Web Chat) channel, and generate a Direct Line Secret. Make sure to enable Direct Line 3.0.
Add Web Chat to your React project via npm install botframework-webchat
. This will install latest stable build. If you want to try out features and fixes direct from our GitHub, install via npm install botframework-webchat@master
.
Include the Chat
component in your React app, e.g.:
import { Chat } from 'botframework-webchat';
...
const YourApp = () => {
<div>
<YourComponent />
<Chat directLine={{ secret: direct_line_secret }} user={{ id: 'user_id', name: 'user_name' }}/>
<YourOtherComponent />
</div>
}
...
- Go to the next level with Advanced Web Chat
- Want to run a custom build of Web Chat in your React app? Read on.
Hard: In your React website, incorporate a custom build of the Web Chat component
The simplest approach is to clone (or fork) this repo, alter it, build it, then reference your local build in your project's package.json
as follows:
dependencies: {
...
'botframework-webchat': 'file:/path/to/your/repo'
...
}
Running npm install
will copy your local repo to node_modules
, and import
/require
references to 'botframework-webchat'
will resolve correctly.
You may also wish to go so far as to publish your repo as its own full-fledged, versioned npm package using npm version
and npm publish
, either privately or publicly.
Different projects have different build strategies, yours may vary considerably from the above. If you come up with a different integration approach that you feel would have broad application, please consider filing a pull request for this README.
Building Web Chat
- Clone (or fork) this repo
npm install
npm run build
(to build on every change npm run watch
, to build production npm run prepublish
)
This builds the following:
/built/*.js
compiled from the TypeScript sources in /src/*.js
- /built/BotChat.js
is the root/built/*.d.ts
declarations for TypeScript users - /built/BotChat.d.ts
is the root/built/*.js.map
sourcemaps for easier debugging/botchat.js
webpacked UMD file containing all dependencies (React, Redux, RxJS, etc.)/botchat.css
base stylesheet/botchat-es5.js
is the Webpack bundle (a.k.a. botchat.js
) plus polyfills for ES5 browsers/botchat-fullwindow.css
media query stylesheet for a full-window experience
Customizing Web Chat
Enabling Speech Capabilities
Web Chat includes support for spoken conversations by leveraging Speech Recognition (audio to text) and Speech Synthesis (text to audio).
Speech support is opt-in. As shown in /samples/speech
, you can customize the speech experience by supplying a specific implementation for speech recognition and speech synthesis to be used.
...
<script src="https://cdn.botframework.com/botframework-webchat/latest/botchat.js"></script>
<script src="https://cdn.botframework.com/botframework-webchat/latest/CognitiveServices.js"></script>
...
var speechOptions = {
speechRecognizer: new CognitiveServices.SpeechRecognizer( { subscriptionKey: 'YOUR_COGNITIVE_SPEECH_API_KEY' } ),
speechSynthesizer: new CognitiveServices.SpeechSynthesizer(
{
subscriptionKey: 'YOUR_COGNITIVE_SPEECH_API_KEY',
gender: CognitiveServices.SynthesisGender.Female,
voiceName: 'Microsoft Server Speech Text to Speech Voice (en-US, JessaRUS)'
})
}
...
BotChat.App({
botConnection: botConnection,
speechOptions: speechOptions,
...
}, document.getElementById("BotChatGoesHere"));
For details related to building a speech enabled bot and leveraging Speech Priming to improve speech recognition accuracy, check out the Speech Support in Bot Framework blog post.
Styling
In the /src/scss/
folder you will find the source files for generating /botchat.css
. Run npm run build-css
to compile once you've made your changes. For basic branding, change colors.scss
to match your color scheme. For advanced styling, change botchat.scss
.
Card Layout
Web Chat uses Adaptive Cards that let the bot developer create cards with advanced layout and interactive capabilities. For more details, see AdaptiveCards.md
Card Sizes / Responsiveness
Web Chat strives to use responsive design when possible. As part of this, Web Chat cards come in 3 sizes: narrow (216px), normal (320px) and wide (416px). In a full-window chat, these sizes are invoked by a CSS media query in the /botchat-fullwindow.css
style sheet. You may customize this style sheet for the media query breakpoints in your own application. Or, if your Web Chat implementation is not a full-window experience, you can manually invoke card sizes by adding the CSS classes wc-narrow
and wc-wide
to the HTML element containing your chat.
Strings
You can alter or add localized strings in /src/Strings.ts:
- Add one or more locales (with associated localized strings) to
localizedStrings
- Add logic to map the requested locale to the supported locale in
strings
- Please help the community by submitting a pull request.
Behaviors
Behavioral customization will require changing the TypeScript files in /src
. A full description of the architecture of Web Chat is beyond the scope of this document, but here are a few starters:
Architecture
Chat
is the top-level React componentApp
creates a React application consists solely of Chat
Chat
largely follows the Redux architecture laid out in this video series- To handle async side effects of Redux actions,
Chat
uses Epic
from redux-observable - here's a video introduction - Underlying
redux-observable
(and also DirectLineJS) is the RxJS
library, which implements the Observable pattern for wrangling async. A minimal grasp of RxJS
is key to understanding Web Chat's plumbing.
Markdown
Web Chat uses markdown-it for markdown rendering. Markdown-it offers many rendering options, such as HTML rendering within markdown. You can change these options in /src/FormattedText.tsx
in your own build of Web Chat.
Contributing
If you feel your change might benefit the community, please submit a pull request.
Advanced Web Chat
Direct Line and DirectLineJS
Web Chat communicates with your bot using the Direct Line 3.0 protocol. Web Chat's implementation of this protocol is called DirectLineJS and can be installed and used independently of Web Chat if you want to create your own user experience.
Direct Line fundamentals
Web Chat exchanges activities with the bot. The most common activity type is 'message', but there is also 'typing', and 'event'. For more information on how to use 'event' activities, see The Backchannel.
Named Direct Line endpoint
If you wish to point to a specific URL for Direct Line (such as a region-specific endpoint), pass it to DirectLine as domain: direct_line_url
.
Secrets versus Tokens
If you don't want to publish your Direct Line Secret (which lets anyone put your bot on their website), exchange that Secret for a Token as detailed in the Direct Line documentation and pass it to DirectLine as token: direct_line_token
. If you do choose to pass a Token instead of a Secret, you may need to handle scenarios where Web Chat has become disconnected from the bot and needs a fresh token to reconnect. See the DirectLineJS reconnect documentation for a few more details on how to do this.
WebSocket
DirectLineJS defaults to WebSocket for receiving messages from the bot. If WebSocket is not available, it will use GET polling. You can force it to use GET polling by passing webSocket: false
in the options you pass to DirectLine.
Note: the standard Web Chat channel does not currently use WebSocket, which is a compelling reason to use this project.
Typing
Web Chat currently defaults to not sending 'typing' activities to the bot when the user is typing. If your bot would find it useful to receive these, pass sendTyping: true
in the options to App
/Chat
. In the future this feature may be enabled by default, so set sendTyping: false
if you want to make sure to disable it.
User identity
You can supply Web Chat with the id (and, optionally, a friendly name) of the current user by passing user: { id: user_id, name: user_name }
to App
/Chat
. This object is passed with every activity sent from Web Chat to the bot, which means it is not available to the bot before any activities are sent. See The Backchannel to find out how your web page can programmatically send non-message activities to the bot.
Replacing DirectLineJS
You can give Web Chat any object that implements IBotConnection
by passing botConnection: your_directline_replacement
to App
/Chat
.
The Backchannel
Web Chat can either create its own instance of DirectLine (as shown in /samples/standalone
), or it can share one with the hosting page (as shown in /samples/backchannel
). In the shared case, Web Chat and/or the page can send and/or receive activities. If they are type 'event', Web Chat will not display them. This is how the backchannel works.
NOTE: The provided backchannel sample requires a bot which can send and receive specific event activities. Follow the instructions here to deploy such a bot.
The backchannel sample provided in this project listens for events of name "changeBackground" and sends events of name "buttonClicked". This highlights the ability for a bot to communicate with the page that embeds Web Chat.
In the sample above, the web page creates a DirectLine object:
var botConnection = new BotChat.DirectLine(...);
It shares this when creating the Web Chat instance:
BotChat.App({
botConnection: botConnection,
user: user
...
}, document.getElementById("BotChatGoesHere"));
It notifies the bot upon the click of a button on the web page:
const postButtonMessage = () => {
botConnection
.postActivity({ type: "event", value: "", from: { id: "me" }, name: "buttonClicked" })
.subscribe(id => console.log("success"));
}
Note the creation of an activity of type 'event' and how it is sent with postActivity
. Also note that the name and value of the event can be anything defined by the developer. It is simply a contract between the web page and the bot.
The client JavaScript also listens for a specific event from the bot:
botConnection.activity$
.filter(activity => activity.type === "event" && activity.name === "changeBackground")
.subscribe(activity => changeBackgroundColor(activity.value))
The bot, in this example, can request the page to change the background color via a specific event with name: 'changeBackground'
. The web page can respond to this in any way it wants, including ignoring it. In this case it cooperates by changing the background color as passed in the value
field of the event.
Essentially the backchannel allows client and server to exchange any data needed, from requesting the client's time zone to reading a GPS location or what the user is doing on a web page. The bot can even "guide" the user by automatically filling out parts of a form and so on. The backchannel closes the gap between client JavaScript and bots.
You can contribute to Web Chat!
- Add localized strings (see above)
- Report any unreported issues
- Propose new features
- Fix an outstanding issue and submit a pull request (please only commit source code, non-generated files)
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
Copyright & License
© 2016 Microsoft Corporation
MIT License