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?


#6

I am also facing the same issue…please suggest


#7

@msurabhi2 , there was a suggestion above from @jainsourabh2 that makes sense to me.


#8

Thanks for your reply.

I am using botpress version v11.

Here is my sample code snippet::
import { axios } from ‘axios’

const callApiMethod = async (state) => {

await axios.get(‘my_api_url’).then(res => {
console.log(res);

})

}
return callApiMethod(args.state)

(NOTE: axios is installed)
ERROR::
11:09:39.444 Actions Running “callApiMethod”
11:09:39.503 DialogEngine Error processing “callApiMethod {}”
Err: An error occurred while executing the action “callApiMethod”


#9

There is some issue while calling API with the botpress latest version.

Here is my sample code snippet::

import { axios } from ‘axios’

const callApiMethod = async (state) => {

await axios.get(‘my_api_url’).then(res => {
console.log(res);

})

}
return callApiMethod(args.state)

(NOTE: axios is installed)
ERROR::
11:09:39.444 Actions Running “callApiMethod”
11:09:39.503 DialogEngine Error processing “callApiMethod {}”
Err: An error occurred while executing the action “callApiMethod”

Can someone please help me?


#10

Try putting axios call into try-catch statement and check an error you are getting.


#11

I had the same issue discussed here. At first I was receiving axios is not defined, but I installed axios in the root of the project and added

const axios = require(‘axios’)

at the start of my implementation class. And it works.


#12

Yes this is the right way