Post status
One script reply has been approved by the moderators Verified

Create a new toot. A required parameter are a mastodon resource with an accessToken and "status" (text content). Optional parameters are an array of media files (base64 encoded), a boolean flag "sensitive", a string "spoiler_text", a string "inReplyToId", an ENUM["public","unlisted","private","direct"] "visibility", an ISO 8601 datetime "scheduledAt" and an ISO 639 language code "language". It should return the JSON object that the API responds.

Created by jaller94 1212 days ago
Submitted by jaller94 Deno
Verified 211 days ago
1
export async function main(
2
  baseUrl: string,
3
  accessToken: string,
4
  status: string,
5
  inReplyToId?: string,
6
  sensitive = false,
7
  spoilerText?: string,
8
  visibility: "public" | "unlisted" | "private" | "direct" = "public",
9
  language?: string,
10
) {
11
  const resp = await fetch(`${baseUrl}/api/v1/statuses`, {
12
    method: "POST",
13
    headers: {
14
      Authorization: `Bearer ${accessToken}`,
15
      "Content-Type": "application/json",
16
    },
17
    body: JSON.stringify({
18
      sensitive,
19
      status,
20
      visibility,
21
      ...(inReplyToId && {
22
        in_reply_to_id: inReplyToId,
23
      }),
24
      ...(spoilerText && {
25
        spoiler_text: spoilerText,
26
      }),
27
      ...(language && {
28
        language,
29
      }),
30
    }),
31
  });
32

33
  if (!resp.ok) {
34
    throw Error(`Failed to post status: Error HTTP${resp.status}`);
35
  }
36

37
  return await resp.json();
38
}
39

Other submissions