Skip to main content

#Loading0 personne en discute

I'm trying to make a slack app and am struggling to save part of an api response as a local variable.

This is my code:

```

import os

import re

import time

from dotenv import load_dotenv

from slack_bolt import App

from slack_bolt.adapter.socket_mode import SocketModeHandler

 

load_dotenv()

#loading env variables

SLACK_APP_TOKEN = os.environ["SLACK_APP_TOKEN"]

SLACK_BOT_TOKEN = os.environ["SLACK_BOT_TOKEN"]

 

app = App(token=SLACK_BOT_TOKEN, name="SlackApp")

 

@app.message(re.compile("^."))#event listener listens to messages matching a wildcard regular expression

def reply(message, say):

print("Message :"+message["text"])

print("Recieved @:"+time.ctime())

 

#job title check

userid = message["user"]

userinfodict = app.client.users_info(

user=userid

)

print(userinfodict)

```

 

This returns an output of:

 

{'ok': True, 'user': {'id': 'U03PMCY1BUG', 'team_id': 'T03NH2KCQA3', 'name': 'John Doe', 'deleted': False, 'color': '9f69e7', 'real_name': 'John Doe', 'tz': 'America/New_York', 'tz_label': 'Eastern Daylight Time', 'tz_offset': -14400, 'profile': {'title': 'Business Analyst', 'phone': '', 'skype': '', 'real_name': 'John Doe', 'real_name_normalized': 'realfakename', 'display_name': '', 'display_name_normalized': '', 'fields': None, 'status_text': '', 'status_emoji': '', 'status_emoji_display_info': [], 'status_expiration': 0, 'avatar_hash': 'gb8c5320087d', 'first_name': 'John', 'last_name': '', 'image_24': 'https://secure.gravatar.com/avatar/b8c5320087d1218227d9c8f54866a381.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0014-24.png', 'image_32': 'https://secure.gravatar.com/avatar/b8c5320087d1218227d9c8f54866a381.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0014-32.png', 'image_48': 'https://secure.gravatar.com/avatar/b8c5320087d1218227d9c8f54866a381.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0014-48.png', 'image_72': 'https://secure.gravatar.com/avatar/b8c5320087d1218227d9c8f54866a381.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0014-72.png', 'image_192': 'https://secure.gravatar.com/avatar/b8c5320087d1218227d9c8f54866a381.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0014-192.png', 'image_512': 'https://secure.gravatar.com/avatar/b8c5320087d1218227d9c8f54866a381.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0014-512.png', 'status_text_canonical': '', 'team': 'T03NH2KCQA3'}, 'is_admin': True, 'is_owner': True, 'is_primary_owner': True, 'is_restricted': False, 'is_ultra_restricted': False, 'is_bot': False, 'is_app_user': False, 'updated': 1659555324, 'is_email_confirmed': True, 'who_can_share_contact_card': 'EVERYONE'}}

```

 

How do I save a part of this output as a local variable? In particular the job title:

 

```

...'profile': {'title': 'Business Analyst',...

 

```

 

It is not a dictionary, it is of type: <class 'slack_sdk.web.slack_response.SlackResponse'>

How do I iterate though this response and save as a local variable?

1 réponse
  1. 29 juil. 2023, 18:06

    Hi @Stephen Cronin​, I don't know python but since Slack is returning you a JSON format it should go like this:

    response.user.profile.title - to get the title value

    response.user.profile - to get the whole section started as 'profile' and so on.

0/9000