2023-04-20

Slack Bot Actions (update form data on dropdown)

I am working on fixing a python bot that another developer wrote. The code is a python-bolt slack bot that creates a shortcut on messages. When clicked it will pop up a Modal Form that allows you to create a Redmine Issue via the redminelib api. The initial developer grabbed all the projects from Redmine and put them in one drop down, and grabbed all the users from Redmine and put it in a second dropdown. This worked (kinda).

The problem is that if you attempt to assign a user to a project that they are not a part of ... the redmine api (correctly) gives you an error.

I have attempted to adapt the code to pull ONLY the users for a project. This functionality works ... the problem is I need this to dynamically update whenever the projects dropdown changes.

To my understanding, the code below "should" work ... but __handle_project_select__ is never called.

# Helper function to print to stderr
def eprint(*args, **kwargs):
  print(*args, file=sys.stderr, **kwargs)

class SlackApp:

  def __init__(self, config, app, handler):
    eprint('__init__')
    # self.__logger__ = logging.getLogger(__name__)

    redmine_api_key = config["REDMINE_API_TOKEN_KEY"]
    redmine_cert_path = config["REDMINE_CERT_FILE_PATH_KEY"]
    self.__redmine_url__ = config["REDMINE_URL_KEY"]

    self.__slack_instance__ = config["SLACK_INSTANCE_KEY"]

    self.__slackBoltApp__  = app # App(token=slackBot_token)
    self.__slackHandler__  = handler # SocketModeHandler(self.__slackBoltApp__, slackApp_token)
    self.__slackClient__   = app.client # WebClient(token=slackBot_token)

    self.__redmineClient__ = RedmineClient(self.__redmine_url__, redmine_api_key, redmine_cert_path)

    self.__slackBoltApp__.shortcut("ssb_redmine_create_issue")(self.__handle_shortcut_redmine__)
    self.__slackBoltApp__.view("ssb_redmine_issue_submission")(self.__handle_view_submission__)
    self.__slackBoltApp__.action("ssb_redmine_project_select_action")(self.__handle_project_select__)

  def __showform__(self, trigger_id, metadata):
    eprint('__showform__')
    try:
      projects = self.__redmineClient__.get_projects()
      users = self.__redmineClient__.get_users()
      response = self.__slackClient__.views_open(
        trigger_id=trigger_id,
        view = json.dumps(SlackModalForm.getFormNewIssue(trigger_id, metadata, projects, users))
      )
    except Exception as e:
      # self.__logger__.exception(f"Error opening view: {e}")
      raise

  def __handle_project_select__(self, ack, body, logger):
    eprint('__handle_project_select__')
    ack()
    
    # Extract the selected project ID
    selected_project_id = body['actions'][0]['selected_option']['value']
    
    # Get the updated list of users based on the selected project (Assuming you have a method for this)
    updated_users = self.__redmineClient__.get_users_for_project(selected_project_id)
    
    # Create the updated users dropdown options
    updated_user_options = [
      {
        "text": {
          "type": "plain_text",
          "text": user_name
        },
        "value": str(user_id)
      }
      for user_name, user_id in updated_users.items()
    ]
    
    # Update the view with the new options for the users dropdown
    self.__slackClient__.views_update(
      view_id=body['container']['view_id'],
      view=json.dumps(
        SlackModalForm.getFormNewIssue(
          body['trigger_id'], 
          body['view']['private_metadata'], 
          projects,  # Pass the original project list
          updated_users
        )
      )
    )

The slack documentation I found seems to been replaced via websocket and "Settings > Interactivity & Shortcuts" menu specifically states:

Socket Mode is enabled. You won’t need to specify an Options Load URL.

And while I have found reference to view api I dont see anything involving views in Bot Token Scopes

I even tried:

@app.action("*")
def handle_all_actions(ack, action, logger):
    eprint('Is this even being called?')
    ack()

but am not getting anything.

Gut feel is that I am missing a permission or setting somewhere ... but have no idea where.



No comments:

Post a Comment