+
Skip to content

feri-irawan/fiwa

Repository files navigation

fiwa

A simple WhatsApp bot built on top of Baileys.

Features

  • Event-driven architecture for handling WhatsApp events like ready, message, qr, and more.
  • Send and receive messages with ease.
  • Group management: join, leave, and fetch group metadata.
  • Automatic reconnection with retry logic.
  • Customizable options for session management, logging, and device configuration.

Installation

You can install fiwa using your preferred package manager, but I recommend using Bun:

bun i fiwa

Usage

Here is a basic example of how to use the library:

import { FiWhatsAppClient } from "fiwa";

const client = new FiWhatsAppClient({
  phoneNumber: "1234567890", // Optional: for pairing code
  maxRetries: 5, // Optional: retry attempts for reconnection
});

client.on("qr", (qr) => {
  console.log("Scan this QR code:", qr);
});

client.on("ready", () => {
  console.log("Client is ready");
});

client.on("message", (message) => {
  const jid = message.key.remoteJid;
  client.sendText(jid!, "Hello world!");
});

client.start();

MongoDB Integration

fiwa uses the file system by default to store authentication state. However, fiwa now supports using MongoDB to store authentication state. This is useful for scenarios where you want to persist session data in a NoSQL database instead of the file system.

Configuration

To use MongoDB for session state, provide the mongodb option when creating the FiWhatsAppClient instance:

const client = new FiWhatsAppClient({
  mongodb: {
    url: "<your-mongodb-url>",
    databaseName: "<your-database-name>", // Optional, defaults to "fiwa"
    collectionName: "<your-collection-name>", // Optional, defaults to "fiwa_auth_state"
  },
});

Example

import { FiWhatsAppClient } from "fiwa";

const client = new FiWhatsAppClient({
  mongodb: {
    url: "mongodb+srv://...",
  },
});

client.on("ready", () => {
  console.log("Client is ready");
});

client.start();

When using MongoDB, the library will automatically handle storing and retrieving authentication credentials and keys from the specified collection.

API Reference

FiWhatsAppClient

Constructor

new FiWhatsAppClient(options?: FiWhatsAppOptions);
  • options (optional):
    • logPath (string): Path to the log file.
    • sessionDir (string): Directory for session data.
    • maxRetries (number): Maximum retry attempts for reconnection.
    • browser (string): Browser type (e.g., macOS, Windows).
    • device (string): Device name.
    • phoneNumber (string): Phone number for pairing.

Methods

  • start(): Promise<void>: Starts the WhatsApp client.
  • disconnect(): Promise<void>: Disconnects the client.
  • sendText(to: string, text: string): Promise<void>: Sends a text message.
  • getGroupMetadata(groupId: string): Promise<any>: Fetches metadata for a group.
  • joinGroup(inviteCode: string): Promise<void>: Joins a group using an invite code.
  • leaveGroup(groupId: string): Promise<void>: Leaves a group.

Events

  • ready: Emitted when the client is ready.
  • message: Emitted when a new message is received.
  • qr: Emitted when a QR code is generated.
  • pairingCode: Emitted when a pairing code is generated.
  • reconnect: Emitted when the client reconnects.
  • logout: Emitted when the client logs out.
  • error: Emitted when an error occurs.

Contribution

See CONTRIBUTING.md for details on how to contribute to this project.

License

This project is licensed under the MIT License. See the LICENSE file for details.

This project was created using bun init in bun v1.2.9. Bun is a fast all-in-one JavaScript runtime.

About

A lightweight WhatsApp bot with built-in MongoDB integration.

Topics

Resources

License

Stars

Watchers

Forks

Contributors 2

  •  
  •  
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载