Search code examples
slackslack-api

How to configure a Slack API OAuth Token which has read-only access to public channels only?


(Slack API n00b here)

I'm trying to use the Slack API for searching through messages in some of our public slack channels (internal to our organization).

I'm using the https://www.slack.com/api/search.messages API via the Python SDK, which means I'm doing the following:

import logging
from slack_sdk import WebClient
from pprint import pprint
    
    
def main():
    logging.basicConfig(level=logging.INFO)

    # TOKEN:
    t = 'xoxp-***'

    print("Getting client")
    c = WebClient(token=t)
    response = c.search_messages(query='in:#some_public_channel some-search-string')
    print("Response Code: {}".format(response.status_code))
    print("Found {} messages.".format(len(response.data['messages']['matches'])))
    
if __name__ == '__main__':
    main()

I can only get the code above working if I use a "User OAuth Token", which unfortunately is a token that also has access to all private messages related to that user.

I would like to have an OAuth token that can only search through public channels (read-only). In that way, it could be shared among members of the same team (for example).

Any suggestions how I could solve this?

Background information:

  1. If I try to use a "Bot User Auth Token" I get not_allowed_token_type error.
DEBUG:slack_sdk.web.slack_response: Received the following response - 
status: 200, 
headers: {...}, 
body: {'ok': False, 'error': 'not_allowed_token_type'}
  1. The tokens I'm using are generated via an "app" which I created only for this purpose:

enter image description here


Solution

  • Access to the search.messages API is only available to users, not bots. The best workaround I could suggest is this -