chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
import { getSuncoClient } from 'src/client'
|
||||
import { getStoredCredentials } from 'src/get-stored-credentials'
|
||||
import * as bp from '.botpress'
|
||||
|
||||
export const getOrCreateConversation: bp.IntegrationProps['actions']['getOrCreateConversation'] = async ({
|
||||
client,
|
||||
input,
|
||||
ctx,
|
||||
}) => {
|
||||
const credentials = await getStoredCredentials(client, ctx)
|
||||
const suncoConversation = await getSuncoClient(credentials).getConversation(input.conversation.id)
|
||||
|
||||
const { conversation } = await client.getOrCreateConversation({
|
||||
channel: 'channel',
|
||||
tags: { id: `${suncoConversation.conversation?.id}` },
|
||||
})
|
||||
|
||||
return {
|
||||
conversationId: conversation.id,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import { getSuncoClient } from 'src/client'
|
||||
import { getStoredCredentials } from 'src/get-stored-credentials'
|
||||
import * as bp from '.botpress'
|
||||
|
||||
export const getOrCreateUser: bp.IntegrationProps['actions']['getOrCreateUser'] = async ({ client, input, ctx }) => {
|
||||
const credentials = await getStoredCredentials(client, ctx)
|
||||
const suncoUser = await getSuncoClient(credentials).getUser(input.user.id)
|
||||
const suncoProfile = suncoUser.user?.profile
|
||||
|
||||
const name = input.name ?? [suncoProfile?.givenName, suncoProfile?.surname].join(' ').trim()
|
||||
const { user } = await client.getOrCreateUser({
|
||||
tags: { id: `${suncoUser.user?.id}` },
|
||||
name,
|
||||
pictureUrl: input.pictureUrl ?? suncoProfile?.avatarUrl,
|
||||
})
|
||||
|
||||
return {
|
||||
userId: user.id,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import { getOrCreateConversation } from './get-or-create-conversation'
|
||||
import { getOrCreateUser } from './get-or-create-user'
|
||||
import { startTypingIndicator, stopTypingIndicator } from './typing-indicator'
|
||||
import * as bp from '.botpress'
|
||||
|
||||
export const actions = {
|
||||
startTypingIndicator,
|
||||
stopTypingIndicator,
|
||||
getOrCreateUser,
|
||||
getOrCreateConversation,
|
||||
} satisfies bp.IntegrationProps['actions']
|
||||
@@ -0,0 +1,60 @@
|
||||
import { getSuncoClient } from 'src/client'
|
||||
import { getStoredCredentials } from 'src/get-stored-credentials'
|
||||
import { getSuncoConversationId } from '../util'
|
||||
import * as bp from '.botpress'
|
||||
|
||||
type SendActivityProps = Pick<bp.AnyMessageProps, 'ctx' | 'client'> & {
|
||||
conversationId: string
|
||||
typingStatus?: 'start' | 'stop'
|
||||
markAsRead?: boolean
|
||||
}
|
||||
|
||||
async function sendActivity({ client, ctx, conversationId, typingStatus, markAsRead }: SendActivityProps) {
|
||||
const credentials = await getStoredCredentials(client, ctx)
|
||||
const { conversation } = await client.getConversation({ id: conversationId })
|
||||
const suncoConversationId = getSuncoConversationId(conversation)
|
||||
const suncoClient = getSuncoClient(credentials)
|
||||
if (markAsRead) {
|
||||
await suncoClient.postActivity(suncoConversationId, {
|
||||
type: 'conversation:read',
|
||||
author: { type: 'business' },
|
||||
})
|
||||
}
|
||||
if (typingStatus) {
|
||||
await suncoClient.postActivity(suncoConversationId, {
|
||||
type: `typing:${typingStatus}`,
|
||||
author: { type: 'business' },
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export const startTypingIndicator: bp.IntegrationProps['actions']['startTypingIndicator'] = async ({
|
||||
client,
|
||||
ctx,
|
||||
input,
|
||||
}) => {
|
||||
const { conversationId } = input
|
||||
await sendActivity({
|
||||
client,
|
||||
ctx,
|
||||
conversationId,
|
||||
typingStatus: 'start',
|
||||
markAsRead: true,
|
||||
})
|
||||
return {}
|
||||
}
|
||||
|
||||
export const stopTypingIndicator: bp.IntegrationProps['actions']['stopTypingIndicator'] = async ({
|
||||
client,
|
||||
ctx,
|
||||
input,
|
||||
}) => {
|
||||
const { conversationId } = input
|
||||
await sendActivity({
|
||||
client,
|
||||
ctx,
|
||||
conversationId,
|
||||
typingStatus: 'stop',
|
||||
})
|
||||
return {}
|
||||
}
|
||||
Reference in New Issue
Block a user