1.1.2 • Published 1 year ago
fca-dunnn-bot v1.1.2
Introdution
Full instructions will be available soon.
- This is a project to help you create a simple Facebook chatbot.
- It is built based on the facebook-chat-api, and you can refer to the methods here: https://github.com/dunneeee/facebook-chat-api (The original project uses callbacks, but I can modify it to use promises, or I can implement both callback and promise-based methods for you.)
- If you encounter any errors during the usage, please report them to me.
- Give me a start on github: https://github.com/dunneeee/Dunn-Bot
- Thanks to using my project.
Contact
- Facebook: https://www.facebook.com/dunndunneee/
- Gmail: corepper03@gmail.com
- Github: https://github.com/dunneeee/
Install
npm i fca-dunnn-bot
Getting started
Note:
- You can get appState with: https://github.com/c3cbot/c3c-fbstate
- Folder tree
commands/
sayhello.js
example.js
node_modules/
appState.json
CustomController.js
CustomHook.js
index.js
package-lock.json
package.json
- Content
index.js
```javascript const { Deploy } = require("fca-dunnn-bot"); const { Fs } = require("fca-dunnn-bot/utils"); const CustomController = require("./CustomController"); const CustomHook = require("./CustomHook"); const APPSTATE = Fs.readJSON("appState.json"); class App extends Deploy { constructor() { super(); // Check has config of bot and set config if (Fs.existsSync("config.json")) { this.setConfig(Fs.readJSON("config.json")); } this.setController(new CustomController()); this.setHook(new CustomHook()); this.facebook(APPSTATE) .then((client) => { // Your code here client.message.setDelay(1000); // set delay to send message (ms) }) .catch(console.log); } } new App(); ```
CustomHook.js
```javascript const { Hook } = require("fca-dunnn-bot"); const { Fca } = require("fca-dunnn-bot/src/namespaces"); class CustomHook extends Hook { /** *You can custom prefix of bot here * Event: https://github.com/dunneeee/facebook-chat-api/blob/master/DOCS.md#listen * @param {Fca.MessageType} event */ async getPrefix(event) { const { bot } = this.config; // You can console.log this.config to see what is in config return bot.prefix || "!"; } } module.exports = CustomHook; ```
CustomController.js
```javascript const { Controller } = require("fca-dunnn-bot"); const sayhello = require("./commands/sayhello"); class CustomController extends Controller { constructor() { super(); this.authens; // this is authens of bot wil check before run command } /** * Function will run when bot start * @param {import("fca-dunnn-bot/src/namespaces/deploy").DeploySpace.Config} config */ async load(config) { //your code here this.commands.add(sayhello); // add Command to bot } /** * Function will run when bot login * @param {import("fca-dunnn-bot/src/namespaces/login").LoginSpace.Client} param0 */ async login({ api }) { //your code here } /** * Function will run when has new message or event * @param {import("fca-dunnn-bot/src/namespaces/controller").ControllerSpace.HookParams} param0 */ async hook({ event, uuid, api, bot, message }) { //your code here } } module.exports = CustomController; ```
commands/sayhello.js
```javascript const { Command } = require("fca-dunnn-bot"); const { CommandSpace } = require("fca-dunnn-bot/src/namespaces"); class SayHello extends Command { constructor() { super({ name: "sayhello", description: "Say hello to you", usage: "<prefix>sayhello", cooldown: 10000, permission: CommandSpace.Permission.USER, author: "LT.Dũng", }); } /** * * @param {CommandSpace.OnCallParams} param0 */ async onCall({ args, event }) { const { message, api } = this.tools; //Send message await api.sendMessage("Hello world!", event.threadID, event.messageID); //Send message with delay await message.reply("Hello world!", event.threadID, event.messageID); //or return "Hello world!"; // or } } module.exports = new SayHello(); ```
commands/example.js (This is example file command)
```javascript const { Command } = require("fca-dunnn-bot"); const { CommandSpace } = require("fca-dunnn-bot/src/namespaces"); /** * Compoment command example * @date 3/24/2023 - 1:47:51 PM * * @class Example * @typedef {Example} * @extends {Command} */ class Example extends Command { constructor() { super({ name: "example", // Name of command (required) It's not allowed to have the same name with other command author: "your name | any", // Author of command (optional) cooldown: 10000, // Cooldown of command (ms) -> 10000 = 10s description: "Example command", // Description of command usage: "<prefix>example", // Usage of command <prefix> will be replaced by bot's prefix permission: CommandSpace.Permission.USER, // Permission of command (USER, ADMIN, OWNER, ADMINBOX) if not set, it will be USER }); } /** * Comment this to let vscode or other IDE suggest variables * This function will be executed when user call command * @param {CommandSpace.OnCallParams} param0 */ async onCall({ args, event }) { // All functions with callback can use async await or promise this.tools.api; // Include api functions: https://github.com/dunneeee/facebook-chat-api/blob/master/DOCS.md# this.tools.bot; // Bot config in file config.json this.tools.controller; // Controller class (CustomController) this.tools.hook; // Hook class (CustomHook) this.tools.message; // Send message with delay this.tools.name; // Name of account this.tools.uuid; // uid of account // write your code } /** * This function will be executed when user reply message in this.tools.controller.queueReply * @param {CommandSpace.OnReplyParams} param0 */ async onReply({ event, replyData }) { // write your code } /** * This function will be executed when user react message in this.tools.controller.queueReaction * @param {CommandSpace.OnReactionParams} param0 */ async onReaction({ event, reactionData }) { // write your code } /** * This function will be executed when any message or event is received * @param {CommandSpace.OnAlwayParams} param0 */ async onAlway({ event }) { // write your code } /** * This function will be executed when user unsend message * @param {CommandSpace.OnUnSendParams} param0 */ async onUnsend({ event }) { // write your code } } // Don't forget to export your command module.exports = new Example(); ```
- Start project
node index.js
- Test your bot.
- Default prefix is !
- In this example you can try !sayhello