1.0.23 • Published 4 months ago

gear-roboto v1.0.23

Weekly downloads
-
License
ISC
Repository
github
Last release
4 months ago

gear-roboto

gear-roboto é um mini framework para criar e organizar a lógica de chatbots, permitindo o transporte de mensagens e o monitoramento de eventos.

🚀 Instalação

npm install gear-roboto

🔥 Exemplo de Uso

import { DefaultChatBot, DefaultCommander, CommandLineEngine, CommandLineTransporter } from "gear-roboto";

async function main() {
    const commander = new DefaultCommander("!");
    commander.addCommand("hello", (engine, author) => engine.send(author, { text: "world", type: "text" }));
    
    const engine = new CommandLineEngine(commander);
    const transporter = new CommandLineTransporter();
    
    const chatbot = new DefaultChatBot(engine, transporter);
    await chatbot.init();
    chatbot.send("you", { type: "text", text: "Digite algum comando iniciando por !" });
}

main();

🛠 Estrutura Principal

A biblioteca possui quatro classes principais:

  1. Commander – Gerencia os comandos do chatbot.
  2. Engine – Controla as interações externas.
  3. Transporter – Gerencia o transporte de mensagens e eventos.
  4. Chatbot – Faz a comunicação entre Engine e Transporter.

🎯 Commander (Gerenciador de Comandos)

O Commander é responsável por gerenciar os comandos do chatbot. Ao instanciá-lo, você define um prefixo para os comandos:

const commander = new DefaultCommander("/");

Adicione comandos com callbacks:

commander.addCommand("hello", (engine, author, args) => {
    engine.send(author, { text: "world", type: "text" });
});

📌 A callback recebe os parâmetros na seguinte ordem: engine, author, args

Para que os comandos sejam processados, o Commander precisa ser injetado em um objeto Engine:

const engine = new CommandLineEngine(commander);

⚙️ Engine (Gerenciador de Interações)

O Engine é responsável por lidar com interações externas. A classe base DefaultEngine pode ser estendida para diferentes plataformas.

A CommandLineEngine, por exemplo, usa readline para interagir com o usuário:

async monitoring() {
    const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
    while (this.status === 'connected') {
        const text = await rl.question("");
        const author = "you";
        this.getEmitter().emit('g.msg', { type: "text", author, text, isGroup: false });
        
        if (this.commander?.isCommand(text)) {
            const { command, args } = this.commander.extractCommandAndArgs(text);
            const fun = this.commander.searchCommand(command);
            fun ? fun(this, author, args) : this.send(author, { type: "text", text: "Comando não encontrado" });
        }
    }
    rl.close();
}

Principais métodos da Engine:

  • send(to: string, message: IMessageSend) – Envia mensagens.
  • monitoring() – Processa mensagens recebidas.

🔄 Transporter (Transportador de Mensagens)

O Transporter gerencia o transporte de eventos e pode ser estendido para diferentes plataformas (RabbitMQ, WebSocket, Kafka, etc.).

Exemplo com a CommandLineTransporter:

const transporter = new CommandLineTransporter();

🤖 Chatbot (Gerenciador Geral)

A classe Chatbot conecta a Engine ao Transporter e permite enviar mensagens sem acessar a Engine diretamente:

const chatbot = new DefaultChatBot(engine, transporter);
await chatbot.init();
chatbot.send("you", { type: "text", text: "Digite algum comando iniciando por !" });

📜 Licença

ISC © isaias-silva

1.0.19

4 months ago

1.0.18

4 months ago

1.0.17

4 months ago

1.0.16

4 months ago

1.0.9

4 months ago

1.0.8

4 months ago

1.0.7

4 months ago

1.0.6

4 months ago

1.0.5

4 months ago

1.0.4

4 months ago

1.0.22

4 months ago

1.0.21

4 months ago

1.0.20

4 months ago

1.0.23

4 months ago

1.0.11

4 months ago

1.0.10

4 months ago

1.0.15

4 months ago

1.0.14

4 months ago

1.0.13

4 months ago

1.0.12

4 months ago

1.0.3

5 months ago

1.0.2

5 months ago

1.0.1

5 months ago

1.0.0

5 months ago