Skip to main content
How To Consume Slack API Using Python

How To Consume Slack API Using Python

in this tutorial, I ll let you know How to access Slack API using python and flask. We’ll go through the free registration of slack user and get an access token. Slack APIs allow you to integrate complex services with Slack.

The Slack Web API is an interface for querying information from and enacting change in a Slack workspace. You can get more information from Slack API docs.

The Pre-Requisite –

You can also checkout other python tutorials:

How To Get Access Slack API

Let’s signup on slack api landing page. You can use a google account to signup and access a token.

Once you’ve signed in you can scroll down on the web API page where you’ll see a button to generate test tokens:

slack-generate-test-token

Integrate Slack API With Python 3

Let’s create a project that’ll contain all project-related files. We’ll also create a new virtualenv to isolate our application dependencies from other Python projects:

$ mkdir slackapi
$ virtualenv venv

Now, activate virtual env –

source venv/bin/activate

Install Slack Client Using pip

This library requires Python 3.6 and above. If you require Python 2, please use SlackClient – v1.x.

Check python version –

python --version
-- or --
python3 --version

Let’s install the slack client using pip –

//python 2
pip install slackclient==1.0.0

//python 3.6+
pip3 install slackclient

We need to obtain a Slack access token for our team and account.

I am assuming you have obtained a Slack API access token, Let’s inject the access token into the environment variable –

export SLACK_TOKEN='slack token pasted here'

How To Send message to Particular User Using Slack API

Here, I’ll send a message using Slack API to a user. Normally, we are sending messages to the channel, but here I am sending a message to a user instead of a channel –

Let’s create test.py file and imported slack client library and added access token into the client –

import os
from slackclient import SlackClient
SLACK_TOKEN = os.environ.get('SLACK_TOKEN')
slack_client = SlackClient(SLACK_TOKEN)

Let’s create method to send message –

import os
from slackclient import SlackClient

slack_token = os.environ["SLACK_API_TOKEN"]
sc = SlackClient(slack_token)

sc.api_call(
  "chat.postEphemeral",
  channel="website",
  text="Hello from Python! :tada:",
  user="Mikey"
)

def send_user_message():
    message_resp = slack_client.api_call(
				  "chat.postEphemeral",
				  channel="userid",
				  text="Hello from Pythonpip",
				  user="Adam"
				)
    
    return message_resp

	
if __name__ == '__main__':
    mesg = send_user_message()
    
        print(mesg)
    else:
        print("Unable to send message.")

The Full Source Code –

import os
from flask import Flask, request, Response
from slackclient import SlackClient
SLACK_TOKEN = os.environ.get('SLACK_TOKEN')
slack_client = SlackClient(SLACK_TOKEN)

app = Flask(__name__)

@app.route('/send_message', methods=['POST'])
def send_user_message():
    message_resp = slack_client.api_call(
				  "chat.postEphemeral",
				  channel="userid",
				  text="Hello from Pythonpip",
				  user="Adam"
				)
    
    return Response(message_resp), 200

if __name__ == '__main__':
   app.run(debug = True)

The post message response would be -   
   
{
    "ok": true,
    "message_ts": "1502210682.580145"
}

The error response, if anything wrong :

{
    "ok": false,
    "error": "user_not_in_channel"
}

Leave a Reply

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