Giter VIP home page Giter VIP logo

tchatche-react's Introduction

tchatche-react

A react port of the tchatche conversational bot UI.

Usage

import React from 'react'
import Tchatche from 'tchatche-react'
import { BotMessage } from 'tchatche-react/dist/types'

const messages: BotMessage[] = [
  // ...
]

const onEnd = (
  { conversation, data }: { conversation: Message[]; data: object }
) => {
  // ...
}

export default <Tchatche messages={messages} onEnd={onEnd} pace={300} />

No styling is provided. tchatche lets you take care of the CSS.

messages example

This is a simple example chat. The bot asks the user for her name (id: name). The conversation always starts with the first message. When the user puts in her name, we validate the input. In this case, we are just checking if the string is at least 2 characters long. If it is, we go over to a question where the user answers by clicking one of two buttons (id: one-or-two). If the name is not valid we go to name-validation-error where the user is asked to enter her name again. The conversation ends when one-or-two has been answered. The .on('end') callback is triggered. It returns the whole conversation and the data collected. In this case the data will look somthing like: { name: 'Xxxx', choice: 'two' }.

const messages: Message[] = [
  {
    id: 'name',
    botSays: () => ([
      'Hello',
      'What is your name?'
    ]),
    userAction: {
      inputType: 'input',
      onSubmit: async (name: string) =>
        name.length >= 2
          ? { nextMessageId: 'one-or-two', data: { property: 'name', value: name } }
          : { nextMessageId: 'name-validation-error', data: { property: 'name', value: name } }
    },
  },
  {
    id: 'name-validation-error',
    botSays: () => ([
      'That is not your name',
      'Seriously...',
      'What is your name?',
    ]),
    userAction: {
      inputType: 'input',
      onSubmit: async (name: string) =>
        name.length >= 2
          ? {
              nextMessageId: 'one-or-two',
              data: { property: 'name', value: name }
            }
          : {
              nextMessageId: 'name-validation-error',
              data: { property: 'name', value: name }
            }
    },
  },
  {
    id: 'one-or-two',
    botSays: (data: any) => ([
      `Thanks, ${data.name}`,
      'Choose one or two',
    ]),
    userAction: {
      inputType: 'buttons',
      buttons: [
        { value: 'one', label: 'One' },
        { value: 'two', label: 'Two' },
      ],
      onSubmit: async ({ value, label }) =>
        ({ isEnd: true, data: { property: 'choice', value, label } })
    },
  }
]

Explanation

message

export interface BotMessage {
  id: string
  botSays: (data: any) => string[]
  userAction: UserAction
}
  • id is used as a reference to get to that particular message in the flow. Must be unique.
  • botSays is a function that takes data as an argument and returns an array of strings. data contains the values previously collected.
  • userAction describes what the user can do after the bot has talked.

userAction

export interface UserActionInput {
  inputType: 'input'
  placeholder?: string
  onSubmit: (userInput: string, data: object, setData: (property: string, value: any) => void) => OnSubmitResponse
}

export interface Button {
  label: string
  value: string
}

export interface UserActionButton {
  inputType: 'buttons'
  buttons: Button[]
  onSubmit: (button: Button, data: object, setData: (property: string, value: any) => void) => OnSubmitResponse
}

export type UserAction = UserActionInput
  | UserActionButton

At the moment there are only two possible user actions:

  1. An input field
  2. A choice of buttons

The onSubmit function is triggered when the user has either clicked a button or pressed enter in an input and has the following arguments:

  • the value of the input or the clicked button
  • the data collected so far
  • a setter to add any property to data (useful for temporary data that needs to be passed between messages)

onSubmit response

onSubmit has to be an async function returning a OnSubmitResponse

export interface OnSubmitData {
  nextMessageId: string
  data: { property: string, value: string, label?: string }
}

export interface OnSubmitEnd {
  data: { property: string, value: string, label?: string }
  isEnd: true
}

export type OnSubmitResponse = Promise<OnSubmitData | OnSubmitEnd>

It either redirects to another message or ends the conversation. In both cases it has to return the data collected from the user.

tchatche-react's People

Contributors

idris-maps avatar stophecom avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.