Mentions and direct messages

Reaching a person — rather than posting into a room and hoping — comes down to two things: mentioning them, and messaging them directly.

Writing a mention

A hand-written @name looks exactly like a real mention and notifies nobody, so mentions are part of the message model rather than something you type:

import { mention, message } from "@wingman/core";

await conversation.reply(message(`${mention(onCall)} could you take this one?`));

mention() accepts a user id, a ChatUser (whose displayName becomes the label), or a Mention. It returns an opaque token you can drop into any text a message carries; each adapter resolves it while rendering:

Platform Renders as
Slack <@U123>
Google Chat <users/123>
Microsoft Teams <at>Ada</at> plus an entities declaration

The Teams pair is the one that is easy to get wrong by hand: without the matching entities entry Teams shows literal markup and notifies nobody, which is exactly why bots do not write the span themselves.

Supply a label when the bot knows one. Slack and Google Chat resolve the id to a current display name themselves; Teams shows the label verbatim.

mentionEveryone() writes the platform's notify-the-room mention — @channel on Slack, <users/all> on Google Chat. Teams has no bot-sendable equivalent, so it degrades to plain text there.

Try it: How do I mention somebody so they are notified?

Reading mentions

for (const named of event.message.mentions) {
  if (named.isSelf) continue;          // that one is us
  conversation.logger.event(EVENTS.ALSO_NAMED, { id: named.id });   // declared with defineEvents
}

The ids are carried separately because message.text has already had the bot's own mention stripped and the rest flattened to @name — which is what a model should read, and useless for addressing anybody. Each adapter parses them from its platform's structured source (Slack's raw text, Chat's annotations, Teams' entities), never from display text.

The mentions capability

Every adapter the SDK ships declares it. Unlike the rest of the catalogue an undeclared mentions does not throw — the mention degrades to @name, which reads correctly and notifies nobody. Check it when the notification is the point:

if (!conversation.canMentionUsers) {
  // Whatever you were about to promise, do not promise it.
}

One more limit: a mention inside a Google Chat card renders as @name rather than notifying, because Chat only resolves mentions in a message's text. Put a mention that has to reach somebody in prose, not in a fields block.

Direct messages

const sent = await conversation.directMessage("U123", "Could you take a look?");
if (sent === null) {
  // Nobody was reached. Say so rather than assuming.
}

Requires the directMessages capability, and returns null — rather than throwing — for a person the platform cannot reach. That is a routine outcome, not an error:

Platform Needs
Slack the im:write scope (conversations.open)
Google Chat a chat the user started; an app cannot open one
Microsoft Teams TEAMS_TENANT_ID, and a known Connector host

Bringing more people into the conversation

if (conversation.canIncludePeople) {
  const result = await conversation.includePeople(
    ["[email protected]", "U024BE7LH"],
    message("I have asked Ann and Ben to help with this."),
    { because: "Escalation: brought in the on-call people" },
  );
  if (!result.included.length) {
    // Nobody was reached. Say so rather than assuming.
  }
}

One call means the same thing everywhere: from this message on, these people are part of the conversation. Name them by email address or by the platform's user id. How each platform gets there:

Platform What happens
Email They are copied on the thread (Cc), on this message and every later reply. Same conversation.
Slack A group direct message is opened with the person who asked, the people and the bot, and the message is posted there. The conversation goes on in that room (result.newConversation is true); the bot answers there when it is mentioned. Needs the mpim:write scope, and holds at most eight people.
Google Chat, Microsoft Teams, web chat Not available — reach each person with directMessage instead.

The result lists who is now in the conversation (included) and who could not be added (notIncluded: no account, a deactivated one, an invalid address, a full room). When nobody could be, nothing is sent and sent is null.

Asking a person to take over

Put together, "the bot cannot help, get a person" is an ordinary message send — there is no special machinery for it. askForHelp in this bot's answer.ts is the worked example: it posts to a configured channel, falls back to a direct message, falls back to asking the room, and returns whether anything actually reached somebody.

The one rule worth copying: never tell a user a person is coming unless one was reached. That includes what an AI tool returns to the model, since the model writes the sentence the user reads. See AI and tools.

Routing a reply back

Asking is half of it. The colleague who picks the request up is in a different conversation from the person waiting, so the request has to carry the place the question came from. Two primitives make that possible, and neither of them knows what a hand-off is:

  • conversation.store — the bot's own deployment-wide key/value space. conversation.state is keyed per channel and thread by construction, which is what makes it safe and also what makes it unable to hold something one conversation writes and another reads. Give each concern its own literal key prefix, and put every dynamic part of a key through encodeKeyComponent.
  • conversation.replyTo(ref, message) — posts under a MessageRef the bot remembered: into that message's thread, or starting one on it, and degrading to a plain post to that channel where the platform has no threads. It is the counterpart to sendTo, which drops the thread because it addresses a different conversation entirely.
// While the question is being handled — after the request has actually landed.
await conversation.store.put(`handoff:${id}`, JSON.stringify({ ref: askedAt }), 86_400);

// While the button on it is being handled, in another channel:
const told = await conversation.replyTo(askedAt, `${mention(helper)} has picked this up.`);

This bot's handoff.ts is the worked example: the request carries an I have got this button, pressing it tells the asker in their own thread who is coming, and anything the claimant then writes in the request's thread is relayed to them. Four rules are worth copying with it.

  • The honesty rule applies to the way back too. The asker is told somebody is coming only once that send came back. If it did not, the claim is given up and the colleague is told, so they can find the person themselves.
  • First press wins. A second person is told who has it, rather than a second promise reaching the asker.
  • A hand-off expires. A day, here — long enough to survive a night, short enough that a thread nobody answered stops being a live route into a stranger's conversation.
  • It degrades. No buttons means no button on the request and no record; no threads means the asker is still told somebody is coming, and the colleague is told plainly that replies cannot be routed from there.
← HTML, embedded pages and filesAI and tools →