Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a slack user ID from response and get the username ,

I need to get all the details from a slack channel and dump it in a simple text file. I am able to get the information using :

payload = {'token': 'XXXXXX', 'channel': 'XXXXXX' , 'count': '10'}
r = requests.get('https://slack.com/api/channels.history', params=payload)
pprint(r.json())
k = r.json()for msg in k['messages']:
    print msg['text']   # this extracts the text field from json

This gets me the response in json which looks like:

{u'has_more': False,
 u'is_limited': True,
 u'messages': [{u'text': u'This is not a test!',
                u'ts': u'1459763505.000003',
                u'type': u'message',
                u'user': u'U03FE3Z7D'},
               {u'subtype': u'bot_message',
                u'text': u'This is a test!',
                u'ts': u'1459750060.000002',
                u'type': u'message',
                u'username': u'facsimile_test'}],
 u'ok': True}

    This is not a test!
    This is a test!

This is good stuff for me.. but not worth until i get to know the username also. I am intersted in passing the userid to slack web API and getting the name and after that dumping the whole messages in a text file as

-- username1
      - messages
-- username2
      - message

1 Answers

Slack provides an API endpoint to get a users info using the user id:

payload = {'token': 'XXXXXX', 'user': 'XXXXXX'}
r = requests.get('https://slack.com/api/users.info', params=payload)

And, from here you can get the name field from the response which is the username in-fact.

If you don't want to get this information for individual users, you could get the users.list as well. And then create a mapping between the userid and username so you could access it wheneven you want to.

like image 159
AKS Avatar answered Sep 13 '25 06:09

AKS