Conversations and retries

Keep a thread together with conversation, tell Dexbi who's talking, send the member's images, stop double-sends with messageId, and hand a conversation back to Dexbi.

conversation: one id per thread

Dexbi remembers each conversation, so a follow-up like "and what about shipping?" makes sense. Send every message of the same thread with the same conversation id, like your Modmail thread id or chat session id.

  • Any string from 1 to 200 characters that's unique to the thread in your app.
  • A new id starts a fresh conversation with no history.
  • API conversations are kept apart from your Discord ones. They never mix with a ticket or channel in your server.
  • Escalations and closes are remembered per conversation. See After an escalation.

member: who wrote it

FieldRequiredWhat it is
member.idYesA stable id for the person, up to 100 characters, like their Discord user id. Per-member rules use it: the daily reply limit, Ignore trolls and people in Blocked.
member.nameNoTheir display name, up to 100 characters.

With author: "staff", member is the teammate who wrote the message.

author: a member or your staff

Dexbi only sees the messages you send it. Send your staff's messages in the thread too, so Dexbi knows what they told the member.

authorWhat Dexbi does
"member" (default)Reads the message and decides: reply, handover, close or none.
"staff"Keeps it as your team's words in the conversation and answers none.

A staff message is free. It isn't an AI reply, and it doesn't count toward any member's daily limit.

With agent: "ticket" and reply: "smart", a staff message gives your staff the floor: Dexbi waits for them before it answers the member. See Waiting for your staff.

{
  "conversation": "modmail-thread-8812",
  "messageId": "1424571901112223344",
  "member": { "id": "staff-77", "name": "Sam" },
  "message": "Hi Alex, I've refunded the second charge. It shows in 3 to 5 days.",
  "author": "staff"
}

images: screenshots the member sent

Send the member's images in images, and Dexbi reads them the way it reads images sent in Discord. It's on every plan.

  • Up to 4 images per message.
  • Each one is an https link on Discord's CDN, cdn.discordapp.com or media.discordapp.net, like a Discord attachment's URL.
  • The link must end in .png, .jpg, .jpeg, .webp or .gif. Send other files as text in message, like [file: invoice.pdf].
  • message can be empty when there's at least one image.
{
  "conversation": "modmail-thread-8812",
  "messageId": "1424571875512345678",
  "member": { "id": "member-1042", "name": "Alex" },
  "message": "I get this error when I try to pay",
  "images": ["https://cdn.discordapp.com/attachments/1424571869923463000/1424571875512345678/error.png"]
}

A link that doesn't fit these rules is refused with a 400 naming it, like images.0. With author: "staff", each image is kept as [image] next to your teammate's words.

directed: talking to Dexbi

Set directed: true when the member is talking to Dexbi directly: they pinged it, or replied to one of its messages. It's false by default. Three things use it:

  • With reply: "pings", Dexbi answers only directed messages.
  • After an escalation, with afterEscalation: "quiet", Dexbi answers only directed messages.
  • In a ticket with reply: "smart", a directed message is answered right away, even while Dexbi waits for your staff.
  • In a ticket Dexbi couldn't answer while your workspace was out of monthly AI replies, Dexbi answers only directed messages. See Why Dexbi sends none.

Hand a conversation back to Dexbi

After an escalation, Dexbi answers only directed messages, so your team can take over. When they're done, send the conversation to POST /v1/conversations/resume and Dexbi answers every message again. It keeps the conversation's history.

{ "conversation": "modmail-thread-8812" }

To start over instead, send it to POST /v1/conversations/forget. Dexbi forgets everything in the conversation, including the escalation, and the next message starts fresh.

Both also hand back a ticket Dexbi couldn't answer while your workspace was out of monthly AI replies. Neither one counts toward your AI replies. With agent: "public" conversations, send agent: "public" here too.

There's no pause endpoint. To keep Dexbi out of a conversation for a while, don't send the member's messages. Keep sending your staff's with author: "staff", so Dexbi knows what they told the member when you resume. See the API reference.

teamWaitOver: your staff didn't reply in time

When Dexbi sent none with teamWaitSeconds and no staff message came in that time, send everything the member wrote meanwhile as one message, with teamWaitOver: true. Dexbi answers it. See Waiting for your staff.

messageId: stop double-sends

Networks fail, so your app will sometimes send the same message twice. Send your own id for each message in messageId, like the Discord message id. We recommend it on every request.

If the same message (the same conversation, messageId, member.id and text) arrives while the first copy is still being answered, Dexbi refuses the copy with a 409 and the code request_in_progress. The copy doesn't run, and it doesn't count toward your AI replies.

Dexbi doesn't keep answers to send again. Once the first copy has finished, or failed, sending it again runs it again, and it counts again. A different message sent under an id you already used is a new message.

Without messageId, every request is a new message.

A safe retry loop

Retry 429, 500 and 503 with the same body. Don't retry a 409: the first copy is still being answered, and sending it again after that runs it a second time. Don't retry other errors: fix the request or the key instead. See Errors and limits.

const RETRYABLE = new Set([429, 500, 503]);

async function askDexbi(body, attempts = 4) {
  for (let attempt = 1; ; attempt++) {
    const response = await fetch("https://api.dexbi.app/v1/messages", {
      method: "POST",
      headers: {
        Authorization: `Bearer ${process.env.DEXBI_API_KEY}`,
        "Content-Type": "application/json",
      },
      body: JSON.stringify(body), // the same messageId on every attempt
    });
    if (response.ok) return response.json();

    const { error } = await response.json();
    if (!RETRYABLE.has(response.status) || attempt === attempts) {
      throw new Error(`Dexbi ${response.status} ${error.code}: ${error.message}`);
    }
    const waitSeconds = Number(response.headers.get("Retry-After")) || 2 ** attempt;
    await new Promise((resolve) => setTimeout(resolve, waitSeconds * 1000));
  }
}

const answer = await askDexbi({
  conversation: "modmail-thread-8812",
  messageId: "1424571869923463198",
  member: { id: "member-1042", name: "Alex" },
  message: "How do I get a refund?",
});

On this page