Created
August 14, 2024 22:59
-
-
Save alexandrebl/9d0b62f3ce76a0a3be09bd8d56ae2246 to your computer and use it in GitHub Desktop.
Telegram Read Message em Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from telethon import TelegramClient, events | |
| from telethon.sync import TelegramClient | |
| # Substitua esses valores pelos seus próprios | |
| api_id = 'APIID' | |
| api_hash = 'API_HASH' | |
| phone_number = 'PHONE NUMBER' | |
| client = TelegramClient('session_name', api_id, api_hash) | |
| session_name = 'my_session' | |
| # Inicializa o cliente Telegram | |
| client = TelegramClient(session_name, api_id, api_hash) | |
| async def main(): | |
| # Conecta e autentica o cliente | |
| await client.start(phone_number) | |
| print("Cliente conectado e autenticado.") | |
| # Escuta todas as mensagens recebidas | |
| @client.on(events.NewMessage()) | |
| async def handler(event): | |
| # Obtém a mensagem | |
| message = event.message | |
| chat = await message.get_chat() | |
| sender = await message.get_sender() | |
| # Exibe informações sobre a mensagem | |
| print(f"De: {sender.id} ({sender.username})") | |
| print(f"Chat: {chat.id} ({chat.username})") | |
| print(f"Mensagem: {message.text}\n") | |
| # Mantém o cliente em execução | |
| print("Escutando novas mensagens...") | |
| await client.run_until_disconnected() | |
| # Executa o cliente | |
| with client: | |
| client.loop.run_until_complete(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment