Programmatically notifying events to slack channel from python
Tags: devops, productivity
Published: 2022-02-17 17:53:06

Suppose, you are in charge of developing a back-end of an E-Commerce website and maybe the the company sales team wants to be notified every time a customer places an order into a specific slack channel. Let's see how we can achieve this easily using the slack API.

For this post, we will assume our back-end is written using a python based web framework like django/flask/fastapi. But, we can do this using any framework. This is because, all we need to do is to make a http POST request and we can do that using any language we want.

#### Creating a new Slack app
1. Click the `+ Add apps` button
![Slack-Add-New-App](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/30t27jaffp4xzfw81ikw.png)

2. Click `Browse App Directory`
![Browse App Directory](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g6ts1tk6nvqyl6ah1wbi.png)

3. This will take you to an external link which will be opened in your browser.
Now, click `build`
![Slack Build](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/82o96o2brdsrjtvolj83.png)

4. On the next page click `Create an App`
![Create an App](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6nyrftha57ktnmxjd0a5.png)

5. Choose `From Scratch` 
![From Scratch](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3rfv4als5zauq0cekmro.png)

6. Lets name our app "Order-Alert-Bot" and choose our slack workspace.
![Order-Alert-Bot](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wvti55vrkt4gz5g9efdk.png)

7. Enable `Incoming Web hooks` for our app
![Incoming Web hooks](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lgwy1s289s3sjq6vw7bc.png)
![Add New](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tjcmd71rsr8ld7xx7jpn.png)

8. Specify the channel at which this bot should post.
![Specify the channel](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xil3wka352pzbfb4bo9q.png)
 
9. Now, a new webhook url would be added for this app. We just need to copy the `Webhook url`. This will be the api endpoint at which we will make our http POST request.
![copy webhook url](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xf81w9bk48hoyv5egfwr.png)

#### Invoking the Slack API.
Lets, assume we set the environment variable `SLACK_ORDER_ALERTS_BOT_WEBHOOK_URL` with the value of the webhook url we copied.
```python
def slack_notify_new_order(order_id: int, username: str, amount: float):
   
    details = f'[Details](https://admin.mysite.com/core/order/{order_id})'
    message = f'*{username}* has placed a new order of amount *{amount}*. {details}'
    payload = {'text': message}

    import requests
    import os

    response = requests.post(
        os.environ.get('SLACK_ORDER_ALERTS_BOT_WEBHOOK_URL'),
        data=str(payload)
    )
    return response
```
This is just a demo message we are building with some possible parameters. For your use case, you can interpolate whatever values you need to your slack message string.

Now, we can just call this function anywhere in our code and our bot will send a message to the specified channel. For example, if we are using django, we can just call this function inside the api view function that handles a new order creation event. DONE :D
![Slack Message](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/brabtme9749ikkthg8ey.png)
 
As expected, our order alerts bot is posting a message every time a customer places a new order.