Hi I’m pretty new to python. I try to make a chatbot with rasa for personal use. Would like to add weather api now. I use weatherstack.com. If I use their example this works, but if I adjust it to my own liking I always end up with the same error.
condition = api_response['current']['weather_descriptions'] TypeError: list indices must be integers or slices, not str
This is the code I am using.
class Actionweatherapi(Action): def name(self): return “actions.weatherapi”
def run(self, dispatcher, tracker, domain):
import requests
location = tracker.get_slot('location')
if location == 'None':
location = 'fetch:ip'
api_result = requests.get('http://api.weatherstack.com/current?access_key=0000000&query={}'.format(location))
api_response = api_result.json()
country = api_response['request']['query']
condition = api_response['current']['weather_descriptions']
temperature_c = api_response['current']['temperature']
humidity = api_response['current']['humidity']
wind_mph = api_response['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, country, temperature_c, humidity, wind_mph)
dispatcher.utter_message(response)
return [AllSlotsReset()]
I have also reviewed the printf-style String Formatting on the python website, but I cannot figure out how to get it working.
If I change action script to how I would like it, I get this as a response in the actions server
Exception occurred while handling uri: ‘http://localhost:5055/webhook’
Traceback (most recent call last):
File “/home/bjorn/venv/lib/python3.6/site-packages/sanic/app.py”, line 976, in handle_request
response = await response
File “/home/bjorn/venv/lib/python3.6/site-packages/rasa_sdk/endpoint.py”, line 102, in webhook
result = await executor.run(action_call)
File “/home/bjorn/venv/lib/python3.6/site-packages/rasa_sdk/executor.py”, line 385, in run
events = await action(dispatcher, tracker, domain)
File “/home/bjorn/venv/lib/python3.6/site-packages/rasa_sdk/interfaces.py”, line 254, in run
raise NotImplementedError(“An action must implement its run method”)
NotImplementedError: An action must implement its run method
I know there is something wrong in the script how I handle the json data output, but don’t know how to get it done.
Everything is working fine, It just had indentation error, If you are using vscode, you can download Python from the market place and format the document easily. Your code is working like a charm. I indented properly and uploaded it here.
Thank you Murali you really helped me with this. It works perfectly now, I had been working on this for 2 days and did not understand what was wrong. Something so small takes me so much time :-D. I am using Atom, but will consider installing vcode. You made me day.