I'm new to coding with Python and I'm trying to create a Telegram reminder bot for my Duolingo practice. I used ChatGPT to help me with the code, but I can't seem to fix an error I'm encountering. I'm running my code on Visual Studio Code on my MacBook, and I receive the following error:
There is no current event loop. loop = asyncio.get_event_loop()
Here is the general code I'm using (note: I have removed the API_TOKEN and CHAT_ID for privacy):
import logging
import schedule
import time
import asyncio # <-- Make sure to import asyncio
from telegram import Bot
from telegram.ext import Application, CommandHandler
# Enable logging
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO
)
logger = logging.getLogger(__name__)
# Your bot's API token
API_TOKEN = 'YOUR_API_TOKEN'
# Your boyfriend's chat ID (replace with his actual chat ID)
CHAT_ID = 'YOUR_CHAT_ID'
async def start(update, context):
"""Send a message when the command /start is issued."""
await update.message.reply_text('Hi! I will remind you to do your Duolingo.')
async def remind_duolingo():
"""Send a reminder message."""
bot = Bot(API_TOKEN)
await bot.send_message(chat_id=CHAT_ID, text='Hey, don’t forget to do your Duolingo lesson today!')
async def main():
"""Start the bot."""
application = Application.builder().token(API_TOKEN).build()
# Add command handler to start the bot
application.add_handler(CommandHandler("start", start))
# Start polling for updates
await application.run_polling()
if __name__ == '__main__':
# Schedule the reminder every day at a specific time
schedule.every().day.at("10:00").do(remind_duolingo) # Set your preferred time
# Create an event loop explicitly and run the main function in it
loop = asyncio.get_event_loop()
loop.create_task(main()) # Schedule the main function to run in the event loop
# Run the scheduler in a loop
while True:
schedule.run_pending()
time.sleep(1)