# Bot quick start

> Write, build, pack and install a /ping bot with zbot, in TypeScript (the default) or Rust.

A Zaun bot is a WebAssembly component that runs inside the community's own host. You can write it
in **TypeScript** with `@zaun/bot` or in **Rust** with the `zaun-bot` crate: both build to the same
kind of package and use the same host features. This guide shows TypeScript first; pick the Rust
tab to follow along in Rust. [TypeScript or Rust?](/docs/guide/bots/typescript-or-rust/) compares them.

1. **Install the tools.**

   **TypeScript:**

   You need Node.js 22 or newer with npm, and `zbot`:

   ```sh
   cargo install --git https://github.com/zaunchat/zaun-bot zbot
   ```

   Or download `zbot` for Linux, macOS or Windows from the
   [zaun-bot releases](https://github.com/zaunchat/zaun-bot/releases). esbuild and jco (which
   turn your TypeScript into a component) are installed into the project by `npm install`, so
   `zbot` never downloads anything itself.

   **Rust:**

   ```sh
   rustup target add wasm32-wasip2
   cargo install --git https://github.com/zaunchat/zaun-bot zbot
   ```

   Or download `zbot` for Linux, macOS or Windows from the
   [zaun-bot releases](https://github.com/zaunchat/zaun-bot/releases).

2. **Create a bot.**

   **TypeScript:**

   ```sh
   zbot new ping --lang ts && cd ping
   npm install
   ```

   - ping/
     - package.json depends on @zaun/bot
     - tsconfig.json
     - zaun-bot.toml the manifest: what the bot asks for
     - src/
       - index.ts a /ping bot

   **Rust:**

   ```sh
   zbot new ping && cd ping
   ```

   - ping/
     - Cargo.toml a cdylib depending on zaun-bot
     - zaun-bot.toml the manifest: what the bot asks for
     - src/
       - lib.rs a /ping bot

3. **Write the bot.**

   **TypeScript:**

   A bot is a `Bot` object: register commands with `bot.command(builder, handler)`, then export
   the functions the host calls, once, at the end of the entry file.

   ```ts title="src/index.ts"
   import { Bot, SlashCommandBuilder } from '@zaun/bot';

   const bot = new Bot();

   bot.command(
     new SlashCommandBuilder().setName('ping').setDescription('Check that the bot is alive'),
     (interaction) => {
       interaction.reply('pong 🏓');
     },
   );

   export const { init, onEvent, onInteraction, onAutocomplete, onTimer, onVoiceEvent } = bot.exports();
   ```

   **Rust:**

   A bot is a type implementing `Bot`, exported once with `export_bot!`. Every handler has a no-op
   default; implement the ones you need.

   ```rust title="src/lib.rs"
   use zaun_bot::prelude::*;

   #[derive(Default)]
   struct Ping;

   impl Bot for Ping {
       fn commands(&mut self, _: &Config) -> Vec<ApplicationCommand> {
           vec![SlashCommand::new("ping", "Check that the bot is alive").build()]
       }

       fn on_command(&mut self, ctx: Context, command: CommandData) {
           if command.name == "ping" {
               let _ = ctx.reply("pong 🏓");
           }
       }
   }

   export_bot!(Ping);
   ```

4. **Say what it needs** in the manifest. Ask for the least you can: owners see every request in
   plain words before they install. The manifest is the same for both languages.

   ```toml title="zaun-bot.toml"
   app_id = "org.example.ping"
   version = "0.1.0"
   name = "Ping"
   description = "Answers /ping with pong."
   permissions = ["VIEW_CHANNEL", "SEND_MESSAGES"]

   [capabilities]
   # nothing beyond interactions: no message content, no network, no storage
   ```

5. **Build, pack and inspect.**

   **TypeScript:**

   ```sh
   zbot build                  # esbuild src/index.ts → jco componentize → target/bot.wasm
   zbot pack --unsigned        # org.example.ping-0.1.0.zbot
   zbot inspect *.zbot         # what it is and what it asks for, in plain words
   ```

   **Rust:**

   ```sh
   zbot build                  # cargo build --release --target wasm32-wasip2
   zbot pack --unsigned        # org.example.ping-0.1.0.zbot
   zbot inspect *.zbot         # what it is and what it asks for, in plain words
   ```

6. **Install it on a host.** For a development package, the host needs `allow_unsigned = true` in
   its `[bots]` configuration.

   ```sh
   zaun-host bot install --community <COMMUNITY_ID> org.example.ping-0.1.0.zbot
   ```

   The command shows what the bot asks for and asks you to confirm. Members now see `/ping` in
   their command picker.

> **Caution: Answer within 3 seconds**
>
> Every interaction must be answered within 3 seconds: with a reply, a modal, an update, or a
> deferral followed later by an edit (`interaction.deferReply()` then `interaction.editReply(..)` in
> TypeScript, `ctx.defer(..)` then `ctx.edit_original(..)` in Rust). The host defers for you at
> 2.5 s if a handler is still running, but one handler runs at a time per bot, so slow handlers hold
> up the others.
> **Tip: TypeScript notes**
>
>
> `setTimeout` and `setInterval` never fire between calls: use `bot.timers` and listen to
> `timer`. There is no `fetch`; use `bot.http.fetch(..)`. An autocomplete handler must call
> `interaction.respond(..)` before its first `await`.

## Next

- [Slash commands](/docs/guide/bots/slash-commands/) and [options](/docs/guide/bots/options-and-autocomplete/)
- [Interactions and responses](/docs/guide/bots/interactions/)
- [Events](/docs/guide/bots/events/), [direct messages](/docs/guide/bots/direct-messages/) and [voice](/docs/guide/bots/voice-and-music/)
- More complete [examples](/docs/examples/)
