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 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('{}'.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)
    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.

The json response is like this example

I did a great deal thanks to JustinaPetr’s Weatherbot Tutorial but they use ApixuClient but it has been taken over by weatherstack.

Hi @medion87,

Welcome to Rasa Community!

Have you passed your access key. In your access_key variable.

Are you getting any response from the requests you are sending.

api_result = requests.get('{}'.format(location))

api_response = api_result.json()

can you print print("api_response ",api_response) after api_response. share your results

Hey thx for welcoming me, Yes I just changed the key for this post. If I try their example with only response locaction and temperature.

I get this as an example:

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/”, line 976, in handle_request response = await response File “/home/bjorn/venv/lib/python3.6/site-packages/rasa_sdk/”, line 102, in webhook result = await File “/home/bjorn/venv/lib/python3.6/site-packages/rasa_sdk/”, line 385, in run events = await action(dispatcher, tracker, domain) File “/home/bjorn/venv/lib/python3.6/site-packages/rasa_sdk/”, 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.

Can you share your ` You can upload it here, I will take look into it. (18.8 KB)

Thanks for looking at this for me


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. :slightly_smiling_face: (18.9 KB)

1 Like

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.

1 Like

No Problem :grin: