I'm developing telegram bot on python (3.7). I'm getting POST updates/messages from users (class telegram.update.Update)
if request.method == "POST":
update = Update.de_json(request.get_json(force=True), bot)
Then I'm storing all the parameters of the incoming "message" in variables. For example, if there is a user status update, I save it this way:
if update and update.my_chat_member and update.my_chat_member.new_chat_member \
and update.my_chat_member.new_chat_member.status:
new_status = update.my_chat_member.new_chat_member.status
else:
new_status = None
It's important to me to save 'None' if this parameter is not provided.
And I need to save about 20 parameters of this "message". Repeating this construction for 20 times for each parameter seems to be inefficient.
I tried to create a checking function to reuse it and to check if parameters exist in one line for each parameter:
def check(f_input):
try:
f_output = f_input
except: # i understand it's too broad exception but as I learned it's fine for such cases
f_output = None
return f_output
new_status = check(update.my_chat_member.new_chat_member.status)
but , if a certain Update doesn't have update.my_chat_member
, the error pops up already on the stage of calling the 'check' function
new_status = check(update.my_chat_member.new_chat_member.status)
>>> 'NoneType' object has no attribute 'new_chat_member'
and not inside the check().
Could someone guide me pls, if there is an intelligent way to save these 20 parameters (or save None), when the set of these parameters changes from message to message?