2019-08-01 10:37:22 +00:00
|
|
|
Scheduling Tasks
|
2019-07-15 12:37:18 +00:00
|
|
|
================
|
|
|
|
|
2019-08-01 10:37:22 +00:00
|
|
|
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.
|
2019-07-15 12:37:18 +00:00
|
|
|
|
2019-08-01 10:37:22 +00:00
|
|
|
Since there's no built-in task scheduler in Pyrogram, this page will only show examples on how to integrate Pyrogram
|
|
|
|
with the main Python schedule libraries such as ``schedule`` and ``apscheduler``. For more detailed information, you can
|
|
|
|
visit and learn from each library documentation.
|
2019-07-15 12:37:18 +00:00
|
|
|
|
2020-04-01 18:08:46 +00:00
|
|
|
.. contents:: Contents
|
|
|
|
:backlinks: none
|
|
|
|
:local:
|
|
|
|
|
|
|
|
-----
|
|
|
|
|
2019-08-01 10:37:22 +00:00
|
|
|
Using ``schedule``
|
|
|
|
------------------
|
|
|
|
|
|
|
|
- Install with ``pip3 install schedule``
|
|
|
|
- Documentation: https://schedule.readthedocs.io
|
2019-07-15 12:37:18 +00:00
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
import time
|
2019-08-01 10:37:22 +00:00
|
|
|
|
2019-07-15 12:37:18 +00:00
|
|
|
import schedule
|
|
|
|
|
2019-08-01 10:37:22 +00:00
|
|
|
from pyrogram import Client
|
|
|
|
|
|
|
|
app = Client("my_account")
|
|
|
|
|
2019-07-15 12:37:18 +00:00
|
|
|
|
|
|
|
def job():
|
|
|
|
app.send_message("me", "Hi!")
|
|
|
|
|
2019-08-01 10:37:22 +00:00
|
|
|
|
2019-08-01 07:11:29 +00:00
|
|
|
schedule.every(3).seconds.do(job)
|
2019-07-15 12:37:18 +00:00
|
|
|
|
|
|
|
with app:
|
|
|
|
while True:
|
|
|
|
schedule.run_pending()
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
|
2019-08-01 10:37:22 +00:00
|
|
|
|
|
|
|
Using ``apscheduler``
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
- Install with ``pip3 install apscheduler``
|
|
|
|
- Documentation: https://apscheduler.readthedocs.io
|
2019-07-15 12:37:18 +00:00
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
from apscheduler.schedulers.background import BackgroundScheduler
|
|
|
|
|
2019-08-01 10:37:22 +00:00
|
|
|
from pyrogram import Client
|
|
|
|
|
|
|
|
app = Client("my_account")
|
|
|
|
|
2019-07-15 12:37:18 +00:00
|
|
|
|
|
|
|
def job():
|
|
|
|
app.send_message("me", "Hi!")
|
|
|
|
|
|
|
|
|
|
|
|
scheduler = BackgroundScheduler()
|
2019-08-01 10:37:22 +00:00
|
|
|
scheduler.add_job(job, "interval", seconds=3)
|
2019-07-15 12:37:18 +00:00
|
|
|
|
|
|
|
scheduler.start()
|
|
|
|
app.run()
|
|
|
|
|
2019-08-01 10:37:22 +00:00
|
|
|
``apscheduler`` does also support async code, here's an example with
|
|
|
|
`Pyrogram Asyncio <https://docs.pyrogram.org/intro/install.html#asynchronous>`_:
|
2019-07-15 12:37:18 +00:00
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
|
|
|
|
2019-08-01 10:37:22 +00:00
|
|
|
from pyrogram import Client
|
|
|
|
|
|
|
|
app = Client("my_account")
|
|
|
|
|
2019-07-15 12:37:18 +00:00
|
|
|
|
|
|
|
async def job():
|
|
|
|
await app.send_message("me", "Hi!")
|
|
|
|
|
|
|
|
|
|
|
|
scheduler = AsyncIOScheduler()
|
2019-08-01 10:37:22 +00:00
|
|
|
scheduler.add_job(job, "interval", seconds=3)
|
2019-07-15 12:37:18 +00:00
|
|
|
|
|
|
|
scheduler.start()
|
|
|
|
app.run()
|
|
|
|
|