const dmThreadId = await adapter.openDM(userId);// Post to the DMconst thread = chat.thread(dmThreadId);await thread.post("Hello! This is a private message.");
interface Adapter { /** * Open a direct message conversation with a user. * * @param userId - The platform-specific user ID * @returns The thread ID for the DM conversation */ openDM?(userId: string): Promise<string>;}
import { Chat, Card, Text, Actions, Button } from "chat";const chat = new Chat({ /* ... */ });chat.onAction("send_dm", async (event) => { // Open DM with the user who clicked const dmThreadId = await event.adapter.openDM(event.user.userId); const dmThread = chat.thread(dmThreadId); await dmThread.post( <Card title="Private Message"> <Text>This is just for you, {event.user.fullName}!</Text> </Card> ); // Confirm in the original thread await event.thread.post("Check your DMs!");});
chat.onNewMention(async (thread, message) => { // Open DM with the user who mentioned the bot const dmThreadId = await thread.adapter.openDM(message.author.userId); const dmThread = chat.thread(dmThreadId); await dmThread.post( "Thanks for mentioning me! How can I help?" );});
interface Thread { readonly isDM: boolean;}// In a handlerchat.onNewMention(async (thread, message) => { if (thread.isDM) { await thread.post("This is a private conversation."); } else { await thread.post("This is a public channel."); }});
try { const dmThreadId = await adapter.openDM(userId); const dmThread = chat.thread(dmThreadId); await dmThread.post("Hello!");} catch (error) { console.error("Failed to open DM:", error); // Fall back to posting in the original thread await thread.post( `@${userName} I couldn't send you a DM. Please check your privacy settings.` );}