Si mund t’ju ndihmojmë?

Na tregoni shkurtimisht për çfarë bëhet fjalë – do t’ju përgjigjemi me email.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
Te faqja kryesore

Webhook: eventi al Suo server

Webhook firmati e REST hook di reactive.chat: eventi, intestazioni, corpo, verifica HMAC-SHA256, tentativi, timeout, disattivazione ed esempi di verifica in PHP, Node e Python.

I webhook comunicano al Suo server che cosa accade nella chat: una nuova conversazione, un messaggio, un passaggio al team, una valutazione. Esistono due modalità:

Eventi

Le conversazioni dalla ricerca nel sito (Smart Answer) attivano eventi solo quando diventano visibili come chat.

Configurare il webhook firmato

Nell’account, in Impostazioni → Integrazioni → Webhook (permesso: gestire integrazioni):

Al salvataggio non viene inviata alcuna chiamata di prova. Dopo l’attivazione possono servire fino a circa 15 secondi prima che arrivino gli eventi; quelli precedenti non vengono consegnati retroattivamente.

La consegna

POST /rc-webhook HTTP/1.1
Host: hooks.your-company.com
Content-Type: application/json; charset=utf-8
User-Agent: reactive.chat-webhook/1.0
X-RC-Event: csat.submitted
X-RC-Delivery: 01JAB3C4D5E6F7G8H9JKMNPQRS
X-RC-Timestamp: 1792503600
X-RC-Signature: sha256=3f0a1c...

{"id":"01JAB3C4D5E6F7G8H9JKMNPQRS","event":"csat.submitted","ts":1792503600,"site_key":"ABCDEFGHJKLMNPQRSTUVWX12","conversation_id":"01JAB2...","session_id":"s_...","visitor_id":"v_...","data":{"score":5,"comment":"Very helpful"}}

Intestazioni:

Corpo:

Verificare la firma

Verifichi prima di qualsiasi elaborazione: timestamp non più vecchio di 300 secondi, firma sul corpo grezzo, confronto a tempo costante.

PHP:

<?php
$geheim = getenv('RC_WEBHOOK_SECRET');
$body   = file_get_contents('php://input');
$ts     = $_SERVER['HTTP_X_RC_TIMESTAMP'] ?? '';
$sig    = $_SERVER['HTTP_X_RC_SIGNATURE'] ?? '';

if (!ctype_digit($ts) || abs(time() - (int) $ts) > 300) { http_response_code(401); exit; }
$soll = 'sha256=' . hash_hmac('sha256', $ts . '.' . $body, $geheim);
if (!hash_equals($soll, $sig)) { http_response_code(401); exit; }

$ereignis = json_decode($body, true);
// detect duplicates: remember $ereignis['id']
http_response_code(204);

Node.js (Express):

const crypto = require('crypto');
app.post('/rc-webhook', express.raw({ type: 'application/json' }), (req, res) => {
  const ts = req.get('X-RC-Timestamp') || '';
  const sig = req.get('X-RC-Signature') || '';
  if (!/^\d+$/.test(ts) || Math.abs(Date.now() / 1000 - Number(ts)) > 300) return res.sendStatus(401);
  const soll = 'sha256=' + crypto.createHmac('sha256', process.env.RC_WEBHOOK_SECRET)
    .update(ts + '.' + req.body.toString('utf8')).digest('hex');
  if (sig.length !== soll.length || !crypto.timingSafeEqual(Buffer.from(sig), Buffer.from(soll))) {
    return res.sendStatus(401);
  }
  const ereignis = JSON.parse(req.body.toString('utf8'));
  res.sendStatus(204);
  verarbeiteSpaeter(ereignis);   // respond quickly, work afterwards
});

Python (Flask):

import hmac, hashlib, os, time
from flask import request, abort

@app.post("/rc-webhook")
def rc_webhook():
    ts = request.headers.get("X-RC-Timestamp", "")
    sig = request.headers.get("X-RC-Signature", "")
    body = request.get_data()                      # raw bytes
    if not ts.isdigit() or abs(time.time() - int(ts)) > 300:
        abort(401)
    soll = "sha256=" + hmac.new(os.environ["RC_WEBHOOK_SECRET"].encode(),
                                ts.encode() + b"." + body, hashlib.sha256).hexdigest()
    if not hmac.compare_digest(soll, sig):
        abort(401)
    return "", 204

Risposte, nuovi tentativi e disattivazione

Risponda rapidamente con un 2xx ed elabori in seguito. Timeout: 4 secondi per stabilire la connessione, 10 secondi totali. I reindirizzamenti non vengono seguiti.

Gli eventi più vecchi di un’ora non vengono più consegnati. Se gli errori si accumulano (5 in 60 secondi), la consegna alla destinazione si sospende per 30 secondi; gli incarichi vengono poi ritentati. La chat non attende mai il Suo webhook.

L’ordine delle consegne non è garantito. Se necessario, ordini per ts o per il seq dei messaggi dell’API.

REST hook (Zapier, Make, n8n)

Gli strumenti di automazione sottoscrivono autonomamente le destinazioni tramite l’API server. Può farlo anche direttamente:

curl -X POST https://reactive.chat/v1/hooks \
  -H "Authorization: Bearer rc_live_..." \
  -H "Content-Type: application/json" \
  -d '{"event":"message.created","target_url":"https://hooks.your-company.com/rc","source":"n8n"}'

Consegna di un REST hook:

POST https://hooks.your-company.com/rc
Content-Type: application/json
User-Agent: reactive.chat-n8n/1.0
X-RC-Event: message.created
X-RC-Delivery: 01JAB3...

{"event":"message.created","delivery_id":"01JAB3...","data":{"name":"message.created","ts":1792503600123,"site_key":"...","conversation_id":"...","session_id":"...","visitor_id":"...","message":{"id":"...","seq":7,"kind":"text","direction":"in","author_type":"bot","author_name":"Assistant","via":null,"body":"…","redacted":false,"ts":1792503600120}}}

Che cosa non esiste

Successivo: API server

Përditësuar së fundi: