. < /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("
# Get channel information
channel_info = await client.get_entity(channel)
# Check if the channel is valid
if not channel_info:
print("
return
try:
# Try fetching one message
async for message in client.iter_messages(channel_info, limit=1):
print(f"
return
# Case where there are no messages available (i.e., unable to fetch any)
print(f"
except Exception as e:
print(f"
print(f"Error details: {e}")
except Exception as e:
print(f"
< /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?