0
votes
import json
from botocore.vendored import requests
#import requests

def weatherfunc(city_name):

    api_key = 'e914e5e16947fe541140de82a88e5888'
    base_url = 'http://api.openweathermap.org/data/2.5/weather?'
    finalurl = base_url + 'appid=' + api_key + '&q=' + city_name

    response = requests.get(finalurl)
    x = response.json()
    y = x['main']
    current_temperature = y['temp']
    current_pressure = y['pressure']
    current_humidiy = y['humidity']
    z = x['weather']
    weather_description = z[0]['description']

    return {
        'current temp': current_temperature,
        'humidity': current_humidiy,
        'pressure': current_pressure,
        'description': weather_description,
        }

 def lambda_handler(event, context):
    city = event['City']
    a = weatherfunc(city)
    return (a)

I want to return weather information to my lex bot, I am coming across error: Response: { "errorMessage": "'City'", "errorType": "KeyError", "stackTrace": [ " File "/var/task/lambda_function.py", line 28, in lambda_handler\n city = event['City']\n" ] }

Plase help me troubleshoot error.

2

2 Answers

0
votes

According to the error message:

  • You call lambda_handler (in lambda_handler)
  • ... that function encountered a KeyError ("errorType": "KeyError")
  • ... at line 28, which is: city = event['City'] (line 28, city = event['City'])

With a 30 second google search on KeyError python doc you will come accross this website documenting KeyError.

The first relevant line in this website reads: Raised when a mapping (dictionary) key is not found in the set of existing keys.

0
votes

Please check the event content passed in request, to fetch city name properly. If u r fetching City from event content of lex... It could be like this - city = event['currentIntent']['slot']['city']. Please refer event content.