What is a Bot?
A bot in Botpress is a conversational application that processes messages, manages state, and executes actions. Bots are composed of:- Definition: Structure and capabilities (states, events, actions, configuration)
- Implementation: Runtime logic (handlers, business logic)
- Dependencies: Integrations and plugins that extend functionality
Creating a Bot
Bot Definition
The bot definition describes what your bot can do. Frompackages/sdk/src/bot/definition.ts:197:
Bot Implementation
The implementation contains the runtime handlers. Frompackages/sdk/src/bot/implementation.ts:43:
Message Handlers
Handle incoming messages from users:Message handlers are executed in order of registration. Use
'*' as a catch-all handler that runs for every message type.Event Handlers
React to custom events:Hooks
Hooks allow you to intercept and modify data at various lifecycle points. Frompackages/sdk/src/bot/implementation.ts:310:
Available Hooks
beforeIncomingMessage/afterIncomingMessagebeforeOutgoingMessage/afterOutgoingMessagebeforeIncomingEvent/afterIncomingEventbeforeOutgoingCallAction/afterOutgoingCallActionbeforeIncomingCallAction/afterIncomingCallAction(experimental)
Adding Integrations
Integrations connect your bot to external platforms:packages/sdk/src/bot/definition.ts:258, integrations can be aliased to distinguish between multiple instances.
You can add multiple instances of the same integration with different aliases and configurations. For example, separate Slack integrations for different workspaces.
Adding Plugins
Plugins provide reusable functionality:State Management
Botpress provides four types of state:User State
User State
Persistent state tied to a specific user across all conversations:
Conversation State
Conversation State
State for a specific conversation:
Bot State
Bot State
Global state shared across the entire bot:
Workflow State
Workflow State
Temporary state during workflow execution (experimental):
State Expiry
You can set automatic expiry for states:Tables
Store structured data in custom tables:Workflows (Experimental)
Workflows are long-running processes that can pause and resume:Starting Your Bot
Cloud Deployment
- Automatic scaling
- Built-in state management
- Integration with Botpress Studio
- Analytics and monitoring
- Webhook management
Local Development
Best Practices
Separate Concerns
Keep definition (structure) and implementation (logic) separate for better organization and testing.
Use Type Safety
Leverage TypeScript and Zod schemas for compile-time and runtime validation.
Handle Errors
Always handle errors gracefully in handlers. Errors bubble up to Botpress Cloud for logging.
State Management
Choose the right state scope (bot/user/conversation/workflow) based on your data’s lifecycle.
Next Steps
Integrations
Learn how to connect your bot to external platforms
Plugins
Extend your bot with reusable plugins
SDK Reference
Explore the complete SDK API
Examples
See complete bot examples