Let AI write emails and messages for you šŸ”„

Send slack messages from the server for important events

Gourav Goyal

Gourav Goyal

Apr 11, 2023

You can send messages on a Slack channel whenever something important happens on the server. For example, new user payment received, some important API threw an error, user submitted some feedback, user was not able to log-in, payment issue, deployment errors, etc so that you could quickly act upon it. Itā€™s also a good way to keep your team notified about it if your team already uses Slack. You can also have different Slack channels to get notified about critical errors, user feedback, etc.

Hereā€™s how it looks:

Create a free Slack Bot that would send messages

They call it Slack App.

  • Go to https://api.slack.com/apps?new_app=1 and click ā€œCreate New Appā€
  • Select ā€œFrom Scratchā€ option ā†’ give App name ā†’ select your workspace
  • Go to OAuth & Permissions
  • Go to Scopes ā†’ enter chat:write

Get Bot Token

Click ā€œInstall to Workspaceā€

  • Copy the Bot token

Get Channel ID to post to

Go to Channel info and copy ā€œChannel IDā€:

Add Bot to that Channel

Go to Channel ā€œIntegrationā€ tab and add that Bot as app.

Send message to Slack channel as Bot

Hereā€™s the Typescript snippet for it:

export async function sendSlackMessage(msg: string) {
  const response = await fetch("https://slack.com/api/chat.postMessage", {
    method: "POST",
    headers: {
      "Content-Type": "application/json; charset=utf-8",
      Authorization: `Bearer ${SLACK_BOT_TOKEN}`,
    },
    body: JSON.stringify({
      channel: CHANNEL_ID,
      text: msg,
    }),
  });

  return response;
}
That's all, folks!

Gourav Goyal

Gourav Goyal