External API - process JSON into objects


#1

Hello,

I’m trying to port my Messenger bot to BotPress, but I’m having a hard time finding references to some simple issues.

I’ve implemented a new action in which my bot is accessing data from an Url in JSON format. The JSON consists in a list of users with some simple string properties.

  1. My first issue is that the request succeeds but nothing is reported back to the user.

  2. I need to pass/save the list of users retrieved from the action, to the next node where the user can manipulate the list. Think of an interaction, where a user requests a list of users then picks one up to check information. I did not see any example of how to do such a thing, and I can’t think of a way to start the implementaion.

This is my code:

const axios = require(‘axios’)
/**

  • Get data
  • @title Get data
  • @category Data
  • @author Author
    */
    const getData = async () => {

const BASE_API = https://api.myjson.com/bins/15182a

try {
let text = ‘’
let response = {}
await axios.get(${BASE_API})
.then((resp) => {
response = resp
text = ‘Success retrieving data!’
console.log(‘Sucess’)
})
.catch((error) => {
console.log(’!! error’, error)
})

const payload = await bp.cms.renderElement(
‘builtin_text’,
{
text,
},
event.channel
)
await bp.events.replyToEvent(event, payload)
}
catch (e) {
// Failed to fetch, this is where ReferenceError: axios is not defined comes from
console.log(’!! Error while trying to fetch weather info’, e)
const payload = await bp.cms.renderElement(
‘builtin_text’,
{
text: ‘Error while trying to fetch data’,
},
event.channel
)
await bp.events.replyToEvent(event, payload)
}
}

return getData()


#2

@Macaret There’s two issues here.

  1. If you use async/await, don’t mix them with then and catch.
  2. Event destination also requires the bot id to get the right content element

Here’s a working version, which will list the name of your users:

const axios = require('axios')

/**
 * Get data
 * @title Get
 * @category Data
 * @author Author
 */
const getData = async () => {
  try {
    const { data } = await axios.get('https://api.myjson.com/bins/15182a')

    const result = data.policies.map(x => x.name).join(',')
    const payload = await bp.cms.renderElement('builtin_text', { text: result }, event)
    await bp.events.replyToEvent(event, payload)
  } catch (e) {
    // Failed to fetch, this is where ReferenceError: axios is not defined comes from
    console.log('!! Error while trying to fetch weather info', e)
    const payload = await bp.cms.renderElement('builtin_text', { text: 'Error ' }, event)
    await bp.events.replyToEvent(event, payload)
  }
}

return getData()

PS: use the three backquote and write js to format it correctly:

```js

#3

Thank you. Your solution works.

I have another question if you please to help. If I want to pass the result to the next node, without the user input, how would I begin to do that? The main idea is that once the list is retrieved, I will ask the user another question and based on his answers I filter the list.


#4

@Macaret There are different types of memory. In your actions you can set some variables and read them in another. Please check the docs for more informations : https://botpress.io/docs/build/memory/#temporary-memory