Skip to content
This repository was archived by the owner on Oct 16, 2018. It is now read-only.

Commit f5403bb

Browse files
committed
feat(web): added support for markdown render of text
1 parent 1bee785 commit f5403bb

5 files changed

Lines changed: 63 additions & 39 deletions

File tree

channels/botpress-channel-web/README.md

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# Botpress Webchat
22

33
This channel is a customizable web chat that can be:
4-
- **Embedded** on any website
5-
- Used as a **Standalone** full-screen chat
4+
5+
* **Embedded** on any website
6+
* Used as a **Standalone** full-screen chat
67

78
## Installation
89

@@ -24,14 +25,20 @@ yarn add @botpress/channel-web
2425
{
2526
on: 'webchat',
2627
typing: true,
27-
text: data.text
28+
text: data.text,
29+
markdown: true
2830
}
2931
]
3032
```
33+
3134
#### `typing` (optional)
3235

3336
Can be `true` to use natural typing speed (based on characters length) or can also be a natural time string (parsed by [`ms module`](https://www.npmjs.com/package/ms)).
3437

38+
#### `markdown` (optional)
39+
40+
Can be `true` to render the text as markdown format. This allows you to include links, bold and italic text.
41+
3542
#### `web-style` (optional)
3643

3744
`web-style` (optional) will pass the arguments as [DOM style](https://www.w3schools.com/jsref/dom_obj_style.asp) properties. This allows you to customize how specific messages look.
@@ -53,8 +60,8 @@ Array of string, with the `<PAYLOAD> Text` format.
5360
```js
5461
quick_replies: data.choices.map(choice => `<${choice.payload}> ${choice.text}`)
5562
```
56-
</details>
5763

64+
</details>
5865

5966
<details>
6067
<summary><b>Form</b> (documentation for Botpress X coming soon)</summary>
@@ -84,16 +91,17 @@ welcome:
8491
```
8592
8693
It's look's like a usually web form. After submitted, you can handle this event with botpress.hear method. For example:
94+
8795
```js
88-
bp.hear({ type: 'form', formId: "survey" }, (event, next) => {
89-
// Your code
90-
});
96+
bp.hear({ type: 'form', formId: 'survey' }, (event, next) => {
97+
// Your code
98+
})
9199
```
92100

93-
You can always catch formId in the hear function, because Id is not an option in the form element. You choose a value to go with your id keys.
101+
You can always catch formId in the hear function, because Id is not an option in the form element. You choose a value to go with your id keys.
94102

95103
```yaml
96-
welcome:
104+
welcome:
97105
- text: "Welcome"
98106
typing: 250ms
99107
form:
@@ -104,7 +112,7 @@ welcome:
104112

105113

106114
form-email:
107-
- text: Provide me your email
115+
- text: Provide me your email
108116
form:
109117
title: Email
110118
id: email
@@ -113,7 +121,6 @@ form-email:
113121
#
114122
```
115123

116-
117124
in your `bp.hear` function
118125

119126
```js
@@ -136,6 +143,7 @@ Has a same attributes like `input`, but has no `subtype` attribute
136143
Has a same attributes like `textarea`, but has no `maxlength` and `minlength` attributes, and has `options` attribute, which contain an option elements.
137144

138145
Example:
146+
139147
```yaml
140148
- select:
141149
label: Select one item
@@ -158,10 +166,8 @@ Example:
158166
159167
</details>
160168
161-
162169
<details>
163170
<summary><b>Carousel</b></summary>
164-
165171
166172
#### `elements` (required)
167173

@@ -196,22 +202,22 @@ Settings to pass the [`react-slick`](https://github.com/akiran/react-slick) comp
196202
In your `index.js` file, add this:
197203

198204
```js
199-
const config = {
200-
botName: '<<REPLACE>>',
201-
botAvatarUrl: '<<REPLACE BY URL>>',
202-
botConvoTitle: '<<REPLACE>>',
203-
botConvoDescription: "<<REPLACE>>",
204-
backgroundColor: '#ffffff',
205-
textColorOnBackground: '#666666',
206-
foregroundColor: '#000000',
207-
textColorOnForeground: '#ffffff'
208-
}
209-
210-
bp.createShortlink('chat', '/lite', {
211-
m: 'channel-web',
212-
v: 'fullscreen',
213-
options: JSON.stringify({ config: config })
214-
})
205+
const config = {
206+
botName: '<<REPLACE>>',
207+
botAvatarUrl: '<<REPLACE BY URL>>',
208+
botConvoTitle: '<<REPLACE>>',
209+
botConvoDescription: '<<REPLACE>>',
210+
backgroundColor: '#ffffff',
211+
textColorOnBackground: '#666666',
212+
foregroundColor: '#000000',
213+
textColorOnForeground: '#ffffff'
214+
}
215+
216+
bp.createShortlink('chat', '/lite', {
217+
m: 'channel-web',
218+
v: 'fullscreen',
219+
options: JSON.stringify({ config: config })
220+
})
215221
```
216222

217223
**Now your bot is available at the following url: `<BOT_URL>/s/chat`, e.g. `http://localhost:3000/s/chat`.**
@@ -228,19 +234,20 @@ To embedded the web interface to a website, you simply need to add this script a
228234
```
229235

230236
## Customize the look and feel
237+
231238
You can customize look and feel of the web chat by passing additional keys to `init` method like this:
232239

233240
```javascript
234241
window.botpressWebChat.init({
235242
host: '<host>',
236-
botName: 'Bot', // Name of your bot
237-
botAvatarUrl: null, // Default avatar url of the image (e.g. 'https://avatars3.githubusercontent.com/u/1315508?v=4&s=400' )
243+
botName: 'Bot', // Name of your bot
244+
botAvatarUrl: null, // Default avatar url of the image (e.g. 'https://avatars3.githubusercontent.com/u/1315508?v=4&s=400' )
238245
botConvoTitle: 'Technical Support', // Title of the first conversation with the bot
239246
botConvoDescription: '',
240-
backgroundColor: '#ffffff', // Color of the background
241-
textColorOnBackground: '#666666', // Color of the text on the background
242-
foregroundColor: '#0176ff', // Element background color (header, composer, button..)
243-
textColorOnForeground: '#ffffff' // Element text color (header, composer, button..)
247+
backgroundColor: '#ffffff', // Color of the background
248+
textColorOnBackground: '#666666', // Color of the text on the background
249+
foregroundColor: '#0176ff', // Element background color (header, composer, button..)
250+
textColorOnForeground: '#ffffff' // Element text color (header, composer, button..)
244251
})
245252
```
246253

channels/botpress-channel-web/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"react-spinkit": "^3.0.0",
6161
"sass-loader": "^6.0.6",
6262
"slick-carousel": "^1.8.1",
63+
"snarkdown": "^1.2.2",
6364
"style-loader": "^0.19.1",
6465
"webpack": "^3.10.0",
6566
"webpack-node-externals": "^1.6.0"

channels/botpress-channel-web/src/umm.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ function processOutgoing({ event, blocName, instruction }) {
220220
// PRE-PROCESSING
221221
////////
222222

223-
const optionsList = ['typing', 'quick_replies', 'file', 'form', 'elements', 'web-style', 'settings']
223+
const optionsList = ['typing', 'quick_replies', 'file', 'form', 'elements', 'web-style', 'settings', 'markdown']
224224

225225
const options = _.pick(instruction, optionsList)
226226

channels/botpress-channel-web/src/views/web/messages/index.jsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import classnames from 'classnames'
44
import format from 'date-fns/format'
55
import differenceInMinutes from 'date-fns/difference_in_minutes'
66
import Linkify from 'react-linkify'
7+
import snarkdown from 'snarkdown'
78

89
import BotAvatar from '../bot_avatar'
910
import QuickReplies from './quick_replies'
@@ -208,12 +209,23 @@ class Message extends Component {
208209
return this.props.data.message_raw && this.props.data.message_raw['web-style']
209210
}
210211

212+
getMarkdownElement() {
213+
let html = snarkdown(this.props.data.message_text)
214+
html = html.replace(/<a href/gi, `<a target="_blank" href`)
215+
216+
return <div dangerouslySetInnerHTML={{ __html: html }} />
217+
}
218+
211219
render_text() {
220+
const element =
221+
this.props.data.message_raw && this.props.data.message_raw.markdown ? (
222+
this.getMarkdownElement()
223+
) : (
224+
<p style={this.getAddStyle()}>{this.props.data.message_text}</p>
225+
)
212226
return (
213227
<Linkify properties={{ target: '_blank' }}>
214-
<div>
215-
<p style={this.getAddStyle()}>{this.props.data.message_text}</p>
216-
</div>
228+
<div>{element}</div>
217229
</Linkify>
218230
)
219231
}

channels/botpress-channel-web/yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5042,6 +5042,10 @@ slick-carousel@^1.6.0, slick-carousel@^1.8.1:
50425042
version "1.8.1"
50435043
resolved "https://registry.yarnpkg.com/slick-carousel/-/slick-carousel-1.8.1.tgz#a4bfb29014887bb66ce528b90bd0cda262cc8f8d"
50445044

5045+
snarkdown@^1.2.2:
5046+
version "1.2.2"
5047+
resolved "https://registry.yarnpkg.com/snarkdown/-/snarkdown-1.2.2.tgz#0cfe2f3012b804de120fc0c9f7791e869c59cc74"
5048+
50455049
sntp@1.x.x:
50465050
version "1.0.9"
50475051
resolved "https://registry.yarnpkg.com/sntp/-/sntp-1.0.9.tgz#6541184cc90aeea6c6e7b35e2659082443c66198"

0 commit comments

Comments
 (0)