Beispiel #1
0
def open_channel(
    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 = arguments["targeted_platform_id"]
    integration = megatron_user.megatronintegration_set.first()
    connection = IntegrationService(integration).get_connection(as_user=False)

    # Ensure platform user exists
    if not PlatformUser.objects.filter(platform_id=platform_user_id).exists():
        user_info = connection._get_user_info(platform_user_id)
        try:
            workspace = CustomerWorkspace.objects.get(platform_id=user_info["user"]["team_id"])
        except (CustomerWorkspace.DoesNotExist, KeyError):
            return {"ok": False, "error": "Customer Workspace not found"}
        WorkspaceService(workspace).get_or_create_user_by_id(platform_user_id)

    new_msg = formatting.user_titled(platform_user_id, "Connecting...")
    response = connection.respond_to_url(request_data.response_url, new_msg)
    if not response.get("ok"):
        return response
    _update_channel_link.delay(
        megatron_user.id, platform_user_id, request_data.response_url
    )
    return {"ok": True}
Beispiel #2
0
def _update_channel_link(
    megatron_user_id: int, platform_user_id: str, response_url: str
):
    megatron_user = MegatronUser.objects.get(id=megatron_user_id)
    integration = megatron_user.megatronintegration_set.first()
    connection = IntegrationService(integration).get_connection(as_user=False)
    platform_user = PlatformUser.objects.get(platform_id=platform_user_id)
    megatron_user = MegatronUser.objects.get(id=megatron_user_id)

    megatron_channel = MegatronChannel.objects.filter(
        platform_user_id=platform_user_id
    ).first()

    if not megatron_channel:
        username = platform_user.username + "_" + platform_user.workspace.domain
        username = re.sub(r"[^\w-]", "", username.lower())
        response = connection.create_channel(f"{settings.CHANNEL_PREFIX}{username}")
        if not response:
            response = connection.respond_to_url(
                response_url, {"text": "Error creating channel."}
            )
            if not response.get("ok"):
                LOGGER.error(f"Problem updating slack message: {response['error']}")
            return

        megatron_channel, created = _create_or_update_channel(
            megatron_user, response["channel"], platform_user
        )
        if created:
            _get_conversation_history(megatron_channel)

    elif megatron_channel.is_archived:
        MegatronChannelService(megatron_channel).unarchive()
        _get_conversation_history(megatron_channel)

    channel_link = _get_channel_link(
        megatron_user, megatron_channel.platform_channel_id
    )
    join_message = formatting.user_titled(
        platform_user_id, f"<{channel_link}|Go to slack conversation>"
    )
    response = connection.respond_to_url(response_url, join_message)
    if not response.get("ok"):
        LOGGER.error(f"Problem updating slack message: {response['error']}")
def open_channel(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 = arguments['targeted_platform_id']
    integration = megatron_user.megatronintegration_set.first()
    connection = IntegrationService(integration).get_connection(as_user=False)
    new_msg = formatting.user_titled(platform_user_id, "Connecting....")
    response = connection.respond_to_url(request_data.response_url, new_msg)
    if not response.get('ok'):
        return response
    _update_channel_link.delay(megatron_user.id, platform_user_id,
                               request_data.response_url)
    return {'ok': True}