ambitious-advantage-67939
08/04/2023, 4:43 PMclient.createMessage
based on their response (dynamic).
If I wanted to make a template where there can be 3 messages sequentially each requiring input from the user, is the best practice using tags? Asked a better way: What's the best practice to determine WHICH message a user is on in a sequence of N messages in a template? Thanks again!colossal-egg-20510
08/04/2023, 6:20 PMstate
api. Here's a small example of a possible way to do it. Obviously without a dialog manager it is missing a lot of feature like retries, plugins, logic gates, etc:colossal-egg-20510
08/04/2023, 6:20 PMimport { Bot } from '@botpress/sdk'
import { z } from 'zod'
type MessageHandlerProps = Parameters<Parameters<typeof bot.message>[0]>[0]
const bot = new Bot({
states: {
dialog: {
type: 'conversation',
schema: z.object({
step: z.number().default(0),
}),
},
},
})
bot.message(async (props) => {
const dialogState = await props.client
.getState({ type: 'conversation', id: props.conversation.id, name: 'dialog' })
.catch(() => ({
state: {
payload: {
step: 0,
},
},
}))
const step = dialogState.state.payload.step
switch (step) {
case 0:
await sendMessage('Hello!', props)
break
case 1:
await sendMessage('You said: ' + props.message.payload.text, props)
break
case 2:
await sendMessage('Your response was: ' + props.message.payload.text, props)
break
default:
break
}
await setState(step + 1, props)
})
async function sendMessage(text: string, { client, conversation, ctx }: MessageHandlerProps) {
await client.createMessage({
conversationId: conversation.id,
userId: ctx.botId,
tags: {},
type: 'text',
payload: {
text,
},
})
}
async function setState(step: number, props: MessageHandlerProps) {
await props.client.setState({ type: 'conversation', id: props.conversation.id, name: 'dialog', payload: { step } })
}
export default bot
ambitious-advantage-67939
08/04/2023, 6:31 PMambitious-advantage-67939
08/04/2023, 8:06 PMcolossal-egg-20510
08/04/2023, 8:09 PMambitious-advantage-67939
08/04/2023, 8:20 PMkind-traffic-18566
08/07/2023, 1:20 AMkind-traffic-18566
08/07/2023, 1:21 AMacceptable-kangaroo-64719
08/07/2023, 10:14 AM