https://discord.gg/botpress logo
How to create parallel conversations with state (C...
# 🤝help
g
Hi! I'm struggling to find the correct way to create multiple, parallel instances of my bot, each with its own state and initial data. My Goal: I have an external server that needs to make up to 10 simultaneous API calls to Botpress. Each API call should: - Create a new, unique conversation instances - Pass a JSON payload of data into that workflow. - The instances invoked must be able to save to state - Process the data and return a response eventually (this is the easy part) Problem 1: Using the POST /conversations (Chat API, I believe it is the incorrect api) Based on the "Try it" docs for the Chat Integration, I tried POST-ing to my webhook URL (https://chat.botpress.cloud/.../conversations) with a body like {"id": "my-custom-id"}. This invokes, but fails. The logs show: It triggers my default Main flow. It fails with Cannot save state ... because the ID is missing. The id I pass in the body seems to be ignored, and the bot's internal conversation ID is blank (""). It also fails with request/body/conversationId must NOT have fewer than 28 characters when the bot tries to send a message. Problem 2: Using the POST /v1/chat/events (Runtime API) I then tried the Runtime API, which seems more correct, but I'm getting new errors: Error A: [Create Event] Event type "api-invoke" isn't defined by the bot... I'm confused about the type field. It says anything less than or equal to length 200, but they all fail. **Problem 3:**POST/v1/chat/conversations Error B (create: Must specify either integrationId or integrationAlias This error is confusing. The createConversation docs said integrationName was deprecated, but now this API requires an integration ID. What is the correct integrationId or integrationAlias for a standard webchat bot? My Core Question: What is the 100% correct API and JSON body to achieve my goal? I just need to create an "instance" of my bot, that can save to state, and flow through workflows like a regular user facing bot.
w
problem 1: use the messaging api - it's a lot better than the chat api problem 2: runtime api is for when the conversation already exists i think problem 3: also solved by using the messaging api i think
by using the messaging api instead of the chat one you skip a lot of the hurdles like signing JWT tokens for each user and such
i'm gonna give you an example request that i currently use in n8n
1. install the "messaging api" integration in your bot and publish it. this is the integration that will be used to manage the conversation, messages, etc 2. copy the integration link after installing 3. when you want to start a conversation: URL: the one you just copied METHOD: POST AUTHENTICATION: Bearer your botpress PAT (not your bot token, your personal access token) HEADER: Content-Type application/json as for the body, i'm not entirely sure what's strictly necessary - but here's what i pass: - userId (created by me) - conversationId (created by me) - messageId (created by me) - type: 'text' - text: (your message) - payload: { (whatever you want) }
after that, botpress returns its internal user and conversation IDs and the one you gave it
then, set your "response endpoint url" in the same messaging api integration and just listen for new events there
g
Thank you for the response bro, that sounds perfect, I am currently out but I’ll message back when it’s done.
w
@gentle-policeman-89141 hey, did you manage to get it working?
f
@wide-umbrella-5226 sorry, i have a question: When you make the POST request, where do you get the conversation ID from? Because it's supposed to be what Botpress gives when it creates the conversation, right? That's what I don't understand, how do I obtain the convo ID in advance?
w
it's a bit weird, but there are TWO conversation ids
when you create your own
i'm going to open my n8n flow really quick and show you, one minute
when i receive a message from a conversation that i created myself, it returns: - conversationId (which is the one i created myself) - botpressConversationId (which is the one botpress created) - botpressUserId (also by them)
so it always returns both conversation ids so you can relate them both in your database
also no need to apologize - feel free to ask anything
w
conversationId and userId are in
event.conversationId
and
event.userId
https://www.botpress.com/docs/learn/guides/advanced/event-properties#event-properties
w
i think they're asking for an external request, at which point the conversation and user (or even the event) don't exist yet
f
thanks!
g
yes bro i managed to get it working, but i had to go back a few steps to make sure it all works in my use case as its difficult, but i managed to invoke an instance and get it send messages to my server via curl request. You definitely solved my problem nice one.
@wide-umbrella-5226 You know the webhook server that we set as the response endpoint url, will it be able to detect if i use a send custom event card in the bot instance that its listening to? then can i send an event from the web hook listener back to the instance that the initial event came from?
w
yes sure that's very easy
first: only send a response to the custom event when you already have processed the data. then, after the return comes with the data, the custom event will save the response and resume processing
g
I tested it using webhook.site, and there was no custom event that arrived, it only seemed to be text responses the bot makes... This isnt fatal anyway im thinking to store the data needed for each instance in a botpress table, keyed by userId and another unique id.
w
that might be because webhook.site returns nothing
i use the exact steps i described to interact with my n8n workflows and it works nicely
also you have to manually extract the response and save it to your variables using the execute code block and such
g
ok! the webhook site may just return the text responses, i found that for my use case, if i save the data needed to a table, instead of using a send event with the id, if query the botpress table with execute code (filter param) this can work better, ill message back when finally everything works together. thanks for the help as always!
w
you're welcome!! feel free to open another thread if you need help with something else
message has been deleted
g
my man! i have invoked the messaging api (bot instances in parallel) aka the 'Fan out' step, now i gotta work out how to fan the results back into the primary bot (the bot instance that the user is currently in a session with). the responses are being posted to my serverless function just need to do some logic there and what not. complicated stuff!
@wide-umbrella-5226 Shen, just to let you know this worked incredibly well, thank you a whole lot.