Resource Management
Always perform a graceful shutdown by calling sdk.close() when the process terminates (e.g., on SIGINT) to prevent hanging connections.
Message Deduplication
To prevent memory leaks from the built-in message deduplication cache, periodically clear old messages using sdk.clearProcessedMessages(count). You can monitor the cache size with sdk.getProcessedMessageCount().
Error Handling
Wrap message sending in try/catch blocks. Errors often include a response.status field (e.g., 404 if a chatGuid is invalid).
Auto-creating Chats
When calling sdk.messages.sendMessage, if the chatGuid does not correspond to an existing chat, the SDK will automatically attempt to create the chat before sending the message.
// Graceful shutdown
process.on("SIGINT", async () => {
await sdk.close();
process.exit(0);
});
// Prevent memory leaks
sdk.clearProcessedMessages(1000);
// Error handling
try {
await sdk.messages.sendMessage({ chatGuid: "invalid", message: "hi" });
} catch (error) {
if (error.response?.status === 404) console.error("Chat not found");
}