UNPKG

ecokind-moderation-sdk

Version:

EcoKind - A privacy-first moderation SDK powered by decentralized LLMs on the Internet Computer. Making the internet safer, one message at a time.

188 lines (159 loc) • 8.01 kB
const ICMessagingClient = require('../echo-client/index.js'); async function runExample() { console.log('šŸš€ Starting IC Messaging Client Example\n'); // Initialize the client const client = new ICMessagingClient(); try { // Step 1: Initialize connection to the canister console.log('šŸ“” Initializing connection to IC canister...'); await client.initialize(); console.log('āœ… Connected successfully!\n'); // Step 2: Authorize with project key console.log('šŸ” Authorizing with project credentials...'); const authorized = await client.authorize('machu', 'machu-1750486429293560682'); if (!authorized) { console.log('āŒ Authorization failed! Cannot proceed with other operations.'); return; } console.log('āœ… Authorization successful!\n'); // Step 3: Now we can use all protected functions // Example 1: Send a message console.log('šŸ“¤ Sending a message...'); const messageSuccess = await client.sendMessage( 'elsnu-exgt6-a6c4w-zvlfc-3oqrj-5ifj3-adiil-cagsu-sdtml-yeed7-mae', 'xraya-wv56e-7ddrm-fwhih-wx6er-3tiru-agv3f-efbkp-uwqbn-od643-rae', 'Hello! How are you doing today?' ); console.log(`Message sent: ${messageSuccess ? 'āœ… Success' : 'āŒ Failed'}\n`); // Example 2: Try to send a potentially harmful message console.log('🚫 Testing harassment detection...'); const harassmentMessage = await client.sendMessage( 'elsnu-exgt6-a6c4w-zvlfc-3oqrj-5ifj3-adiil-cagsu-sdtml-yeed7-mae', 'xraya-wv56e-7ddrm-fwhih-wx6er-3tiru-agv3f-efbkp-uwqbn-od643-rae', 'You are terrible and I hate you!' ); console.log(`Harassment message blocked: ${!harassmentMessage ? 'āœ… Blocked' : 'āŒ Not blocked'}\n`); // Example 2: Try to send a message console.log('🚫 Testing harassment detection...'); const harassmentMessages = await client.sendMessage( 'elsnu-exgt6-a6c4w-zvlfc-3oqrj-5ifj3-adiil-cagsu-sdtml-yeed7-mae', 'xraya-wv56e-7ddrm-fwhih-wx6er-3tiru-agv3f-efbkp-uwqbn-od643-rae', 'You are beautiful!' ); console.log(`Harassment message blocked: ${!harassmentMessages ? 'āœ… Blocked' : 'āŒ Not blocked'}\n`); // Example 3: Check harassment level console.log('šŸ” Checking harassment level of a message...'); const level = await client.harassmentLevel('This is a normal friendly message'); console.log(`Harassment level: ${level}\n`); // Example 4: Get message improvement suggestion console.log('šŸ’” Getting message improvement suggestion...'); const improved = await client.suggestImprovedMessage('ur message was bad and dumb'); console.log(`Original: "ur message was bad and dumb"`); console.log(`Improved: "${improved}"\n`); // Example 5: Receive messages console.log('šŸ“„ Receiving messages for receiver...'); const messages = await client.receiveMessages('xraya-wv56e-7ddrm-fwhih-wx6er-3tiru-agv3f-efbkp-uwqbn-od643-rae'); console.log(`Found ${messages.length} messages:`); messages.forEach((msg, index) => { const date = new Date(msg.timestamp / 1000000); // Convert nanoseconds to milliseconds console.log(` ${index + 1}. From: ${msg.sender.substring(0, 10)}... | "${msg.content}" | ${date.toLocaleString()}`); }); console.log(''); // Example 6: Validate different keys (this doesn't require authorization) console.log('šŸ”‘ Testing key validation...'); const validKey = await client.validateKey('machu', 'machu-1750486429293560682'); const invalidKey = await client.validateKey('machu', 'wrongproject-abc123'); console.log(`Valid key (machu-1750486429293560682): ${validKey ? 'āœ… Valid' : 'āŒ Invalid'}`); console.log(`Invalid key (wrongproject-abc123): ${invalidKey ? 'āœ… Valid' : 'āŒ Invalid'}\n`); // Example 7: Edit a message (if we have any) if (messages.length > 0) { console.log('āœļø Editing the first message...'); const editSuccess = await client.editMessage( 'elsnu-exgt6-a6c4w-zvlfc-3oqrj-5ifj3-adiil-cagsu-sdtml-yeed7-mae', // sender address 0, // index of first message 'Hello! How are you doing today? Hope you have a wonderful day!' ); console.log(`Message edited: ${editSuccess ? 'āœ… Success' : 'āŒ Failed'}\n`); } // Example 8: Check messages again after edit console.log('šŸ“„ Checking messages after edit...'); const updatedMessages = await client.receiveMessages('xraya-wv56e-7ddrm-fwhih-wx6er-3tiru-agv3f-efbkp-uwqbn-od643-rae'); console.log(`Updated messages count: ${updatedMessages.length}`); updatedMessages.forEach((msg, index) => { const date = new Date(msg.timestamp / 1000000); console.log(` ${index + 1}. From: ${msg.sender.substring(0, 10)}... | "${msg.content}" | ${date.toLocaleString()}`); }); console.log('\nšŸŽ‰ Example completed successfully!'); } catch (error) { console.error('āŒ Error running example:', error); // Additional error information if (error.message.includes('Not authorized')) { console.log('šŸ’” Tip: Make sure to call authorize() with valid project name and key'); } else if (error.message.includes('not initialized')) { console.log('šŸ’” Tip: Make sure the canister is deployed and accessible'); } else if (error.message.includes('network')) { console.log('šŸ’” Tip: Check your internet connection and IC network status'); } } } // Helper function to demonstrate different usage patterns async function demonstrateUsagePatterns() { console.log('\nšŸ“š Demonstrating different usage patterns...\n'); const client = new ICMessagingClient(); await client.initialize(); // Must authorize first const authorized = await client.authorize('machu', 'machu-1750486429293560682'); if (!authorized) { console.log('āŒ Cannot demonstrate patterns without proper authorization'); return; } // Pattern 1: Batch messaging console.log('šŸ“¦ Batch messaging pattern:'); const users = [ 'xraya-wv56e-7ddrm-fwhih-wx6er-3tiru-agv3f-efbkp-uwqbn-od643-rae', 'elsnu-exgt6-a6c4w-zvlfc-3oqrj-5ifj3-adiil-cagsu-sdtml-yeed7-mae' ]; const message = 'Hello everyone! This is a broadcast message.'; for (const user of users) { try { await client.sendMessage('system', user, message); console.log(` āœ… Message sent to ${user.substring(0, 10)}...`); } catch (error) { console.log(` āŒ Failed to send to ${user.substring(0, 10)}...: ${error.message}`); } } // Pattern 2: Message moderation workflow console.log('\nšŸ›”ļø Message moderation workflow:'); const testMessage = 'This message might be problematic'; const harassmentLevel = await client.harassmentLevel(testMessage); console.log(` Harassment level: ${harassmentLevel}`); if (harassmentLevel === 'High') { console.log(' 🚫 Message blocked due to high harassment level'); } else if (harassmentLevel === 'Moderate') { const improved = await client.suggestImprovedMessage(testMessage); console.log(` šŸ’” Suggested improvement: "${improved}"`); } else { const sent = await client.sendMessage( 'elsnu-exgt6-a6c4w-zvlfc-3oqrj-5ifj3-adiil-cagsu-sdtml-yeed7-mae', 'xraya-wv56e-7ddrm-fwhih-wx6er-3tiru-agv3f-efbkp-uwqbn-od643-rae', testMessage ); console.log(` āœ… Message sent: ${sent}`); } // Pattern 3: Authorization error handling console.log('\nšŸ”’ Demonstrating authorization requirement:'); const unauthorizedClient = new ICMessagingClient(); await unauthorizedClient.initialize(); try { await unauthorizedClient.sendMessage('test', 'test', 'This should fail'); } catch (error) { console.log(` āœ… Correctly blocked unauthorized access: ${error.message}`); } } // Run the examples if (require.main === module) { runExample() .then(() => demonstrateUsagePatterns()) .catch(console.error); } module.exports = { runExample, demonstrateUsagePatterns };