Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help Create intent in js #4

Open
chickenstealers opened this issue Dec 29, 2020 · 0 comments
Open

Help Create intent in js #4

chickenstealers opened this issue Dec 29, 2020 · 0 comments

Comments

@chickenstealers
Copy link

Hi, i want to create weather.js intent that return current weather from city
for example, when user type "weather new york" it return current temperature, humidity, wind, cloud.
I'm using your example code but got the following error

D:\Pproject\WAbot\whatsbot\computed_intents\weather.js:13
fetchCities ()
^

ReferenceError: fetchCities is not defined
at new module.exports (D:\Pproject\WAbot\whatsbot\computed_intents\weather.js:13:9)
at LanguageProcessor.loadIntent (D:\Pproject\WAbot\LanguageProcessor.js:64:55)
at D:\Pproject\WAbot\LanguageProcessor.js:50:37
at Array.forEach ()
at LanguageProcessor.loadIntents (D:\Pproject\WAbot\LanguageProcessor.js:50:15)
at new LanguageProcessor (D:\Pproject\WAbot\LanguageProcessor.js:37:14)
at Object. (D:\Pproject\WAbot\bot.js:11:19)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
[nodemon] app crashed - waiting for file changes before starting...

this is my code

const got = require('got');

module.exports = class {
    constructor (processor) {
        this.processor = processor // the instance of LanguageProcessor.js that created this instance
        this.keywords = ["weather", "like"] // keywords to identify the intent
        this.entities = {}
        this.meta = { // set some metadata
            "userFacingName": ["weather"],
            "description": "Ask about the weather in different cities",
            "examples": ["weather in SF?", "new york weather", "listen fren, you better tell me what its like in Bombay"]
        }
        fetchCities ()
    }
    async fetchCities () {
        const url = `http://api.openweathermap.org/data/2.5/weather?q=dhaka&units=metric&appid=060a6bcfa19809c2cd4d97a212b19273&language=tr`;
        const response = await got(url).then(async ok => {
            const resp = JSON.parse(ok.body)
            temp = resp.main.temp_max
            humi = resp.main.humidity
            wind = resp.wind.speed
            clou = resp.clouds.all
            desc = resp.weather[0].description
            console.log(temp, humi, wind, clou, desc)
          })
        // this.processor.updateEntities ("weather", {"new york": "Rainy", "sf": "Bright", "new delhi": "Sunny", "tokyo": "Heavy Rain"})
    }
    /**
     * Async function that is called when somebody calls the `weather` command
     * @param {string[]} entities - what city?
     * @param {string} user - ID of the user
     */
    async answer (entities, user) {
        // fetch the cities you can answer for, possibly using a REST API
        // fetch weather.com/rest or something
        return entities
        //console.log(entities)
    }
}

I have successfully fetch the data from openweathermap.org API and test it using repl.it
this is the code that i use to fetch the data

const got = require('got');

const url = `http://api.openweathermap.org/data/2.5/weather?q=dhaka&units=metric&appid=060a6bcfa19809c2cd4d97a212b19273&language=tr`;

(async () => {
  const response = await got(url).then(async ok => {
    const resp = JSON.parse(ok.body)
    temp = resp.main.temp_max
    humi = resp.main.humidity
    wind = resp.wind.speed
    clou = resp.clouds.all
    desc = resp.weather[0].description
    console.log(temp, humi, wind, clou, desc)
  })
})();

it return this in repl.it
https://prnt.sc/wbxhmd

you can check the code here
https://repl.it/@AbdulAziz27/FloweryMindlessBytecode?lite=true

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant