NameError in twilio API Python

Viewed 38

im trying to run my code but it show "NameError: name 'message' is not defined " but "message name is defined as shown in code" at ==> if init is True:


I'm just started api in python,

 mport requests
from twilio.rest import Client

weather_id = []

API_KEY = "example"
account_sid = "AC123abc"
auth_token = "example"
client = Client(account_sid, auth_token)

para = {
    "lat": 27.677904,
    "lon": 78.230310,
    "appid": API_KEY,
    "exclude": "current, minutely, daily",
}

responced = requests.get(url="https://api.openweathermap.org/data/2.5/onecall", params=para)
responced.raise_for_status()
weather_data = responced.json()
holly_data = responced.json()["hourly"][:12]

for data in holly_data:
    weather_res = data["weather"][0]["id"]
    weather_id.append(weather_res)

init = False

for ids in weather_id:
    if ids < 700:
        init = True

if init is True:
    message = client.messages.create(body="Weather Alert  by S.P \n To you have a clear day \n Enjoy sir A.K.P \n "
                                          "Thanks YOu",
                                     from_="+19853042683",
                                     to="+919354932229"
                                     )
print(message.sid)
1 Answers

You need to set your init=False to init=True or add something like a

try:
   print(message.sid)
except Exception as e:
   print('Not initialized. Error: ', str(e))

so your script won't downright crash.

Also, never post your api_keys when you are a posting question.

Related