opentele icon indicating copy to clipboard operation
opentele copied to clipboard

Interception of updates from the client does not work

Open TopNowchik opened this issue 3 years ago • 3 comments

I plan to use event interception (namely messages) from the client, but this does not work. Here is the code:

import asyncio

from opentele.td import TDesktop
from opentele.tl.telethon import UseCurrentSession
from telethon.events import NewMessage


async def handler(event):
    print(type(event))


async def main():
    tdata_folder = "path_to_tdata"
    tdesktop = TDesktop(tdata_folder)
    client = await tdesktop.ToTelethon("name_of_session", flag=UseCurrentSession)
    await client.connect()
    client.add_event_handler(handler, NewMessage(outgoing=True))
    await client.run_until_disconnected()


asyncio.run(main())

When I use the "regular" telethon, the outgoing message handler works.

What do you think about this?

TopNowchik avatar Apr 12 '22 13:04 TopNowchik

I also noticed that events are not captured when flag=UseCurrentSession, but are captured when flag=CreateNewSession.

TopNowchik avatar Apr 12 '22 14:04 TopNowchik

I plan to use event interception (namely messages) from the client, but this does not work. Here is the code:

import asyncio

from opentele.td import TDesktop
from opentele.tl.telethon import UseCurrentSession
from telethon.events import NewMessage


async def handler(event):
    print(type(event))


async def main():
    tdata_folder = "path_to_tdata"
    tdesktop = TDesktop(tdata_folder)
    client = await tdesktop.ToTelethon("name_of_session", flag=UseCurrentSession)
    await client.connect()
    client.add_event_handler(handler, NewMessage(outgoing=True))
    await client.run_until_disconnected()


asyncio.run(main())

When I use the "regular" telethon, the outgoing message handler works.

What do you think about this?

Thanks for reporting the bug, it seems like I messed up something in telethon internal.

For the time being, what you can do is convert tdata to .session file first, then load the .session file normally with telethon.

thedemons avatar Apr 13 '22 13:04 thedemons

I tried your method and this is what I found out:

If you create a session file with the UseCurrentSession flag, then the error remains (events of outgoing messages are ignored). The problem remains even if you use the teleton client, and not yours. If you send a message, then after that no events will be processed at all (neither incoming nor outgoing). If you create a session file with the CreateNewSession flag, then there is no error either in your client or in the teleton. I suspect that the problem is in the session file (I did not find a difference in the files described in points 1 and 3) or in the session itself. If you manage to do something, please write here.

This is the code for using the client from teleton:

import asyncio

from telethon import TelegramClient
from telethon.events.newmessage import NewMessage

api_id = 2040
api_hash = "b18441a1ff607e10a989891a5462e627"
device_model = "Desktop"
system_version = "Windows 10"
app_version = "3.4.3 x64"
lang_code = "en"
system_lang_code = "en-US"
lang_pack = "tdesktop"


async def handler(event):
    print(type(event))


async def main():
    telegram_client = TelegramClient("name_of_session", api_id, api_hash, device_model=device_model,
                                     system_version=system_version, app_version=app_version, lang_code=lang_code,
                                     system_lang_code=system_lang_code)
    telegram_client.add_event_handler(handler, NewMessage(outgoing=True))
    await telegram_client.connect()
    await telegram_client.run_until_disconnected()


asyncio.run(main())

TopNowchik avatar Apr 17 '22 15:04 TopNowchik