totigm / bot-builder

This is a library to create bots for different platforms. It handles all the commands stuff behind the scenes, so you can focus on your bot's logic.
MIT License
6 stars 1 forks source link

Does it support multi device❓ #2

Closed Cyberkingcr7 closed 3 weeks ago

Cyberkingcr7 commented 3 weeks ago

hi great work btw, does this work on multi device? And do you think you’d ever make one based on whiskeysockets/baileys?

totigm commented 3 weeks ago

Hi! Thank you!

Currently, it should work on multiple devices. If you encounter any specific issues, please feel free to report them.

Regarding @whiskeysockets/baileys, I'm not familiar with it yet, but it does look interesting. While I don't have immediate plans to adapt my project for it, I'm definitely open to the idea. If you or anyone else is interested in this integration, feel free to fork the repo and/or create your own version tailored to @whiskeysockets/baileys. For inspiration or guidance on how it might be integrated with WhatsApp services, you might find the whatsapp-bot repo helpful. Contributions are always welcome, and I'm curious to see what you might come up with!

Thanks for your interest!

totigm commented 3 weeks ago

I have done a small experiment with @whiskeysockets/baileys based on the whatsapp-bot repo. It's still a work in progress and isn't fully functional yet, but it could serve as a useful starting point. Here's a brief overview of what I tried:

index.ts:

import WhatsappBot from "./bot";
import { useMultiFileAuthState } from "@whiskeysockets/baileys";

async function main() {
    const creds = await useMultiFileAuthState("auth_info_baileys");

    new WhatsappBot({
        creds,
    });
}

main();

index/types.ts:

import { Options as BotBuilderOptions } from "@totigm/bot-builder";
import { AuthenticationState } from "@whiskeysockets/baileys";

export * from "@totigm/bot-builder";

type BotOptions = Omit<BotBuilderOptions, "contentProp" | "textFormatting" | "authData"> & {
    messageEvent?: "message" | "message_create";
};

export type WhatsappBotOptions = {
    bot?: BotOptions;
    creds: {
        state: AuthenticationState;
        saveCreds: () => Promise<void>;
    };
};

bot/index.ts:

import Bot from "@totigm/bot-builder";
import { WhatsappBotOptions } from "../types";
import makeWASocket, { DisconnectReason } from "@whiskeysockets/baileys";
import { Boom } from "@hapi/boom";
import { EventEmitter } from "events";

export default class WhatsappBot extends Bot {
    private socket: ReturnType<typeof makeWASocket>;

    constructor(options: WhatsappBotOptions) {
        const { state, saveCreds } = options.creds;
        const socket = makeWASocket({
            printQRInTerminal: true,
            auth: state,
        });

        const emitter = new EventEmitter();
        super(socket.ev as unknown as typeof emitter, {
            contentProp: "body",
            messageEvent: "message",
            ...options.bot,
        });

        socket.ev.on("creds.update", saveCreds);

        this.socket = socket;
    }

    protected auth() {
        this.client.on("connection.update", (update) => {
            const { connection, lastDisconnect } = update;
            if (connection === "close") {
                const shouldReconnect = (lastDisconnect.error as Boom)?.output?.statusCode !== DisconnectReason.loggedOut;
                console.log("connection closed due to ", lastDisconnect.error, ", reconnecting ", shouldReconnect);
                // reconnect if not logged out
                if (shouldReconnect) {
                    this.socket = makeWASocket({
                        printQRInTerminal: true,
                        auth: null,
                    });
                    this.auth();
                }
            } else if (connection === "open") {
                console.log("opened connection");
            }
        });

        this.client.on("messages.upsert", async (m) => {
            console.log(JSON.stringify(m, undefined, 2));

            console.log("replying to", m.messages[0].key.remoteJid);
            await this.socket.sendMessage(m.messages[0].key.remoteJid!, { text: "Hello there!" });
        });
    }
}

If you're interested, feel free to dive into the code, fork the repo, or even contribute to its development. Any insights or improvements are welcome!