npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-chatbot-with-text-and-speech

v0.5.0

Published

Create a ChatBot Frontend with Text and speech using React

Downloads

5

Readme

react-chatbot-with-text-and-speech

Made with create-react-library

NPM JavaScript Style Guide

Install

npm install --save react-chatbot-with-text-and-speech

Demo

https://muthuishere.github.io/react-chatbot-with-text-and-speech/ Demo requires an API server to be running. See API Server section below.

Setup an API Server Github

Usage

You can use a simple method to return responses based on questions, The method can return promise as well.



```jsx
import React, {useEffect,useRef} from 'react'

import { ChatBotComponent } from 'react-chatbot-with-text-and-speech'
import 'react-chatbot-with-text-and-speech/dist/index.css'


const ChatBotWithStatic = () => {
  const options = {
    botImageUrl: "https://upload.wikimedia.org/wikipedia/commons/1/19/Boss-icon.png",
    speechRecognition: false,
    textToSpeech: true,
    inputBoxPlaceholder: 'Your Text Here',

  }
  const handleMessage = (message) => {
    return { text: 'Handle static ' + message.data }
  }
  return <ChatBotComponent options={options} handleMessage={handleMessage} />
}

Chatbot with HTTP Request

A sample HTTP Request server in python is available on Github


import { ChatBotComponent } from 'react-chatbot-with-text-and-speech'
 import 'react-chatbot-with-text-and-speech/dist/index.css'


const ChatBotWithHttp = () => {

  /*
  result of http://localhost:5000/api/answer?question=hi

  {"result":"How are you doing?"}


   */

  const handleMessage = async (message) => {
    try {
      const result = await fetch('http://localhost:5000/api/answer?question=' + message.data);
      const json = await result.json();
      return {text: json.result}

    } catch (e) {
      console.log(e)
      // handle error
    }

  };


  const options = {
    botImageUrl: "https://upload.wikimedia.org/wikipedia/commons/1/19/Boss-icon.png",
    speechRecognition: false,
    textToSpeech: true,
    inputBoxPlaceholder: 'Your Text Here',

  }


  return <ChatBotComponent options={options} handleMessage={handleMessage} />
}

Chatbot with websocket

A sample Websocket in python is available on Github


import { ChatBotComponent } from 'react-chatbot-with-text-and-speech'
 import 'react-chatbot-with-text-and-speech/dist/index.css'

const ChatBotWithWebsocket = () => {


  /*
Implementation ws://127.0.0.1:5000/bot

> Log will appear here
> Connecting to: ws://127.0.0.1:5000/bot
> Connection established
> SENT: hi
> RECEIVED: Hello


   */


  const [webSocketOpened, setWebSocketOpened] = React.useState(false);
  const ws = useRef(null);
  useEffect(() => {
    ws.current = new WebSocket("ws://127.0.0.1:5000/bot");
    ws.current.onopen = () => setWebSocketOpened(true);
    ws.current.onclose = () =>  setWebSocketOpened(false);

    const wsCurrent = ws.current;

    return () => {
      wsCurrent.close();
    };
  }, []);



  const handleMessage = (message) => {
    if(!webSocketOpened){
      console.log('Socket not open')
    }

    return new Promise((resolve, reject) => {
      ws.current.addEventListener('message', (event) => {
        console.log('Received message from server',event.data)
        resolve({ text: event.data})
        ws.current.onMessage = null;
      });
      ws.current.send(message.data)
    });

  };


  const options = {
    botImageUrl: "https://upload.wikimedia.org/wikipedia/commons/1/19/Boss-icon.png",
    speechRecognition: false,
    textToSpeech: true,
    inputBoxPlaceholder: 'Your Text Here',

  }


  return <ChatBotComponent options={options} handleMessage={handleMessage} />
}

License

MIT © muthuishere