Prerequisites
Before you begin, make sure you have:- A bot created in the Developer Portal
- Your bot project set up locally with dependencies installed
- Your .envfile configured withAPP_PRIVATE_DATAandJWT_SECRET
Using ngrok for Local Testing
ngrok creates a secure tunnel from a public URL to your local development server, allowing Towns to send webhook events to your bot running onlocalhost.
1. Start Your Bot Locally
Run your bot in development mode:http://localhost:5123 (or the port specified in your .env file).
2. Install and Start ngrok
Follow the ngrok installation guide to install ngrok for your platform. Once installed, start ngrok to forward port 5123 (or the port specified in your.env file):
https://abc123.ngrok-free.app). This is your public-facing URL that Towns will use to send events to your bot.
The free tier of ngrok provides a random URL each time you restart. You can use a paid plan to get a consistent URL.
3. Configure Webhook URL
Update your bot’s webhook URL to point to your ngrok tunnel:- Go to app.towns.com/developer
- Click on your bot
- Under “Webhook URL”, enter your ngrok URL with the /webhookpath:
- Click “Save”
4. Test Your Bot
Now you’re ready to test! Install your bot in a test space and interact with it:- In the Towns app, go to a space you own or admin
- Go to Space Settings → Bots
- Search for your bot and install it
- Try sending messages, mentioning the bot, or using slash commands
Debugging
By default, your bot template includes request logging. You’ll see each incoming webhook event in your console:DEBUG=csb:bot bun run dev to see more detailed logs about events in your local terminal.
Switching Back to Production
When you’re ready to deploy, remember to:- Push your changes to GitHub
- Deploy to your production hosting (e.g., Render)
- Update your bot’s webhook URL back to your production URL:
Don’t forget to switch the webhook URL! Leaving it pointed at ngrok will break your bot once you close the ngrok tunnel.
Next Steps
- Learn about event handlers - Handle different event types
- Add slash commands - Create custom /help,/poll, etc.
- External interactions - Timers, webhooks, custom APIs