* Sends a message to a Slack channel using chat.postMessage
( accessToken: string, channel: string, text: string, threadTs?: string | null, blocks?: unknown[] | null )
| 9 | * Sends a message to a Slack channel using chat.postMessage |
| 10 | */ |
| 11 | async function postSlackMessage( |
| 12 | accessToken: string, |
| 13 | channel: string, |
| 14 | text: string, |
| 15 | threadTs?: string | null, |
| 16 | blocks?: unknown[] | null |
| 17 | ): Promise<{ ok: boolean; ts?: string; channel?: string; message?: any; error?: string }> { |
| 18 | const response = await fetch('https://slack.com/api/chat.postMessage', { |
| 19 | method: 'POST', |
| 20 | headers: { |
| 21 | 'Content-Type': 'application/json', |
| 22 | Authorization: `Bearer ${accessToken}`, |
| 23 | }, |
| 24 | body: JSON.stringify({ |
| 25 | channel, |
| 26 | text, |
| 27 | ...(threadTs && { thread_ts: threadTs }), |
| 28 | ...(blocks && blocks.length > 0 && { blocks }), |
| 29 | }), |
| 30 | }) |
| 31 | |
| 32 | return response.json() |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Creates a default message object when the API doesn't return one |