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

context-chatbot

v1.0.2

Published

Context module for bot chat

Downloads

16

Readme

Context For Chat Bot

Simplify your code for chatbot without headache.

⚠️Breaking change⚠️


version 0.1.7 and above no longer use context variable, if you want update version from 0.1.6 and below, please check variable naming

Installation


Copy this code to your command line.

npm i context-chatbot

and add this code to your project.

import { Ctx }  from "context-chatbot";

Quick Start


  • first, register context to module.
Ctx.registerContext('welcome', (id, payload)=>{
    console.log('hello ' + id)
    console.log(payload)
})

or register from array.

Ctx.registerArrayContext([
    {
        state: 'welcome',
        callback: (id, payload)=>{
            console.log('welcome' + id)
            console.log(payload)
        }
    }
])
  • then, set state for id (person/number/etc).
Ctx.setState('6281955551111', 'welcome')
  • finally, put this line code to your bot file.
Ctx.Context('6281955551111', 'this is payload')
// output:
// 'welcome 6281955551111'
// 'this is payload'

Context with timer


This function allow you to restricted response from user (time out), if the time is up, then the state will change to the desired state

Ctx.registerArrayContext([
    {
        state: 'welcome',
        callback: (id, payload, timeOut)=>{
            console.log('welcome' + id)
            console.log(payload)
            //Dont forget to clear timeout
            clearTimeOut(timeOut)
        }
    }
])
//if there is no input for 5 seconds, state will change from 'welcome' to 'base'
Ctx.setState(id, "welcome", 5000, "base");

Using middleware


if you dont know where to place setState function to change state, maybe this can help you

Ctx.setMiddleware((id, payload, next) => {
	if (payload.message.includes("!login")) {
		Ctx.setState(id, "login_ask_username");
	}
	next();
});

API



Ctx.registerContext(state, callback)


Create a context with named state

  • state: string
  • callback: Function
    • id: string
    • payload: any
    • timeOut: optional setTimeOut function for Ctx with timer

Ctx.registerArrayContext(arr)


Create a context from array of object

  • arr: Array of object
    • state: string
    • callback: Function
      • id: string
      • payload: any
      • timeOut: optional setTimeOut function for context with timer

Ctx.setState(id, state)


change the state of an id

  • id: string
  • state: string
  • timer: miliseconds number (Optional) set time out context response
  • backTo: string (Optional) go to target state if timer is timeout

Ctx.Context(id, payload)


determine context of an id (if state id not found, 'base' status will be added automatically. Make sure to add 'base' context first, but you can change the default state, check API below)

  • id: string
  • state: any

Ctx.setDefaultState(state)


set default state if state of id not found

  • state: string

Ctx.setMiddleware(midFunc)


set middleware before enter the context

  • midFunc: Function
    • id: string
    • payload: any
    • next: Function

@adiwajshing/baileys implementation


coming soon~