/**
* Create Sentiment Analytics Dashboard
* For: https://hypelive.sg.larksuite.com/base/IZ3cbKvLsakfGZsZgjvl38ifgwg
* Table: Social Daily Sentiment (tblkVYEcPh410eFw)
*/
import { LarkDashboardClient } from '../src/client';
import {
ChartBlockBuilder,
MetricsBlockBuilder,
TextBlockBuilder,
ViewBlockBuilder
} from '../src/builders';
import { AggregationType, ChartType } from '../src/types';
// Configuration
const APP_TOKEN = 'IZ3cbKvLsakfGZsZgjvl38ifgwg';
const TABLE_ID = 'tblkVYEcPh410eFw';
// Field names from the table
const FIELDS = {
ID: 'Social-tracking-id',
DATE: 'Date',
COMMENT_TEXT: 'Comment Text',
SENTIMENT: 'Sentiment',
SENTIMENT_POSITIVE: 'Sentiment Score Positive',
SENTIMENT_NEUTRAL: 'Sentiment Score Neutral',
SENTIMENT_NEGATIVE: 'Sentiment Score Negative',
SENTIMENT_CONFIDENCE: 'Sentiment Confidence',
PLATFORM: 'Platform',
POST_URL: 'Post URL',
POST_TIMESTAMP: 'Post Timestamp',
POST_COMMENTS: 'Post Comments Count',
POST_LIKES: 'Post Likes Count',
POST_SHARES: 'Post Shares Count',
REPLIES_COUNT: 'Replies Count',
THEME_CATEGORY: 'Theme Category',
ISSUE_TYPE: 'Issue Type',
PROFILE_NAME: 'Profile Name',
LIKES_COUNT: 'Likes Count',
STATUS: 'Status',
};
async function createSentimentDashboard() {
console.log('╔═══════════════════════════════════════════════════════════════╗');
console.log('║ Sentiment Analytics Dashboard Creator ║');
console.log('║ Using lark-dashboard-sdk ║');
console.log('╚═══════════════════════════════════════════════════════════════╝');
console.log('');
// Initialize client
const apiKey = process.env.LARK_API_KEY || process.env.LARK_MCP_API_KEY;
if (!apiKey) {
console.error('❌ Error: LARK_API_KEY or LARK_MCP_API_KEY environment variable not set');
console.log('');
console.log('Please set your API key:');
console.log(' export LARK_API_KEY="your-api-key"');
console.log('');
process.exit(1);
}
console.log('Configuration:');
console.log(` App Token: ${APP_TOKEN}`);
console.log(` Table ID: ${TABLE_ID}`);
console.log(` API Key: ${apiKey.substring(0, 10)}...`);
console.log('');
const client = new LarkDashboardClient({
apiKey,
region: 'sg',
logging: true,
});
// Build dashboard blocks
console.log('Building dashboard blocks...');
console.log('');
const blocks = [];
// 1. Title Block
console.log(' 1. Creating title block...');
const titleBlock = TextBlockBuilder
.heading('📊 World App Sentiment Analytics Dashboard')
.build();
blocks.push(titleBlock);
// 2. KPI: Total Comments
console.log(' 2. Creating Total Comments KPI...');
const totalCommentsBlock = MetricsBlockBuilder
.count(FIELDS.ID)
.dataSource(APP_TOKEN, TABLE_ID)
.title('Total Comments')
.build();
blocks.push(totalCommentsBlock);
// 3. KPI: Total Likes
console.log(' 3. Creating Total Likes KPI...');
const totalLikesBlock = MetricsBlockBuilder
.sum(FIELDS.POST_LIKES)
.dataSource(APP_TOKEN, TABLE_ID)
.title('Total Likes')
.build();
blocks.push(totalLikesBlock);
// 4. KPI: Total Shares
console.log(' 4. Creating Total Shares KPI...');
const totalSharesBlock = MetricsBlockBuilder
.sum(FIELDS.POST_SHARES)
.dataSource(APP_TOKEN, TABLE_ID)
.title('Total Shares')
.build();
blocks.push(totalSharesBlock);
// 5. KPI: Avg Sentiment Confidence
console.log(' 5. Creating Avg Sentiment Confidence KPI...');
const avgConfidenceBlock = MetricsBlockBuilder
.average(FIELDS.SENTIMENT_CONFIDENCE)
.dataSource(APP_TOKEN, TABLE_ID)
.title('Avg Confidence')
.decimals(2)
.build();
blocks.push(avgConfidenceBlock);
// 6. Sentiment Pie Chart
console.log(' 6. Creating Sentiment Pie Chart...');
const sentimentPieBlock = ChartBlockBuilder
.pie()
.dataSource(APP_TOKEN, TABLE_ID)
.xAxis(FIELDS.SENTIMENT)
.title('Sentiment Distribution')
.legend(true)
.build();
blocks.push(sentimentPieBlock);
// 7. Platform Bar Chart
console.log(' 7. Creating Platform Bar Chart...');
const platformBarBlock = ChartBlockBuilder
.bar()
.dataSource(APP_TOKEN, TABLE_ID)
.xAxis(FIELDS.PLATFORM)
.yAxis(FIELDS.ID, AggregationType.COUNT, 'Comment Count')
.title('Comments by Platform')
.legend(true)
.build();
blocks.push(platformBarBlock);
// 8. Comments Over Time Line Chart
console.log(' 8. Creating Comments Over Time Line Chart...');
const timelineBlock = ChartBlockBuilder
.line()
.dataSource(APP_TOKEN, TABLE_ID)
.xAxis(FIELDS.DATE)
.yAxis(FIELDS.ID, AggregationType.COUNT, 'Comments')
.title('Comments Over Time')
.legend(true)
.build();
blocks.push(timelineBlock);
// 9. Theme Category Bar Chart
console.log(' 9. Creating Theme Category Bar Chart...');
const themeBarBlock = ChartBlockBuilder
.bar()
.dataSource(APP_TOKEN, TABLE_ID)
.xAxis(FIELDS.THEME_CATEGORY)
.yAxis(FIELDS.ID, AggregationType.COUNT, 'Comment Count')
.title('Comments by Theme')
.legend(true)
.build();
blocks.push(themeBarBlock);
// 10. Recent Comments Table
console.log(' 10. Creating Recent Comments Table...');
const tableBlock = ViewBlockBuilder
.grid()
.dataSource(APP_TOKEN, TABLE_ID)
.title('Recent Comments')
.toolbar(true)
.build();
blocks.push(tableBlock);
console.log('');
console.log(`Total blocks to create: ${blocks.length}`);
console.log('');
// Create dashboard
console.log('Creating dashboard...');
console.log('');
try {
const results = await client.createDashboard({
name: 'Sentiment Analytics Dashboard',
appToken: APP_TOKEN,
blocks,
});
console.log('');
console.log('╔═══════════════════════════════════════════════════════════════╗');
console.log('║ DASHBOARD CREATED! ║');
console.log('╚═══════════════════════════════════════════════════════════════╝');
console.log('');
let successCount = 0;
let failCount = 0;
results.forEach((result, index) => {
if (result.success) {
console.log(` ✅ Block ${index + 1}: Created (ID: ${result.blockId})`);
successCount++;
} else {
console.log(` ❌ Block ${index + 1}: Failed - ${result.error}`);
failCount++;
}
});
console.log('');
console.log(`Summary: ${successCount} succeeded, ${failCount} failed`);
console.log('');
console.log('View your dashboard at:');
console.log(` https://hypelive.sg.larksuite.com/base/${APP_TOKEN}`);
console.log('');
} catch (error: any) {
console.error('');
console.error('╔═══════════════════════════════════════════════════════════════╗');
console.error('║ ERROR ║');
console.error('╚═══════════════════════════════════════════════════════════════╝');
console.error('');
console.error(`Error: ${error.message}`);
console.error('');
if (error.response) {
console.error('API Response:', JSON.stringify(error.response.data, null, 2));
}
console.error('');
console.error('Troubleshooting:');
console.error(' 1. Check your LARK_API_KEY is valid');
console.error(' 2. Ensure you have permissions to the base');
console.error(' 3. The Dashboard Block API may require a dashboard view to exist first');
console.error('');
console.error('If the API requires an existing dashboard view:');
console.error(' 1. Open: https://hypelive.sg.larksuite.com/base/IZ3cbKvLsakfGZsZgjvl38ifgwg');
console.error(' 2. Create a Dashboard view manually');
console.error(' 3. Then run this script again to add blocks');
console.error('');
process.exit(1);
}
}
// Run
createSentimentDashboard();