pythonentitytelegram-apipyrogram

How do I get a phone_number field in enum MessageEntityType in Pyrogram Python?


I want to understand how Pyrogram works. So I tried to get a phone number from the group for educational purposes, similar to what I successfully did with an url.

def main():
    with app:                             
        messages = app.get_chat_history(group_url)
        for message in messages:
            if message.entities:
                for entity in message.entities:
                    if entity.url:
                        print(entity.url)  

main()

But I cannot get a phone number when I do it in a similar way.

#_______________________________________________________________ It looks likely I cannot publish a question here if I do not reply to the AI suggestions. Here they are:

Question assistant suggestions Define your problem

Describe the overall goal of your program. Edu. What is the purpose of extracting phone numbers from messages? Edu. Provide context on why you need these phone numbers. Example Explain the desired outcome more clearly. Example have been provided What should happen when a phone number is found? It should be printed out Add details on what you tried. I've done

Explain why your second code snippet didn't work. I've done What specific behavior did you observe? None Did you check the type of message.entities? Yes Did you examine the structure of entity to see if 'PHONE_NUMBER' is a valid attribute? Yes Include the output of print(entity) from your loop to show the available attributes. I've done Include details about the group_url variable, is it correct? Yes Include error information No error information available

Add any error messages you encountered. No If there are no error messages, state this explicitly. Yes Include debugging logs showing the contents of the message and entity objects at relevant points in your code. It is simple question Logs no need Show the data type of variables. If you are using a debugger, provide relevant screenshots no need #_______________________________________________________________

I have tried this code.

def main():
    with app:
        messages = app.get_chat_history(group_url, limit=100)
        for message in messages:
            if message.entities:
                for entity in message.entities:
                    if entity.type=='MessageEntityType.PHONE_NUMBER':
                        print(message.text)
main()

I expected to get something like a phone number, i.e. 74659000976. I have no error message but I did not get into the "if". Type of variable 'entity' is <enum 'MessageEntityType'> MessageEntityType.PHONE_NUMBER

A fragment of the TG message is as follows

 "text": "73455300000",
    "entities": [
        {
            "_": "MessageEntity",
            "type": "MessageEntityType.PHONE_NUMBER",
            "offset": 0,
            "length": 11
        }
    ],

Solution

  • You're comparing the instance of the MessageEntityType enum with the string representation of the enum member.
    The code should be

    from pyrogram.enums import MessageEntityType
    
    def main():
        with app:
            messages = app.get_chat_history(group_url, limit=100)
            for message in messages:
                if message.entities:
                    for entity in message.entities:
                        if entity.type == MessageEntityType.PHONE_NUMBER:  # Correct comparison
                            print(message.text[entity.offset:entity.offset + entity.length])
    
    main()