Send telegram notifications
Project description
What is this?
Super fast (<5min setup) telegram notifications for task-completion, task progress, errors and more. This is an minimal module; no pip dependencies at all.
How do I use this?
-
pip install telepy_notify
-
Take 30sec to get a token:
- open up a chat with https://t.me/botfather
- send
/newbot
to ^ - copy the token out of the response message
-
DM your bot to initialize it (important!)
-
Use it
from telepy_notify import Notifier
#
# setup
#
notify = Notifier(
# read from file
token_path="some/file/thats/git/ignored/telegram.token",
# # OR read from ENV var, ex:
# token_env_var="TELEGRAM_TOKEN",
# # OR give the token directly, ex:
# token="alkdsfjakjfoirj029294ijfoi24j4-2",
# optional: prefix give all messages
message_prefix="Lambda Machine: Experiment 19:",
# optional: set to true when debugging
disable=False,
)
# NOTE:
# other people can run your code without setting up a bot
# because telepy will NEVER throw errors
# if a notification fails, or if a token is bad
# it will intentionally only print warnings instead of
# crashing whatever long-running process you have
#
# 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 notify-rate (by time passed or percent-progress)
# e.g. percent_per_notify=50 => message me after 50% progress
# e.g. minutes_per_notify=30 => message once every 30min
for progress, epoch in notify.progress(range(100), percent_per_notify=50, minutes_per_notify=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.2.0.tar.gz
(8.5 MB
view details)
Built Distribution
telepy_notify-0.2.0-py3-none-any.whl
(238.5 kB
view details)
File details
Details for the file telepy_notify-0.2.0.tar.gz
.
File metadata
- Download URL: telepy_notify-0.2.0.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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9b2f3202692fe59f279b6444264dee5c62c4813ca1990a0390213d35c8bebc6d |
|
MD5 | 2bf1fb3890dbae8717e6f80efb638744 |
|
BLAKE2b-256 | bc3b002b9946ec573a33361b515c9c631ef170412ef91568c4bf228ea107a625 |
File details
Details for the file telepy_notify-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: telepy_notify-0.2.0-py3-none-any.whl
- Upload date:
- Size: 238.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
Algorithm | Hash digest | |
---|---|---|
SHA256 | e793848f33a75657f6fa5a3dae465e1e0e32234ff39420845e325523fb6bcfa4 |
|
MD5 | 5177da071d521ebde8e8b3e66d7e990e |
|
BLAKE2b-256 | 7a93752a6fe23255c707b027755ad2dc5b96b7d708448e3e7115106e41a37a36 |