|
1 | | -# Microsoft Bot Framework WebChat |
| 1 | +# Microsoft Bot Framework Direct Line library for JavaScript |
2 | 2 |
|
3 | | -Embeddable web chat control for the [Microsoft Bot Framework](http://www.botframework.com) using the [DirectLine](https://docs.botframework.com/en-us/restapi/directline3/) API. |
| 3 | +Client ibrary for the [Microsoft Bot Framework](http://www.botframework.com) [DirectLine](https://docs.botframework.com/en-us/restapi/directline3/) protocol. |
4 | 4 |
|
5 | | -Used by the Bot Framework developer portal, [Emulator](https://github.com/Microsoft/BotFramework-Emulator), WebChat channel, and [Azure Bot Service](https://azure.microsoft.com/en-us/services/bot-service/) |
6 | | - |
7 | | -You can easily play with a recent build using [botchattest](https://botchattest.herokuapp.com) |
| 5 | +Used by [WebChat](https://github.com/Microsoft/BotFramework-WebChat) and thus (by extension) [Emulator](https://github.com/Microsoft/BotFramework-Emulator), WebChat channel, and [Azure Bot Service](https://azure.microsoft.com/en-us/services/bot-service/). |
8 | 6 |
|
9 | 7 | ## FAQ |
10 | 8 |
|
11 | | -### *How is it made?* |
12 | | - |
13 | | -WebChat is a [React](https://facebook.github.io/react/) component built in [TypeScript](http://www.typescriptlang.org) using [Redux](http://redux.js.org) for state management and [RxJS](http://reactivex.io/rxjs/) for wrangling async. |
14 | | - |
15 | | -### *How can I use it?* |
16 | | - |
17 | | -* As an IFRAME in any website using the standard Bot Framework WebChat channel. In this case you don't need this repo or any of the information in it. |
18 | | -* As a standalone website, primarily for testing purposes |
19 | | -* As an IFRAME in any website, pointed at an instance hosted by you, customized to your needs |
20 | | -* Inline in your non-React webapp, customized to your needs |
21 | | -* Inline in your React webapp, customized to your needs |
22 | | - |
23 | | -See more detailed instructions [below](#getting-webchat-up-and-running). |
| 9 | +### *Who is this for?* |
24 | 10 |
|
25 | | -### *How do I customize it?* |
| 11 | +Anyone who is building a Bot Framework JavaScript client who does not want to use [WebChat](https://github.com/Microsoft/BotFramework-WebChat). |
26 | 12 |
|
27 | | -* Follow the [below instructions](#1-install-and-build) to install and build |
28 | | -* Customize the visuals by altering the [static/botchat.css](https://github.com/Microsoft/BotFramework-WebChat/blob/master/static/botchat.css) file |
29 | | -* Or go farther and change the HTML/JSX and/or TypeScript |
| 13 | +### *What is Rx?* |
30 | 14 |
|
31 | | -### *How do I contribute to it?* |
| 15 | +**R**eactive E**x**tensions are a set of libraries for different languages implementing Reactive Functional Programming a.k.a. the Observable pattern. This library uses [RxJS](http://reactivex.io/rxjs/). |
32 | 16 |
|
33 | | -* File [issues](https://github.com/Microsoft/BotFramework-WebChat/issues) and submit [pull requests](https://github.com/Microsoft/BotFramework-WebChat/pulls)! |
| 17 | +### *Can I use [TypeScript](http://www.typescriptlang.com)?* |
34 | 18 |
|
35 | | -### *Do you have a roadmap?* |
| 19 | +You bet. |
36 | 20 |
|
37 | | -Not the most formal one you'll ever see, but: |
| 21 | +### How ready for prime time is this library? |
38 | 22 |
|
39 | | -* Unit tests |
40 | | -* DirectLine 3.0 WebSocket support for retrieving messages |
41 | | -* Improved styling |
42 | | -* Simple UI customization |
43 | | -* npm package(s) |
44 | | -* CDN |
| 23 | +This library is an official Microsoft-supported library, and is considered largely complete. Future changes (aside from supporting future updates to the Direct Line protocol) will likely be limited to performance improvements, tutorials, and samples. The big missing piece here is unit tests. |
45 | 24 |
|
46 | | -Feel free to suggest features by filing an [issue](https://github.com/Microsoft/BotFramework-WebChat/issues) (please make sure one doesn't already exist). |
| 25 | +## How to build from source |
47 | 26 |
|
48 | | -### How can I help? |
| 27 | +0. Clone this repo |
| 28 | +1. `npm install` |
| 29 | +2. `npm run build` (or `npm run watch` to rebuild on every change) |
49 | 30 |
|
50 | | -* Add localized strings (see [below](#to-add-localized-strings)) |
51 | | -* Report any unreported [issues](https://github.com/Microsoft/BotFramework-WebChat/issues) |
52 | | -* Propose new [features](https://github.com/Microsoft/BotFramework-WebChat/issues) |
53 | | -* Fix an outstanding [issue](https://github.com/Microsoft/BotFramework-WebChat/issues) and submit a [pull request](https://github.com/Microsoft/BotFramework-WebChat/pulls) *(please only commit source code, non-generated files)* |
| 31 | +## How to include in your app |
54 | 32 |
|
55 | | -## Getting WebChat up and running |
| 33 | +There are several ways: |
56 | 34 |
|
57 | | -### 1. Install and build |
| 35 | +1. Build from scratch and include either `/directLine.js` (webpacked with rxjs) or `built/directline.js` in your app |
| 36 | +2. Use the unpkg CDN, e.g. `<script src="http://unpkg.com/botframework-directlinejs/directLine.js"/>` |
| 37 | +3. `npm install botframework-directlinejs` |
58 | 38 |
|
59 | | -1. Clone this repo |
60 | | -2. `npm install` |
61 | | -3. `npm run build` (to build on every change `npm run watch`, to build minified `npm run minify`) |
| 39 | +## How to create and use a directLine object |
62 | 40 |
|
63 | | -### 2. Obtain security credentials for your bot |
| 41 | +### First, obtain security credentials for your bot: |
64 | 42 |
|
65 | 43 | 1. If you haven't already, [register your bot](https://dev.botframework.com/bots/new). |
66 | | -2. Add a DirectLine channel, and generate a Direct Line Secret. Make sure to enable Direct Line 3.0. |
| 44 | +2. Add a DirectLine (**not WebChat**) channel, and generate a Direct Line Secret. Make sure to Direct Line 3.0 is enabled. |
67 | 45 | 3. For testing you can use your Direct Line Secret as a security token, but for production you will likely want to exchange that Secret for a Token as detailed in the Direct Line [documentation](https://docs.botframework.com/en-us/restapi/directline3/). |
68 | 46 |
|
69 | | -### 3. Decide how to run WebChat |
70 | | - |
71 | | -#### Using the WebChat channel |
72 | | - |
73 | | -1. Head over to the [Bot Framework developer portal](https://dev.botframework.com/bots) and add the WebChat channel to your bot. You don't need this repo or any of the information on this page. |
74 | | - |
75 | | -#### As a standalone web page, for quick and easy testing |
76 | | - |
77 | | -This is a quick and dirty method, perfect for testing. It requires embedding your Direct Line Secret in the web page or querystring, and as such should primarily be used for local testing. |
| 47 | +### Second, create a DirectLine object: |
78 | 48 |
|
79 | | -0. Build |
80 | | -1. Start a local web server using `npm run start` and aim your browser at `http://localhost:8000/samples?s={Direct Line Secret}` |
| 49 | + var directLine = new DirectLine({ |
| 50 | + secret: /* put your Direct Line secret here */, |
| 51 | + token: /* or put your Direct Line token here (supply secret OR token, not both) */, |
| 52 | + domain: /* optional: if you are not using the default Direct Line endpoint, e.g. if you are using a region-specific endpoint, put its full URL here */ |
| 53 | + webSocket: /* optional: true if you want to use WebSocket to receive messages. Currently defaults to false. */, |
| 54 | + }); |
81 | 55 |
|
82 | | -#### Embedded via IFRAME |
| 56 | +### Post activities to the bot: |
83 | 57 |
|
84 | | -In this scenario you will host two web pages, one for WebChat and one for the page which embeds it. They could be hosted by the same web server, or two entirely different web servers. |
| 58 | + directLine.postActivity({ |
| 59 | + from: { id: 'myUserId', name: 'myUserName' }, // required (from.name is optional) |
| 60 | + type: 'message', |
| 61 | + text: 'a message for you, Rudy' |
| 62 | + }).subscribe( |
| 63 | + id => console.log("Posted activity, assigned ID ", id), |
| 64 | + error => console.log("Error posting activity", error) |
| 65 | + ); |
85 | 66 |
|
86 | | -1. Serve the botchat in its own standalone web page, as described [above](#as-a-standalone-web-page-for-quick-and-easy-testing) |
87 | | -2. Optionally, on your web server, exchange the Direct Line Secret for a Token as detailed in the Direct Line [documentation](https://docs.botframework.com/en-us/restapi/directline3/). |
88 | | -3. In a second web page, embed the botchat via `<iframe src="http://{host}:{port}/samples?[s={Direct Line Secret}|t={Direct Line Token}]" width="320" height="500"/>` |
89 | | -4. You will probably want to customize the supplied sample index.html page |
| 67 | +You can also post messages with attachments, and non-message activities such as events, by supplying the appropriate fields in the activity. |
90 | 68 |
|
91 | | -(An example of this approach is [botchattest](https://github.com/billba/botchattest)) |
| 69 | +### Listen to activities sent from the bot: |
92 | 70 |
|
93 | | -#### Inline in your non-React website |
| 71 | + directLine.activity$ |
| 72 | + .subscribe( |
| 73 | + activity => console.log("received activity ", activity) |
| 74 | + ); |
94 | 75 |
|
95 | | -In this scenario you will include a JavaScript file which embeds its own copy of React, which will run in a DOM element. |
| 76 | +You can use RxJS operators on incoming activities. To see only message activities: |
96 | 77 |
|
97 | | -0. Build |
98 | | -1. Include `webpacked/botchat.js` and you will get an object called `BotChat` |
99 | | -2. For TypeScript users there is a type definition file called [static/botchat.d.ts](https://github.com/Microsoft/BotFramework-WebChat/blob/master/static/botchat.d.ts). |
100 | | -3. Incorporate [static/botchat.css](https://github.com/Microsoft/BotFramework-WebChat/blob/master/static/botchat.css) into your website deployment |
101 | | -4. Optionally, on your web server, exchange the Direct Line Secret for a Token as detailed in the Direct Line [documentation](https://docs.botframework.com/en-us/restapi/directline3/). |
102 | | -5. Create an instance of `BotChat.DirectLine` using your Direct Line Secret or Token |
103 | | -6. Call `BotChat.App` with the DOM element where you want your chat instance, your DirectLine instance, user and bot identities, and other properties as demonstrated in [samples/index.html](https://github.com/Microsoft/BotFramework-WebChat/blob/master/samples/index.html). |
| 78 | + directLine.activity$ |
| 79 | + .filter(activity => activity.type === 'message') |
| 80 | + .subscribe( |
| 81 | + message => console.log("received message ", message) |
| 82 | + ); |
104 | 83 |
|
105 | | -#### Inline in your React website |
| 84 | +Direct Line will helpfully send your client a copy of every sent activity, so a common pattern is to filter incoming messages on `from`: |
106 | 85 |
|
107 | | -In this scenario you will incorporate WebChat's multiple JavaScript files into your React webapp. |
| 86 | + directLine.activity$ |
| 87 | + .filter(activity => activity.type === 'message' && activity.from.id !== 'yourBotHandle') |
| 88 | + .subscribe( |
| 89 | + message => console.log("received message ", message) |
| 90 | + ); |
108 | 91 |
|
109 | | -0. Build |
110 | | -1. Incorporate the files in the [/built](https://github.com/Microsoft/BotFramework-WebChat/blob/master/built) folder into your build process |
111 | | -2. Incorporate [static/botchat.css](https://github.com/Microsoft/BotFramework-WebChat/blob/master/static/botchat.css) into your website deployment |
112 | | -3. For TypeScript users there is a definition file called [static/botchat.d.ts](https://github.com/Microsoft/BotFramework-WebChat/blob/master/static/botchat.d.ts). |
113 | | -4. Optionally, on your web server, exchange the Direct Line Secret for a Token as detailed in the Direct Line [documentation](https://docs.botframework.com/en-us/restapi/directline3/). |
114 | | -5. Create an instance of `DirectLine` using your Direct Line Secret or Token |
115 | | -6. Call the `Chat` React component with your DirectLine instance, user and bot identities, and other properties as demonstrated in [samples/index.html](https://github.com/Microsoft/BotFramework-WebChat/blob/master/samples/index.html). |
| 92 | +### Monitor connection status |
116 | 93 |
|
117 | | -## Misc. notes |
| 94 | +Subscribing to either `postActivity` or `activity$` will start the process of connecting to the bot. Your app can monitor the current connection status and react appropriately : |
118 | 95 |
|
119 | | -### To see WebChat logging |
| 96 | + directLine.connectionStatus$ |
| 97 | + .subscribe(connectionStatus => |
| 98 | + switch(connectionStatus) { |
| 99 | + case ConnectionStatus.Uninitialized: // the status when the DirectLine object is first created/constructed |
| 100 | + case ConnectionStatus.Connecting: // currently trying to connect to the conversation |
| 101 | + case ConnectionStatus.Online: // successfully connected to the converstaion. Connection is healthy so far as we know. |
| 102 | + case ConnectionStatus.ExpiredToken: // last operation errored out with an expired token. Your app should supply a new one. |
| 103 | + case ConnectionStatus.FailedToConnect: // the initial attempt to connect to the conversation failed. No recovery possible. |
| 104 | + case ConnectionStatus.Ended: // the bot ended the conversation |
| 105 | + } |
| 106 | + ); |
120 | 107 |
|
121 | | -* When IFRAMEd, set `window.frames["{iframe_id}"].botchatDebug = true` from the browser console |
122 | | -* Otherwise set `window.botchatDebug = true` or `var botchatDebug = true` from the browser console |
| 108 | +### Reconnecting to a conversation |
123 | 109 |
|
124 | | -### To add localized strings |
| 110 | +If your app created your DirectLine object by passing a token, DirectLine will refresh that token every 15 minutes. |
| 111 | +Should your client lose connectivity (e.g. close laptop, fail to pay Internet access bill, go under a tunnel), `connectionStatus$` |
| 112 | +will change to `ConnectionStatus.ExpiredToken`. Your app can request a new token from its server, which should call |
| 113 | +the [Reconnect](https://docs.botframework.com/en-us/restapi/directline3/#reconnecting-to-a-conversation) API. |
| 114 | +The resultant Conversation object can then be passed by the app to DirectLine: |
125 | 115 |
|
126 | | -In [src/Strings.ts](https://github.com/Microsoft/BotFramework-WebChat/blob/master/src/Strings.ts) : |
127 | | -* Add one or more locales (with associated localized strings) to `localizedStrings` |
128 | | -* Add logic to map the requested locale to the support locale in `strings` |
129 | | -* If you just adding a new locale for an existing set of strings, just update `strings` to return the existing locale's strings |
130 | | -* ... and please help the community by submitting a [pull request](https://github.com/Microsoft/BotFramework-WebChat/pulls)! |
| 116 | + var conversation = /* a Conversation object obtained from your app's server */; |
| 117 | + directLine.reconnect(conversation); |
131 | 118 |
|
132 | 119 | ## Copyright & License |
133 | 120 |
|
134 | | -© 2016 Microsoft Corporation |
| 121 | +© 2017 Microsoft Corporation |
135 | 122 |
|
136 | 123 | [MIT License](/LICENSE) |
0 commit comments