Weatherstack.com api

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.

The json response is like this example http://api.weatherstack.com/current?access_key=1c80e8b9fe4fcef4f8d6fd7514a8e9e9&query=New%20York

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('http://api.weatherstack.com/current?access_key=0000000&query={}'.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/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.

Can you share your `actions.py. You can upload it here, I will take look into it.

actions.py (18.8 KB)

Thanks for looking at this for me

@medion87,

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:

actions.py (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: