Wa-OpenAI Documentation

repository·main·Indexed 19 days ago

https://github.com/sansekai/wa-openai

A NodeJS-based WhatsApp bot version 2.1.0 that integrates OpenAI's ChatGPT and DALL-E services using the Baileys library. It allows users to generate text responses via the /ai command and images via the /img command. The bot features multi-device support, session persistence via an in-memory store, and automatic reconnection logic.

Tokens
997
Snippets
4
Records
7
Agent score
18%

What's inside Wa-OpenAI

  1. Overview of Wa-OpenAI features

    main

    Wa-OpenAI is a NodeJS library that integrates OpenAI's ChatGPT and DALL-E capabilities with WhatsApp using the Baileys library.

    It provides two primary features accessible via WhatsApp commands:

    • ChatGPT: Generates text responses using the /ai command.
    • DALL-E: Generates images from text descriptions using the /img command.
    Commands:
    - /ai <text> (ChatGPT)
    - /img <text> (DALL-E)
  2. Install and Run Wa-OpenAI

    main

    Follow these steps to set up the project locally:

    1. Clone the repository:
      git clone https://github.com/Sansekai/Wa-OpenAI.git
    2. Install dependencies:
      npm install
    3. Start the application:
      node index.js
    git clone https://github.com/Sansekai/Wa-OpenAI.git
    npm install
    node index.js
  3. Manage session persistence with the in-memory store

    main

    The application uses a makeInMemoryStore to keep track of messages and state. To ensure data is not lost on restart, the store is configured to:

    1. Read from ./session.json on startup.
    2. Write to ./session.json every 10,000 milliseconds (10 seconds) via a setInterval loop.

    This store is also used by the getMessage helper to retrieve message content from the cache using a WAMessageKey.

    // Internal implementation detail of how the store is managed:
    const store = Store(logger);
    store?.readFromFile("./session.json");
    
    setInterval(() => {
    	store?.writeToFile("./session.json");
    }, 10_000);
  4. How the WhatsApp bot handles incoming messages

    main

    The bot uses an event-driven architecture to process incoming messages. When a messages.upsert event occurs with the type notify, the following flow is executed:

    1. The raw upsert event is passed to the Messages(upsert, sock) function to normalize the message data.
    2. The bot checks if the message is valid and ensures the sender is not status@broadcast (WhatsApp Status updates).
    3. If valid, the event is passed to the core logic handler: require("./sansekai.js")(upsert, sock, store, message).

    This pattern allows the main index.js to act as a lightweight entrypoint/orchestrator while delegating business logic to sansekai.js.

  5. Connect to WhatsApp using connectToWhatsApp()

    main

    The connectToWhatsApp function initializes the WhatsApp connection using the Baileys library. It manages authentication via multi-file state, handles connection lifecycle events (open/close), and sets up an event processor for incoming messages.

    Key behaviors:

    • Authentication: Uses useMultiFileAuthState with the directory yusril to persist session data.
    • Connection Lifecycle: If the connection closes due to an error other than DisconnectReason.loggedOut, the function automatically attempts to reconnect.
    • Message Handling: When a new message arrives (messages.upsert with type notify), it processes the message using the Messages utility and then passes it to the main logic defined in sansekai.js.
    • Session Persistence: Uses an in-memory store that is automatically backed up to ./session.json every 10 seconds.
    // The function is called internally at the end of the file
    // but can be used to initiate the bot connection.
    const sock = await connectToWhatsApp(false);