Connect your bot to AI providers with webhooks - from basic setup to advanced patterns
Master webhook integrations to connect your chatbot to any AI provider or custom backend. This comprehensive guide covers everything from basic OpenAI/Anthropic setup to advanced patterns like A/B testing, async processing, and custom AI pipelines.
What You’ll Learn
By the end of this guide, you’ll understand:
How webhook integrations work in Chatbot Platform
Request and response payload formats
Setting up OpenAI, Anthropic, and custom backends
Authentication and security best practices
Error handling and retry strategies
Advanced patterns like A/B testing and async processing
Performance optimization techniques
Prerequisites
Before starting, you should:
Have a bot created in Chatbot Platform
Understand HTTP POST requests and JSON
Have API keys for your AI provider (OpenAI, Anthropic, etc.)
Know basic webhook/API concepts
Have access to deploy a simple proxy service (optional for advanced setups)
How Webhook Integrations Work
The Flow
1. User sends message → Chatbot Platform2. Platform packages message + history → POST to your webhook3. Your webhook processes request → Calls AI provider4. AI provider returns response → Your webhook5. Your webhook formats response → Returns to Platform6. Platform delivers response → User
Why Webhooks?
Webhooks give you full control:
Choose any AI provider: OpenAI, Anthropic, Cohere, custom models
Add custom logic: RAG, knowledge base search, data lookups
A/B test models: Compare GPT-4 vs Claude, different prompts
Transform data: Add/remove context, format responses
Every webhook integration receives this standard payload:
{ "messages": [ { "role": "system", "content": "You are a helpful assistant." }, { "role": "user", "content": "Hello, how are you?" }, { "role": "assistant", "content": "I'm doing well, thank you! How can I help you today?" }, { "role": "user", "content": "What's the weather like?" } ], "conversation_id": "conv_abc123", "user": { "id": "user_123", "platform": "telegram", "name": "John Doe", "username": "johndoe" }, "bot": { "id": "bot_456", "name": "Support Bot" }, "metadata": { "channel": "telegram", "channel_id": "channel_789", "chat_id": "123456789", "message_id": "999", "timestamp": "2024-01-15T10:30:00Z" }}
Field Descriptions
messages (array): OpenAI-format conversation history
Includes system prompt if configured
Contains conversation context based on your window setting
Latest message is always last
conversation_id (string): Unique ID for this conversation threaduser (object): Information about the user
id: Platform-specific user ID
platform: telegram | slack | discord | whatsapp
name: User’s display name
username: Platform username (if available)
bot (object): Your bot’s information
id: Bot ID in Chatbot Platform
name: Bot’s configured name
metadata (object): Platform-specific data
channel: Platform name
channel_id: Channel configuration ID
chat_id: Platform-specific chat identifier
message_id: Platform-specific message ID
timestamp: When message was received (ISO 8601)
Response Format
Your webhook must return JSON with a content field:
Simple Response
{ "content": "The weather is sunny and 72°F today!"}
Response with Metadata
{ "content": "The weather is sunny and 72°F today!", "metadata": { "model": "gpt-4", "tokens": 45, "processing_time_ms": 1250, "temperature": 0.7, "custom_field": "any value you want to track" }}
The metadata object is optional but useful for:
Tracking which model was used
Logging token usage and costs
Debugging performance issues
A/B test analytics
Custom business metrics
Step 1: Basic Integration Setup
Let’s create a basic integration that connects to OpenAI.
catch (error) { // Log full error internally console.error('Integration error:', error); // Return user-friendly message res.json({ content: "I'm having trouble processing your request. Please try again in a moment.", metadata: { error_id: generateErrorId(), // For support lookups timestamp: new Date().toISOString() } });}