Discord Bot with LangGraph & Composio
Built with Composio ๐
[!NOTE] ๐ I have a blog post about this project on freeCodeCamp. Check it out here.
[!NOTE] This project demonstrates how graph-based workflows work in practice, using LangGraph to create intelligent, context-aware bot interactions with dynamic message routing and tool integration. Take this as an idea to how you can use LangGraph in unusual ways ;)
๐ Overview
An AI-powered Discord bot that leverages LangGraph for sophisticated workflow management and Composio for seamless tool integration. The bot intelligently routes messages through a graph-based system, providing context-aware responses and automated support ticket management.
Purpose
- Demonstrate Graph-Based AI Workflows: Show how LangGraph enables complex, intelligent decision-making
- Smart Message Processing: Automatically categorize and route Discord messages
- Tool Integration: Seamlessly connect with external services (Google Sheets, APIs, etc.)
- Support Automation: Intelligent support ticket classification and handling
- Contextual Responses: Maintain conversation context across different scenarios
Tools & Technologies
Core Technologies
- LangGraph - Graph-based workflow orchestration for AI applications
- Composio - Universal tool integration platform for AI agents
- Discord.js - Powerful library for interacting with Discord API
- OpenAI - Language model for intelligent message processing
- Bun - Fast JavaScript runtime and package manager
Architecture
Graph-Based Workflow
The bot uses a state graph with multiple nodes that process messages intelligently:
START โ process-message โ [routing decisions] โ ENDNode Structure
process-message: Initial message categorization and routingprocess-support: Support request classification and handlingprocess-tool-call: External tool execution and integrationprocess-other: General conversation and miscellaneous queriesprocess-support-question: Specific support question handlingprocess-support-help: Support assistance workflow
Why LangGraph? ๐ค
No hard reasons. Just to show you all how you can use it at places you don't think you can. This is a fun little project to visualize LangGraph.
Why Composio? ๐ง
Composio provides:
- Universal Tool Integration: Connect to 100+ tools and services
- Standardized Interface: Consistent API across different tools
- Authentication Management: Handle OAuth and API keys securely
- Action Execution: Reliable tool calling with proper error handling
- Extensibility: Easy to add custom tools and integrations
๐ Getting Started
Prerequisites
- Bun (latest version)
- Discord Bot Token
- OpenAI API Key
- Composio API Key (for tool integrations)
Installation
-
Clone the repository
git clone <repository-url> cd discord-bot-langgraph -
Install dependencies
bun install -
Environment Setup Create a
.envfile with:OPENAI_API_KEY=<YOUR_OPENAI_API_KEY> COMPOSIO_API_KEY=<YOUR_COMPOSIO_API_KEY> DISCORD_BOT_TOKEN=<YOUR_DISCORD_BOT_TOKEN> DISCORD_BOT_GUILD_ID=<YOUR_DISCORD_BOT_GUILD_ID> DISCORD_BOT_CHANNEL_ID=<YOUR_DISCORD_BOT_CHANNEL_ID> -
Deploy Slash Commands
bun run commands:deploy-
Start the Bot
# Development mode (with hot reload) bun run dev # Production mode bun start
๐ Project Structure
discord-bot-langgraph/
โโโ src/
โ โโโ index.ts # Bot entry point and Discord client setup
โ โโโ graph.ts # LangGraph workflow definition
โ โโโ nodes.ts # Individual workflow nodes
โ โโโ edges.ts # Conditional edge logic
โโโ types/
โ โโโ types.ts # TypeScript type definitions
โโโ utils/
โ โโโ env-validator.ts # Environment variable validation
โ โโโ helpers.ts # Utility functions
โ โโโ logger.ts # Logging configuration
โ โโโ slash-deploy.ts # Slash command deployment
โโโ graph/
โ โโโ graph.png # Visual representation of the workflow
โโโ package.json # Dependencies and scripts๐ Workflow Details
Message Processing Flow
- Message Received: Discord message triggers the workflow
- Initial Processing:
process-messagenode categorizes the message - Routing Decision: Based on AI analysis, message is routed to:
- Support Path: For help requests, bugs, or questions
- Tool Path: For actions requiring external tool integration
- General Path: For casual conversation or other queries
- Response Generation: Appropriate response is generated and sent
- Context Preservation: Message history is maintained for follow-ups
Support Ticket Management
- Automatic Classification: AI determines if message is a support request
- Category Assignment: Support requests are further categorized (question, help, bug)
- Thread Creation: Complex issues automatically create Discord threads
- Context Tracking: Maintains conversation history across thread interactions
Tool Integration
- Dynamic Tool Calling: AI decides when external tools are needed
- Iteration Support: Up to 5 iterations for complex tool sequences
- Error Handling: Graceful handling of tool failures with user feedback
- Response Formatting: Tool outputs are formatted for Discord presentation
๐งช Available Scripts
# Development
bun run dev # Start with hot reload
bun run start # Production start
bun run build # Build for production
# Utilities
bun run commands:deploy # Deploy slash commands๐ค Contributing
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit changes (
git commit -m 'Add amazing feature') - Push to branch (
git push origin feature/amazing-feature) - Open a Pull Request
Key Features
- Intelligent Message Routing - AI-powered message categorization
- Automated Support Tickets - Context-aware support request handling
- External Tool Integration - Seamless connection to external services
- Context Preservation - Maintains conversation history and context
- Dynamic Thread Creation - Automatic thread management for complex discussions
- Structured Responses - Consistent, well-formatted bot responses
- Iterative Processing - Multi-step tool execution capabilities
- Error Handling - Robust error handling and user feedback
๐ Understanding Graph-Based Workflows
This project is mainly build as a practical example of how graph-based workflows can create more intelligent and flexible AI applications. Unlike traditional linear bot logic, the graph approach allows for:
- Dynamic Path Selection: The bot chooses different paths based on message analysis
- State Management: Information flows between nodes maintaining context
- Conditional Logic: Complex decision trees that adapt to user input
- Extensible Design: New capabilities can be added as additional nodes
- Visual Representation: The workflow can be visualized and understood easily