Created
April 8, 2021 15:05
-
-
Save serycjon/808d7304a1e73832df59db47e1b0ce27 to your computer and use it in GitHub Desktop.
Telegram notification on fail
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import traceback | |
import time | |
def with_telegram(orig_fn): | |
start = time.time() | |
try: | |
import telegram_send | |
def push_msg_fn(msg): | |
now = time.time() | |
diff_m = (now - start) / 60 | |
if diff_m > 1: | |
try: | |
telegram_send.send(messages=[str(msg)]) | |
except Exception as e: | |
print(traceback.format_exc()) | |
print(e) | |
print('push notification failed') | |
except Exception: | |
push_msg_fn = lambda msg: None | |
def new_fn(*args, **kwargs): | |
try: | |
return orig_fn(*args, **kwargs) | |
except Exception as e: | |
push_msg_fn(e) | |
raise | |
return new_fn | |
@with_telegram | |
def main(): | |
print('waiting for a minute') | |
import time | |
time.sleep(60 + 5) | |
a = b + c # should fail horribly | |
return 0 | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
/newbot
to BotFather and follow instructionspip install telegram-send
telegram-send configure
(enter details given by BotFather)( https://medium.com/@robertbracco1/how-to-write-a-telegram-bot-to-send-messages-with-python-bcdf45d0a580 )