I’m new to making bots and need help with a Telegram bot I built. It works fine, but I want it to send messages every few hours when users type a command. I tried using a while loop, but it’s not great because users can’t do anything else while it’s running.
Here’s what I want:
Users can start the scheduled messages with /begin_schedule
Users can stop the scheduled messages with /end_schedule
The bot should still work normally while sending scheduled messages
I’ve looked at other answers, but they don’t seem to work with my code. Here’s a simple version of what I have now:
import asyncio
from telegram import Bot
async def send_scheduled_message(bot, chat_id):
while True:
await bot.send_message(chat_id, 'Scheduled message!')
await asyncio.sleep(3600) # Wait for 1 hour
async def handle_begin_schedule(update, context):
chat_id = update.effective_chat.id
context.bot_data['task'] = asyncio.create_task(send_scheduled_message(context.bot, chat_id))
await update.message.reply_text('Scheduled messages started!')
async def handle_end_schedule(update, context):
if 'task' in context.bot_data:
context.bot_data['task'].cancel()
await update.message.reply_text('Scheduled messages stopped!')
else:
await update.message.reply_text('No scheduled messages running!')
# Add these handlers to your application
Can someone help me fix this code or suggest a better way to do it? Thanks!
i’ve had similar issues. try apscheduler instead of asyncio tasks so your bot can schedule messages without blocking.
set it up in your bot to add and remove jobs when needed. hope that helps!
I’ve tackled this problem before in a project I worked on. Instead of using asyncio or APScheduler, I found that the schedule library works really well for this use case. It’s lightweight and easy to integrate with Telegram bots.
Here’s a basic implementation that might help:
import schedule
import time
import threading
def run_scheduler():
while True:
schedule.run_pending()
time.sleep(1)
def send_scheduled_message(bot, chat_id):
bot.send_message(chat_id, 'Scheduled message!')
async def handle_begin_schedule(update, context):
chat_id = update.effective_chat.id
schedule.every().hour.do(send_scheduled_message, context.bot, chat_id)
await update.message.reply_text('Scheduled messages started!')
async def handle_end_schedule(update, context):
schedule.clear()
await update.message.reply_text('Scheduled messages stopped!')
# Start the scheduler in a separate thread
threading.Thread(target=run_scheduler, daemon=True).start()
This approach keeps your bot responsive while handling scheduled tasks efficiently. Just make sure to install and import the schedule library and you’re good to go.
Hey there! I’ve actually run into this exact problem before when building a Telegram bot for my personal finance tracking. Using APScheduler like ClimbingLion suggested is definitely a solid approach, but I found that integrating it with the python-telegram-bot library can be a bit tricky.
What worked really well for me was using the job queue that’s built into python-telegram-bot. It’s super easy to set up and integrates seamlessly with the rest of your bot code. Here’s a quick example of how you could modify your existing code:
This approach lets you easily manage scheduled tasks without interfering with other bot functionalities. Give it a shot and let me know if you need any help implementing it!