# Ping (hosted bot)

> The smallest hosted bot: /ping answers pong.

The smallest bot there is: one slash command, one reply. It asks only for `VIEW_CHANNEL` and `SEND_MESSAGES`, with no message content, no network and no storage.

Source in zaun-bot: [`examples/ts/ping`](https://github.com/zaunchat/zaun-bot/tree/main/examples/ts/ping) (TypeScript) and [`examples/rust/ping`](https://github.com/zaunchat/zaun-bot/tree/main/examples/rust/ping) (Rust).

## The code

**TypeScript:**

```ts title="src/index.ts"
// `/ping` → "pong", in TypeScript. Shows "Listening to /ping" as its activity.

const bot = new Bot();

bot.on('init', () => bot.presence.set('online', Activity.listening('/ping')));

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:**

```rust title="src/lib.rs"
//! `/ping` → "pong", with the round trip the bot saw. Shows "Listening to /ping" as its
//! activity.

use zaun_bot::prelude::*;
use zaun_bot::presence;

#[derive(Default)]
struct Ping;

impl Bot for Ping {
    fn on_start(&mut self, _: &Config) {
        presence::set(Status::Online, Activity::listening("/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 Err(e) = ctx.reply("pong 🏓")
        {
            log_info!("reply refused: {}", e.code);
        }
    }
}

export_bot!(Ping);
```

## The manifest

**TypeScript:**

```toml title="zaun-bot.toml"
app_id = "org.zaun.examples.ts-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.
```

**Rust:**

```toml title="zaun-bot.toml"
app_id = "org.zaun.examples.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.
```

## How it works

- The command is registered every time the bot starts (`bot.command(..)` in TypeScript, `Bot::commands` in Rust).
- When the instance starts, it sets its activity: members see "Listening to /ping" next to it ([Status and presence](/docs/guide/bots/status-and-presence/)). Setting its own status needs no capability.
- The handler answers with a reply. A refused reply (for example, the bot lost `SEND_MESSAGES` in that channel) is logged (TypeScript: the `error` event and the host log; Rust: `log_info!`).

## Run it

**TypeScript:**

```sh
git clone https://github.com/zaunchat/zaun-bot && cd zaun-bot/examples/ts/ping
npm install
zbot build && zbot pack --unsigned
zaun-host bot install --community <ID> *.zbot   # on a host with allow_unsigned = true
```

**Rust:**

```sh
git clone https://github.com/zaunchat/zaun-bot && cd zaun-bot/examples/rust/ping
zbot build && zbot pack --unsigned
zaun-host bot install --community <ID> *.zbot   # on a host with allow_unsigned = true
```
