Thanks for the reply. I understand creating a mess...
# 💻developers
a
Thanks for the reply. I understand creating a message
client.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!
c
Hi @ambitious-advantage-67939, what you are trying to do is really hard without having a dialog manager. Basically, the SDK allows you to receive and send messages but managing a conversation flow in code is done with another internal package called the dialog manager. This package isn't public yet because the interface to it isn't as clean as we would like but at some point we'll open source it for sure! 🚀 In your case, if you have a simple use case to manage a conversation you should be using the
state
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:
Copy code
import { 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
a
Awesome, thanks for the reply. Basically my company is trying to create a ManyChat replacement (Without building from scratch) for conversational messaging integrating with FB Messenger. We were looking at Botpress to achieve this. Do you have any other technology you recommend or have an estimate for the dialog manager polish?
Hmm, maybe you answered this, but I'm not totally looking to control the conversation. Ideally, I could programmatically build a conversation flow of N messages (With logic to determine order/sequence of messages), send the request to Botpress, and let Botpress keep track of which message the user is on. For example, is it possible to build a conversation flow like this from code: (An example template in your cloud platform) - Happy to call quick if it's easier as well! Thanks again!
c
Releasing the dialog manager isn't a priority since not many people are trying to build a bot as code. We are more focusing on improving the integration development experience. I do not know any really good dialog manager but a state machine library could be used for this purpose. The best one I know is called xstate. https://xstate.js.org/
a
So just to confirm, there's not a good way to programmatically create entire conversation flows in Botpress yet without additional state management?
k
it would be awesome if we could create entire conversation flows from code instead of manually drag-dropping nodes
does this mean it is currently impossible to programmatically creatw conversation flows in Botpress? or is there an api or module that faciliates this that's publicly available?
a
that is correct. Mike mentioned the dialog manager, which is the backend that powers the bot-building interface. This would be the tool for building bots with code. However, we are choosing to prioritize integration and product features before releasing the dialog manager API + documentation.
47 Views