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

pgi-chatbot

v1.1.0

Published

A small npm library for handling chatbot necessities for Saddlemountain Technologies

Downloads

4

Readme

PGI Chabot

PGI chatbot is a lightweight, easy to use question & response API for working with automated responses.

This bot uses a Typescript module to organize and better understand the code's structure. This module exports the main ChatBot class that works as the API's backbone.

Usage

To install the package, use the following command:

$ npm i pgi-chatbot

To initialize the bot, first, import the main class into the desired project:

import ChatBot from 'pgi-chatbot'

Once is initialized, you'll need to create an instance of it:

const chatbot = new ChatBot();

The bot's instance takes some pre-created parameters: a container where the chat will take place and a library object that contains the desired question/answers duos:

// Container Element with id 'chat-container'
const container = document.getElementById('chat-container');

// Library of Q/A
const library = {
  'hello': "Hi!"
  'goodbye|bye': "Bye-bye!"
};

So, an instance of the API will end up looking something like this:

import ChatBot from 'pgi-chatbot'

const container = document.getElementById('chat-container');

const library = {
  'hello': "Hi!"
  'goodbye|bye': "Bye-bye!"
};

const chatbot = new ChatBot(container, library);

Methods

Welcome Message

The bot contains a customizable welcoming message; to run it, first, initialize the bot and then run the welcome() method:

const chatbot = new ChatBot(container, library);

chatbot.welcome();

To customize the message, pass a string argument to the welcome() method:

chatbot.welcome('Hello there!');

Listen

The listen method is the basic entry point for the bot; it receives the message that the user has submitted and displays it in the previously defined container, then analyzes said message with the current library, and responds with either the defined answer or an error message if the message wasn't found in the library.

const container = document.getElementById('chat-container');

const library = { 'hello': "Hi!" };

const chatbot = new ChatBot(container, library);

chatbot.listen('Hello');

// Container Data:
// -> Hello
// -> Hi!

chatbot.listen('Hi there');

// Container Data:
// -> Hello
// -> Hi!
// -> Hi there
// -> Sorry, I didn't quite catched that

Respond

The respond method works similar to the listen one, except that it receives two parameters: a string message and an iterator instead of just the message. It's recommended to use this method when handling array libraries since the iterator will search for the respective answer pair in the array.

const container = document.getElementById('chat-container');

const library = ["Hi!"]

const chatbot = new ChatBot(container, library);

chatbot.respond('Hello!', 0);

// Container Data:
// -> Hello!
// -> Hi!

chatbot.respond('Hey!', 1);

// Container Data:
// -> Hello!
// -> Hi!
// -> Hey!
// -> Sorry, I didn't quite catched that

Library

The library may be one of the most important parts of the API; its format is a Javascript object that contains a series of string keys and string answers.

The keys must be in lowercase and can have a | separator if an answer can be triggered by more than one question.

Additionally, the library supports an array as its argument for listening to the user. It's recommended to use this method to implement an automatic form submission via the bot.

Aditional Notes

Setters

You can personalize the class name for the bot messages, the user messages and the error message utilizing the class' setters:

chatbot.botClassName = 'new-bot-class';

chatbot.userClassName = 'new-user-class';

chatbot.errorMessage = 'New error message!';

NPM Scripting

If there are changes to the .ts file, please run the

$ npm run compile

command to compile them into the .js file.