我正在修复另一个开发人员编写的 python 机器人。该代码是一个
python-bolt
松弛机器人,它在 shortcut
上创建一个 messages
。单击时,它将弹出一个模态表单,允许您通过 redminelib api 创建一个 Redmine Issue。最初的开发人员从 Redmine 中抓取所有项目并将它们放在一个下拉列表中,然后从 Redmine 中抓取所有用户并将其放入第二个下拉列表中。这有效(有点)。
问题是,如果您尝试将用户分配给他们不属于的项目……redmine api(正确地)会给您一个错误。
我试图调整代码以仅拉取项目的用户。这个功能有效......问题是我需要这个在项目下拉列表发生变化时动态更新。
根据我的理解,下面的代码“应该”有效......但是
__handle_project_select__
从未被调用过。
# 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
)
)
)
我发现的 slack 文档 似乎已通过 websocket 替换,“设置 > 交互性和快捷方式”菜单明确指出:
套接字模式已启用。您不需要指定选项加载 URL。
虽然我找到了reference
view
api,但我没有看到任何涉及Bot Token Scopes
的观点
我什至试过:
@app.action("*")
def handle_all_actions(ack, action, logger):
eprint('Is this even being called?')
ack()
但我什么也没得到。
直觉是我缺少权限或设置某处......但不知道在哪里。