SAL
SAL

Reputation: 545

Can't retrieve user_id of replied message python-telegram-bot

I'm trying to make a simple bot that mutes users but I'm having some issues with retrieving the user id of a replied message.

I can't access to user_id because the library is using the from key like:

def mute_user(update, context):
    bot = context.bot
    print(update.message.reply_to_message.from.id)

I cannot use the "from" key because it is reserved so it gives me an error.

If i remove the .from.id, the output will be like this:

[...] 'from': {'is_bot': False, 'username': '*****', 'first_name': '*****', 'id': 150*****}}

How can I access to that id?

Upvotes: 0

Views: 864

Answers (2)

SAL
SAL

Reputation: 545

I resolved this problem reading the python-telegram-bot documentation

https://python-telegram-bot.readthedocs.io/en/stable/telegram.message.html?highlight=telegram.Message#telegram.Message

I used update.message.reply_to_message.from_user instead of update.message.reply_to_message.from

Upvotes: 2

Robin Sage
Robin Sage

Reputation: 959

Not sure if this is the answer to what you're looking for, but since I see you are using dictionaries, most likely nested dictionaries, here is a sample on how to get keys.

user_dict = {
    'user1':{
        'is_bot': False,
        'first_name': "Bob",
        'username': '******',
        'id': '150'
    },
    'user2':{
        'is_bot': False,
        'first_name': "Joe",
        'username': '******',
        'id': '250'
    }
}

# ******** Search Record *******
usrnm = input("Enter username: ")
usr_id = input("Enter usrr id: ")

for usr in user_dict:
    u = user_dict[usr].get("Username")
    p = user_dict[usr].get("id")
    if usrnm != u and usr_id != p:
        continue
    else:
        print("found!")

Upvotes: -1

Related Questions