# Your first bot

> A bot that answers every mention in its thread. One file, Node, no packages.

Source: https://slaiky.xyz/docs/quickstart

You will make an app in a workspace, give it a token, and run one file that listens over Socket Mode and answers when someone mentions it. Nothing to host: your computer opens the connection to Slaiky.

## 1. Make the app

In Slaiky, open **Agents and integrations** in the sidebar, then **Apps & bots → Create app**. Give it a name (say *Deploy bot*). Slaiky shows its bot token once: copy it now (step 2). Then, on the app’s page, turn on **Socket Mode** under **Events**. A new app already holds the permissions this bot needs: read where it is (`channels:history`), hear mentions (`app_mentions:read`) and post (`chat:write`).

In the same place, under **Events it hears**, add `app_mention`.

> **Tip:** Prefer one document over clicks? Paste a [manifest](/docs/manifests) with **Apps & bots → From a manifest**. This one makes the same bot:
> 
> *your app → Slaiky · `manifest.json`*
> 
> ```json
> {
>   "display_information": { "name": "Deploy bot" },
>   "features": { "bot_user": { "display_name": "deploybot" } },
>   "oauth_config": { "scopes": { "bot": ["chat:write", "channels:history", "app_mentions:read"] } },
>   "settings": { "event_subscriptions": { "bot_events": ["app_mention"] }, "socket_mode_enabled": true }
> }
> ```

## 2. Copy its token

The bot token starts with `slk-bot-`. It acts as the bot, with the app’s permissions, wherever the bot is a member. Slaiky shows it only once; lost it? **Token and secret → New token** on the app’s page makes another (the old one stops at once). Keep it out of your code:

*you run*

```sh
export SLAIKY_BOT_TOKEN=slk-bot-…
```

## 3. Run the bot

Save this as `bot.mjs` and run it with Node 22 or later (it uses the built-in `fetch` and `WebSocket`).

*your app → Slaiky · `bot.mjs`*

```js
// A Slaiky bot in one file: it answers every mention in the thread. Node 22+, no packages.
// SLAIKY_BOT_TOKEN=slk-bot-… node bot.mjs
const API = process.env.SLAIKY_API ?? 'https://app.slaiky.xyz/api/methods';
const TOKEN = process.env.SLAIKY_BOT_TOKEN;

async function call(method, body = {}) {
  const res = await fetch(`${API}/${method}`, {
    method: 'POST',
    headers: { authorization: `Bearer ${TOKEN}`, 'content-type': 'application/json' },
    body: JSON.stringify(body),
  });
  const data = await res.json();
  if (!data.ok) throw new Error(`${method}: ${data.error.code} ${data.error.message}`);
  return data;
}

async function connect() {
  const { url } = await call('apps.connections.open'); // a one-time WebSocket URL
  const socket = new WebSocket(url);

  socket.onmessage = async ({ data }) => {
    const frame = JSON.parse(data);
    if (frame.t !== 'app.envelope') return;

    // 1. Say "got it" first, or Slaiky sends the same envelope again.
    socket.send(JSON.stringify({ t: 'app.ack', d: { envelopeId: frame.d.envelopeId } }));

    // 2. Then do the work.
    const event = frame.d.payload.event;
    if (frame.d.type === 'events_api' && event?.type === 'app_mention') {
      await call('chat.postMessage', {
        channel: event.channelId,
        threadId: event.message.parentId ?? event.message.id,
        text: `Hi ${event.message.author?.name ?? 'there'}! You said: ${event.message.body}`,
      });
    }
  };

  // Settings changes and Slaiky's releases close the socket: connect again.
  socket.onclose = () => setTimeout(connect, 1000);
  socket.onopen = () => console.log('Connected. Mention the bot in a channel it is in.');
}

connect();
```

*you run*

```sh
node bot.mjs
```

It asks for a one-time WebSocket address (`apps.connections.open`), connects, and then for each envelope Slaiky sends it does two things, in this order:

1. **Acknowledges it** with `app.ack` and the envelope’s id. Without that, Slaiky sends the same envelope again after 30 seconds.
2. **Answers** with `chat.postMessage`, in the thread of the message that mentioned it.

## 4. Mention it

Add the bot to a channel (type `/invite @deploybot` there), then write `@deploybot hello`. The answer shows up in the thread a moment later.

*Slaiky → your app · `app.envelope`*

```json
{
  "t": "app.envelope",
  "d": {
    "envelopeId": "7f3c0006-5a1e-4c2b-9d6f-1a2b3c4d5e05",
    "type": "events_api",
    "payload": { "type": "event_callback", "event": { "type": "app_mention", "channelId": "…", "message": { "id": "…", "body": "@deploybot hello" } } },
    "acceptsResponsePayload": false,
    "retryAttempt": 0
  }
}
```

*your app → Slaiky · `app.ack`*

```json
{ "t": "app.ack", "d": { "envelopeId": "7f3c0006-5a1e-4c2b-9d6f-1a2b3c4d5e05" } }
```

The full envelope, field by field, is on [Socket Mode frames](/docs/reference/socket-mode); every event is on [Events](/docs/reference/events).

## Where to next

- Handle more than mentions: [Socket Mode](/docs/socket-mode) covers reconnecting, retries and what you missed while away.
- Add a slash command, a button or a modal: [Commands, shortcuts and buttons](/docs/interactivity).
- Receive events on your own server instead: [Events over HTTP](/docs/events).
