Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How To Access Slack API Using Python

This Python tutorial help to Access Slack API using python and flask.I ll let you know, how to work with Slack via the API and the official SlackClient Python helper library.We ll grab an API access token and access some data.

You can get more information from Slack Api docs.

The Pre-Requisite –

  • The system must have installed Python 2/3
  • Flask web micro-framework
  • Create a free Slack account with a team on which you have API access or sign up for the Slack Developer Hangout team
  • Official Python slackclient code library.
  • Slack API testing token

Access Slack API

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 sklack 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 obtain Slack API access token, Let’s inject access token into environment variable-

export SLACK_TOKEN='slack token pasted here'

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

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

Let’s get all channel list –

def list_channels():

    channels_call = slack_client.api_call("channels.list")

    if channels_call['ok']:

        return channels_call['channels']

    return None
 if name == 'main':
     channels = list_channels()
     if channels:
         print("Fetching Channels: ")
         for channel in channels:
             print(channel['name'] + " (" + channel['id'] + ")")
     else:
         print("Unable to authenticate.")

The final 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('/get_channels', methods=['GET'])
 ef list_channels():
     channels_call = slack_client.api_call("channels.list")
     if channels_call['ok']:
         return Response(channels_call['channels']), 200
     return Response('No found channels'), 200 
 if name == 'main':
    app.run(debug = True)

The post How To Access Slack API Using Python appeared first on Rest Api Example.



This post first appeared on Rest Api Example, please read the originial post: here

Share the post

How To Access Slack API Using Python

×

Subscribe to Rest Api Example

Get updates delivered right to your inbox!

Thank you for your subscription

×