I have developed a Telegram bot utilizing Python and the Telethon framework. The intended functionality is for it to operate from 10:00 AM to 4:15 PM, from Saturday through Thursday, while remaining inactive all day on Fridays. However, I am facing issues with the bot failing to stop at the designated time of 16:15. Below is a portion of my code:
import logging
from datetime import datetime
import asyncio
import pytz
# Initialize logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Function to get current time in Tehran
def current_tehran_time():
tehran_tz = pytz.timezone('Asia/Tehran')
return datetime.now(tehran_tz)
async def bot_main():
while True:
now = current_tehran_time()
if now.weekday() != 4 and (10 <= now.hour < 16 or (now.hour == 16 and now.minute < 15)):
# Bot operations
await asyncio.sleep(60)
else:
# Stop bot operations
logger.info("Bot is not working now.")
await asyncio.sleep(60)
# Start the bot operation
asyncio.run(bot_main())
Despite my efforts to ensure the bot operates correctly within these time frames, it does not cease as intended. Can anyone assist in troubleshooting this issue? Thank you for any guidance!