159 lines
4.4 KiB
Bash
159 lines
4.4 KiB
Bash
#!/bin/bash
|
|
|
|
# Script pour tester le webhook N8N depuis la VM
|
|
# Utilise les outils disponibles: curl, wget, python3, node
|
|
|
|
# Charger .env.local si présent
|
|
if [ -f .env.local ]; then
|
|
export $(grep -v '^#' .env.local | xargs)
|
|
fi
|
|
|
|
# Variables
|
|
WEBHOOK_URL="https://brain.slm-lab.net/webhook-test/mission-created"
|
|
MISSION_ID="${1:-3103ec1a-acde-4025-9ead-4e1a0ddc047c}"
|
|
API_URL="${NEXT_PUBLIC_API_URL:-https://hub.slm-lab.net/api}"
|
|
API_KEY="${N8N_API_KEY}"
|
|
|
|
# Vérifier que l'API key est définie
|
|
if [ -z "$API_KEY" ]; then
|
|
echo "❌ Erreur: N8N_API_KEY n'est pas définie"
|
|
echo " Vérifiez votre fichier .env.local ou exportez N8N_API_KEY"
|
|
exit 1
|
|
fi
|
|
|
|
echo "🧪 Test du webhook N8N depuis la VM"
|
|
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
|
|
echo "Webhook URL: $WEBHOOK_URL"
|
|
echo "Mission ID: $MISSION_ID"
|
|
echo "API URL: $API_URL"
|
|
echo ""
|
|
|
|
# Préparer le JSON
|
|
JSON_DATA=$(cat <<EOF
|
|
{
|
|
"name": "SEFFIR",
|
|
"oddScope": ["odd-4"],
|
|
"niveau": "s",
|
|
"intention": "",
|
|
"missionType": "remote",
|
|
"donneurDOrdre": "group",
|
|
"projection": "long",
|
|
"services": [],
|
|
"participation": "ouvert",
|
|
"profils": [],
|
|
"guardians": {},
|
|
"volunteers": [],
|
|
"creatorId": "203cbc91-61ab-47a2-95d2-b5e1159327d7",
|
|
"missionId": "${MISSION_ID}",
|
|
"logoPath": "missions/${MISSION_ID}/logo.png",
|
|
"logoUrl": "https://hub.slm-lab.net/api/missions/image/missions/${MISSION_ID}/logo.png",
|
|
"config": {
|
|
"N8N_API_KEY": "${API_KEY}",
|
|
"MISSION_API_URL": "${API_URL}"
|
|
}
|
|
}
|
|
EOF
|
|
)
|
|
|
|
# Essayer curl d'abord, puis wget, puis python3
|
|
if command -v curl &> /dev/null; then
|
|
echo "📤 Utilisation de curl..."
|
|
echo ""
|
|
curl -X POST "${WEBHOOK_URL}" \
|
|
-H "Content-Type: application/json" \
|
|
-d "$JSON_DATA" \
|
|
-v
|
|
elif command -v wget &> /dev/null; then
|
|
echo "📤 Utilisation de wget..."
|
|
echo ""
|
|
echo "$JSON_DATA" | wget --method=POST \
|
|
--header="Content-Type: application/json" \
|
|
--body-data=- \
|
|
--output-document=- \
|
|
--server-response \
|
|
"${WEBHOOK_URL}" 2>&1
|
|
elif command -v python3 &> /dev/null; then
|
|
echo "📤 Utilisation de python3..."
|
|
echo ""
|
|
python3 <<PYTHON_SCRIPT
|
|
import urllib.request
|
|
import urllib.parse
|
|
import json
|
|
import sys
|
|
|
|
url = "${WEBHOOK_URL}"
|
|
data = ${JSON_DATA}
|
|
|
|
json_data = json.dumps(data).encode('utf-8')
|
|
req = urllib.request.Request(url, data=json_data, headers={'Content-Type': 'application/json'})
|
|
|
|
try:
|
|
with urllib.request.urlopen(req) as response:
|
|
print(f"Status: {response.status} {response.reason}")
|
|
print(f"Headers: {dict(response.headers)}")
|
|
print(f"\nResponse Body:")
|
|
print(response.read().decode('utf-8'))
|
|
except urllib.error.HTTPError as e:
|
|
print(f"HTTP Error {e.code}: {e.reason}")
|
|
print(e.read().decode('utf-8'))
|
|
sys.exit(1)
|
|
PYTHON_SCRIPT
|
|
elif command -v node &> /dev/null; then
|
|
echo "📤 Utilisation de node..."
|
|
echo ""
|
|
node <<NODE_SCRIPT
|
|
const https = require('https');
|
|
const http = require('http');
|
|
|
|
const url = new URL('${WEBHOOK_URL}');
|
|
const data = ${JSON_DATA};
|
|
|
|
const options = {
|
|
hostname: url.hostname,
|
|
port: url.port || (url.protocol === 'https:' ? 443 : 80),
|
|
path: url.pathname,
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Content-Length': Buffer.byteLength(JSON.stringify(data))
|
|
}
|
|
};
|
|
|
|
const client = url.protocol === 'https:' ? https : http;
|
|
|
|
const req = client.request(options, (res) => {
|
|
console.log(\`Status: \${res.statusCode} \${res.statusMessage}\`);
|
|
console.log(\`Headers:\`, res.headers);
|
|
console.log(\`\nResponse Body:\`);
|
|
|
|
let body = '';
|
|
res.on('data', (chunk) => { body += chunk; });
|
|
res.on('end', () => {
|
|
try {
|
|
console.log(JSON.stringify(JSON.parse(body), null, 2));
|
|
} catch (e) {
|
|
console.log(body);
|
|
}
|
|
});
|
|
});
|
|
|
|
req.on('error', (e) => {
|
|
console.error(\`Erreur: \${e.message}\`);
|
|
process.exit(1);
|
|
});
|
|
|
|
req.write(JSON.stringify(data));
|
|
req.end();
|
|
NODE_SCRIPT
|
|
else
|
|
echo "❌ Aucun outil disponible (curl, wget, python3, node)"
|
|
exit 1
|
|
fi
|
|
|
|
echo ""
|
|
echo ""
|
|
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
|
|
echo "✅ Test terminé"
|
|
echo ""
|
|
echo "💡 Vérifiez les logs N8N pour voir la structure de la réponse RocketChat"
|