Add auto copy code feature in botbuilder
SUSI botbuilder lets you create your own skill bot and deploy on your website. After you have customised your bot, you will get a javascript code that you need to paste in your website’s source code. To make the process of copying the code easy, we have developed a feature for auto copying of the code to your clipboard. You just need to click on a button “copy” and the code will be copied to your clipboard.
Installing package
We use react-copy-to-clipboard package to enable auto copy feature. Install it in your project using the following command.
npm i react-copy-to-clipboard --save
Adding code inside render function
Inside the render() function in react file, paste the following code where you want the copy button to be displayed. Here we need to provide the text to be copied to the CopyToClipboard component via the text props. We pass the script code. We get the access token for the bot via the saved cookie. Inside the children of CopyToClipboard we need to pass the copy button which we want to show.
<CopyToClipboard
text={
" +cookies.get('uuid') +"' data-group='" +
group + "' data-language='" + language + "' data-skill='" + skill + "' src='" + api + "/susi-chatbot.js'>"
}
onCopy={() => this.setState({ copied: true })}
>
<span className="copy-button">copy</span>
</CopyToClipboard>
Thus, when the user clicks on the “copy” button, the code will be automatically copied to the user’s clipboard.
Showing snackbar message
After the code has been copied to the user’s clipboard, we can show a snackbar message to inform the user. First we pass a function onCopy to the CopyToClipboard component. This sets the state variable copied to true. Then we have a snackbar component which displays the message.
<Snackbar open={this.state.copied} message="Copied to clipboard!" autoHideDuration={2000} onRequestClose={() => { this.setState({ copied: false }); }} />
Result:
Resources
- Snackbar in Material-ui: https://material-ui.com/demos/snackbars/
- NPM package for copying to clipboard: https://www.npmjs.com/package/react-copy-to-clipboard
- State in react: https://reactjs.org/docs/state-and-lifecycle.html
You must be logged in to post a comment.