Querying rest api

I’m trying to use open weather map to make a weather bot. I’m using the code from the demo and everything is fine except actions. I have slot location but i don’t know how to replace the q in the url with the slot. It looks like this:

	    	loc = tracker.get_slot('location')
    		current = requests.get('http://api.openweathermap.org/data/2.5/weather?q&appid=cc24cc7c42f06680e1bec75abb70ca0c').json()

My question is how do i make the q value be the slot value without using Apixu?

Hi @Dnnsmoyo,

q will be the location

In your code you have used

loc

to get location. you can use that to replace q.

-Murali

How do i replace it in the url? Ive tried q=loc but its not working Can you show me an example?

response = requests.get(“http://api.openweathermap.org/data/2.5/weather?q=“+loc+”&appid=”);

Thanks let me try that

Now getting this error TypeError: can only concatenate str (not "NoneType") to str

Try str() in-built function

+str(loc)+

or go for this approach

response = requests.get(“http://api.openweathermap.org/data/2.5/weather?q={}&appid=”.format(loc))

Thanks

Okay Sir, I fixed your problem This is the Json format you got from api.openweather.org. It key and value pairs does not matched your previous solution. In my understanding the Apixu.weather.api is different than api.openweather.org

This is the solution and working

Hope this helps!!

P.S Haha where is the post? :smiley:

Here it is: Tried to solve it myself lol

from rasa_sdk import Action
from rasa_sdk.events import SlotSet
import requests

class ActionWeather(Action):
	def name(self):
		return 'action_weather'
		
	def run(self, dispatcher, tracker, domain):
		api_key = 'cc24cc7c42f06680e1bec75abb70ca0c' #your apixu key
		
		loc = tracker.get_slot('location')
		current = requests.get('http://api.openweathermap.org/data/2.5/weather?q={}&appid=cc24cc7c42f06680e1bec75abb70ca0c'.format(loc)).json()
		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)]

Thanks let me implement your solution

2 Likes