Skip to main content

Send telegram notifications

Project description

What is this?

<5min setup for telegram notifications about errors, progress, and task-completion. This is an extremely minimal module, with no pip dependencies.

How do I use this?

  1. pip install telepy_notify

  2. 30 sec:

  1. DM your bot to initialize it (important!)

  2. profit

from telepy_notify import Notifier

# 
# basic setup
# 
# NOTE: no errors will be raised, even if someone failed to setup their bot
#       (only warnings will be printed if a token doesnt work)
notify = Notifier(
    # read from file
    token_path="some/file/thats/git/ignored/telegram.token",
    # OR read from ENV var:
    token_env_var="TELEGRAM_TOKEN",
    # OR give the token directly
    token="alkdsfjakjfoirj029294ijfoi24j4-2",
)

# 
# Basic notification
# 
notify.send("Howdy!")
notify.send("Howdy! <b>I'm bold</b>")
notify.send("Howdy! <b>I'm bold</b>\n<code>thing = [1,3,4]</code>")

# 
# get duration and/or error information
# 
with notify.when_done:
    blah_blah_blah = 10
    for blah in range(blah_blah_blah):
        from time import sleep
        sleep(0.1)
    raise Exception(f'''Hello Telegram :)''')
# """
# process took: 1sec, ended at: Sun Aug 20 11:23:38 2023 however it ended with an error: Exception('Hello Telegram :)')
# """

# 
# Progress notifications
# 
#    - gives ETA and other time-info
#    - can limit print-rate (by time passed or percent-progress)
#        e.g. percent_per_print=50 => message me after 50% progress
#        e.g. seconds_per_print=60*30 => message once every 30min
for progress, epoch in notify.progress(range(100), percent_per_print=50, seconds_per_print=60*30):
    index = progress.index
    
    # do stuff
    import random
    accuracy = random.random()
    if accuracy > 0.99:
        notify.send("Gottem 🎉🎉🎉")
    # end do stuff
    
    progress.message = f"recent accuracy: <code>{accuracy}</code>"

# that^ outputs:
# message1:
#     [=================>.................] 50.00% 
#     |  50/100 
#     | remaining: 0sec 
#     | eta: 11:52:48 
#     | elapsed: 0sec 
#     | recent accuracy: 0.5086382690344122
# message2:
#     Done in 1sec at 11:52:49

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

telepy_notify-0.0.2.tar.gz (8.5 MB view details)

Uploaded Source

Built Distribution

telepy_notify-0.0.2-py3-none-any.whl (237.5 kB view details)

Uploaded Python 3

File details

Details for the file telepy_notify-0.0.2.tar.gz.

File metadata

  • Download URL: telepy_notify-0.0.2.tar.gz
  • Upload date:
  • Size: 8.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/6.6.0 pkginfo/1.9.6 requests/2.30.0 requests-toolbelt/1.0.0 tqdm/4.65.0 CPython/3.8.13

File hashes

Hashes for telepy_notify-0.0.2.tar.gz
Algorithm Hash digest
SHA256 e8667b2a771f62de0454f4153d40ac25fa97060df2f1a7cfac7077ded77f1905
MD5 2d6158d09faec6904ae7c5469bbdf5d6
BLAKE2b-256 c5b0933f859e47475634c3415fea099fd1de09c017003a35d9e65d9fa4b32c88

See more details on using hashes here.

File details

Details for the file telepy_notify-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: telepy_notify-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 237.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/6.6.0 pkginfo/1.9.6 requests/2.30.0 requests-toolbelt/1.0.0 tqdm/4.65.0 CPython/3.8.13

File hashes

Hashes for telepy_notify-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0bea2b49cafda2a80716d6d8c39cedbe0abf5df9abeb0d36886cab0f16f76cfa
MD5 fa6ec93d5db830b53f3f070e0e1202f1
BLAKE2b-256 4e597bd8cdd88abb8d700ae89036f95d6f6d5141b162a71f3210011da4dc3583

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page