Giter Site home page Giter Site logo

kb2abot's Introduction

kb2abot

size code-version visitors CodeFactor

About

kb2abot is a powerful Node.js module that allows you to easily interact with the Facebook Chat API and more!

  • Object-oriented
  • Performant
  • Easy to use

Installation

Node.js 16.6.0 or newer is required.

npm install kb2abot
yarn add kb2abot
pnpm add kb2abot

Example usage

Create a report command (report bug/error to specific user on Facebook):

import {Command} from "kb2abot"
const khoakomlemID = "100007723935647"

class Report extends Command {
	keywords = ["report", "bug"];
	description = "report bug/error to admin";
	guide = "<message>";
	permission = {
		"*": "*"
	};

	// Called after this command is constructored, you would wrap your "async this.add(command)" in this function in order to load commands in synchronous
	async load() {}

	// Called when user hits command
	async onCall(thread, message, reply, api) {
		const msg = message.args[0]
		if (msg.length > 0) {
			await api.sendMessage(`"${msg}"\n\n-ID: ${message.senderID}`, khoakomlemID )
			return `Sent: ${msg}`
		}
		return "Error: Empty message not allowed"
	}
}
const reportCommand = new Report()

Afterwards we add that command to a plugin:

import {readFileSync} from "fs"
import {resolve} from "url"
import {Plugin} from "kb2abot"

class MyPlugin extends Plugin {
	package = JSON.parse(
		readFileSync(new URL(resolve(import.meta.url, "package.json")))
	);

	// Called after this plugin is constructored (you would wrap your "async this.commands.add(command)" in this function in order to load commands in synchronous)
	async load() {
		// Add report command to this plugin
		await this.commands.add(reportCommand)
	}

	// Called when this plugin is disabled
	async onDisable() {}

	// Called when this plugin is enabled
	async onEnable() {}
}
const myPlugin = new MyPlugin()

And we add that plugin to a plugin manager

import {PluginManager} from "kb2abot"
const configDirectory = "./config"
const userdataDirectory = "./userdata" // relative to cwd
const pluginManager = new PluginManager(configDirectory, userdataDirectory)
await pluginManager.add(myPlugin)

Finally, now we add pluginManager to hook function:

import {Deploy, Datastore} from "kb2abot"
import {readHJSON} from "kb2abot/util/common"
Datastore.init("./datastores") // If you dont init datastore, your bot will be freeze and throw timeout error
const botOptions = readHJSON("./bot.hjson") // Read and parse bot.hjson file (relative to cwd)

botOptions is the options of your bot (see the example template at example-bot.hjson

const client = await Deploy.facebook(botOptions.credential, {
	apiOptions: botOptions.fcaOptions,
	pluginManager: pluginManager
})

All done! Now, imagine a user sending a message with body: "/report Hello admin", the bot will reply "Sent: Hello admin". Now you can code what you want and create a wonderful bot ๐ŸŒŸ

Links

Contributing

Before creating an issue, please ensure that it hasn't already been reported/suggested See the contribution guide if you'd like to submit a PR.

Help

If you don't understand something in the documentation, you are experiencing problems, or you just need a gentle nudge in the right direction, please don't hesitate to join our community KB2A Community.

kb2abot's People

Contributors

khoakomlem 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.