Warum fällt die Verbindung von Telegram -Client -Verbindung während der Ausführung in Telethon unerwartet ab?Python

Python-Programme
Anonymous
 Warum fällt die Verbindung von Telegram -Client -Verbindung während der Ausführung in Telethon unerwartet ab?

Post by Anonymous »

Ich benutze die Telethon -Bibliothek, um mit der API von Telegram zu interagieren. Nach dem Aufrufen von tel_channel_access () wird der Client zunächst erfolgreich mit dem Server verbunden. Kurz vor dem Ausführen erwarten Sie jedoch start_10_messages (), der Client ist in der Hauptfunktion weiterhin verbunden. Wenn jedoch start_10_messages () von Scraper.py ausgeführt wird, fällt die Verbindung unerwartet ab. Ich bin mir nicht sicher, wo die Trennung auftritt. Ich brauche Hilfe, um zu verstehen, warum der Client während der Ausführung von start_10_messages () und wie die Verbindung während des gesamten Prozesses aufrechterhalten wird. < /P>
. < /P>
async def main():
await start_client()
. . .
await tel_channel_access()
print(f"Starting...")

from src.scraper import start_10_messages
await start_10_messages()

if __name__ == '__main__':
asyncio.run(main())
< /code>
async def start_client():
await client.start(phone=PHONE_NUMBER)
print("Login Success")
< /code>
async def tel_channel_access():
channels = load_channels()

for channel in channels:
try:
print("🔍 Checking if the channel is accessible...\n")

# Get channel information
channel_info = await client.get_entity(channel)

# Check if the channel is valid
if not channel_info:
print("❌ Could not find the channel.")
return

try:
# Try fetching one message
async for message in client.iter_messages(channel_info, limit=1):
print(f"✅ Access granted to the channel: {channel}")
return

# Case where there are no messages available (i.e., unable to fetch any)
print(f"⚠️ The channel exists, but no messages can be fetched: {channel}")
except Exception as e:
print(f"❌ Unable to access the channel. You may not have permission: {channel}")
print(f"Error details: {e}")

except Exception as e:
print(f"❌ Error occurred while fetching channel information: {e}")
< /code>
async def start_10_messages():
matching_messages = []
for channel in load_channels():
print(f"== Channel name: {channel}")

# Using async for to fetch messages asynchronously
async for message in client.iter_messages(channel, limit=10):
if channel_has_keyword(message.text):
matching_messages.append(message) # Add message object
print(f"{message: 10f} | Message added.")

print(f"End of start_10_messages for channel {channel}")
< /code>
I have tried ensuring the client is connected before and after executing each function. I expected that the client would remain connected throughout the execution, but the connection is lost specifically when running the start_10_messages() function.
What am I missing? How can I ensure the connection remains active during the execution of start_10_messages() in scraper.py?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post