Skip to content

Commit

Permalink
Merge pull request #46 from ErhoSen/extract_user_data_made_easy
Browse files Browse the repository at this point in the history
Use special property from python-telegram-bot, instead self-written solution
  • Loading branch information
shamaevnn authored Aug 23, 2022
2 parents 7c97790 + 94e97e2 commit 69c4e46
Showing 1 changed file with 2 additions and 13 deletions.
15 changes: 2 additions & 13 deletions tgbot/handlers/utils/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,7 @@ def command_func(update, context, *args, **kwargs):

def extract_user_data_from_update(update: Update) -> Dict:
""" python-telegram-bot's Update instance --> User info """
if update.message is not None:
user = update.message.from_user.to_dict()
elif update.inline_query is not None:
user = update.inline_query.from_user.to_dict()
elif update.chosen_inline_result is not None:
user = update.chosen_inline_result.from_user.to_dict()
elif update.callback_query is not None and update.callback_query.from_user is not None:
user = update.callback_query.from_user.to_dict()
elif update.callback_query is not None and update.callback_query.message is not None:
user = update.callback_query.message.chat.to_dict()
else:
raise Exception(f"Can't extract user data from update: {update}")
user = update.effective_user.to_dict()

return dict(
user_id=user["id"],
Expand All @@ -38,4 +27,4 @@ def extract_user_data_from_update(update: Update) -> Dict:
for k in ["username", "first_name", "last_name", "language_code"]
if k in user and user[k] is not None
},
)
)

0 comments on commit 69c4e46

Please sign in to comment.