How to Create Slack Alerting from BigQuery Using Python for Beginner

Risha Zahra Aditya
3 min readJan 4, 2021

On my previous sprint, I was assigned to a ticket to create slack alerting for my DWH Data Quality. As a junior associate, I thought it would be easy since I could directly use Grafana feature for the alerting. Then when I was working on it, I realized that Grafana doesn’t support slack alerting for datasource from BigQuery (please CMIIW, I’m referring to this site).

So what I did was creating a script to read BQ tables and send them to Slack channel with Airflow instead.

Create Slack App

Go to https://api.slack.com/apps and select Create New App. Then you will need to set the slack app name.

Now that we have the slack app, we need to set the permission for Incoming Webhook.

Then to get the webhook URL, we need to add the webhook to our workspace and select a slack channel as your destination.

Here’s our webhook URL that we will use when sending the data later.

Getting Data from BigQuery

First we need to set BQ credential. Make sure not to put your credential on your script.

Create query to pull data from BQ. Here, I have 4 queries to get list of table with consistency and/or validity issues and the percentage of DWH consistency and validity.

Get query result using query we have created.

To make it more pretty, I also added color and emoji based on the percentage I got from my query. You can check the cheat sheet for slack emoji here: https://www.webfx.com/tools/emoji-cheat-sheet/.

Sending Data to Slack Channel

Build slack block and/or attachment. Slack has provided a block kit builder to make it easier for us: https://app.slack.com/block-kit-builder.

Send Data to slack webhook URL

And here’s my result!

Hope this help!

--

--