/**
* Copy TikTok Dashboard Script (Approach A: Bitable Dashboard)
* Demonstrates how to copy the existing TikTok dashboard
* Uses the lark-mcp MCP server proxy for enhanced reliability
*
* Run with: npm run tiktok:copy
*/
import { LarkDashboardClient } from '../src';
// Configuration
const CONFIG = {
APP_TOKEN: 'C8kmbTsqoa6rBesTKRpl8nV8gHd',
TABLE_ID: 'tblG4uuUvbwfvI9Z',
EXISTING_DASHBOARD_ID: 'blkxYx6MmEeujy0v',
MCP_PROXY_URL: process.env.LARK_MCP_PROXY_URL || 'https://lark-mcp.hypelive.app',
API_KEY: process.env.LARK_API_KEY || '',
REGION: (process.env.LARK_REGION as 'sg' | 'cn' | 'us') || 'sg',
};
async function main() {
try {
console.log('╔═══════════════════════════════════════════════════════════════╗');
console.log('║ TikTok Dashboard Copy Tool (Approach A) ║');
console.log('║ Using lark-mcp MCP Proxy ║');
console.log('╚═══════════════════════════════════════════════════════════════╝\n');
// Initialize client with MCP proxy URL
const client = new LarkDashboardClient({
apiKey: CONFIG.API_KEY || 'mcp-proxy-no-key-needed',
region: CONFIG.REGION,
apiUrl: CONFIG.MCP_PROXY_URL,
logging: true,
});
console.log('Configuration:');
console.log(` App Token: ${CONFIG.APP_TOKEN}`);
console.log(` Table ID: ${CONFIG.TABLE_ID}`);
console.log(` Source Dashboard: ${CONFIG.EXISTING_DASHBOARD_ID}`);
console.log(` MCP Proxy: ${CONFIG.MCP_PROXY_URL}`);
console.log(` Region: ${CONFIG.REGION}\n`);
// Step 1: List existing dashboards
console.log('Step 1: Listing existing dashboards...');
const dashboards = await client.listDashboards(CONFIG.APP_TOKEN);
console.log(`Found ${dashboards.length} existing dashboard(s):\n`);
dashboards.forEach((dashboard, index) => {
console.log(` ${index + 1}. ${dashboard.name || 'Unnamed Dashboard'}`);
console.log(` Block ID: ${dashboard.block_id}`);
console.log(` Type: ${dashboard.block_type}`);
console.log();
});
// Step 2: Copy the dashboard
console.log('Step 2: Copying dashboard...');
const newDashboardName = `TikTok Analytics - ${new Date().toISOString().split('T')[0]}`;
const newDashboardId = await client.createDashboard(
{
name: newDashboardName,
appToken: CONFIG.APP_TOKEN,
},
CONFIG.EXISTING_DASHBOARD_ID
);
console.log(`Successfully created new dashboard!`);
console.log(` Name: ${newDashboardName}`);
console.log(` Dashboard ID: ${newDashboardId}\n`);
// Step 3: Get dashboard details
console.log('Step 3: Fetching new dashboard details...');
const dashboardDetails = await client.getDashboard(CONFIG.APP_TOKEN, newDashboardId);
console.log('Dashboard details:');
console.log(JSON.stringify(dashboardDetails, null, 2));
console.log();
// Success message
console.log();
console.log('╔═══════════════════════════════════════════════════════════════╗');
console.log('║ ✓ Dashboard Copied Successfully! ║');
console.log('╚═══════════════════════════════════════════════════════════════╝\n');
console.log('View your new dashboard at:');
console.log(` https://hypelive.sg.larksuite.com/base/${CONFIG.APP_TOKEN}?dashboard=${newDashboardId}`);
console.log();
console.log('Next steps:');
console.log(' 1. Open the dashboard in Lark');
console.log(' 2. Customize the charts and metrics');
console.log(' 3. Share with your team');
console.log();
console.log('Related Commands:');
console.log(' npm run tiktok:analyze - Analyze TikTok data');
console.log(' npm run tiktok:create - Create new dashboard');
console.log(' npm run approach-a:quickstart - Run all Approach A tasks');
console.log();
} catch (error: any) {
console.error('\nError:', error.message);
if (error.message.includes('Authentication')) {
console.error('\nAuthentication failed. Please check:');
console.error('1. Your LARK_API_KEY is correct');
console.error('2. The API key has proper permissions');
console.error('3. The region setting matches your workspace');
}
if (error.message.includes('not found')) {
console.error('\nResource not found. Please verify:');
console.error('1. The app token is correct');
console.error('2. The dashboard ID exists');
console.error('3. You have access to the base');
}
process.exit(1);
}
}
// Run if executed directly
if (require.main === module) {
main();
}
export { main };