Weatherbot_Tutorial icon indicating copy to clipboard operation
Weatherbot_Tutorial copied to clipboard

client.getCurrentWeather(q=loc) (in actions.py) fails

Open jmourato opened this issue 6 years ago • 7 comments

client.getCurrentWeather(q=loc) (in actions.py line 33) fails as apixu has changed the api.

Solve this issue by replacing client.getCurrentWeather(q=loc) with
current = client.current(q=loc) to run according to the current code in https://github.com/apixu/apixu-python

check https://github.com/apixu/apixu-python/blob/master/apixu/client.py line 46 ... def current(self, q=None): ....

jmourato avatar Feb 12 '19 23:02 jmourato

@jmourato Nice! Would you be up for creating a PR for this? :)

JustinaPetr avatar Mar 01 '19 09:03 JustinaPetr

Hi Justina

I'll do a pull request for this issue, np

jmourato avatar Mar 01 '19 11:03 jmourato

Hi @JustinaPetr

Just made a PR with the changes.

Regards João

jmourato avatar Mar 04 '19 12:03 jmourato

Thanks for the commend...this works perfectly!

from future import absolute_import from future import division from future import unicode_literals

from rasa_core.actions.action import Action from rasa_core.events import SlotSet

class ActionWeather(Action): def name(self): return 'action_weather'

def run(self, dispatcher, tracker, domain):
	from apixu.client import ApixuClient
	api_key = '................................' #your apixu key
	client = ApixuClient(api_key)
	
	loc = tracker.get_slot('location')
	current = client.current(q=loc)
	
	country = current['location']['country']
	city = current['location']['name']
	condition = current['current']['condition']['text']
	temperature_c = current['current']['temp_c']
	humidity = current['current']['humidity']
	wind_mph = current['current']['wind_mph']

	response = """It is currently {} in {} at the moment. The temperature is {} degrees, the humidity is {}% and the wind speed is {} mph.""".format(condition, city, temperature_c, humidity, wind_mph)
					
	dispatcher.utter_message(response)
	return [SlotSet('location',loc)]

cobusgreyling avatar Mar 11 '19 12:03 cobusgreyling

use below as actions.py

from future import absolute_import from future import division from future import unicode_literals

from rasa_core_sdk import Action from rasa_core_sdk.events import SlotSet import requests

class ActionWeather(Action): def name(self): return 'action_weather'

def run(self, dispatcher, tracker, domain):
        from apixu.client import ApixuClient
        api_key = '###########' #your apixu key
        client = ApixuClient(api_key)

        loc = tracker.get_slot('location')
        params = {
        'access_key': api_key,
        'query': str(loc)
        }
        print("Searching in location ", loc)
        api_result = requests.get('http://api.weatherstack.com/current', params)
        current= api_result.json()
        print ('res ', current)
        country = current['location']['country']
        city = current['location']['name']
        condition = current['current']['weather_descriptions']
        temperature_c = current['current']['temperature']
        humidity = current['current']['humidity']
        wind_mph = current['current']['wind_speed']

        response = """It is currently {} in {} at the moment. The temperature is {} degrees, the humidity is {}% and the wind speed is {} mph.""".format(condition, city, temperature_c, humidity, wind_mph)

        dispatcher.utter_message(response)
        return [SlotSet('location',loc)]

gurtajs4 avatar Jan 03 '20 11:01 gurtajs4

Yeah please check, i just tested few minutes ago, with Free Account. It only support http request

gurtajs4 avatar Jan 03 '20 12:01 gurtajs4

Hi gurtajs4

Works well, although as I installed it from scratch I had to change the config file as I was getting some errors.

jmourato avatar Jan 17 '20 16:01 jmourato