Exemple #1
0
def clear_context(
    megatron_user_id: int, serialized_request_data: dict, arguments: dict
) -> dict:
    request_data = RequestData(**serialized_request_data)
    megatron_user = MegatronUser.objects.get(id=megatron_user_id)
    platform_user_id = User.objects.get(id=arguments["platform_user_id"])
    command_url = getattr(megatron_user, "command_url", None)
    if not command_url:
        return {"ok": False, "error": "MegatronUser has not provided a command url."}
    # TODO: Use the command that called this to standardize the 'command' param
    response = requests.post(
        megatron_user.command_url,
        json={
            "command": "clear-context",
            "platform_user_id": platform_user_id,
            "megatron_verification_token": megatron_user.verification_token,
        },
    )
    if not response.status_code == 200:
        return {"ok": False, "error": response.content}

    # TODO: This is probably better suited to being part of the integration itself
    integration = megatron_user.megatronintegration_set.first()
    integration_connection = IntegrationService(integration).get_connection(
        as_user=False
    )
    platform_username = PlatformUser.objects.get(platform_id=platform_user_id).username
    msg = {"text": f"Context cleared for *{platform_username}*."}
    integration_response = integration_connection.ephemeral_message(request_data, msg)

    if not integration_response.get("ok"):
        return {"ok": False, "error": "Failed to post confirmation to slack."}

    return {"ok": True}
Exemple #2
0
def notify_user(request) -> MegatronResponse:
    msg = request.data['message']
    user_id = request.data['user_id']
    channel_id = request.data['channel_id']
    platform_type = request.data['platform_type']
    request_data = RequestData(channel_id=channel_id,
                               user_id=user_id,
                               response_url="")
    platform_type = PlatformType[platform_type.capitalize()].value
    megatron_channel = MegatronChannel.objects.get(
        platform_channel_id=channel_id, workspace__platform_type=platform_type)
    connection = IntegrationService(
        megatron_channel.megatron_integration).get_connection(as_user=False)
    response = connection.ephemeral_message(request_data, msg)
    if response.get('ok'):
        return MegatronResponse({'ok': True, 'track': True}, 200)
    return MegatronResponse({
        'error': response.get('error'),
        'track': False
    }, 500)
Exemple #3
0
def notify_user(request) -> MegatronResponse:
    msg = request.data["message"]
    user_id = request.data["user_id"]
    channel_id = request.data["channel_id"]
    platform_type = request.data["platform_type"]
    request_data = RequestData(channel_id=channel_id,
                               user_id=user_id,
                               response_url="")
    platform_type = PlatformType[platform_type.capitalize()].value
    megatron_channel = MegatronChannel.objects.get(
        platform_channel_id=channel_id, workspace__platform_type=platform_type)
    connection = IntegrationService(
        megatron_channel.megatron_integration).get_connection(as_user=False)
    response = connection.ephemeral_message(request_data, msg)
    if response.get("ok"):
        return MegatronResponse({"ok": True, "track": True}, 200)
    return MegatronResponse({
        "error": response.get("error"),
        "track": False
    }, 500)