Try Serenytics for free - 14 Days
>> Back to tutorials menu

How to send KPIs to Slack in Python?

Enable your Slack workspace

The very first space is to allow Serenytics to post messages in the channels of your Slack workspace.

To do that, in Serenytics, open the configuration tab in the Admin section. Then click on the button "Add to slack":

Slack integration icon

This will popup a window (from the Slack website) asking you to allow Serenytics to get the list of your channels and post in it. You need to allow these actions and then, you will be redirected to Serenytics:

Slack Oauth dialog

Once your Slack integration is configured, you will see this message in the Admin/configuration section:

Slack integration done
Send a single KPI in a Slack message
Let's say we have created a formula value called "sales_of_the_day" on a datasource (e.g. sum_if([sales], [date] in day(0)) ). To send this KPI the Slack channel named "sales", create a new Python automation and paste the code below:
import serenytics
client = serenytics.Client()

source = client.get_data_source_by_uuid(uuid='MY_SOURCE_UUID')

kpi = source.get_value_formula("sales_of_the_day")

client.send_slack_msg('sales', 'Sales data', attachments=[
    {
        "text": f'Sales of the day: ${kpi}',
        "color": "#2b4e82"
    }
])
When you run the script (from the Execution tab), you will receive this message in Slack:
Slack integration done
If you want to learn how to customize the formatting of your messages, the best way is to use the Slack Message Builder.
Send the top 3 performers of the day

Using the Python client to access your data and a bit of Slack messages formatting, you can create some nice messages.

For example, let's say we have a datasource, with three columns: datetime, user, nb_of_points (that could be sales for a salesteam, number of tasks achieved by an operational team...).

To send the top3 performers of the day, we need to create a Python automation and to schedule it at the end of each workday. Its code needs to retrieve the top 3 performers from the datasource and to send it to Slack:

import serenytics
client = serenytics.Client()

source = client.get_data_source_by_uuid(uuid=MY_SOURCE_UUID)

# define the query to the Serenytics datasource
options = {
            'order': 'row_by_row',
            'data_processing_pipeline': [{
                'group_by': ['authenticated_user_name'],
                'order_by': [{'name': '$$count$$', 'agg_function': 'sum', 'direction': 'desc'}],
                'select': [{'name': 'authenticated_user_name'},
                           {'name': '$$count$$', 'agg_function': 'sum'}],
                'where': [{'name': 'serenytics_timestamp', 'op': 'in', 'value': 'day(0)'}],
                'limit': 3,
            }]
        }

# run the query
data = source.get_data(options=options)

# format the message from the data obtained
full_msg = f':first_place_medal: {data.rows[0][0]} with {data.rows[0][1]} points\n'
full_msg += f':second_place_medal: {data.rows[1][0]} with {data.rows[1][1]} points\n'
full_msg += f':third_place_medal: {data.rows[2][0]} with {data.rows[2][1]} points'

# send it to slack
client.send_slack_msg('sales', 'Top 3 of the day!', attachments=[
    {
        "text": full_msg,
        "color": "#2b4e82"
    }
])
You will receive this message in Slack:
Top 3 message in Slack

That's all for this tutorial. You can now schedule Slack messages with KPI in a few lines of Python!

Try Serenytics for free - 14 Days
>> Back to tutorials menu
×