Chat Application Integrations
Relevant source files
The following files were used as context for generating this wiki page:
Chat Application Integrations within the browser-use project enable AI agents to interact with external communication platforms such as Discord and Slack. These integrations act as a bridge between the chat interface and the browser automation engine, allowing users to trigger complex web tasks using natural language commands directly from their preferred chat client.
The system is designed to listen for specific command prefixes (typically $bu), process the requested task using a Large Language Model (LLM), execute the browser actions via the Agent class, and return the final results or extracted content back to the chat channel.
Discord Integration
The Discord integration is implemented as a specialized bot class that inherits from discord.ext.commands.Bot. It utilizes asynchronous event handling to monitor channel messages and execute browser tasks.
Architecture and Data Flow
The Discord bot operates by listening for the on_message event. When a message starts with the designated prefix, the bot extracts the task description and initializes a new Browser and Agent instance to fulfill the request.
The flow shows how a user message triggers the internal agent execution and returns the result to the chat.
Sources: examples/integrations/discord/discord_api.py:53-125, examples/integrations/discord/discord_example.py:46-62
Key Components
| Class/Function | Description |
|---|---|
DiscordBot | Main class extending commands.Bot to handle Discord-specific logic. |
on_message | Event listener that filters and triggers tasks based on the command prefix. |
run_agent | Orchestrates the lifecycle of the Browser and Agent for a single task. |
DiscordBot.run() | Starts the bot using a Discord bot token. |
Sources: examples/integrations/discord/discord_api.py:12-51, examples/integrations/discord/discord_example.py:65-67
Slack Integration
The Slack integration utilizes the slack_sdk and a FastAPI web server to handle events via webhooks. Unlike the Discord bot, which maintains a persistent connection, the Slack bot reacts to HTTP POST requests sent by Slack's Event Subscriptions.
Architecture and Event Handling
Slack events are received via a /slack/events endpoint. The implementation includes a SignatureVerifier to ensure requests originate from Slack. Events are processed asynchronously, and a set of processed_events is maintained to prevent duplicate execution of the same event ID.
This sequence illustrates the webhook-based communication and the internal deduplication logic.
Sources: examples/integrations/slack/slack_api.py:32-106, examples/integrations/slack/README.md:14-30
Configuration and Setup
Slack integration requires several environment variables and specific bot scopes:
- Scopes:
chat:write,channels:history,im:history. - Events:
message.channels,message.im. - Environment Variables:
SLACK_BOT_TOKEN,SLACK_SIGNING_SECRET.
Sources: examples/integrations/slack/README.md:8-58
Common Integration Logic
Both integrations share a standardized way of interacting with the core browser-use library.
Agent Execution Workflow
The run_agent method is a common pattern across both implementations. It encapsulates the browser automation logic into a single asynchronous call:
- Initialization: Creates a
Browserinstance with a providedBrowserConfig. - Task Setup: Initializes an
Agentwith the user's task, the provided LLM, and the browser. - Execution: Calls
agent.run()and waits for completion. - Result Extraction: Retrieves the
extracted_contentfrom the last step in the agent's history.
async def run_agent(self, task: str) -> str:
try:
browser = Browser(config=self.browser_config)
agent = Agent(task=task, llm=self.llm, browser=browser)
result = await agent.run()
agent_message = None
if result.is_done():
agent_message = result.history[-1].result[0].extracted_content
if agent_message is None:
agent_message = 'Oops! Something went wrong while running Browser-Use.'
return agent_message
except Exception as e:
raise Exception(f'Browser-use task failed: {str(e)}')
Sources: examples/integrations/discord/discord_api.py:107-125, examples/integrations/slack/slack_api.py:75-93
Integration Parameters
The following parameters are standard for initializing chat-based bots:
| Parameter | Type | Default | Description |
|---|---|---|---|
llm | BaseChatModel | Required | The language model used by the agent (e.g., GPT-4, Gemini). |
prefix | str | "$bu" | The command string used to trigger the bot in chat. |
ack | bool | False | If True, the bot replies immediately to acknowledge receipt of the task. |
browser_config | BrowserConfig | headless=True | Configuration for the underlying Playwright browser. |
Sources: examples/integrations/discord/discord_api.py:43-51, examples/integrations/slack/slack_api.py:22-30
Summary
Chat Application Integrations provide an accessible interface for interacting with the browser-use agent. By leveraging platform-specific libraries (discord.py and slack_sdk), the project allows developers to deploy browser automation tools that are controllable via standard messaging apps. Both integrations follow a similar pattern of filtering messages by prefix, executing tasks through the Agent service, and returning extracted web content to the user.