예제 #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)
예제 #2
0
def _get_conversation_history(channel: MegatronChannel):
    connection = WorkspaceService(channel.workspace).get_connection()
    response = connection.open_im(channel.platform_user_id)
    channel_id = response['channel']['id']
    prev_messages = connection.im_history(channel_id, 10)
    integration_interpreter = IntegrationService(
        channel.megatron_integration).get_interpreter()
    messages = prev_messages['messages']
    messages.sort(key=lambda message: message['ts'])
    previous_ts = None
    for message in messages:
        timestamp = datetime.fromtimestamp(int(message['ts'].split('.')[0]))
        formatted_timestamp = _format_slack_timestamp(timestamp, previous_ts)
        message['text'] = f"{formatted_timestamp}{message['text']}"
        previous_ts = timestamp

        if message.get('bot_id'):
            integration_interpreter.outgoing(message, channel)
        else:
            integration_interpreter.incoming(message, channel)
    return prev_messages
예제 #3
0
파일: api.py 프로젝트: ryptozee/megatron
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)