Bots in direct messages
Let people talk to your bot one to one: they open its conversation from its profile, Discover or a link, press Start, and your bot answers.
You will need
- Node 22 or newer and a bot token (
YOUR_BOT_TOKENbelow). - The bot set to Public in the Developer Portal. Anyone can then write to it, with no server in common. A private bot is reached only by people who share a server with it.
- The
INTERACTIONSintent, which is on by default.
export ZRZUR_BOT_TOKEN=YOUR_BOT_TOKEN
export ZRZUR_API_URL=https://api.zrzur.com
export ZRZUR_WS_URL=wss://events.zrzur.comOn Windows, in PowerShell:
$env:ZRZUR_BOT_TOKEN = "YOUR_BOT_TOKEN"
$env:ZRZUR_API_URL = "https://api.zrzur.com"
$env:ZRZUR_WS_URL = "wss://events.zrzur.com"How a conversation starts
- The person opens the conversation: Message on the bot's profile or its Discover page, or a bot link.
- While the conversation is empty, Zrzur shows the bot's name, its description, up to five of its global commands and a Start button.
- Pressing Start sends your bot an interaction of type
Start. From then on your bot may write to the person. Writing a message to your bot works the same way.
Your bot hears of the conversation only then: its ChannelCreate comes with the Start or the first message, not when the person opens the conversation. Until then your bot cannot write to the person: POST /channels/{id}/messages answers 403. A person who shares a server with your bot follows their own privacy setting, as before.
Bot links
A bot link opens the conversation with Start waiting. Add start to pass your bot a short value, for example where the person came from:
https://zrzur.cc/b/YOUR_BOT_ID?start=spring-sale| Part | Rule |
|---|---|
YOUR_BOT_ID | The bot's id, as in its invite link |
start | 1 to 64 characters of A-Z, a-z, 0-9, _ and -. Optional: ?start alone opens the conversation without a value. Anything else is dropped and the conversation still opens. |
The value arrives once, when the person presses Start.
The start interaction
InteractionCreate with type: "Start", in the person's direct message with your bot (no server_id). start.payload is the link's value and is missing when there was none.
{
"_id": "01JD0000000000000000000000",
"type": "Start",
"application_id": "YOUR_BOT_ID",
"channel_id": "01JD0000000000000000000001",
"user": { "_id": "01JD0000000000000000000002", "username": "sami" },
"start": { "payload": "spring-sale" },
"token": "INTERACTION_TOKEN"
}Answer it like any interaction, within 3 seconds: a message, a deferred message or a form. See Interactions. bot.on("start", …) gives you the interaction, and interaction.startPayload the value (or null).
The code
// Bots in direct messages: greet a person who presses Start, read the
// payload of the bot link they came from, and answer with a button.
// Open the conversation with a link like https://zrzur.cc/b/BOT_ID?start=spring-sale
import { Client } from "@zrzur/sdk/bot";
const bot = new Client({
token: process.env.ZRZUR_BOT_TOKEN,
apiUrl: process.env.ZRZUR_API_URL,
wsUrl: process.env.ZRZUR_WS_URL,
});
bot.on("start", (i) => {
// the `?start=` of the link, or null when they came another way
const from = i.startPayload;
return i.reply({
content: from
? `Welcome! You came from the link "${from}".`
: "Welcome! Press the button to see what I can do.",
components: [
{
type: "button",
style: "primary",
label: "Show tips",
customId: "tips",
},
],
});
});
bot.component("tips", (i) =>
i.reply({ content: "Tip: type / or press Commands to see my commands." }),
);
bot.command("help", (i) =>
i.reply({ content: "Press Start, or use the buttons I send you." }),
);
await bot.login();
// global commands: they are listed on the Start screen and under Commands
await bot.syncCommands([{ name: "help", description: "What this bot does" }]);
console.log("ready");
Run it with node dm-start.mjs.
What you should see
Open https://zrzur.cc/b/YOUR_BOT_ID?start=spring-sale, then press Start. The bot answers Welcome! You came from the link "spring-sale". with a Show tips button. Pressing it gives the tip. Its /help command is listed on the Start screen and under Commands in the message box.
Stop and block
The person can choose Stop bot in the conversation's menu. It blocks your bot: your messages and answers there are refused with 403, and new interactions from them stop. After Restart bot they press Start again, and you get a new Start interaction. What they wrote before the stop no longer counts: until a new Start or a new message, your bot can't write to them.
Common errors
| Error | Cause | Fix |
|---|---|---|
DirectMessagesRestricted | The bot is private and shares no server with the person | Make the bot public, or share a server |
403 on sending | The person has not pressed Start or written yet, or stopped the bot | Wait for Start or their first message |
FailedValidation | A start value with other characters or over 64 characters | Keep to A-Z a-z 0-9 _ - |
FeatureDisabled | Bots in direct messages are off on this server | Ask the server's admin |