Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
3.8 kB
0
Indexable
    async receiveKnownMessage(
        type: SubChannel,
        recipient: string, // this is a string of recipients if it is a group
        payload: TwilioMessage
    ) {
        const existingChannelNode =
            await this.channelNodeService.getChannelNodeByTypeAndValue(
                type,
                recipient
            )

        if (!existingChannelNode)
            throw new Error(
                `Channel node not found;; type: ${type}, value: ${recipient}`
            )

        await this.channelNodeTwilio.addAndUpdateLastMessageData(
            existingChannelNode._id,
            payload
        )

        // await this.ctx.scheduler.runAfter(
        //     0,
        //     internal.actions.ai.tools.summarizer
        //         .runSummarizationAndSentimentFlow,
        //     {
        //         workspaceId: this.workspaceId,
        //         conversationId: (
        //             existingConversationMap.conversationData as ConversationDataTwilio
        //         ).conversationSid,
        //         subChannel: type,
        //     }
        // )
    }

    async receivedUnknownMessage(
        type: SubChannel,
        from: string,
        recipients: string[], // This is a string of recipients if it is a group
        provider: string,
        body: string,
        dateCreated: string
    ) {
        const combinedRecipientsString = recipients.join(",")

        const existingChannelNode =
            await this.channelNodeService.getChannelNodeByTypeAndValue(
                type,
                combinedRecipientsString
            )

        let finalChannelNodeId: Id<"channelNode"> | null = null

        if (existingChannelNode) {
            const existingConversationMap =
                await this.channelNodeService.getConversationMapByChannelNodeIdAndProvider(
                    existingChannelNode._id,
                    provider
                )

            if (existingConversationMap) return

            finalChannelNodeId = existingChannelNode._id
        } else {
            const contactId =
                await this.contactNodeService.createUnmappedContact()

            finalChannelNodeId =
                await this.channelNodeService.createChannelNode(
                    contactId,
                    {
                        type,
                        value: combinedRecipientsString,
                    }
                )
        }

        const { conversationSid, participantBindings } =
            await this.channelNodeTwilio.createConversation(
                recipients,
                finalChannelNodeId
            )

        const messageSender = participantBindings.find(
            (participantBinding) => {
                return participantBinding.address === from
            }
        )

        if (!messageSender) {
            throw new Error(
                `ParticipantBinding not found;; participantBindings: ${participantBindings}`
            )
        }

        await this.channelNodeTwilio.addAndUpdateLastMessageData(
            finalChannelNodeId,
            {
                workspaceId: this.workspaceId,
                messageSid: uuidv4(),
                participantSid: messageSender.participantSid,
                conversationSid,
                body,
                date: moment(dateCreated).utc().toISOString(),
                isIncoming: true,
            }
        )

        // await this.ctx.scheduler.runAfter(
        //     0,
        //     internal.actions.ai.tools.summarizer
        //         .runSummarizationAndSentimentFlow,
        //     {
        //         workspaceId: this.workspaceId,
        //         conversationId: conversationSid,
        //         subChannel: type,
        //     }
        // )
    }
Leave a Comment