|
1 | | -Scheduling tasks |
| 1 | +Scheduling Tasks |
2 | 2 | ================ |
3 | 3 |
|
4 | | -Pyrogram itself as Telegram MTProto API Framework contains only stuff |
5 | | -related to Telegram. Scheduling is out of it's scope. |
| 4 | +Scheduling tasks means executing one or more functions periodically at pre-defined intervals or after a delay. This is |
| 5 | +useful, for example, to send recurring messages to specific chats or users. |
6 | 6 |
|
7 | | -But it is easy to integrate pyrogram with your favourite scheduler. |
| 7 | +Since there's no built-in task scheduler in Pyrogram, this page will only show examples on how to integrate Pyrogram |
| 8 | +with the main Python schedule libraries such as ``schedule`` and ``apscheduler``. For more detailed information, you can |
| 9 | +visit and learn from each library documentation. |
8 | 10 |
|
9 | | -schedule |
10 | | --------- |
| 11 | +Using ``schedule`` |
| 12 | +------------------ |
| 13 | + |
| 14 | +- Install with ``pip3 install schedule`` |
| 15 | +- Documentation: https://schedule.readthedocs.io |
11 | 16 |
|
12 | 17 | .. code-block:: python |
13 | 18 |
|
14 | 19 | import time |
| 20 | +
|
15 | 21 | import schedule |
16 | 22 |
|
| 23 | + from pyrogram import Client |
| 24 | +
|
| 25 | + app = Client("my_account") |
| 26 | +
|
17 | 27 |
|
18 | 28 | def job(): |
19 | 29 | app.send_message("me", "Hi!") |
20 | 30 |
|
| 31 | +
|
21 | 32 | schedule.every(3).seconds.do(job) |
22 | 33 |
|
23 | 34 | with app: |
24 | 35 | while True: |
25 | 36 | schedule.run_pending() |
26 | 37 | time.sleep(1) |
27 | 38 |
|
28 | | -Note that schedule is not suitable for async version of pyrogram. |
29 | | -For more information read `library <https://schedule.readthedocs.io/>`_ docs. |
30 | 39 |
|
31 | | -apscheduler |
32 | | ------------ |
| 40 | +
|
| 41 | +Using ``apscheduler`` |
| 42 | +--------------------- |
| 43 | + |
| 44 | +- Install with ``pip3 install apscheduler`` |
| 45 | +- Documentation: https://apscheduler.readthedocs.io |
33 | 46 |
|
34 | 47 | .. code-block:: python |
35 | 48 |
|
36 | | - import time |
37 | 49 | from apscheduler.schedulers.background import BackgroundScheduler |
38 | 50 |
|
| 51 | + from pyrogram import Client |
| 52 | +
|
| 53 | + app = Client("my_account") |
| 54 | +
|
39 | 55 |
|
40 | 56 | def job(): |
41 | 57 | app.send_message("me", "Hi!") |
42 | 58 |
|
43 | 59 |
|
44 | 60 | scheduler = BackgroundScheduler() |
45 | | - scheduler.add_job(job, 'interval', seconds=3) |
| 61 | + scheduler.add_job(job, "interval", seconds=3) |
46 | 62 |
|
47 | 63 | scheduler.start() |
48 | 64 | app.run() |
49 | 65 |
|
50 | | -Apscheduler supports async version of pyrogram too, here is async example: |
| 66 | +``apscheduler`` does also support async code, here's an example with |
| 67 | +`Pyrogram Asyncio <https://docs.pyrogram.org/intro/install.html#asynchronous>`_: |
51 | 68 |
|
52 | 69 | .. code-block:: python |
53 | 70 |
|
54 | 71 | from apscheduler.schedulers.asyncio import AsyncIOScheduler |
55 | 72 |
|
| 73 | + from pyrogram import Client |
| 74 | +
|
| 75 | + app = Client("my_account") |
| 76 | +
|
56 | 77 |
|
57 | 78 | async def job(): |
58 | 79 | await app.send_message("me", "Hi!") |
59 | 80 |
|
60 | 81 |
|
61 | 82 | scheduler = AsyncIOScheduler() |
62 | | - scheduler.add_job(job, 'interval', seconds=3) |
| 83 | + scheduler.add_job(job, "interval", seconds=3) |
63 | 84 |
|
64 | 85 | scheduler.start() |
65 | 86 | app.run() |
66 | 87 |
|
67 | | -For more information read `library <https://apscheduler.readthedocs.io/>`_ docs. |
|
0 commit comments