How to schedule slack alerts in a Wave/Python application.

Shamildilshan
4 min readDec 29, 2022

--

This document guides anyone with an interest on using slack alerts and scheduling tasks using python. Since H2O wave is using python for their developments, you can easily integrate these concepts on your wave applications as well. The guidance you get from this blog will mostly be helpful for the users who are looking to upgrade their features in python (wave) applications.

Pre-requisites:

· Python 3.

· Requests library

· Apscheduler library

· Admin privilege to slack workspace/Permission to create new slack apps.

Here we are going to use Slack API to send notifications to a slack channel/user on a scheduled time via wave app.

  1. Create an app in slack workspace which has permission to post messages to the slack channel.

Click “Manage app” in Administration section which is in the slack workspace dropdown. It will redirect you to a new browser tab.

Fig 1: Slack workspace dropdown

Click on “Build” in top right corner. You can find this in the new browser tab which opened in the previous step. This redirects you to a new page with a form to create new slack app as shown in the next step.

Fig 2: New slack app build button

Create new app by giving an app name and selecting namespace of the organization.

Fig 3: New slack app form

Activate incoming webhooks and add the channel which need to get notifications. Here you can enable incoming webhooks to the newly created slack app. You can find this under the features section in the new slack application.

Fig 4: Section to add webhook configurations
Fig 5: Dropdown to select channel/user to send alerts

We are going to use the webhook URL which we get after adding the channel configurations in the above step, in our wave application to use in the code base to send alerts.

2. Python code to use in wave app.

By using the requests library and slack channel webhook URL we can send messages to slack. Here we can use a text message and we can integrate symbols into it. In a wave application, we can use the webhook URL as a wave secret. The sample function which you can use within your codebase and the sample output are shown below.

def send_slack_alert(q: Q) -> None:

"""Send slack alert to the channel"""

if <condition>:

slack_alert_message = ":alert: Sample alert message"

payload = '{"text":"%s"}' % slack_alert_message


response = requests.post(

os.getenv("SLACK_CHANNEL_WEBHOOK_URL"), data=payload

)

logger.debug(f"Slack alert status: {response.text}")
Fig 6: Sample output slack message

3. How to schedule the task.

This uses the Apscheduler library for scheduling tasks. There is a pattern to use in the scheduler to indicate at what time we need to run the assigned function. We can use this kind of approach within an application to send slack alerts repeatedly at a given time if anything needs to notify. When we define a cronjob we need to pass the function and its relevant arguments. Also, you should include the trigger time in the unix-cron string format.

Fig 7: unix-cron format

The values for each asterisk varies as below.

Minute: 0–59, Hour: 0–23, Day of month: 1–31, Month: 1–12, Day of the week: 0–6

Here you can see how to use this scheduling mechanism to trigger our slack alerting function at the scheduled time.

from apscheduler.schedulers.asyncio import AsyncIOScheduler

from apscheduler.triggers.cron import CronTrigger
scheduler = AsyncIOScheduler()

scheduler.start()
scheduler.add_job(

id="send_slack_alert_cron_id",

func=send_slack_alert,

args=[q],

trigger=CronTrigger.from_crontab("15 01 * * *"),

)

As a summary, this blog guides you on how to send slack alerts at a given time via the wave (python) application. Also, you can find sample code blocks that are capable to use within your codebase and fulfill your requirements.

References:

  1. Slack API Tutorial.
  2. Configure cron job schedules.

--

--