Axios + Botpress


#1

When trying to use axios to query an external Weather API, I get this error

ReferenceError: axios is not defined
    at getTropicalCyclones (vm.js:16:9)

Here is the code for my action getTropicalCyclones {}
Located in global/actions/getTropicalCyclones.js
(of course I have to hide my client ID and secret)

const getTropicalCyclones = async () => {
   
        const BASE_WEATHER_API = `https://api.aerisapi.com/tropicalcyclones/`
        const CLIENT_ID_SECRET = `SECRET`
        const BASIN = `currentbasin=wp`
        const PLACE = `p=25,115,5,135` // rough coords for PH area of responsibility
        const ACTION = `within` // within, closest, search, affects or ''
        try {
            let text = ''
            let response = {}
            await axios.get(
                `${BASE_WEATHER_API}${ACTION}?${CLIENT_ID_SECRET}&${BASIN}&${PLACE}`
        )
            .then((resp) => {]
                response = resp
                text = 'Success retrieving weather!'
            })
            .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 weather info.',
            },
            event.channel
        )
        await bp.events.replyToEvent(event, payload)
      }
    }

    return getTropicalCyclones()

So my question is, how do I import axios? I’ve tried

`const axios = require('axios') or import axios from 'axios'`

but doing this causes a different error:

Error processing "getTropicalCyclones {}"
    Err: An error occurred while executing the action "getTropicalCyclones"

Looking at the package.json on GitHub, it looks like axios is already installed

However, I cannot locate this package.json on my bot directory…

Secondly, based on an old version doc it looks like this example code just used axios straight

How do I use axios on Botpress?
Any leads would be appreciated

Botpress: v11.0.0


#2

I use axios on botpress you have to install it first in command prompt root of your project npm install axios


#3

Thanks for replying!
By root project, where do you mean? Because I don’t see any package.json anywhere.

Please see screenshot below.

A, B or none of these?
@Soufyanbreda
More info: this action is located in global/actions/getTropicalCyclones.js


#4

I would say at point A but i currently work in botpress x not in botpress server


#5

axios is like any another npm module and you need to install it using npm. How did you setup your bot? You would have initialized it as bp init… correct?