Skip to content

Utilities#

The library provides several utility functions for common tasks:

Handling media files#

import { Utils } from '@green-api/whatsapp-chatgpt';

// Downloading a media file from a URL
const tempFile = await Utils.downloadMedia("https://example.com/image.jpg");

// Transcription of audio
const openai = new OpenAI({apiKey: "your-openai-api-key"});
const transcript = await Utils.transcribeAudio("/path/to/audio.ogg", openai);

// Clean up after processing
fs.unlinkSync(tempFile);

Conversation management#

import { Utils } from '@green-api/whatsapp-chatgpt';

// Trim conversation history
const trimmedMessages = Utils.trimConversationHistory(
messages,
10, // max messages
true // save system message
);

// Estimate token usage
const estimatedTokens = Utils.estimateTokens(messages);