This example shows you a multi-language conversation flow to acquire an Internet or a cell phone rate using buttons and replies.
What's in this document?
- From your command line, download the example by running:
$ botonic new <botName> telco-offers
-
cd
into<botName>
directory that has been created. - Run
botonic serve
to test it in your local machine.
Routes map user inputs to actions which consist of simple units of logic that your bot can perform and the response that your bot generates.
Here we can see a few examples of how we have captured the user input.
src/routes.js
import Start from './actions/start'
import ChooseLanguage from './actions/choose_language'
import Phone from './actions/phone'
import BuyPhone from './actions/buy-phone'
import Bye from './actions/bye'
export const routes = [
{ path: 'hi', payload: 'hi', action: ChooseLanguage },
{ path: 'set-language', payload: /language-.*/, action: Start },
{
path: 'phone',
payload: 'phone',
action: Phone,
childRoutes: [
{
path: 'buyPhone',
payload: /buyPhone-.*/,
action: BuyPhone,
},
],
},
{ path: 'bye', text: /.*/, payload: /bye-.*/, action: Bye },
]
If a rule matches it will trigger an action:
src/actions/choose-language.jsx
import React from 'react'
import { Text, Reply } from '@botonic/react'
export default class extends React.Component {
render() {
return (
<>
<Text>Hi! Before we start choose a language: {'\n'}</Text>
<Text>
Hola! Antes de empezar elige un idioma:
<Reply payload='language-es'>Español</Reply>
<Reply payload='language-en'>English</Reply>
</Text>
</>
)
}
}
The Locales allows us to build a bot that supports different languages. To do so, we have separated our string literals from the code components.
In the src/locales
folder we have added a js file for each language we want to support.
src/locales/en.js
export default {
internet: ['Internet'],
phone: ['Cell Phone'],
tv: ['TV'],
extra_phone: ['Extra Cell Phone'],
speed: ['Speed'],
price: ['Price'],
start_text: [
'Welcome, I am your virtual assistant of Botonic Telco, select which service you want to hire?',
],
ask_more: ['Do you want to hire any more rate?'],
}
src/locales/es.js
export default {
internet: ['Fibra'],
phone: ['Móvil'],
tv: ['TV'],
extra_phone: ['Extra móvil'],
speed: ['Velocidad'],
price: ['Precio'],
start_text: [
'Bienvenido soy tu asistente virtual de Botonic Telco, selecciona que servicio quieres contratar?',
],
ask_more: ['Quieres contratar alguna tarifa más?'],
}
Then, we have exported these languages.
src/locales/index.js
import en from './en'
import es from './es'
export const locales = { en, es }
In the initial action we have set the locale and then we can access an object from locales with this.context.getString
method.
src/actions/start.jsx
import React from 'react'
import { RequestContext, Text, Button } from '@botonic/react'
export default class extends React.Component {
static contextType = RequestContext
static async botonicInit(request) {
const language = request.input.payload.split('-')[1]
return { language }
}
render() {
this.props.language && this.context.setLocale(this.props.language)
let _ = this.context.getString
return (
<>
<Text>
{_('start_text')}
<Button payload='internet'>{_('internet')}</Button>
<Button payload='phone'>{_('phone')}</Button>
</Text>
</>
)
}
}
The Webchat Settings component can be appended at the end of a message to change Webchat properties dynamically.
We have used it to enable the user input in one of the last actions.
src/actions/confirm.jsx
import React from 'react'
import { RequestContext, Text, WebchatSettings } from '@botonic/react'
export default class extends React.Component {
static contextType = RequestContext
render() {
let _ = this.context.getString
return (
<>
<Text> {_('confirm.text')}</Text>
<WebchatSettings
theme={{
userInput: {
box: {
style: {
background: '#F5F5F5',
},
},
},
}}
enableUserInput={true}
/>
</>
)
}
}
...and we are done 🎉