dry-rainbow-66344
09/05/2025, 2:16 PMtall-airline-16756
09/05/2025, 2:16 PMtall-airline-16756
09/05/2025, 2:17 PMdry-rainbow-66344
09/05/2025, 2:17 PMtall-airline-16756
09/05/2025, 2:18 PMrequests to send an HTTP POST request right to your n8n webhook URL.
◦ Here’s a quick example you can pop into your function:
import requests
webhook_url = "<YOUR_N8N_WEBHOOK_URL>" # Just swap this with your actual n8n webhook URL
data = {"key": "value"} # Put in whatever data you want to send
response = requests.post(webhook_url, json=data)
print(response.status_code, response.text)
◦ Make sure your n8n workflow is all set up to handle the data coming in.
1. LiveKit Data Messages:
◦ You can send a data message from the client side during the call to give your server a heads-up.
◦ Then, on the server, listen for that message and trigger the n8n webhook with an HTTP request.
◦ This way keeps all the webhook logic nicely tucked away on the server side.
Just let me know which way sounds better for you, or if you want a hand setting it all up—I’m here to help!dry-rainbow-66344
09/05/2025, 2:18 PMtall-airline-16756
09/05/2025, 2:19 PMaiohttp.
◦ Here’s a simple example:
import aiohttp
from livekit.agents import llm
class WebhookFnc(llm.FunctionContext):
@llm.ai_callable(description="Send a notification to an external service via webhook")
async def call_webhook(self, url: str, message: str):
try:
async with aiohttp.ClientSession() as session:
async with session.post(url, json={"message": message}) as response:
if response.status == 200:
return "Webhook sent successfully."
else:
return f"Webhook failed with status: {response.status}"
except Exception as e:
return f"Error sending webhook: {str(e)}"
fnc_ctx = WebhookFnc()
1. Attach the Function to Your Agent:
◦ Just use the PipelineVoiceAgent or MultimodalAgent and hook up the function context to it.
◦ For example:
agent = VoicePipelineAgent(fnc_ctx=fnc_ctx, ...)
1. Trigger the Function During the Call:
◦ When the LLM picks up a user intent (like "Book a visit"), it’ll call the call_webhook function with the webhook URL and message you provide.
If you want, I’m here to help you with setting this up or tweaking your webhook configuration—just let me know!
Sources: Function calling with Voice Agents | LiveKit Docs | examples/webhooks-nextjs/README.md | examples/webhook.pydry-rainbow-66344
09/05/2025, 2:20 PMtall-airline-16756
09/05/2025, 2:21 PMimport aiohttp
from livekit.agents import llm
from livekit.agents.voice import VoicePipelineAgent
# Step 1: Define the webhook function
class WebhookFnc(llm.FunctionContext):
@llm.ai_callable(description="Send a notification to an external service via webhook")
async def call_webhook(self, url: str, message: str):
"""Trigger a webhook to notify an external system."""
try:
async with aiohttp.ClientSession() as session:
async with session.post(url, json={"message": message}) as response:
if response.status == 200:
return "Webhook sent successfully."
else:
return f"Webhook failed with status: {response.status}"
except Exception as e:
return f"Error sending webhook: {str(e)}"
# Step 2: Attach the function context to the agent
fnc_ctx = WebhookFnc()
agent = VoicePipelineAgent(
fnc_ctx=fnc_ctx,
stt_service="your_stt_service", # Replace with your Speech-to-Text service
tts_service="your_tts_service", # Replace with your Text-to-Speech service
llm_service="your_llm_service" # Replace with your LLM service
)
# Step 3: Define a sample prompt
prompt = "If the user says they want to book a visit, call the webhook function with the appropriate message."
# Step 4: Simulate a user input
async def simulate_user_input():
user_input = "I want to book a visit to the clinic."
response = await agent.process_input(user_input)
print("Agent response:", response)
# Run the simulation
import asyncio
asyncio.run(simulate_user_input())
To break it down:
• The call_webhook function sends a POST request to your webhook URL with the message you want to share.
• The VoicePipelineAgent is set up with your function context, plus the Speech-to-Text, Text-to-Speech, and LLM services you’re using.
• The prompt guides the LLM to call the webhook function whenever the user mentions booking a visit.
• The simulate_user_input function shows how the agent handles user input and triggers the webhook in action.
If you’d like, I’m here to help you tweak this code to fit your specific needs—just let me know!dry-rainbow-66344
09/05/2025, 2:23 PMtall-airline-16756
09/05/2025, 2:24 PM