Hey folks! I’m stuck trying to set up a bot that sends messages every hour. I’ve searched everywhere and tried a bunch of solutions but can’t get the job queue to work. Here’s what I’ve got so far:
from telegram.ext import Updater
bot_updater = Updater('my_secret_token', use_context=True)
scheduler = bot_updater.job_queue
def hourly_reminder(context):
context.bot.send_message(chat_id='my_group_chat', text='Time for an hourly check-in!')
hourly_job = scheduler.run_repeating(hourly_reminder, interval=3600)
bot_updater.start_polling()
bot_updater.idle()
When I run this, I get an error indicating that new futures cannot be scheduled after the interpreter has shut down. Any ideas on what might be going wrong? I’m new to this so any help would be greatly appreciated. Thanks!
I’ve been working with Python Telegram bots for a while now, and I can share some insights that might help you out, Emma. The issue you’re facing is likely related to how the bot is handling its main loop and event scheduling.
One approach that’s worked well for me is using the asyncio
library along with the aioschedule
package. This combination allows for more efficient handling of asynchronous tasks and scheduling. Here’s a basic structure you could try:
import asyncio
import aioschedule
from telegram.ext import Application
async def send_hourly_message(bot, chat_id):
await bot.send_message(chat_id=chat_id, text='Time for an hourly check-in!')
async def scheduler():
aioschedule.every().hour.do(send_hourly_message, bot, 'your_chat_id')
while True:
await aioschedule.run_pending()
await asyncio.sleep(1)
async def main():
application = Application.builder().token('YOUR_BOT_TOKEN').build()
asyncio.create_task(scheduler())
await application.run_polling()
if __name__ == '__main__':
asyncio.run(main())
This setup should avoid the shutdown errors you’re experiencing and provide a more robust solution for your hourly messaging needs. Remember to replace ‘YOUR_BOT_TOKEN’ and ‘your_chat_id’ with your actual values. Let me know if you need any clarification on this approach!
hey emma, i’ve had that issue too. try adding a try-except block around ur main code. it might catch any unexpected errors. also, make sure ur bot token is correct and the chat ID exists. sometimes small typos can cause weird errors. good luck with ur bot!
I’ve encountered a similar issue before. The problem likely stems from how you’re handling the bot’s lifecycle. Try wrapping your main code in a function and calling it from within a ‘if name == “main”:’ block. This ensures your script runs properly as a standalone program.
Also, consider using the ‘schedule’ library instead of the job queue. It’s more straightforward for time-based tasks. Here’s a quick example:
import schedule
import time
from telegram.ext import Updater
def send_message():
bot.send_message(chat_id='your_chat_id', text='Hourly check-in!')
def main():
global bot
updater = Updater('your_token', use_context=True)
bot = updater.bot
schedule.every().hour.do(send_message)
while True:
schedule.run_pending()
time.sleep(1)
if __name__ == '__main__':
main()
This approach should resolve your shutdown error and keep your bot running smoothly.