Send messages from Python to Slack

As an Amazon Associate I earn from qualifying purchases.

4 Min Read

If you have followed this series so far, you should have a fully functional trading bot that can trade the financial and crypto markets. Today, I will be showing you how to send messages from Python to slack from your trading bot. This can be useful for things such as getting notified when a trade is opened; when a trade is closed; If your trading bot crashes or goes offline etc.

Setting up the Slack Bot

To begin, you will need to download slack. Once you have signed up, click the ‘Create a Workspace’ button:

Go through the various steps filling out any required information. You should now have your workspace and should look something like this:

Create a new channel and name it ‘algotrader’. You can does this by clicking on ‘Channels’. Once this has done, go to the API section on slack (https://api.slack.com/apps) and start creating your bot. Once there, click ‘Create an App’:

Create a name for your app, and add it to your workspace:

Click on ‘Install to Workspace’ and then click the ‘Permission Scope’ Link:

Find the ‘Scopes’ section and add the chat:write and channels:join permission. This will allow you python application to join and post into a channel:

After adding this, scroll to the top of the page and click ‘Install to Workspace’

You will now see a token on your screen. Copy this token and save it. This is the token python will need to communicate with your slack workspace:

Go back to your slack workspace, select your channel and click on the exclamation mark button:

Click ‘more’ -> ‘Add apps’ then find your app and add it to the channel:

Sending a message from Python to Slack

Start by installing the slack python library here or by using the following command:

pip install slackclient

Import the WebClient from slack. (Do this in trader.py) :

import MetaTrader5 as mt5
from datetime import datetime, timedelta
import pandas as pd
import pytz
import schedule
import talib as ta
import time
import strategy
import constants
import sys
from slack import WebClient

Directly under the imports, initialize the client with the argument token and your token that you saved from earlier and assign in to a variable named client:

client = WebClient(token='YOUR TOKEN')

Now, create a method called send_notification with the argument message:

def send_notification(message):

Finally, call chat_postMessage from client adding channel and text as arguments (Use the channel name that you created earlier):

    client.chat_postMessage(channel='#algotrader', text=message)

Testing the code

To send messages from Python to Slack call the send_notification method with a test message. This should push the message to my channel in slack.

> send_notification("Test message!")

As you can see, my message is posted successfully to slack:

If you’re interested in learning Python I highly recommend this book. In the first half of the book, you”ll learn basic programming concepts, such as variables, lists, classes, and loops, and practice writing clean code with exercises for each topic. In the second half, you”ll put your new knowledge into practice with three substantial projects: a Space Invaders-inspired arcade game, a set of data visualizations with Python”s handy libraries, and a simple web app you can deploy online. Get it here.

That’s all for now! Check back on Wednesday to see how you can send trading alerts to your phone via email! As always, if you have any questions or comments please feel free to post them below. Additionally, if you run into any issues please let me know.

1 thought on “Send messages from Python to Slack”

Leave a Comment

Your email address will not be published. Required fields are marked *

Subscribe to my newsletter to keep up to date with my latest posts

Holler Box