JavaScript quickstart

This walks you from nothing to a bot that posts a message, answers !ping with pong, and plays an audio file in a voice room, in plain JavaScript (no build step). It uses the SDK in sdk/js (yuva-bot); it is written in TypeScript, so editors like VS Code still show its types and docs as you type. Node.js 22 or newer. The TypeScript quickstart covers the same SDK in more detail; every call below is the same there.

1. Create the bot#

  1. In Yuva, open Ayarlar → Geliştirici and use Bot oluştur (a name, and optionally what it does).

  2. Copy the token (yb_…) right away: it is shown once. If you lose it, Anahtarı yenile gives a new one and the old one stops working. Keep it out of your code and out of git; the examples read it from YUVA_BOT_TOKEN.

  3. Copy the bot id (bot_…) with Kimliği kopyala.

See authentication for how the token is sent and what to do when it is revoked.

2. Get it into a lodge#

A bot only sees the lodges it was added to. Someone with the manageBots staff right in the lodge (the owner, or a role that has it) opens the lodge settings, Botlar, pastes the bot id and picks what the bot may do: read, write, speak, mention, and so on. They can change or take away those rights at any time. To post you need write, to join voice speak. Permissions lists every right.

3. Install the SDK#

The SDK is not on npm; install it from your checkout of the Yuva repository. In your bot's own folder:

sh

npm init -ynpm pkg set type=modulenpm install /path/to/yuva/sdk/js

npm install builds the SDK and links it, so after pulling a newer Yuva run npm run build in sdk/js. The package is ES modules only: keep "type": "module" in your package.json (or name your files .mjs) and use import, not require. Run a bot with node bot.js.

Voice playback needs FFmpeg: the ffmpeg command must be on your PATH (ffmpeg -version should work in the same terminal). Text-only bots don't need it.

4. Post a message#

js

import { Client } from "yuva-bot";const bot = new Client(process.env.YUVA_BOT_TOKEN, "https://yuva.chat");const me = await bot.me();console.log("I am", me.displayName, me.userId);for (const lodge of await bot.lodges()) {  const { channels } = await bot.lodge(lodge.id);  const room = channels.find((c) => c.kind === "text" && c.perms.write);  if (room) await bot.sendMessage(room.id, "Merhaba! 👋");}

The second argument is your Yuva site; the SDK talks to <site>/api/bot/v1. Every method maps to one endpoint of the API reference (the TypeScript quickstart has the full table) and resolves to its JSON. A failed call rejects with ApiError, which has .status and .message (the server's Turkish message, fine to show to people):

js

import { ApiError } from "yuva-bot";try {  await bot.sendMessage(301, "selam @everyone");} catch (err) {  if (err instanceof ApiError && err.status === 403) console.log("not allowed:", err.message);  else throw err;}

Files: const file = await bot.uploadFile(channelId, "rapor.pdf") (or { data: buffer, name: "rapor.pdf" }), then await bot.sendMessage(channelId, { body: "Rapor", fileId: file.id }). Attachments need a trusted account, see the reference.

5. Answer messages#

js

import { Client } from "yuva-bot";const bot = new Client(process.env.YUVA_BOT_TOKEN, "https://yuva.chat");bot.on("message.create", async ({ channelId, message }) => {  if (message.body.trim() === "!ping") {    await bot.sendMessage(channelId, "pong");    await bot.react(message.id, "heart"); // or one Unicode emoji: "🔥", "👍🏽", "🇹🇷"  }});bot.on("error", (err) => console.error("bot error:", err));await bot.start(); // runs until bot.stop(), or until the token stops working

start() polls GET /events every 1.5 seconds across every text room the bot can read, keeps the cursor, polls again at once after a full page, and backs off on 429, 5xx and network errors. It skips the bot's own messages (new Client(token, site, { includeOwn: true }) to get them). On 401 it reports the ApiError to "error" and resolves. Handlers run one at a time in event order; one that throws goes to "error" and the loop carries on. Also available: message.update (edits, reactions, pins) and message.delete (event.messageId).

Stay within the rate limits: the events loop and a voice connection use a fraction of them.

6. Play audio in a voice room#

js

import { ApiError, Client } from "yuva-bot";const bot = new Client(process.env.YUVA_BOT_TOKEN, "https://yuva.chat");const [lodge] = await bot.lodges();const { channels } = await bot.lodge(lodge.id);const room = channels.find((c) => c.kind === "voice" && c.perms.speak);const voice = await bot.joinVoice(lodge.id, room.id);voice.on("lost", (err) => console.log("seat lost:", err.message));try {  await voice.play("song.mp3"); // resolves when the file ends} catch (err) {  if (err instanceof ApiError) console.log("kicked out:", err.message);  else throw err;} finally {  await voice.leave();}

joinVoice takes the seat, connects to the room's LiveKit room (or, in an older mesh lodge, dials everyone in the room) and keeps the seat alive; people who join later hear the bot too. The bot shows as muted while nothing plays (or after voice.setMuted(true)) and always as deafened. voice.stop() ends the current file early. If a moderator moves the bot it follows; if it is disconnected, loses speak, is removed from the lodge or another process takes the seat over, "lost" handlers run and a pending play() rejects with the ApiError. See the TypeScript quickstart for how playback works and the voice guide for the protocol underneath.

Next#