Merge pull request #283 from MrNaif2018/develop
Add section to docs about scheduling
This commit is contained in:
commit
ce8d9a5005
4
.gitignore
vendored
4
.gitignore
vendored
@ -11,6 +11,9 @@ pyrogram/api/all.py
|
|||||||
# PyCharm stuff
|
# PyCharm stuff
|
||||||
.idea/
|
.idea/
|
||||||
|
|
||||||
|
# VS Code
|
||||||
|
.vscode/
|
||||||
|
|
||||||
# Byte-compiled / optimized / DLL files
|
# Byte-compiled / optimized / DLL files
|
||||||
__pycache__/
|
__pycache__/
|
||||||
*.py[cod]
|
*.py[cod]
|
||||||
@ -78,6 +81,7 @@ instance/
|
|||||||
|
|
||||||
# Sphinx documentation
|
# Sphinx documentation
|
||||||
docs/_build/
|
docs/_build/
|
||||||
|
docs/source/_build
|
||||||
|
|
||||||
# PyBuilder
|
# PyBuilder
|
||||||
target/
|
target/
|
||||||
|
@ -40,6 +40,7 @@ Welcome to Pyrogram
|
|||||||
topics/more-on-updates
|
topics/more-on-updates
|
||||||
topics/config-file
|
topics/config-file
|
||||||
topics/smart-plugins
|
topics/smart-plugins
|
||||||
|
topics/scheduling
|
||||||
topics/auto-auth
|
topics/auto-auth
|
||||||
topics/session-settings
|
topics/session-settings
|
||||||
topics/tgcrypto
|
topics/tgcrypto
|
||||||
|
87
docs/source/topics/scheduling.rst
Normal file
87
docs/source/topics/scheduling.rst
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
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.
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
Using ``schedule``
|
||||||
|
------------------
|
||||||
|
|
||||||
|
- Install with ``pip3 install schedule``
|
||||||
|
- Documentation: https://schedule.readthedocs.io
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
import time
|
||||||
|
|
||||||
|
import schedule
|
||||||
|
|
||||||
|
from pyrogram import Client
|
||||||
|
|
||||||
|
app = Client("my_account")
|
||||||
|
|
||||||
|
|
||||||
|
def job():
|
||||||
|
app.send_message("me", "Hi!")
|
||||||
|
|
||||||
|
|
||||||
|
schedule.every(3).seconds.do(job)
|
||||||
|
|
||||||
|
with app:
|
||||||
|
while True:
|
||||||
|
schedule.run_pending()
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Using ``apscheduler``
|
||||||
|
---------------------
|
||||||
|
|
||||||
|
- Install with ``pip3 install apscheduler``
|
||||||
|
- Documentation: https://apscheduler.readthedocs.io
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
from apscheduler.schedulers.background import BackgroundScheduler
|
||||||
|
|
||||||
|
from pyrogram import Client
|
||||||
|
|
||||||
|
app = Client("my_account")
|
||||||
|
|
||||||
|
|
||||||
|
def job():
|
||||||
|
app.send_message("me", "Hi!")
|
||||||
|
|
||||||
|
|
||||||
|
scheduler = BackgroundScheduler()
|
||||||
|
scheduler.add_job(job, "interval", seconds=3)
|
||||||
|
|
||||||
|
scheduler.start()
|
||||||
|
app.run()
|
||||||
|
|
||||||
|
``apscheduler`` does also support async code, here's an example with
|
||||||
|
`Pyrogram Asyncio <https://docs.pyrogram.org/intro/install.html#asynchronous>`_:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
||||||
|
|
||||||
|
from pyrogram import Client
|
||||||
|
|
||||||
|
app = Client("my_account")
|
||||||
|
|
||||||
|
|
||||||
|
async def job():
|
||||||
|
await app.send_message("me", "Hi!")
|
||||||
|
|
||||||
|
|
||||||
|
scheduler = AsyncIOScheduler()
|
||||||
|
scheduler.add_job(job, "interval", seconds=3)
|
||||||
|
|
||||||
|
scheduler.start()
|
||||||
|
app.run()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user