简体   繁体   中英

Unable to use external API on Botpress (axios)

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

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

Here is my action for getTropicalCyclones {} (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 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 https://github.com/botpress/botpress/blob/master/package.json

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 https://botpress.io/docs/10.31/recipes/apis/

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

Botpress: v11.0.0

Simply use ES6 import . include this line at the top of your code.

import axios from 'axios';

Note : I'm expecting that the axios is already installed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM