(
method: string,
params: Record<string, unknown> = {},
token = BOT_TOKEN,
)
| 16 | const ENDPOINT = "https://slack.com/api/"; |
| 17 | |
| 18 | async function slack( |
| 19 | method: string, |
| 20 | params: Record<string, unknown> = {}, |
| 21 | token = BOT_TOKEN, |
| 22 | ): Promise<Record<string, unknown> & { ok: boolean }> { |
| 23 | // Slack's Web API accepts form-encoded bodies on every method. |
| 24 | // JSON body is rejected by read endpoints like conversations.replies. |
| 25 | const form = new URLSearchParams(); |
| 26 | for (const [k, v] of Object.entries(params)) form.set(k, String(v)); |
| 27 | const res = await fetch(`${ENDPOINT}${method}`, { |
| 28 | method: "POST", |
| 29 | headers: { |
| 30 | Authorization: `Bearer ${token}`, |
| 31 | "Content-Type": "application/x-www-form-urlencoded; charset=utf-8", |
| 32 | }, |
| 33 | body: form.toString(), |
| 34 | }); |
| 35 | const json = (await res.json()) as Record<string, unknown> & { ok: boolean }; |
| 36 | if (!json.ok) |
| 37 | throw new Error(`slack ${method} failed: ${JSON.stringify(json)}`); |
| 38 | return json; |
| 39 | } |
| 40 | |
| 41 | export async function postAsUser( |
| 42 | channel: string, |
no outgoing calls
no test coverage detected