From bb1470e57f90a114f0334588055c772debfde48d Mon Sep 17 00:00:00 2001 From: MrNaif2018 Date: Mon, 15 Jul 2019 15:37:18 +0300 Subject: [PATCH 1/4] Add section to docs about scheduling --- docs/source/index.rst | 1 + docs/source/topics/scheduling.rst | 73 +++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 docs/source/topics/scheduling.rst diff --git a/docs/source/index.rst b/docs/source/index.rst index 5cb9bb2e..b8927657 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -40,6 +40,7 @@ Welcome to Pyrogram topics/more-on-updates topics/config-file topics/smart-plugins + topics/scheduling topics/auto-auth topics/session-settings topics/tgcrypto diff --git a/docs/source/topics/scheduling.rst b/docs/source/topics/scheduling.rst new file mode 100644 index 00000000..70f70215 --- /dev/null +++ b/docs/source/topics/scheduling.rst @@ -0,0 +1,73 @@ +Scheduling tasks +================ + +Pyrogram itself as Telegram MTProto API Framework contains only stuff +related to Telegram. Scheduling is out of it's scope. + +But it is easy to integrate pyrogram with your favourite scheduler. + +schedule +-------- + +Note that schedule is not suitable for async version of pyrogram. + +.. code-block:: python + + import time + import schedule + + + def job(): + app.send_message("me", "Hi!") + + + schedule.every(10).minutes.do(job) + schedule.every().hour.do(job) + schedule.every().day.at("10:30").do(job) + schedule.every(5).to(10).minutes.do(job) + schedule.every().monday.do(job) + schedule.every().wednesday.at("13:15").do(job) + schedule.every().minute.at(":17").do(job) + + with app: + while True: + schedule.run_pending() + time.sleep(1) + + +apscheduler +----------- + +.. code-block:: python + + import time + from apscheduler.schedulers.background import BackgroundScheduler + + + def job(): + app.send_message("me", "Hi!") + + + scheduler = BackgroundScheduler() + scheduler.add_job(job, 'interval', seconds=3) + + scheduler.start() + app.run() + +Apscheduler supports async version of pyrogram too, here is async example: + +.. code-block:: python + + from apscheduler.schedulers.asyncio import AsyncIOScheduler + + + async def job(): + await app.send_message("me", "Hi!") + + + scheduler = AsyncIOScheduler() + scheduler.add_job(job, 'interval', seconds=3) + + scheduler.start() + app.run() + From 85c21308757d21fbb55b610ab7f59b8a8ba1f12a Mon Sep 17 00:00:00 2001 From: MrNaif2018 Date: Mon, 15 Jul 2019 16:56:59 +0300 Subject: [PATCH 2/4] Update .gitignore with vscode and alternate docs build dir --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.gitignore b/.gitignore index 0b1a0699..ce3407dd 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,9 @@ pyrogram/api/all.py # PyCharm stuff .idea/ +# VS Code +.vscode/ + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] @@ -78,6 +81,7 @@ instance/ # Sphinx documentation docs/_build/ +docs/source/_build # PyBuilder target/ From 93a2fed8e662a1cd581065b0c2c50f4778c8b065 Mon Sep 17 00:00:00 2001 From: MrNaif2018 <39452697+MrNaif2018@users.noreply.github.com> Date: Thu, 1 Aug 2019 10:11:29 +0300 Subject: [PATCH 3/4] Improved examples, added links to docs --- docs/source/topics/scheduling.rst | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/docs/source/topics/scheduling.rst b/docs/source/topics/scheduling.rst index 70f70215..1f4e1e01 100644 --- a/docs/source/topics/scheduling.rst +++ b/docs/source/topics/scheduling.rst @@ -9,8 +9,6 @@ But it is easy to integrate pyrogram with your favourite scheduler. schedule -------- -Note that schedule is not suitable for async version of pyrogram. - .. code-block:: python import time @@ -20,20 +18,15 @@ Note that schedule is not suitable for async version of pyrogram. def job(): app.send_message("me", "Hi!") - - schedule.every(10).minutes.do(job) - schedule.every().hour.do(job) - schedule.every().day.at("10:30").do(job) - schedule.every(5).to(10).minutes.do(job) - schedule.every().monday.do(job) - schedule.every().wednesday.at("13:15").do(job) - schedule.every().minute.at(":17").do(job) + schedule.every(3).seconds.do(job) with app: while True: schedule.run_pending() time.sleep(1) +Note that schedule is not suitable for async version of pyrogram. +For more information read `library `_ docs. apscheduler ----------- @@ -71,3 +64,4 @@ Apscheduler supports async version of pyrogram too, here is async example: scheduler.start() app.run() +For more information read `library `_ docs. From 6973f584886bc5d65e6aa5d88a5f98abeb8a287e Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Thu, 1 Aug 2019 12:37:22 +0200 Subject: [PATCH 4/4] Update scheduling.rst --- docs/source/topics/scheduling.rst | 50 +++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/docs/source/topics/scheduling.rst b/docs/source/topics/scheduling.rst index 1f4e1e01..3cb95ec7 100644 --- a/docs/source/topics/scheduling.rst +++ b/docs/source/topics/scheduling.rst @@ -1,23 +1,34 @@ -Scheduling tasks +Scheduling Tasks ================ -Pyrogram itself as Telegram MTProto API Framework contains only stuff -related to Telegram. Scheduling is out of it's scope. +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. -But it is easy to integrate pyrogram with your favourite scheduler. +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. -schedule --------- +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: @@ -25,43 +36,52 @@ schedule schedule.run_pending() time.sleep(1) -Note that schedule is not suitable for async version of pyrogram. -For more information read `library `_ docs. -apscheduler ------------ + +Using ``apscheduler`` +--------------------- + +- Install with ``pip3 install apscheduler`` +- Documentation: https://apscheduler.readthedocs.io .. code-block:: python - import time 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.add_job(job, "interval", seconds=3) scheduler.start() app.run() -Apscheduler supports async version of pyrogram too, here is async example: +``apscheduler`` does also support async code, here's an example with +`Pyrogram Asyncio `_: .. 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.add_job(job, "interval", seconds=3) scheduler.start() app.run() -For more information read `library `_ docs.