Messaging apps are essential for internal collaboration and critical notifications such as reminders, alerts, and other time-sensitive updates. While integrating with the Slack and Microsoft APIs is fairly straightforward, implementing these tools as a part of a full-featured notification system that embeds with your app experience can be far more daunting.
In this article, we’ll show you how to send notifications via Slack and Microsoft Teams, using Courier to make these tools a seamless part of how your app communicates with users.
- Get access to your Courier API Key
- Install Python 3.x on your computer
- Install trycourier by running the
pip install trycourier
command
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.
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.
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.
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.
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
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.
Step 4: Send a Slack DM with Python
Add the code snippet below to your codebase (e.g. index.py
) and update the following properties:
- replace
with your Courier API key access_code
: replacexoxb-abcd
with the Bot User OAuth Token- replace
example@gmail.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
- replace
85S5NWXJVQ4GN8J21JSKV3JVCSV2
with your Notification ID from the first part of this tutorial
1
from trycourier import Courier
2
client = Courier(auth_token="
") 4
resp = client.send_message(
8
"access_token": "xoxb-abcd",
9
"email": "example@gmail.com",
12
"template": "85S5NWXJVQ4GN8J21JSKV3JVCSV2",
17
print(resp['requestId'])
Execute the Python script to see your notification popup in the user’s Slack direct message!
Check your Courier logs for errors if your user did not receive the message.
Slack Resources
- Slack Integration documentation
- Python quick start guide
- Video walkthrough: Automating Slack Messages with Courier API
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.
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.
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.
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.
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.
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.
Then, select the Channels option from the left menu and tick the Microsoft Teams option.
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.
Step 6: Sending a Simple Teams Notification with Python
Add the code snippet below to your codebase (e.g. index.py ) and update the following properties:
- replace
with your Courier API key conversation_id
: get the conversation id from threadId query parameter from the URL after opening Microsoft Teams in the browserservice_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 dot