Skip to content

Black Friday

Annual Pro is $90 for the first year, normally $120. Or start monthly at $6 for two months, then $12.

Ends Dec 4, 11:59 PM PT.

Create an account
← Public packages

@tannerlinsley/imessage

Read, search, draft, and explicitly send iMessages on a Mac, using only built-in macOS tools.

FollowFork

Files

send.ts

29 lines · 1.5 KB · TypeScript
import { native, object, validateDraft } from "./core.ts";
import type { MessageDraft } from "./core.ts";
import type { SendReceipt } from "./types.ts";
/**
 * Preview a reviewed draft by default, or explicitly send it through this Mac's Messages app.
 * @param input - draft from /draft, and confirmSend (default false). Set true only after the user explicitly approves this exact target and text. Never use true in smoke tests.
 * @returns Dry-run preview without any host command, or a local send receipt. recorded is not a delivery receipt. Never retry an uncertain result automatically.
 * @example
 * import prepare from 'kody:@tannerlinsley/imessage/draft';
 * import send from 'kody:@tannerlinsley/imessage/send';
 * const draft = await prepare({target:{recipient:'test@example.invalid'},text:'Synthetic preview, do not send.'});
 * const preview = await send({draft,confirmSend:false});
 */
export default async function send(input: {
  draft: MessageDraft;
  confirmSend?: boolean;
}): Promise<SendReceipt | { sent: false; dryRun: true; draft: MessageDraft }> {
  const p = object(input, ["draft", "confirmSend"]);
  if (p.confirmSend !== undefined && typeof p.confirmSend !== "boolean")
    throw new Error("confirmSend must be a boolean.");
  const reviewed = await validateDraft(p.draft);
  if (p.confirmSend !== true) return { sent: false, dryRun: true, draft: reviewed };
  return native<SendReceipt>({
    action: "send",
    ...reviewed.target,
    text: reviewed.text,
    service: reviewed.service,
  });
}