Skip to content
← Public packages

@kentcdodds/x

X API v2 helpers for tweets, search, legacy DMs, and encrypted X Chat via a Fly XDK sidecar.

src/send-dm.ts

27 lines · 1.3 KB · TypeScript
import { sendDm } from './client.ts'
import type { XSendDmParams } from './client.ts'

/**
 * Send or dry-run an X direct message; sending requires confirm: true.
 * Target one of: conversationId, participantId, username, or participantIds (group).
 * Pass `account: '<purpose>'` for integration `x-<purpose>` (default omit → `x`).
 * Requires the OAuth token to have the dm.write scope.
 *
 * Note: for encrypted X Chat threads prefer `send-chat` (sidecar encrypt +
 * `/2/chat/conversations/.../messages`). Legacy send can still succeed, but
 * `list-dm-events` / `get-dm-conversation` will not read that plaintext.
 *
 * Alias targets (`to`/`userId`/`recipient`) and text (`message`/`body`/`content`) are mapped; unknown keys are rejected. Dry-run echoes `targetMode`.
 *
 * Pass optional `idempotencyKey` for retry-safe sends: same `(account, key)`
 * within 7 days returns the prior successful result from package storage.
 *
 * @returns Sent DM payload ({ dm_conversation_id, dm_event_id }), or dry-run request description.
 * @example
 * import sendDm from 'kody:@kentcdodds/x/send-dm'
 * const draft = await sendDm({ username: 'kentcdodds', text: 'Hello!', dryRun: true })
 * // => { dryRun: true, method: 'POST', url: '...', requiresConfirm: true }
 */
export default async function sendDmEntrypoint(params: XSendDmParams = {}) {
  return await sendDm(params)
}