Platform
Docs
Solutions
ContactLog In

Start Routing Notifications Today!

Courier is a notification service that centralizes all of your templates and messaging channels in one place which increases visibility and reduces engineering time.

Sign-up

automate-slack-msteams-nodejs-header
TUTORIAL

Automate Slack and MS Teams Notifications using Node.js

Shreya Gupta

March 22, 2023

There are ways to integrate cross-channel notifications via individual APIs, but we’re here to provide you with a quick guide to send across your preferred channels in one single API call. This guide will focus on sending and automating messages via Slack, MS Teams, and any other channel of your choice from your Node.js applications.

You can also automate Slack and Microsoft Teams notifications using Python.

This blog is a comprehensive setup guide. If you would like to get started quickly, checkout the code in this GitHub repository.

Before Starting

  • Get access to your Courier API Key
  • Install trycourier by running the npm install @trycourier/courier command

Send with Slack

Step 1: Add the Slack integration to Courier

First, head to the Slack channel in Courier https://app.courier.com/channels/slack and click "Install Provider". If you only want to send with Teams, you can skip to the “Send with Microsoft Teams” section.

channel slacks

Step 2: Create a Slack App

Navigate to the Slack Apps page and log into Slack. Click the Create an App button and provide your App Name as depicted below.

slack-app

Step 3: Add OAuth Permission and Scopes

Define the permissions that your Slack App is authorized to do.

To do so, navigate to the OAuth & Permissions page in the sidebar menu and select the following options from the Scopes section:

  • chat:write
  • im:write
  • users:read
  • users:read.email

slack-app-scopes

Afterward, click the Install App to Workspace or Reinstall App button at the top of the page. This will generate an output -"Bot User OAuth Token" as depicted below, which will be needed later.

slack-app-oath-token

Step 4: Send a Slack DM with Node.js

Add the code snippet below to your codebase (e.g. index.js ) and update the following properties:

  • replace auth-token with your Courier API key
  • replace slack_access_token with the Bot User OAuth Token
  • replace example@email.com with your user's email address (this is the email associated with the recipient's Slack Account)
  • Recommended: use your own email for testing
1
const { CourierClient } = require("@trycourier/courier");
2
3
const courier = CourierClient({ authorizationToken: "auth_token" });
4
5
async function send_message() {
6
const { requestId } = await courier.send({
7
message: {
8
to: {
9
slack: {
10
access_token: "slack_access_token",
11
email: "example@email.com"
12
}
13
},
14
content: {
15
title: "Important Survey Reminder",
16
body: "This is a reminder to fill out your survey by the end of this week.",
17
},
18
routing: {
19
method: "single",
20
channels: ["direct_message"]
21
}
22
},
23
});
24
console.log(requestId)
25
}
26
27
send_message()

Run the Node.js program to see your notification popup in the user's Slack direct message!

Slack message

Check your Courier logs for errors if your user did not receive the message.

Slack Resources

Send with Microsoft Teams

We can now try sending the same message via Teams. If you only want to send with Slack, you can skip to the “Routing to multiple channels” section.

Step 1: Sign up for a Microsoft 365 Developer Account

If you do not have a Microsoft 365 developer account, follow the instructions from this guideline to create an account. If you already have an account, you can skip this step.

Step 2: Create a Teams App

Create a new App in Teams. You will need to install the Developer Portal from the Team Apps.

After installing the Developer Portal, navigate to the Apps tab and click the New App button. Then, you will get prompted to enter the application name.

ms teams add app

After clicking the Add button, you will be redirected to a new window where you can see the App ID. Make sure to copy the App ID for later use.

MS Teams Bot Basic Info

Then, click App features from the left menu and click on the tile named Bot. It will open a new window for selecting a bot and bot scopes.

Identify Bot

If you do not have a bot created already, follow these steps to create a new one. Also, make sure to save the password generated during the process. Keep the Messaging endpoint blank for the moment.

MS Teams App Configuration

Step 3: Deploy the bot

Now you need to deploy the App to create the messaging endpoint. For that, open the Microsoft Teams Bot Starter Repo and click the Deploy to Netlify button. There, you will have to connect to your GitHub account and enter your App ID (Bot ID), App Password, Courier Auth Token, and a name for the repo.

Deploy app on netlify

Once the site is deployed, copy your site URL since you need it to finish installing the bot.

Step 4: Install the bot

Now, go back to the Tools > Bot Management tab in Developer Portal and select the created bot to finalize the installation process. There, select the Configure option and copy the site URL to the Bot endpoint address field.

Bot Endpoint Developer Portal

Then, select the Channels option from the left menu and tick the Microsoft Teams option.

Channels Developer Portal

Step 5: Add the Teams integration to Courier

Now, you need to create a Teams integration in Courier. For that, navigate to the Channels tab and select Microsoft Teams from the options. Then, it will show a window like the one below. Enter the App ID, App Password and click the Install Provider button.

Channel TeamsGet link to team

Step 6: Sending a Simple Teams Notification with Node.js

Add the code snippet below to your codebase (e.g. index.js ) and update the following properties:

  • replace auth-token with your Courier API key
  • conversation_id: get the conversation id from threadId query parameter from the URL after opening Microsoft Teams in the browser
  • service_url: the service URL associated with that Microsoft Teams tenant (if you are located in the Americas Region, the service url is https://smba.trafficmanager.net/amer)
  • tenant_id: go to https://teams.microsoft.com/?tenantId and copying the value from the redirected URL tenantId query parameter, or click the three dots next to your Team and click Get link to team to find a link with the tenantId parameter
1
const { CourierClient } = require("@trycourier/courier");
2
3
const courier = CourierClient({ authorizationToken: "auth_token" });
4
5
async function send_message() {
6
const { requestId } = await courier.send({
7
message: {
8
to: {
9
ms_teams: {
10
conversation_id: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
11
tenant_id: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
12
service_url: "https://smba.trafficmanager.net/amer"
13
}
14
},
15
content: {
16
title: "Important Survey Reminder",
17
body: "This is a reminder to fill out your survey by the end of this week.",
18
},
19
routing: {
20
method: "single",
21
channels: ["direct_message"]
22
}
23
},
24
});
25
console.log(requestId)
26
}
27
28
send_message()

Finally, you can run the Node.js program, and you will see a notification in your Team channel.

Microsoft Teams Resources

Routing to multiple channels

Courier's multi-channel functionality allows you to send notifications across multiple channels, including email, SMS, push notifications, voice, chatbots, and social media platforms, using a single API.

For example, if you want to send an urgent alert to your team, you would use Courier to send this alert as both Slack and Teams notifications.

1
const { CourierClient } = require("@trycourier/courier");
2
3
const courier = CourierClient({ authorizationToken: "auth_token" });
4
5
async function send_message() {
6
const { requestId } = await courier.send({
7
message: {
8
to: {
9
slack: {
10
access_token: "access_token",
11
email: "example@email.com"
12
},
13
ms_teams: {
14
conversation_id: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
15
tenant_id: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
16
service_url: "https://smba.trafficmanager.net/amer"
17
}
18
},
19
content: {
20
title: "Important Survey Reminder",
21
body: "This is a reminder to fill out your survey by the end of this week.",
22
},
23
routing: {
24
method: "all",
25
channels: ["direct_message"]
26
}
27
},
28
});
29
console.log(requestId)
30
}
31
32
send_message()

As you can see below, Courier supports a large selection of Email, SMS, chat, and push notification providers.

Channels

We can also send this as an email by adding the email property within the to object and adding the ”email” channel to the channels array in the routing object.

1
const { CourierClient } = require("@trycourier/courier");
2
3
const courier = CourierClient({ authorizationToken: "auth_token" });
4
5
async function send_message() {
6
const { requestId } = await courier.send({
7
message: {
8
to: {
9
slack: {
10
access_token: "access_token",
11
email: "example@email.com"
12
},
13
ms_teams: {
14
conversation_id: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
15
tenant_id: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
16
service_url: "https://smba.trafficmanager.net/amer"
17
},
18
email: "example@email.com"
19
},
20
// template: "template_id",
21
content: {
22
title: "Important Survey Reminder",
23
body: "This is a reminder to fill out your survey by the end of this week.",
24
},
25
routing: {
26
method: "all",
27
channels: ["direct_message", "email"]
28
}
29
},
30
});
31
console.log(requestId)
32
}
33
34
send_message()

If you only want to send to the best of these three channels, you can simply change the method type from all to single in the routing object. This will send a Slack message. If that fails, it will send a Teams message. If that fails as well, it will send an email.

Automating Slack and Teams Notifications

If you prefer a visual workflow, you can create an automation template within Courier.

Create Notification Templates

Head over to the Notification Designer and create a new notification. Once you have created the notification, you will be able to select your channels. Here you can add Slack, Teams, and/or any other channels you would like to send with.

Click on any of the channels to customize your message. Once you have created your message, you can use the drag-and-drop "Library" on the left side to recreate the message for the remaining channels.

library

Click on the settings (gear) icon next to the template name (top right) to access the Notification ID, which you will need later to send this message.

Building the Automations

The notification template will automatically send to selected channels (based on your template’s channel settings).

Create a new Automation, select a schedule Trigger, and add a Send step for your notification. We will update the Type to "Date" and Date to the specific date/time this message should be sent.

In the Send step, select JSON and use the Editor to add the recipient information. In the editor, click "+ Add field" and add slack as an Object. Then click "+Add nested field" and add access_token and email with the appropriate values from the code above.

For Teams, click "+ Add field" again and add ms_teams as an Object. Then click "+Add nested field" and add conversation_id, tenant_id, and service_id, with the appropriate values from the code above.

This automation will now have the required user profile data to send to both Slack and MS Teams. If you also want to send an email or SMS, edit the JSON object to add a field for "email" and "phone_number" with the appropriate values.

automation slack and teams

Conclusion

Automating notifications in team communication platforms such as Slack and Teams can significantly improve team productivity and communication. However, building automated notifications from scratch can be complex and time-consuming. That's where Courier comes in with its easy-to-use Node.js SDK that allows developers to send notifications to multiple channels with just a few lines of code.

By following the steps outlined in this article, you can set up notification automation with Courier and Node.js for both Slack and Teams. Additionally, you can leverage Courier's multi-channel functionality to integrate other applications and channels into your notification system.

FAQs

1. What is Courier?

Courier connects communication APIs, data, and development tools that your team already uses to deliver a best-in-class notification system that will trigger messages from your app at just the right time, using just the right channel. It provides powerful API primitives and reusable UI components for building, plus no-code tools for designing templates and communication sequences.

2. What is Slack?

Slack is a cloud-based messaging platform that enables teams to communicate and collaborate in real-time. It is known for its user-friendly interface and extensive third-party integrations, making it a highly customizable platform for teams of all sizes. Slack allows users to easily organize their conversations into channels, share files, and collaborate with other team members. It also offers multiple app integrations, including project management and automation tools.

3. What is Microsoft Teams?

Microsoft Teams is another popular communication and collaboration platform that is part of the Microsoft Office 365 suite. It enables teams to chat, share files, and collaborate in real time, both synchronously and asynchronously. With features such as video conferencing, screen sharing, and group chats, Microsoft Teams is ideal for businesses and organizations that require a comprehensive and centralized collaboration platform.

4. Missing Provider Support error message

If your message did not go through, check your Courier logs. For a "MISSING_PROVIDER_SUPPORT" error message, you will need to double-check if the Slack and/or Teams channels were installed properly in Courier. Try removing the channel(s) (go to the channel and click "Remove Channel" at the bottom of the page) that is causing the error and then adding it again.

Start Routing Notifications Today!

Courier is a notification service that centralizes all of your templates and messaging channels in one place which increases visibility and reduces engineering time.

Sign-up

More from Tutorial

How to set up automatic push notifications based on Segment events thumbnail
TUTORIAL

How to Set Up Automatic Push Notifications Based on Segment Events

Push notifications have carved their own niche as a powerful tool for continuous user engagement. Regardless of whether an app is actively in use, they deliver your messages straight to your user's device. Two key players that can combine to enhance your push notification strategy are Segment and Courier. In this tutorial, we show you how to set up Courier to listen to your Segment events and then send push notifications to an Android device based on data from these events.

Sarah Barber

Sarah Barber

November 17, 2023

image14
TUTORIAL

How to Send Firebase Notifications to iOS Devices Using Courier

This tutorial explains how to send push notifications to iOS devices from your iOS application code using Firebase FCM and Courier’s iOS SDK.

Martina Caccamo

Martina Caccamo

November 01, 2023

Build your first notification in minutes

Send up to 10,000 notifications every month, for free.

Get started for free

Email & push notification

Build your first notification in minutes

Send up to 10,000 notifications every month, for free.

Get started for free

Email & push notification

Platform

Users

Content

Channels

Sending

Workflows

Preferences

Inbox

Workspaces

Observability

API Status

Changelog

© 2024 Courier. All rights reserved.