Scheduling Tasks¶
Scheduling tasks means executing one or more functions periodically at pre-defined intervals or after a delay. This is useful, for example, to send recurring messages to specific chats or users.
This page shows how to integrate wzgram with apscheduler. For more detail, see the
library’s own documentation.
Using apscheduler¶
Install with
pip3 install apschedulerDocumentation: https://apscheduler.readthedocs.io
Asynchronously¶
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from pyrogram import Client
app = Client("my_account")
async def job():
await app.send_message(chat_id="me", text="Hi!")
scheduler = AsyncIOScheduler()
scheduler.add_job(job, "interval", seconds=3)
scheduler.start()
app.run()
From a background thread¶
BackgroundScheduler runs jobs in threads, and a wzgram method called from a thread has
no running loop to await on. Hand the coroutine to the client’s loop instead:
import asyncio
from apscheduler.schedulers.background import BackgroundScheduler
from pyrogram import Client
app = Client("my_account")
def job():
asyncio.run_coroutine_threadsafe(
app.send_message(chat_id="me", text="Hi!"), app.loop
)
scheduler = BackgroundScheduler()
scheduler.add_job(job, "interval", seconds=3)
scheduler.start()
app.run()
AsyncIOScheduler is the better fit whenever you can use it — it needs none of this. See
Synchronous Usage for the general rule about crossing a thread boundary.
Telegram-side scheduling¶
For a message that should be sent at a fixed time, the server can hold it for you: every
send_* method takes a schedule_date, and no process has to stay running.
from datetime import datetime, timedelta
await app.send_message(
chat_id="me",
text="Sent an hour from now",
schedule_date=datetime.now() + timedelta(hours=1),
)
get_scheduled_messages(),
send_scheduled_messages() and
delete_scheduled_messages() manage what is queued.