0.1.7 • Published 5 years ago

rechatbot v0.1.7

Weekly downloads
4
License
MIT
Repository
github
Last release
5 years ago

React-ChatBot

This is a ready-made simple chat bot component, to develop your own chat bots for the site.

Install

npm install rechatbot

or

yarn add rechatbot

Usage

import React, { Component } from 'react';

import ChatBot from 'react-chatbot';

const options = [
  {
    id: 'welcome',
    text: 'Welcome, to React ChatBot!',
    buttons: [
      {
        label: 'Docs',
        call: 'docs',
      },
      {
        label: 'Github',
        call: () => {
          global.location.href = 'https://github.com/Archakov06/react-chatbot';
        },
      },
    ],
  },
  {
    id: 'docs',
    buttons: [
      {
        label: 'Open docs on GitHub',
        callback: () => {
          global.location.href = 'https://github.com/Archakov06/rechatbot';
        },
      },
    ],
  },
  {
    id: null,
    text: 'Sorry, I can not understand what you mean.',
  },
];

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      messages: [],
    };
    this.addMessage = this.addMessage.bind(this);
  }

  addMessage(obj) {
    this.setState({
      messages: [...this.state.messages, obj],
    });
  }

  render() {
    const { messages } = this.state;
    return (
      <div className="chat">
        <ChatBot
          welcomeId="welcome"
          delay={800}
          messages={messages}
          options={options}
          onSendMessage={this.addMessage}
          avatars={{
            user: 'https://pp.userapi.com/c834104/v834104145/5ca01/AsZGGgLNr-4.jpg',
            bot: 'https://pp.userapi.com/c631216/v631216247/21c8b/qF8SubyAdsU.jpg',
          }}
        />
      </div>
    );
  }
}

ReactDOM.render(<App />, document.getElementById('root'));

Documentation

ChatBot

PropertyTypeDescription
welcomeIdPropTypes.oneOfType(PropTypes.string, PropTypes.number)First message when you start a chat
messagesPropTypes.array.isRequiredList of messages (You must be maintained in a this.state.messages)
optionsPropTypes.array.isRequiredList of all available commands
avatarsPropTypes.objectUser and bot avatar
delayPropTypes.numberTime delay message from the bot
hideUserMessagePropTypes.boolHide user messages
hideAvatarPropTypes.boolHide avatars
inputPlaceholderPropTypes.stringInput placeholder text
onSendMessagePropTypes.func.isRequiredCallback function when a new message comes from a bot or user

ChatMessage

PropertyTypeDescription
loadingPropTypes.boolMessage is loading
textPropTypes.oneOfType(PropTypes.string, PropTypes.object, PropTypes.func)Message text (text, component)
buttonsPropTypes.arrayInline buttons
sendActionPropTypes.funcInline buttons callback
avatarsPropTypes.objectUser and bot avatar
imagePropTypes.stringAttached image
isUserPropTypes.stringMessage from the user
hideAvatarPropTypes.boolHide avatars

Author

License

MIT

0.1.7

5 years ago

0.1.6

6 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago