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.
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
.
Actually 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
}
],
The problem is solved by @Lewis.
from pyrogram.enums import MessageEntityType
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()