Search code examples
python-2.7httprequestslack-apislack

How to make use of Slack Web API?


I want to use the provided slack web API https://api.slack.com/web#basics to get some messages out of a channel. I looked at https://api.slack.com/methods/channels.history & used the Request API to call the service.

payload = {'token': 'XXXXXXXXXXXX', 'channel': '#scanbot' , 'count' : '10'}
r = requests.get('https://slack.com/api/channels.history', params=payload)
print r.status_code
print r.text

But i am getting the error:

200
{"ok":false,"error":"channel_not_found"}

I am pretty sure the channel exists and I am providing correct API key. Can someone point me at the correct directions please?


Solution

  • You need to pass the channel ID as the argument to the channels.history endpoint.

    The channel IDs may be fetched by checking the channels.list endpoint.

    See for example the source of the Slacker package.

    The JSON-formatted response may be parsed with:

    import json
    data = json.loads(r.text)
    print data