Hello everyone. How can I catch a session termination event?
I can except for ConnectionError or AuthKeyUnregisteredError, but they work only in 50% of cases when my session is terminated.
If they don't, I try to use .is_connected() and .is_user_authorized(), buy they are showing both True after termination, and if I try to do .get_me() (or actually any client-related method) it just hangs forever. No error, no response.
Like which part of my code should I implement this? Sorry I don't really know what catch_up is or how it works. Can you explain in greater detail?
Читать полностью…This is the current code I have. Is this the most optimized/right way of writing the code? Previously I was not using asyncio (just using client.start) but a member here recommended me to try it out. But the result is the same, still seeing delays in receiving message.
Читать полностью…
async def monitor_messages():
async with TelegramClient("session1", API_ID, API_HASH) as client:
# Define the event handler
@client.on(events.NewMessage(chats=SOURCE_CHANNEL))
async def handler(event):
if "Market Cap" in event.message.message:
try:
# Extract token contract address
contract_address = re.search(CONTRACT_REGEX, event.message.message).group(0)
# Construct and send the /buy command to the trading bot
command = f"/buy {contract_address} {BUY_AMOUNT}"
await client.send_message(GMGN_BOT_USERNAME, command)
except Exception as e:
logging.error(f"Error: {e}")
await client.run_until_disconnected()
if __name__ == "__main__":
asyncio.run(monitor_messages())
@disk6969 Hey sir. If you can recall, I have a similar use case over here where I need to detect new messages from a channel in real-time. You've mentioned switching to iter_messages is an option but has restrictive limits (i.e. unrealistic to be polling every second), and that using catch_up might be an option too. From my understanding, catch_up is intended for scenarios where the client might disconnect and you might want to process missed messages. But my purpose is focused on monitoring new incoming messages in real-time only. With that said, should I still try iter_message with a polling frequency of 1 second, or is that totally not recommended?
Читать полностью…Thanks, but i fix it in this way:
# media = message.mediaЧитать полностью…
# self.obj = message
if isinstance(media, MessageMediaPhoto):
self.downloadMedia(f"P{self.obj.id}.jpg")
self.write(f"[ PHOTO ] - ({self.obj.id})")
Hey Juan Carlos Cautter, don't do that again (offtopic non-English) or I will ban you. You have been warned.
Читать полностью…You don't 'need' to, unless it's imperative you create custom filename.. then prefer message.file.ext and message.file.mime_type and message.file.media.id
Читать полностью…Can't we send messages in broadcast to users accepted by bot in chat join request?
Читать полностью…MessageDeleted.Event
doesn't contain any reference to the topic ID, only group ID, and topic msg IDs are not unique
there's no way to know from which topic a delete event originates from?
Is there any way to detect media type?
This code always ends with an error
if self.obj.media.photo:
self.downloadMedia(f"P{self.obj.id}.jpg")
self.write("[ PHOTO ] - (self.obj.id)")
elif self.obj.media.voice:
self.downloadMedia(f"A{self.obj.id}.mp3")
self.write("[ VOICE ] - (self.obj.id)")
elif self.obj.media.video:
self.downloadMedia(f"V{self.obj.id}.mp4")
self.write("[ VIDEO ] - (self.obj.id)")
elif self.obj.media.document:
self.downloadMedia(f"D{self.obj.id}.mp4")
self.write("[ DOCUMENT ] - (self.obj.id)")
else:
self.downloadMedia(f"-{self.obj.id}")
self.write("[ FILE ] - (self.obj.id)")
'MessageMediaDocument' object has no attribute 'photo'Читать полностью…
It isn't solely for that. mainly when catch_up=True isn't passed to TelegramClient
Читать полностью…you make a pull request if you want something added, providing the timeit benchmark code of old and new
but features aren't added in V1 telethon, but may be merged for V2
thanks bro, i just wanted to switch completely from pyro to tele thats why i asked,
Читать полностью…