0

I'm currently working with the 12.28.0 version of Botpress and I'm encountering an issue related to displaying multiple selectable options in the chat interface. I have an API integration where I fetch data and generate a message using bp.events.replyToEvent, but the response is currently appearing only in the server logs and not in the chat.

Here's the code snippet I'm using:

const axios = require('axios');

const generateOptions = async event => {
  try {
    const response = await axios.get('https://rickandmortyapi.com/api/character');
    const data = response.data.results;

    if (data && data.length > 0) {
      const options = data.map((item, index) => ({
        label: item.name,
        value: `${index + 1}`
      }));

      event.state.dynamicOptions = options;

      const message = {
        type: 'text',
        text: 'Choose an option:',
        content: {
          type: 'builtin_single-choice',
          choices: options.map(option => option.label)
        }
      };

      await bp.events.replyToEvent(event, [message]);
    } else {
      throw new Error('No valid results found in the API response.');
    }
  } catch (error) {
    throw new Error('Error fetching data from the API: ' + error.message);
  }
};

return generateOptions(event);

I want the API response to be displayed as multiple selectable options in the chat interface, allowing the user to make a selection directly in the chat. However, the current implementation only logs the response. Can anyone guide me on how to achieve this using the SDK version of Botpress?

Thank you in advance for your help and suggestions!

GD7
  • 1
  • 2

0 Answers0