React e Next.js
Aggiungi il widget dell'agente alle applicazioni React e Next.js.
Integra il widget dell'agente nelle applicazioni React e Next.js con poche righe di codice.
Installazione
Aggiungi lo script del widget alla tua app React. Ottieni il tuo codice di embed da Chatbot > Pubblica, poi adattalo al tuo framework.
Next.js (App Router)
Aggiungi lo script al tuo root layout usando il componente Script di Next.js:
// app/layout.tsx
import Script from 'next/script';
export default function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
return (
<html lang="en">
<body>
{children}
{/* Get your src and data-chatbot values from Chatbot > Publish */}
<Script
src="YOUR_WIDGET_URL"
data-chatbot="YOUR_CHATBOT_ID"
strategy="afterInteractive"
/>
</body>
</html>
);
}
Next.js (Pages Router)
Aggiungi a _app.tsx o _document.tsx:
// pages/_app.tsx
import Script from 'next/script';
import type { AppProps } from 'next/app';
export default function App({ Component, pageProps }: AppProps) {
return (
<>
<Component {...pageProps} />
{/* Get your src and data-chatbot values from Chatbot > Publish */}
<Script
src="YOUR_WIDGET_URL"
data-chatbot="YOUR_CHATBOT_ID"
strategy="afterInteractive"
/>
</>
);
}
Create React App
Aggiungi a public/index.html. Copia il tuo codice di embed da Chatbot > Pubblica:
<!DOCTYPE html> <html lang="en"> <head> <!-- ... --> </head> <body> <div id="root"></div> <!-- Paste your embed code from Chatbot > Publish here --> </body> </html>
Vite
Aggiungi a index.html. Copia il tuo codice di embed da Chatbot > Pubblica:
<!DOCTYPE html> <html lang="en"> <head> <!-- ... --> </head> <body> <div id="root"></div> <script type="module" src="/src/main.tsx"></script> <!-- Paste your embed code from Chatbot > Publish here --> </body> </html>
ID agente dinamico
Per app multi-tenant in cui l'ID agente varia:
// components/ChatbotWidget.tsx
'use client';
import Script from 'next/script';
interface ChatbotWidgetProps {
chatbotId: string;
widgetUrl: string;
}
export function ChatbotWidget({ chatbotId, widgetUrl }: ChatbotWidgetProps) {
if (!chatbotId) return null;
return (
<Script
src={widgetUrl}
data-chatbot={chatbotId}
strategy="afterInteractive"
/>
);
}
Caricamento condizionale
Mostra il widget solo su determinate pagine:
// app/support/layout.tsx
import Script from 'next/script';
export default function SupportLayout({
children,
}: {
children: React.ReactNode;
}) {
return (
<>
{children}
{/* Get your src and data-chatbot values from Chatbot > Publish */}
<Script
src="YOUR_WIDGET_URL"
data-chatbot="YOUR_CHATBOT_ID"
strategy="afterInteractive"
/>
</>
);
}
Tipi TypeScript
Il widget non richiede tipi TypeScript perché viene caricato esternamente e gestisce la propria interfaccia.
Test durante lo sviluppo
Per lo sviluppo locale:
- Aggiungi
localhostai domini consentiti - Usa l'ID del tuo agente di produzione
- Testa le interazioni normalmente
Variabili d'ambiente
Salva l'ID agente e l'URL del widget in variabili d'ambiente:
# .env.local NEXT_PUBLIC_CHATBOT_ID=your-chatbot-id NEXT_PUBLIC_WIDGET_URL=your-widget-url
<Script
src={process.env.NEXT_PUBLIC_WIDGET_URL}
data-chatbot={process.env.NEXT_PUBLIC_CHATBOT_ID}
strategy="afterInteractive"
/>
Ottieni entrambi i valori dalla scheda Pubblica del tuo agente.
Nota sui Server Component
Il componente Script funziona con i React Server Component. Non è necessario contrassegnare il layout come client component.
Prossimi passi
- Personalizza l'aspetto del widget
- Configura le restrizioni di dominio
- Usa la REST API per interfacce personalizzate