skills/e-commerce-automation/index.ts · 电商自动化核心逻辑
import { Skill } from '@openclaw/core';
import { ShopifyClient } from './clients/shopify';
import { AmazonClient } from './clients/amazon';
import { TikTokClient } from './clients/tiktok';
interface AutomationTask {
type: 'product_selection' | 'listing' | 'customer_service' |
'order_processing' | 'inventory' | 'ads_optimization';
platform: 'shopify' | 'amazon' | 'tiktok';
parameters?: Record<string, any>;
}
export const ecommerceAutomationSkill = new Skill({
name: 'e-commerce-automation',
description: '跨境电商全链路自动化:选品/上架/客服/订单/库存/营销',
config: {
platforms: ['shopify', 'amazon', 'tiktok'],
autoReplyEnabled: true,
inventoryThreshold: 50,
priceMonitoringInterval: 300000
},
onStart: async (context) => {
context.logger.info('🚀 启动跨境电商自动化系统...');
const shopify = new ShopifyClient(process.env.SHOPIFY_API_KEY);
const amazon = new AmazonClient(process.env.AMAZON_SP_API);
const tiktok = new TikTokClient(process.env.TIKTOK_ACCESS_TOKEN);
await startProductSelection(context);
await startCustomerService(context);
await startOrderProcessing(context);
await startInventoryMonitoring(context);
await startAdsOptimization(context);
context.on('automation_task', async (task: AutomationTask) => {
try {
switch (task.type) {
case 'product_selection':
return await selectProducts(task.platform, task.parameters);
case 'listing':
return await autoListProducts(task.platform, task.parameters);
case 'customer_service':
return await handleCustomerInquiry(task.platform, task.parameters);
default:
throw new Error(`未知任务类型:${task.type}`);
}
} catch (error) {
context.logger.error('❌ 自动化任务失败:', error);
await context.notify('dingtalk', {
message: `⚠️ 电商自动化异常:${error.message}`,
level: 'high'
});
}
});
},
onStop: async (context) => {
context.logger.info('🛑 停止电商自动化系统');
}
});
async function handleCustomerInquiry(
platform: string,
params: any
) {
const inquiry = params.inquiry;
const response = await context.llm.generate(`
你是专业跨境电商客服,请用${params.language || '英语'}回复客户:
客户问题:${inquiry.question}
订单号:${inquiry.orderId}
产品类型:${inquiry.productType}
要求:
1. 语气友好专业
2. 解答具体问题
3. 提供解决方案
4. 引导好评
`);
if (platform === 'shopify') {
await shopify.sendEmail(inquiry.customerEmail, response);
} else if (platform === 'amazon') {
await amazon.replyToMessage(inquiry.messageId, response);
}
await context.db.save({
type: 'customer_service',
platform,
inquiry,
response,
timestamp: new Date()
});
}
config/ecommerce-automation.json · 参数配置
{
"platforms": {
"shopify": {
"enabled": true,
"apiKey": "${SHOPIFY_API_KEY}",
"password": "${SHOPIFY_PASSWORD}",
"storeUrl": "${SHOPIFY_STORE_URL}"
},
"amazon": {
"enabled": true,
"spApiId": "${AMAZON_SP_API_ID}",
"secret": "${AMAZON_SECRET}",
"marketplace": "ATVPDKIKX0DER"
},
"tiktok": {
"enabled": true,
"accessToken": "${TIKTOK_ACCESS_TOKEN}",
"shopId": "${TIKTOK_SHOP_ID}"
}
},
"automation": {
"autoReply": {
"enabled": true,
"responseTime": "<1min",
"languages": ["en", "es", "de", "fr", "ja"],
"escalationRules": {
"negativeSentiment": true,
"refundRequest": true,
"complexIssue": true
}
},
"inventory": {
"lowStockThreshold": 50,
"autoReorder": true,
"reorderPoint": 100,
"reorderQuantity": 500
},
"pricing": {
"monitorCompetitors": true,
"autoAdjust": true,
"minMargin": 0.15,
"maxDiscount": 0.20
}
}
}