Search code examples
pythonslack

unable to print username for person texting in slack bot


how to get username like "Jonh" instead of WLLACM2HK??

code

starterbot_id = web_client.api_call("auth.test")["user_id"]
print("starterbot id------->"+starterbot_id))

current output for above code

starterbot id------->WLLACM2HK  

I want code to print something like below output

starterbot id------->Jonh

data in web_client.api_call("auth.test")

{
    'ok': True,
    'url': 'https: //bby-sandbox-general.slack.com/',
    'team': 'SandboxGeneralWorkspace',
    'user': 'ipt-autobot',
    'team_id': 'TFHGD33RV',
    'user_id': 'WLLACM2HK',
    'bot_id': 'BLDVB7EKB',
    'enterprise_id': 'EFGBDRLNP',
    'warning': 'superfluous_charset',
    'response_metadata': {
        'warnings': [
            'superfluous_charset'
        ]
    }
}

I feel im not calling the right api , because i dont see the username in above json , the user attribute is actually the name of the slack bot , im looking to print the user who is texting in the slack bot

on trying print(web_client.users_info(user=user))

{
    'ok': True,
    'user': {
        'id': 'WGUHC67D1',
        'team_id': 'TJK2GBSC9',
        'name': 'a6002043',
        'deleted': False,
        'color': 'c386df',
        'real_name': 'RaviKanth',
        'tz': 'America/Chicago',
        'tz_label': 'CentralStandardTime',
        'tz_offset': -21600,
        'profile': {
            'title': 'CW-Other',
            'phone': '+15614128585',
            'skype': '',
            'real_name': 'RaviKanth',
            'real_name_normalized': 'RaviKanth',
            'display_name': 'RaviKanth',
            'display_name_normalized': 'RaviKanth',
            'status_text': '',
            'status_emoji': '',
            'status_expiration': 0,
            'avatar_hash': '8f97c61708c1',
            'image_original': 'https: //avatars.slack-edge.com/2019-11-27/852421659589_8f97c61708c1553628c8_original.jpg',
            'is_custom_image': True,

how grab real_name value and print it?


Solution

  • I am just working on it, If you have user_id then you can use user.info method to get specific user information.

    Here is an example: Image

    Link: https://api.slack.com/methods/users.info