| 5 | const SLACK_TOKEN = process.env.SLACK_TOKEN |
| 6 | |
| 7 | async function toChannel(message, channel) { |
| 8 | // eslint-disable-next-line |
| 9 | console.log(`sending to channel: ${channel} message: ${message}`) |
| 10 | try { |
| 11 | const res = await fetch('https://slack.com/api/chat.postMessage', { |
| 12 | method: 'POST', |
| 13 | headers: { |
| 14 | 'Content-Type': 'application/json; charset=utf-8', |
| 15 | Authorization: `Bearer ${SLACK_TOKEN}`, |
| 16 | }, |
| 17 | body: JSON.stringify({ channel: `#${channel}`, text: message }), |
| 18 | }) |
| 19 | const data = await res.json() |
| 20 | if (!data.ok) throw new Error(data.error) |
| 21 | } catch (error) { |
| 22 | // eslint-disable-next-line |
| 23 | console.log(`Error posting to Slack:${error}`) |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | module.exports = toChannel |