botpress/botpress

request/body must have required property 'conversationId' - Webhook integration

jonra1993 opened this issue ยท 16 comments

I am trying to send a message after a webhook event and I get this error message after the event is triggered and tries to send a message back:

Error sending outgoing message {
  integration: 'webhook',
  messageType: 'text',
  payload: '{"type":"text","text":"Send this text after webhook","typing":true}',
  error: yt [Error]: request/body must have required property 'conversationId' (Error ID: err_1692749696997xB539FD1C)
      at Mr (/var/task/customer_code.js:8:27984)
      at Ot (/var/task/customer_code.js:8:27764)
      at w (/var/task/customer_code.js:8:67151)
      at /var/task/customer_code.js:8:62051
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
      at async Object.flushSendMessages (/var/task/customer_code.js:294:5069)
      at async runDialogEngine (/var/task/customer_code.js:315:3624)
      at async startEventProcessing (/var/task/customer_code.js:315:3220)
      at async Object.onEvent (/var/task/customer_code.js:315:8254)
      at async handler (/var/task/customer_code.js:429:2262) {
    isApiError: true,
    code: 400,
    description: 'The request payload is invalid.',
    type: 'InvalidPayload',
    error: undefined
  }
}
image

Steps to reproduce:

  1. Activate webhook integration
  2. Create a node that send a message after a webhook event is triggered.
image
  1. Use a http client to send a web hook event as documentation recomends: https://botpress.com/docs/cloud/channels/webhook/ (Note when using secret-token, the documentation suggests x-botpress-secret but in the source code seems to be "x-bp-secret" as you can check here )
image

I tried with documentation body changing the user id and it neither worked.

{
  "type": "text",
  "text": "Hello Webhooks!",
  "user": {
    "id": "user-id",
    "name": "User Name"
  },
}

Which is the correct way to send the user and conversation id?

Hi @jonra1993 ! This is happening because automatic binding of webhooks to a conversation ID isn't available yet, but for the time being you can add an Execute Code card right after your webhook trigger in the bot with the following code:

const data = JSON.parse(event.payload.body);
event.conversationId = data.conversationId;

Then make sure the payload sent to your bot's webhook looks like this:

{
  "conversationId": "CONVERSATION_ID_GOES_HERE",
  [the other fields of the webhook payload go here]
}

To do this you'll need to somehow allow the caller of the webhook know the original conversation ID. To do that you can use the event.conversationId variable in your bot's main workflow and store it somewhere that the caller of the webhook can later retrieve it to send it back to the bot in the webhook payload data as shown above.

Please let us know if this helps.

Hello, @AbrahamLopez10 thanks for your suggestion. I have tested it and still, it does not work.

I had to use this code.

try {
  const data = event.payload.body;
  console.log("income data from webhook", data);
  event.conversationId = data.conversationId;
  event.userId = data.userId;
  event.botId = data.botId;
} catch (error) {
  console.log('error')
}

This is the current payload.

{
  "type": "text",
  "text": "Hello Webhooks!",
  "conversationId": "a7068b30-d1d1-48bc-824d-375814f5d347",
  "userId": "4d7200f6-cada-4c32-a5f7-d96c6bd6c2d7",
  "botId": "7bb5857e-c0af-4f09-8e8d-0937e615fd63"
}

I made sure that I get conversationId, userId, and botId correctly.

image image

But now I get this different error message. I see the payload from the webhook arrives correctly the error happens again when trying to send back a message.

image image

A is shown on the above screenshots, "conversationId": "a7068b30-d1d1-48bc-824d-375814f5d347" and "botId": "7bb5857e-c0af-4f09-8e8d-0937e615fd63" are correct. How conversationId is stored in a bot and why it say that the conversationId does not exists??

@jonra1993 By any chance are you testing this on the emulator in the Studio? Or on which messaging channel are you testing this?

Hello @AbrahamLopez10 I tested on both the emulator and chat integration. In both cases the same result happened

The conversation ID you sent earlier was sent from the emulator or from the webchat? If from the emulator can you send me the conversation ID (for this same bot ID) that you got on webchat?

Reason I ask is that on the emulator the conversation ID is a fake one, as it's all running locally (not in the cloud), so we need to use the real messaging channels to verify this.

Hello @AbrahamLopez10 no the ID I sent you was from webchat. The one you get from here.

image

@jonra1993 I checked the conversation ID you provided and it doesn't exist in our database (which is why I asked about the emulator), although the bot ID does exist.

I did a quick test and event.conversationId in the bot is giving me a conversation ID which I looked up in the DB and it does exist, so not sure how you got that UUID for the conversation ID as it couldn't be found in the DB.

To make sure you're getting the correct conversation ID, could you open the webchat of your bot and type //whoami and then compare the conversation ID shown versus the one being sent to the webhook?

image

Hello @AbrahamLopez10 the command does not work is something need to add on the diagrams
image
image

Before I got conversationId using the console

image

@jonra1993 Could you try again? There was a transient issue with the webchat in the past hour. I checked on my end and it's working:

image

@AbrahamLopez10 I see the commands works in emulator but not in webchat yet

image

Not sure what's going on, did you see the screenshot of my previous comment? The //whoami command should work just fine in the webchat too.

@AbrahamLopez10 yes your solution worked I created a new bot in a new account and it worked once but I think the platform is inestable now because it does not load the studio.

image image

Seems that just conversationId, and userId are required.

Glad you got it working!

Thanks for your assistance @AbrahamLopez10

Hi, I'm facing the same problem and cannot find a way around it. After creating the Execute code card with @jonra1993 's code (thanks!) and looking at the logs, it looks like it's reading the onversationId but not the userId. Here's a screenshot of the logs:
image

Any pointer on the issue would be appreciated. Thanks

Hi, I'm facing the same problem and cannot find a way around it. After creating the Execute code card with @jonra1993 's code (thanks!) and looking at the logs, it looks like it's reading the onversationId but not the userId. Here's a screenshot of the logs: image

Any pointer on the issue would be appreciated. Thanks

I also have the same problem, how can I solve it?