Files
akmon/server/gateway-mqtt-node/scripts/simulate_webhook.js
2026-01-20 08:04:15 +08:00

53 lines
1.4 KiB
JavaScript

import 'dotenv/config'
import http from 'http'
const HTTP_PORT = parseInt(process.env.HTTP_PORT || '3000', 10)
const WEBHOOK_TOKEN = process.env.WEBHOOK_TOKEN || ''
const conversationId = process.env.SIM_CONVERSATION_ID || '00000000-0000-0000-0000-000000000000'
const senderId = process.env.SIM_SENDER_ID || '00000000-0000-0000-0000-000000000001'
const env = {
id: process.env.SIM_MESSAGE_ID || undefined,
ts: new Date().toISOString(),
type: 'chat.message',
source: 'webhook.sim',
conversation_id: conversationId,
sender_id: senderId,
content: process.env.SIM_CONTENT || 'hello from webhook',
content_type: 'text',
metadata: { sim: true }
}
const body = JSON.stringify({
event: 'message.publish',
topic: `chat/send/${conversationId}`,
// emulate raw string payload
payload: JSON.stringify(env)
})
const options = {
hostname: '127.0.0.1',
port: HTTP_PORT,
path: '/webhooks/mqtt',
method: 'POST',
headers: {
'content-type': 'application/json',
'content-length': Buffer.byteLength(body),
...(WEBHOOK_TOKEN ? { 'x-webhook-token': WEBHOOK_TOKEN } : {})
}
}
const req = http.request(options, (res) => {
let data = ''
res.on('data', (chunk) => data += chunk)
res.on('end', () => {
console.log('status:', res.statusCode)
console.log('body :', data)
})
})
req.on('error', (err) => console.error('request error:', err))
req.write(body)
req.end()