Home > front end >  How to iterate through a slack api response and save as local variable?
How to iterate through a slack api response and save as local variable?

Time:08-04

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://a.slack-edge.com/df10d/img/avatars/ava_0014-24.png', 'image_32': 'https://secure.gravatar.com/avatar/b8c5320087d1218227d9c8f54866a381.jpg?s=32&d=https://a.slack-edge.com/df10d/img/avatars/ava_0014-32.png', 'image_48': 'https://secure.gravatar.com/avatar/b8c5320087d1218227d9c8f54866a381.jpg?s=48&d=https://a.slack-edge.com/df10d/img/avatars/ava_0014-48.png', 'image_72': 'https://secure.gravatar.com/avatar/b8c5320087d1218227d9c8f54866a381.jpg?s=72&d=https://a.slack-edge.com/df10d/img/avatars/ava_0014-72.png', 'image_192': 'https://secure.gravatar.com/avatar/b8c5320087d1218227d9c8f54866a381.jpg?s=192&d=https://a.slack-edge.com/df10d/img/avatars/ava_0014-192.png', 'image_512': 'https://secure.gravatar.com/avatar/b8c5320087d1218227d9c8f54866a381.jpg?s=512&d=https://a.slack-edge.com/df10d/img/avatars/ava_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'>

CodePudding user response:

Since you already have that output dict, extract the title with [] operator, aka __getitem__():

userinfodict['user']['profile']['title']  # => 'Business Analyst'
  • Related