Skip to main content

#Profile0 discussing

Hi all,

I created slack bot using python sdk and my current situation is i was able to create multple buttons in single response and the buttons are name after items inside a python list. So I looped through the list and generated buttons. Now the problem is that I want a single @app.action listenr function which can handle all the these button clicks. How can i use create such function in slack bolt python

 

Given below is the code which is used to generate the buttons

@app.view("login_modal")

def handle_view_submission_events(ack, body, say, client):

  ack()

 

  username = body["view"]["state"]["values"]["Dn/DV"]["username_input"]["value"]

  password = body["view"]["state"]["values"]["FTQxY"]["password_input"]["value"]

  channel_id = body['user']['id']

 

  packet = {

    "userName": username,

    "password": password

  }

 

  url = f"{host}/api/login"

  response = requests.post(url=url, headers=headers, json=packet).json()

 

  if response != 'Please check your credentials':

    #print("body", body)

    cloud_types = response['items']['cloudTypes']

    buttons = [

      {

        "type": "button",

        "text": {"type": "plain_text", "text": cloud_type},

        "value": cloud_type,

        "action_id": f"cloud_type_selection_{i}"

      }

      for i, cloud_type in enumerate(cloud_types)

    ]

 

    say(

      blocks=[

        {

          "type": "section",

          "text": {"type": "mrkdwn", "text": f"Welcome to SkynetCloudKIT \n Dear user you are logged in as @{response['items']['userId']}"}

        },

        {

          "type": "section",

          "text": {"type": "mrkdwn", "text": f"Dear {response['items']['userId']} the registered clouds are ....! \n"}

        },

        {

          "type": "actions",

          "elements": buttons

        }

      ],

      channel=channel_id

    )

 

    # Store cloud types for later use

    #client.users_profile_set(

     # user=body["user"]["username"],

      #profile={

        #"cloud_types": cloud_types

      # }

    # )

  else:

    say(

      blocks=[

        {

          "type": "section",

          "text": {"type": "mrkdwn", "text": f"Login Failed \n Please check your credentials and try again! "}

        }

      ],

      channel=channel_id

    )

 

now i need a @app.action listner fuction for handling all these button clicks

Given below is the code I used for creating this type of function

@app.action("cloud_type_selection_*") # Handles all button clicks

def cloud_selection(body, ack, say, client):

  ack()

  print("here")

  cloud_type = body["actions"][0]["value"]

 

  # Retrieve stored cloud types

  user_profile = client.users_profile_get(user=body["user"]["id"])

  stored_cloud_types = user_profile["profile"]["cloud_types"]

 

  # Perform actions based on the selected cloud type (e.g., fetch data, display information)

  if cloud_type in stored_cloud_types:

    say(f"You selected: {cloud_type}")

  else:

    say(f"Invalid cloud type: {cloud_type}")

 

but im getting error saying

Unhandled request ({'type': 'block_actions', 'block_id': '0vR', 'action_id': 'cloud_type_selection_0'})

---

[Suggestion] You can handle this type of event with the following listener function:

 

@app.action("cloud_type_selection_0")

def handle_some_action(ack, body, logger):

  ack()

  logger.info(body)

4 answers
  1. Sep 20, 2024, 8:12 PM

    If you want to use the same action handler for all the buttons, then set the "value" parameter different for each button. That way you will get a different value returned in the payload, which you can then evaluate in your action handler.

     

    See here for parameters you can use with the buttons: https://api.slack.com/reference/block-kit/block-elements#button

0/9000