pyrogram/docs/source/topics/scheduling.rst

66 lines
1.4 KiB
ReStructuredText
Raw Normal View History

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
2020-11-27 18:05:58 +00:00
This page will show examples on how to integrate Pyrogram with ``apscheduler`` in both asynchronous and
non-asynchronous contexts. For more detailed information, you can visit and learn from the library documentation.
2019-07-15 12:37:18 +00:00
2020-04-01 18:08:46 +00:00
.. contents:: Contents
:backlinks: none
:depth: 1
2020-04-01 18:08:46 +00:00
:local:
-----
2022-01-07 09:18:51 +00:00
Using apscheduler
-----------------
2019-08-01 10:37:22 +00:00
- Install with ``pip3 install apscheduler``
- Documentation: https://apscheduler.readthedocs.io
2019-07-15 12:37:18 +00:00
2020-11-27 18:05:58 +00:00
Asynchronously
^^^^^^^^^^^^^^
2019-07-15 12:37:18 +00:00
.. code-block:: python
2020-11-27 18:05:58 +00:00
from apscheduler.schedulers.asyncio import AsyncIOScheduler
2019-07-15 12:37:18 +00:00
2019-08-01 10:37:22 +00:00
from pyrogram import Client
app = Client("my_account")
2019-07-15 12:37:18 +00:00
2020-11-27 18:05:58 +00:00
async def job():
await app.send_message("me", "Hi!")
2019-07-15 12:37:18 +00:00
2020-11-27 18:05:58 +00:00
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()
2020-11-27 18:05:58 +00:00
Non-Asynchronously
^^^^^^^^^^^^^^^^^^
2019-07-15 12:37:18 +00:00
.. code-block:: python
2020-11-27 18:05:58 +00:00
from apscheduler.schedulers.background import BackgroundScheduler
2019-07-15 12:37:18 +00:00
2019-08-01 10:37:22 +00:00
from pyrogram import Client
app = Client("my_account")
2019-07-15 12:37:18 +00:00
2020-11-27 18:05:58 +00:00
def job():
app.send_message("me", "Hi!")
2019-07-15 12:37:18 +00:00
2020-11-27 18:05:58 +00:00
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()