Getting Started
Install the Bot SDK and create your first Hazel bot
This guide walks you through installing the Bot SDK and creating a simple bot that responds to messages.
Prerequisites
- Bun v1.0 or later (or Node.js 20+)
- A Hazel bot token (get one from your organization settings)
- Basic familiarity with TypeScript and async/await
Installation
bun add @hazel/bot-sdk effectOr with npm:
npm install @hazel/bot-sdk effectEnvironment Setup
Create a .env file with your bot token:
BOT_TOKEN=your-bot-token-hereFor local development, you may also need:
# Optional: Override for local development
ELECTRIC_URL=http://localhost:8787/v1/shape
BACKEND_URL=http://localhost:3003
REDIS_URL=redis://localhost:6379Your First Bot
Create an index.ts file:
import { Effect } from "effect"
import { runHazelBot } from "@hazel/bot-sdk"
runHazelBot({
setup: (bot) =>
Effect.gen(function* () {
// Log when the bot starts
yield* Effect.log("Bot is starting...")
// Respond to every message
yield* bot.onMessage((message) =>
Effect.gen(function* () {
// Don't respond to our own messages
if (message.content.startsWith("Echo:")) {
return
}
yield* bot.message.reply(message, `Echo: ${message.content}`)
}),
)
}),
})Run the Bot
bun run index.tsYou should see:
Bot is starting...Send a message in any channel the bot has access to, and it will echo it back!
Understanding the Code
runHazelBot
The main entry point for creating bots. It handles:
- Loading configuration from environment variables
- Connecting to the Hazel real-time event stream
- Graceful shutdown on SIGINT/SIGTERM
- Error logging and recovery
bot.onMessage
Registers a handler for new messages. The handler receives the full message object with:
message.id- Unique message IDmessage.content- Message text contentmessage.channelId- Channel where the message was sentmessage.authorId- User who sent the messagemessage.createdAt- When the message was created
bot.message.reply
Sends a reply to a message. The SDK provides several message operations:
bot.message.send(channelId, content)- Send a new messagebot.message.reply(message, content)- Reply to a messagebot.message.update(message, newContent)- Edit a messagebot.message.delete(messageId)- Delete a messagebot.message.react(message, emoji)- React with an emoji