Exemple #1
0
def outgoing(request) -> MegatronResponse:
    user_id = request.data["user"]
    message = request.data["message"]
    try:
        message["attachments"] = json.loads(message["attachments"])
    except TypeError:
        pass
    try:
        megatron_channel = MegatronChannel.objects.get(
            platform_user_id=user_id)
    except MegatronChannel.DoesNotExist:
        return MegatronResponse({"ok": True, "track": False}, 200)
    if megatron_channel.is_archived:
        return MegatronResponse({"ok": True, "track": False}, 200)

    megatron_integration = megatron_channel.megatron_integration
    interpreter = IntegrationService(megatron_integration).get_interpreter()
    customer_workspace_id = megatron_channel.workspace.platform_id
    response = interpreter.outgoing(message, megatron_channel)
    if response.get("ok"):
        if response.get("watched_channel"):
            front_integration.outgoing_message.delay(user_id,
                                                     customer_workspace_id,
                                                     message)
            megatron_msg = MegatronMessage(
                integration_msg_id=response["ts"],
                customer_msg_id=request.data["ts"],
                megatron_channel=megatron_channel,
            )
            megatron_msg.save()

        return MegatronResponse({"ok": True, "track": True}, 200)
    return MegatronResponse({
        "error": response.get("error"),
        "track": False
    }, 500)
Exemple #2
0
def incoming(request) -> MegatronResponse:
    msg = request.data["message"]
    megatron_user = request.user
    integration = megatron_user.megatronintegration_set.first()
    team_interpreter = IntegrationService(integration).get_interpreter()
    channel = MegatronChannel.objects.filter(
        platform_user_id=msg["user"]).first()
    if not channel or channel.is_archived:
        return MegatronResponse({"ok": True, "track": False}, 200)
    response = team_interpreter.incoming(msg, channel)
    if response.get("ok"):

        if response.get("watched_channel"):
            front_integration.incoming_message.delay(msg)
            channel = MegatronChannel.objects.filter(
                platform_user_id=request.data["message"]["user"]).first()
            MegatronMessage.objects.create(
                integration_msg_id=response["ts"],
                customer_msg_id=request.data["message"]["ts"],
                megatron_channel=channel,
            )

        return OK_RESPONSE
    return MegatronResponse(response.get("error"), 500)