Pyrogram user-bot does not output message from telegram, why?

97 views Asked by At

The task of the bot is to track all messages sent by me to any Telegram chat. The bot starts, no error appears, but when sending messages, the bot does not see them and does not display the text of the message.

import os
import asyncio
from pyrogram import Client, filters
from pyrogram.errors import SessionPasswordNeeded, PasswordHashInvalid, PhoneCodeInvalid


async def create_session_telegram():
    nickname = input('Enter a nickname to create a new record:\n')
    number = input('Enter the phone number for registration in the application\n(For example: +79991113322):\n')
    print('Next, you will need api_id and api_hash. To obtain them, visit my.telegram.org\n'
          '     - Log in to your account\n'
          '     - Go to the "API development tools" tab\n'
          '     - Copy the api_id and api_hash')
    api_id = str(input('Enter the api_id:\n'))
    api_hash = str(input('Enter the api_hash:\n'))

    while True:
        client = Client(name=nickname, api_id=api_id, api_hash=api_hash, workdir=f'{os.getcwd()}/session')
        await client.start()

        try:
            send_code_info = await client.send_code(phone_number=number)
            phone_code = input("Enter code from Telegram message:\n")
            await client.sign_in(number, send_code_info.phone_code_hash, phone_code)
            return client
        except SessionPasswordNeeded:
            password = input("Password required (2FA)/n Enter password:/n")
            await client.check_password(password)
            return client
        except PhoneCodeInvalid as e:
            print(f'The wrong code from the message is {e}')
        except PasswordHashInvalid as e:
            print(f'Incorrect password - {e}')
        await client.send_message("me", "Login complete!\n Save session!")
        print('Verification message sent, session created.')

async def choose_account():
    session_dir = f'{os.getcwd()}/session'
    accounts = os.listdir(session_dir)
    if len(accounts) != 0:
        print("Select an account from the list:")
        for i, account in enumerate(accounts, 1):
            print(f"{i}. {account.split('.')[0]}")
        while True:
            try:
                choice = input("Enter the account number (0 to register a new session):")
                choice = int(choice)
                if 1 <= choice <= len(accounts):
                    return Client(f'session/{accounts[choice - 1].split(".")[0]}')
                elif choice == 0:
                    return await create_session_telegram()
                else:
                    print("Wrong number. Try again.")
            except ValueError:
                print("Invalid entry. Try again.")
    else:
        return await create_session_telegram()


async def main():
    app = await choose_account()

    @app.on_message(filters.me)
    async def echo(client, message):
        print(message.text)

    app.run()

if __name__ == '__main__':
    asyncio.run(main())


I tried many ways, but the message never popped up in the Python console How to fix it?

0

There are 0 answers