コード例 #1
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
コード例 #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)
コード例 #3
0
ファイル: api.py プロジェクト: ryptozee/megatron
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)