Skip to content

Commit ce8d9a5

Browse files
authored
Merge pull request pyrogram#283 from MrNaif2018/develop
Add section to docs about scheduling
2 parents b207b02 + 6973f58 commit ce8d9a5

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ pyrogram/api/all.py
1111
# PyCharm stuff
1212
.idea/
1313

14+
# VS Code
15+
.vscode/
16+
1417
# Byte-compiled / optimized / DLL files
1518
__pycache__/
1619
*.py[cod]
@@ -78,6 +81,7 @@ instance/
7881

7982
# Sphinx documentation
8083
docs/_build/
84+
docs/source/_build
8185

8286
# PyBuilder
8387
target/

docs/source/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Welcome to Pyrogram
4040
topics/more-on-updates
4141
topics/config-file
4242
topics/smart-plugins
43+
topics/scheduling
4344
topics/auto-auth
4445
topics/session-settings
4546
topics/tgcrypto

docs/source/topics/scheduling.rst

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
Scheduling Tasks
2+
================
3+
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+
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.
10+
11+
Using ``schedule``
12+
------------------
13+
14+
- Install with ``pip3 install schedule``
15+
- Documentation: https://schedule.readthedocs.io
16+
17+
.. code-block:: python
18+
19+
import time
20+
21+
import schedule
22+
23+
from pyrogram import Client
24+
25+
app = Client("my_account")
26+
27+
28+
def job():
29+
app.send_message("me", "Hi!")
30+
31+
32+
schedule.every(3).seconds.do(job)
33+
34+
with app:
35+
while True:
36+
schedule.run_pending()
37+
time.sleep(1)
38+
39+
40+
41+
Using ``apscheduler``
42+
---------------------
43+
44+
- Install with ``pip3 install apscheduler``
45+
- Documentation: https://apscheduler.readthedocs.io
46+
47+
.. code-block:: python
48+
49+
from apscheduler.schedulers.background import BackgroundScheduler
50+
51+
from pyrogram import Client
52+
53+
app = Client("my_account")
54+
55+
56+
def job():
57+
app.send_message("me", "Hi!")
58+
59+
60+
scheduler = BackgroundScheduler()
61+
scheduler.add_job(job, "interval", seconds=3)
62+
63+
scheduler.start()
64+
app.run()
65+
66+
``apscheduler`` does also support async code, here's an example with
67+
`Pyrogram Asyncio <https://docs.pyrogram.org/intro/install.html#asynchronous>`_:
68+
69+
.. code-block:: python
70+
71+
from apscheduler.schedulers.asyncio import AsyncIOScheduler
72+
73+
from pyrogram import Client
74+
75+
app = Client("my_account")
76+
77+
78+
async def job():
79+
await app.send_message("me", "Hi!")
80+
81+
82+
scheduler = AsyncIOScheduler()
83+
scheduler.add_job(job, "interval", seconds=3)
84+
85+
scheduler.start()
86+
app.run()
87+

0 commit comments

Comments
 (0)