Bot SDK Fundamentals
Learn the fundamentals of the Towns Protocol Bot SDK and set up your development environment. Understand what the Bot SDK is, how it works, and prepare your project for building and deploying a production-ready bot.
Learning Objectives
By the end of this module, you will be able to:
What is the Bot SDK?
Learn what the Towns Protocol Bot SDK is and how it helps you build bots.
The Towns Protocol Bot SDK (@towns-protocol/bot) is a framework that makes it easy to build interactive bots. It handles webhook communication, slash commands, and event processing so you can focus on your bot's functionality.
How It Works
Your bot runs as a web server that receives events from Towns via webhooks. When users interact with your bot (messages, commands, reactions), the SDK processes these events and provides simple handler functions to respond.
Key Features
- • Send messages to channels
- • Handle slash commands
- • React to user events
- • Secure webhook validation
Main Packages
- •
@towns-protocol/bot - •
@towns-protocol/sdk - •
hono(web server)
Initialize Your Bot
Use the official scaffolding tool to create your bot project with all necessary files.
Create Your Bot Project
Run this command to create a new bot project with the official scaffolding:
bunx towns-bot init my-towns-bot
cd my-towns-botGenerated Project Structure
The init command creates a complete bot project with this structure:
my-towns-bot/
├── src/
│ ├── index.ts # Main bot file with webhook setup
│ └── commands.ts # Slash command definitions
├── .env.sample # Environment variable template
├── .gitignore # Git ignore (protects secrets)
├── package.json # Dependencies and scripts
└── tsconfig.json # TypeScript configurationWhat You Get
- • Bot server setup with Hono and webhook handling
- • Example commands ready to customize
- • TypeScript configuration optimized for bot development
- • Build scripts for development and production
Install Dependencies
Install all required packages for your bot.
Install Packages
The generated project includes all necessary dependencies. Install them with:
bun installAvailable Scripts
Your package.json includes these useful scripts:
bun run devStart development server with hot reload (watches for file changes)
bun run startRun the production build (used by Render in Module 6)
Configure Environment
Set up environment variables for your bot.
Create .env File
Copy the sample environment file to create your .env:
cp .env.sample .envEnvironment Variables
Your .env file should contain:
# Bot Credentials (you'll get these in Module 6)
APP_PRIVATE_DATA=your_private_data_here
JWT_SECRET=your_jwt_secret_here
# Server Port
PORT=5123Getting Your Credentials
You'll get your actual APP_PRIVATE_DATA and JWT_SECRET from the Towns Developer Portal in Module 6: Deployment. For now, the placeholder values are fine for local development.
Understanding the Bot Code
Walk through the generated bot code to understand how it works.
Generated src/index.ts
The init command created a complete bot server. Here are the key parts:
import { Hono } from 'hono'
import { makeTownsBot } from '@towns-protocol/bot'
import commands from './commands.js'
// Initialize bot with credentials and commands
const bot = await makeTownsBot(
process.env.APP_PRIVATE_DATA!,
process.env.JWT_SECRET!,
{ commands }
)
// Start bot and get middleware
const { jwtMiddleware, handler } = bot.start()
// Create Hono app
const app = new Hono()
// Webhook endpoint - receives events from Towns
app.post('/webhook', jwtMiddleware, handler)
export default app
How It Works
1. Bot Initialization
makeTownsBot() creates your bot using credentials from .env and registers your slash commands.
2. Webhook Setup
The /webhook endpoint receives events from Towns (messages, commands, reactions). The jwtMiddleware validates requests for security.
3. HTTP Server
Hono creates a lightweight HTTP server on port 5123 (or your configured PORT).
Command Definitions src/commands.ts
The generated file includes example commands. We'll customize these in Module 2:
export default [
{
name: 'hello',
description: 'Say hello to the bot',
},
{
name: 'help',
description: 'Get help with bot commands',
},
]Module 1 Complete!
Excellent! You've initialized your bot project and installed dependencies. Your bot foundation is ready for adding commands and features in the next modules!