1- ###Overview
1+ ### Overview
22The chatbot UI can be embedded in an existing web site by loading it as
33an iframe. This includes embedding it in a cross-origin setup where the
44chatbot is served from a server, S3 bucket or CloudFront distribution
55in a domain that is different from the hosting web site.
66
77If you want to know more about the chatbot UI component, please refer to
8- its [ README] ( https://github.com/awslabs/aws-lex-web-ui/lex-web-ui ) file.
8+ its [ README] ( https://github.com/awslabs/aws-lex-web-ui/blob/master/ lex-web-ui/README.md ) file.
99
10- ###Adding the ChatBot UI to your Website
10+ ### Adding the ChatBot UI to your Website
1111This project provides a sample JavaScript loader
1212[ bot-loader.js] ( ./bot-loader.js" ) and CSS file [ bot.css] ( ./bot.css )
1313that can be used to add the chatbot to an existing web site using a
@@ -22,12 +22,12 @@ tags to your web page:
2222 <script src =" https://myboturl.example.com/static/iframe/bot-loader.js" ></script >
2323```
2424
25- ###Passing Data Between Parent and ChatBot UI
26- The chatbot iframe supports passing data to and from the hosting site. This
27- is done using the JavaScript
25+ ### Passing Data Between Parent Page and ChatBot UI
26+ The chatbot iframe supports passing data to and from the hosting parent
27+ page. This is done using the JavaScript
2828[ postMessage] ( https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage )
29- call. This enables use cases such as passing credentials from
30- the parent hosting site to the chat iframe or passing events (e.g.
29+ call. This mechanism enables use cases such as passing credentials
30+ from the parent hosting site to the chat iframe or passing events (e.g.
3131windows resize) from the chat window to the parent window.
3232
3333The chatbot iframe sends Lex bot state update events to the parent
@@ -36,18 +36,32 @@ of the bot. The bot-loader.js script relays these messages back to the
3636parent by emitting the ` updatelexstate ` events. The event object will
3737contain the Lex state variables in the ` details.state ` field.
3838
39- ###Configuration
40- ####Parent Configuration
41- The parent page configuration is held in a JSON config file:
42- [ config.json] ( ./config.json ) . This file is loaded by
43- the bot-loader.js script. Here's an example of the file format:
44- ``` json
39+ ### Configuration
40+ #### Parent Configuration File
41+ The [ bot-loader.js] ( ./bot-loader.js ) script loads its initial
42+ configuration from the JSON config file: [ config.json] ( ./config.json ) .
43+ This file is meant to be used as the build-time configuration of the
44+ bot-loader.js script. It serves as the base config so the root level
45+ keys in the JSON object should not be removed.
46+
47+ NOTE: The values in this file may be overwritten by environmental
48+ variables in the build process.
49+
50+ Here's an example of the file format:
51+ ```
4552 {
53+ // iframe origin - see: Cross Origin Configuration section below
4654 "iframeOrigin": "http://localhost:8080",
55+
56+ // time to wait for the config event in ms
57+ "configEventTimeOutInMs": 10000,
58+
59+ // used to initialize the AWS SDK and Cognito
4760 "aws": {
4861 "cognitoPoolId": "us-east-1:deadbeef-cac0-babe-abcd-abcdef01234",
4962 "region": "us-east-1"
5063 }
64+ // chatbot UI configuration passed from parent - see: ChatBot UI Configuration section below
5165 "iframeConfig": {
5266 ...
5367 "lex": {
@@ -59,35 +73,67 @@ the bot-loader.js script. Here's an example of the file format:
5973 }
6074```
6175
62- ####ChatBot UI Configuration
63- The chatbot UI has a local build-time config (see:
64- ` src/config/config.prod.json ` ). You can also pass or override this
65- configuration from the parent site via two mechanisms:
66-
67- - ** ChatBot UI Configuration from File.** The parent
68- [ config.json] ( ./config.json ) file contains the ` iframeConfig ` field
69- which is passed to the chatbot UI. This configuration is dynamically
70- sent to the chatbot UI as a response of the the ` onInitIframeConfig `
71- event. The values delivered via this mechanism override the chatbot
72- ui local config files and URL config parameter.
73- - ** ChatBot Configuration form URL Parameter.** The chatbot UI
74- configuration can be initialized using the ` config ` URL parameter. Your
75- application can dynamically add the parameter to the URL This is supported
76- both in iframe and stand-alone mode of the chatbot UI. This config URL
77- parameter should follow the same JSON structure of the ` configDefault `
78- object in the ` src/config/index.js ` file. This parameter should be a JSON
79- serialized and URL encoded string. Values from this parameter override
80- the ones from the chatbot ui local config files. For example to change
81- the initialText config field, you can use a URL like this:
82- ` https://mybucket.s3.amazonaws.com/index.html#/?config=%7B%22lex%22%3A%7B%22initialText%22%3A%22Ask%20me%20a%20question%22%7D%7D `
83-
84- ####Cross Origin Configuration
76+ #### Parent Event Configuration
77+ The parent page can also set the bot loader configuration via an
78+ event. The bot-loader.js script emits the ` receivelexconfig ` event which
79+ signals to the parent that it is ready to receive a configuration
80+ object. At which point, the bot-loader.js script will wait a 10
81+ seconds timeout (by default) to receive a event named ` loadlexconfig ` .
82+ The timeout is controlled by the ` configEventTimeOutInMs ` field in
83+ the config JSON file. The event object contains the config in the
84+ ` detail.config ` field.
85+
86+ The configuration from the JSON file is merged with the value for this
87+ event. The values received via this event take precedence over the
88+ JSON file.
89+
90+ For example, to pass the browser
91+ [ user agent] ( https://developer.mozilla.org/en-US/docs/Web/API/NavigatorID/userAgent ) ,
92+ you can add code to your site along the lines of:
93+ ``` javascript
94+ document .addEventListener (' receivelexconfig' , onReceiveLexConfig, false );
95+
96+ function onReceiveLexConfig () {
97+ document .removeEventListener (' receivelexconfig' , onReceiveLexConfig, false );
98+ var config = {
99+ iframeConfig: {
100+ lex: {
101+ sessionAttributes: {
102+ userAgent: navigator .userAgent ,
103+ },
104+ },
105+ },
106+ };
107+
108+ var event = new CustomEvent (' loadlexconfig' , { detail: { config: config } });
109+ document .dispatchEvent (event );
110+ }
111+ ```
112+
113+ #### ChatBot UI Configuration
114+ The chatbot UI has its own configuration (see the
115+ [ README] ( https://github.com/awslabs/aws-lex-web-ui/blob/master/lex-web-ui/README.md#configuration-and-customization )
116+ for details. You can also pass or override the chatbot UI configuration
117+ from the parent site via the following mechanisms:
118+
119+ 1 . ** Config Object.** The parent configuration config object (either from
120+ the [ config.json] ( ./config.json ) file or passed via the ` loadlexconfig `
121+ event) contains the ` iframeConfig ` field which is passed to the chatbot
122+ UI. This configuration is dynamically sent to the chatbot UI as a
123+ response of the the ` onInitIframeConfig ` event. The values delivered
124+ via this mechanism override the chatbot UI local config files and URL
125+ config parameter.
126+ 2 . ** URL Parameter.** The chatbot UI configuration can be initialized using
127+ the ` config ` URL parameter. For details, see the
128+ [ URL Parameter] ( https://github.com/awslabs/aws-lex-web-ui/blob/master/lex-web-ui/README.md#url-parameter )
129+ section. NOTE: the bot-loader.js script does not use URL parameters.
130+
131+ #### Cross Origin Configuration
85132If the chatbot UI is hosted on a different
86133[ Origin] ( https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy )
87134from the parent window, you need to configure the ` iframeOrigin ` field
88- in the parent config.json file to point to the origin of the iframe. This
135+ in the parent ` config.json ` file to point to the origin of the iframe. This
89136origin configuration is used to control which sites can communicate with
90137the iframe. Conversely, you would need to configure the ` ui.parentOrigin `
91138field in the iframe config. The origin configuration of this sample page
92139was done at build time by the CloudFormation stack that created it.
93-
0 commit comments