如何最好地通过单个URL路由多个松弛交互?

时间:2019-07-10 17:03:41

标签: python flask slack

我正在使用Slack的Interactive Components开发Slack Bot,以通过对话框请求信息。所有交互式组件(按钮和对话框)都必须通过单个URL。解析Slack发送的POST请求并采取我在其他地方定义的必要操作的最佳方法是什么?

现在,我有两个函数可以一起完成此任务。首先,我解析POST请求以获得一个标识符,该标识符用作可能的函数调用的命令的键。

然后,我使用键将索引索引到我预定义的函数字典中,以确定应该调用哪个函数。返回值True表示函数具有必须返回到Slack的返回值。

def get_action(request):
    data = form_json = json.loads(request.form["payload"])
    key = None

    if data['type'] == 'interactive_message':
        key = data['actions'][0]['value']

    elif data['type'] == 'dialog_submission':
        key = data['callback_id']

    elif data['type'] == 'dialog_cancellation':
        key = 'cancellation_' + data['callback_id']

    return key, data


def get_func(key):
    options = {
        'issue_dialog': (False, dialog.issue),
        'announcement_dialog': (False, dialog.announcement),
        'event_dialog': (False, dialog.event),
        'travel_dialog': (False, dialog.travel), 
        'good_news_dialog': (False, dialog.good_news), 
        'ask_dialog': (False, dialog.ask),
        'pulse_dialog': (False, dialog.pulse), 
        'check_in_dialog': (False, dialog.check_in), 
        'test_dialog': (False, dialog.test),
        'pulse_message': (False, message.pulse), 
        'cancel': (True, message.cancel), 
        'issue_submission': (False, submit.issue.delay),
        'announcement_submission': (False, submit.announcement.delay), 
        'event_submission': (True,  submit.event),
        'travel_submission': (True,  submit.travel), 
        'good_news_submission': (False, submit.good_news.delay),
        'check_in_submission': (False, submit.check_in.delay), 
        'ask_submission': (False, submit.ask),
        'pulse_submission': (True,  submit.pulse), 
        'pulse_history': (False, submit.pulse_history.delay),
        'test_submission': (False, submit.test.delay), 
        'cancellation_pulse_submission': (False, submit.cancel_pulse)
        }
    ret = options[key]
    return ret

当我添加更多功能时,我现在拥有的功能字典已失控。我应该使用哪种组织来解析和检索正确的功能?

0 个答案:

没有答案