Use Cases#
This library supports two different use cases depending on your needs:
1. Standalone bot#
You can run the bot as a standalone service that automatically listens and processes messages WhatsApp:
const bot = new WhatsappGptBot({
idInstance: "your-instance-id",
apiTokenInstance: "your-token",
openaiApiKey: "your-openai-api-key",
model: "gpt-4o",
systemMessage: "You are a helpful assistant."
});
// Start listening for webhooks and processing messages
bot.start();
2. Message handler#
Alternatively, you can use the bot as a utility to handle messages in your own bot or app:
const gptBot = new WhatsappGptBot({
idInstance: "your-instance-id",
apiTokenInstance: "your-token",
openaiApiKey: "your-openai-api-key",
model: "gpt-4o",
systemMessage: "You are a helpful assistant."
});
// No need to call start() - just use processMessage when needed
const { response, updatedData } = await gptBot.processMessage(message, sessionData);
// Handle the response in your own way
await yourBot.sendText(message.chatId, response);
// Store updated session data in your state system
yourSessionData.gptSession = updatedData;
Integration example#
Here's how to integrate GPT bot into your own state-based bot:
interface CustomSessionData {
lang?: string;
gptSession?: GPTSessionData; // Store GPT session data
}
const gptState: State<CustomSessionData> = {
name: "gpt_state",
async onEnter(message, data) {
// Initialize GPT session
data.gptSession = {
messages: [{ role: "system", content: gptBot.systemMessage }],
lastActivity: Date.now()
};
await bot.sendText(message.chatId, "Chat with GPT started!");
},
async onMessage(message, data) {
// Process messages with the GPT bot
const { response, updatedData } = await gptBot.processMessage(
message,
data.gptSession
);
await bot.sendText(message.chatId, response);
data.gptSession = updatedData;
return undefined; // Stay in the current state
}
};
This flexibility allows you to either run the bot independently or integrate its GPT capabilities into your system, while maintaining full control over the conversation flow and state management.
Key points about these options:
- Standalone Bot
- Uses internal state management
- Automatically handles webhooks
- Best for simple, single-function GPT chatbots
-
Requires calling
bot.start()
-
Message Handler
- Does not require internal state management
- Does not handle webhooks
- Ideal for integrating into existing bots
- Uses only GPT processing capabilities
- More flexible and controllable
- Never call
start()
- just useprocessMessage()