Files

85 lines
2.4 KiB
TypeScript
Raw Permalink Normal View History

import 'dotenv/config';
/**
* 测试飞书 Webhook
*/
async function testFeishuWebhook() {
const webhookUrl = process.env.WEBHOOK_URL;
if (!webhookUrl) {
console.error('❌ 未配置 WEBHOOK_URL 环境变量');
return;
}
console.log('📡 Webhook URL:', webhookUrl);
console.log('🔍 开始测试...\n');
// 测试消息格式
const testMessages = [
{
name: '飞书文本消息',
message: {
msg_type: 'text',
content: {
text: '价格变化告警测试\n时间: 2026/1/13 00:31:21\n变化类型: 下跌\n美白银: 86.14\n伦敦银: 86.023\n价差: 0.117'
}
}
},
{
name: '自定义文本消息(当前代码格式)',
message: {
msgtype: 'text',
text: {
content: '价格变化告警\n时间: 2026/1/13 00:31:21\n变化类型: 下跌\n美白银: 86.14\n伦敦银: 86.023\n价差: 0.117'
}
}
}
];
for (const test of testMessages) {
console.log(`\n========================================`);
console.log(`测试: ${test.name}`);
console.log('========================================');
console.log('请求体:', JSON.stringify(test.message, null, 2));
try {
const response = await fetch(webhookUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(test.message)
});
console.log(`\n状态码: ${response.status} ${response.statusText}`);
console.log(`ok: ${response.ok}`);
// 获取响应体
const responseText = await response.text();
console.log(`响应体: ${responseText || '(空)'}`);
// 尝试解析为 JSON
try {
const jsonResponse = JSON.parse(responseText);
console.log('响应 JSON:', JSON.stringify(jsonResponse, null, 2));
} catch {
// 不是 JSON
}
// 显示响应头
console.log('\n响应头:');
response.headers.forEach((value, key) => {
console.log(` ${key}: ${value}`);
});
} catch (error: any) {
console.error(`\n❌ 请求失败:`, error.message);
console.error('错误详情:', error);
}
}
console.log('\n========================================');
console.log('✅ 测试完成');
console.log('========================================');
}
// 运行测试
testFeishuWebhook().catch(console.error);