You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
3.7 KiB

// 专用客服消息发送调试脚本
const WebSocket = require('ws');
const WS_URL = 'ws://localhost:3003';
const TEST_MANAGER_ID = '22';
const TEST_CUSTOMER_ID = 'test_customer_1';
let managerWs = null;
// 启动调试
async function startDebug() {
console.log('=== 启动客服消息发送调试 ===');
try {
// 连接客服WebSocket
await connectManager();
// 等待连接稳定
await new Promise(resolve => setTimeout(resolve, 2000));
// 提示输入会话ID
console.log('请先让客户发送一条消息,然后输入获取到的会话ID:');
// 模拟会话ID(在实际调试时,这应该从客户消息中获取)
const conversationId = '4fa4b92f-df20-40ae-94b9-f906753a4cfd'; // 这是之前测试中获取的会话ID
if (conversationId) {
console.log(`使用会话ID: ${conversationId}`);
await sendTestMessage(conversationId);
}
} catch (error) {
console.error('❌ 调试过程中出现错误:', error);
} finally {
console.log('=== 调试结束 ===');
if (managerWs && managerWs.readyState === WebSocket.OPEN) {
managerWs.close();
}
}
}
// 连接客服WebSocket
function connectManager() {
return new Promise((resolve, reject) => {
managerWs = new WebSocket(WS_URL);
managerWs.on('open', () => {
console.log('✅ 客服WebSocket连接已建立');
// 发送认证消息
const authMsg = JSON.stringify({
type: 'auth',
managerId: TEST_MANAGER_ID,
userType: 'manager'
});
console.log('🔍 发送认证消息:', authMsg);
managerWs.send(authMsg);
});
managerWs.on('message', (data) => {
const message = JSON.parse(data.toString());
console.log('📥 收到消息:', JSON.stringify(message));
if (message.type === 'auth_success') {
console.log('✅ 客服认证成功');
resolve();
}
});
managerWs.on('error', (error) => {
console.error('❌ WebSocket错误:', error);
reject(error);
});
setTimeout(() => {
reject(new Error('连接或认证超时'));
}, 5000);
});
}
// 发送测试消息
async function sendTestMessage(conversationId) {
// 尝试多种消息格式
const testFormats = [
{
name: '基础格式(使用payload)',
data: {
type: 'chat_message',
payload: {
conversationId: conversationId,
content: '这是测试消息 - 基础格式(使用payload)',
contentType: 1
}
}
},
{
name: '带messageId格式(使用payload)',
data: {
type: 'chat_message',
payload: {
messageId: 'test_msg_' + Date.now(),
conversationId: conversationId,
content: '这是测试消息 - 带messageId(使用payload)',
contentType: 1
}
}
},
{
name: '完整格式(使用payload)',
data: {
type: 'chat_message',
payload: {
messageId: 'test_msg_' + Date.now(),
conversationId: conversationId,
content: '这是测试消息 - 完整格式(使用payload)',
contentType: 1,
senderType: 2,
senderId: TEST_MANAGER_ID,
receiverId: TEST_CUSTOMER_ID,
createdAt: new Date().toISOString()
}
}
}
];
// 依次测试每种格式
for (const format of testFormats) {
console.log(`\n🔄 测试格式: ${format.name}`);
console.log(`发送数据: ${JSON.stringify(format.data)}`);
managerWs.send(JSON.stringify(format.data));
// 等待响应
await new Promise(resolve => setTimeout(resolve, 3000));
}
}
// 启动调试
startDebug();