Introduction
Many SaaS applications send notifications to Slack when important events
occur. Instead of checking dashboards throughout the day, updates can
appear directly in a Slack message. While documenting a small TaskAPI
that I recently built, I wondered whether it could be
integrated with Slack to send task updates. I decided to connect the
Task API to Slack using a webhook.
In this guide, I will show you how I set up this integration and give
step-by-step instructions on how to create the webhook in Slack,
integrate it into your Python scripts, and set the scripts to run
automatically to generate daily DM updates from the Task API. By the end of this guide, you'll have a solid foundation for integrating Slack with a
third-party API. You can apply the concepts you learned to more complex
integrations.
Getting Started
Once I connected the two apps, I built three Python scripts to scan the
Task API for different task statuses: incomplete tasks (pending or in
progress), incomplete tasks past their deadline, and completed tasks.
Each script posts a message to the Slack webhook, triggering a
notification in the Slack DM channel. After testing the scripts, I
installed them on my server and set them to query the Task API on a set
schedule, giving me a daily DM in Slack to stay updated on my tasks.
Architecture Overview
The integration workflow:

Each script queries the API and checks a different condition:
- incomplete tasks
- overdue tasks
- completed tasks
When a condition is detected, the script generates a message and sends it to Slack through the webhook.
To follow along, you will need:
- Python installed
- A Slack workspace
- An API token for the Task API
What you will build:
- A Slack app with a webhook to integrate into your code to receive and post messages to the Slack DM channel.
- Three Python scripts that send reminders about your tasks from the Task API to your Slack DM.
Create the webhook
To connect the Task API to Slack, you need to create an app with a
webhook in Slack to receive notifications. A Slack webhook is a URL that
allows external applications to post messages to Slack. For more detail
on how Slack webhooks work, see the official Slack documentation
here.
Steps:
Go to api.slack.com/apps and click
Create New App.
Select From scratch, enter an app name, and choose the workspace you
want to connect to.
From the app settings page, select Incoming Webhooks in the left
sidebar.
Toggle Activate Incoming Webhooks to on.
Click Add New Webhook to Workspace at the bottom of the page.
Select the channel or DM you want the webhook to post to and click
Allow.
Copy the webhook URL that appears–it will look like
https://hooks.slack.com/services/T00/B00/xxx.
Note: Your actual webhook will display a code instead of the ‘xxx’
shown in the above example.
Query the Task API
The Task API stores all of your personal tasks with their statuses and
due dates. To complete the steps below, you need to set up a user
account in the Task API and generate a token. You also need to have one
task in pending or in-progress status, one in complete status, and one
overdue task. This will allow you to call the API task endpoint to see
what the response looks like. For a full reference on creating an
account and setting up tasks in the Task API, see the documentation
here.
Let’s analyze a response. Make a GET request to retrieve all of the
fields associated with an example task.
curl -X GET "http://your-server-address:3000/api/tasks"
-H "Authorization: Bearer your-api-key-here"
Your API key is your JWT (Your unique token associated with your
account). The API returns all tasks associated with your token:
{
"tasks": [
{
"id": "task_001",
"title": "Update API documentation",
"status": "pending",
"due_date": "2024-03-20",
"priority": "high"
},
{
"id": "task_002",
"title": "Review pull request",
"status": "in_progress",
"due_date": "2024-03-22",
"priority": "medium"
},
{
"id": "task_003",
"title": "Fix login bug",
"status": "completed",
"due_date": "2024-03-18",
"priority": "high"
}
]
}
The scripts scan three fields from the API response to determine what to
post in the Slack DM:
| Field | Type | Description |
|---|---|---|
| title | string | The name of the task |
| status | string | The task status: pending, in_progress, or completed |
| due_date | string | The task deadline in ISO 8601 format, for example 2024-12-31 |
Connect the Task API and Slack
You now have a webhook that connects Slack and the Task API. They can
communicate, but you need scripts to query the Task API and post the
results to Slack.
First, you need to run a test script to make sure your Slack webhook
works. Replace the placeholder webhook URL in the following script with
your own and run it from your command line or server.
import requests
SLACK_WEBHOOK = "https://hooks.slack.com/services/xxx/xxx/xxx"
payload = {"text": "Webhook test successful!"}
response = requests.post(SLACK_WEBHOOK, json=payload)
if response.status_code == 200:
print("Message sent successfully")
else:
print(f"Failed to send message. Status code: {response.status_code}")
If you get "Message sent successfully" and a DM appears in Slack, your
webhook URL is configured correctly. If nothing happens, check that you
replaced the placeholder URL with your actual webhook URL
Next, you need to query the /tasks endpoint and evaluate the response
fields. Each script sends a different DM in Slack with one of the
following notifications: “Incomplete Tasks,” “Overdue Tasks,” and “Tasks
Completed.”
Script 1: Incomplete task reminders
To generate the “Incomplete Tasks” notification, run:
import requests
TOKEN = "your-api-key-here"
SLACK_WEBHOOK = "https://hooks.slack.com/services/xxx/xxx/xxx"
API_URL = "http://your-server-address:3000/api/tasks"
response = requests.get(
f"{API_URL}?status=pending,in_progress",
headers={"Authorization": f"Bearer {TOKEN}"}
)
tasks = response.json()["tasks"]
incomplete = [
f"🔵 {task['title']} -- {task['status']}"
for task in tasks
]
if incomplete:
message = "📋 *Incomplete Tasks:*\n" + "\n".join(incomplete)
requests.post(SLACK_WEBHOOK, json={"text": message})
The script queries the /tasks endpoint and scans the response for tasks
with a status of pending or in_progress. It then posts an Incomplete
Tasks notification with a list of task titles to the Slack webhook.
Script 2: Overdue task alerts
To generate the “Overdue Tasks” notification, run:
import requests
from datetime import date
TOKEN = "your-api-key-here"
SLACK_WEBHOOK = "https://hooks.slack.com/services/xxx/xxx/xxx"
API_URL = "http://your-server-address:3000/api/tasks"
response = requests.get(
f"{API_URL}?status=pending",
headers={"Authorization": f"Bearer {TOKEN}"}
)
tasks = response.json()["tasks"]
today = date.today()
overdue = [
f"⚠️ {task['title']} -- due {task['due_date']}"
for task in tasks
if task["due_date"] and date.fromisoformat(task["due_date"]) < today
]
if overdue:
message = "🔴 *Overdue Tasks:*\n" + "\n".join(overdue)
requests.post(SLACK_WEBHOOK, json={"text": message})
This script looks for tasks that have a pending status and a due_date in
the past. It then posts an Overdue Tasks notification to the webhook.
This notification contains a list of task titles and their due dates.
Script 3: Completed task notifications
To generate the “Completed Tasks” notification, run:
import requests
TOKEN = "your-api-key-here"
SLACK_WEBHOOK = "https://hooks.slack.com/services/xxx/xxx/xxx"
API_URL = "http://your-server-address:3000/api/tasks"
response = requests.get(
f"{API_URL}?status=completed",
headers={"Authorization": f"Bearer {TOKEN}"}
)
tasks = response.json()["tasks"]
completed = [
f"✅ {task['title']}"
for task in tasks
]
if completed:
message = "🟢 *Completed Tasks:*\n" + "\n".join(completed)
requests.post(SLACK_WEBHOOK, json={"text": message})
This script scans for any task with a status of completed and sends a
Completed Tasks message to the webhook.
Running the scripts on a schedule
You can configure these scripts to run automatically so you don't miss a
task update or deadline. To schedule a script on a Linux server, open
your crontab file with crontab -e and add a line specifying when and how
often to run it. For example, to run the overdue tasks script every day
at 8am:
0 8 \* \* \* python3 /path/**to**/overdue_tasks.py
Conclusion
You have built three scripts that query the Task API and send Slack
DMs using a webhook. Scheduling these scripts to run on a cron job will
ensure that you receive reminder messages and stay up to date with your
tasks. You now understand how to use webhooks to integrate third-party
apps into Slack. You can build on what you’ve learned in this guide to:
Extend the existing scripts to filter tasks by priority and route
notifications to different Slack channels.Configure the Task API to send a DM the moment a task is updated,
removing the need for a scheduled cron job.
Top comments (0)