Skip to content

Instantly share code, notes, and snippets.

@serycjon
Created April 8, 2021 15:05
Show Gist options
  • Save serycjon/808d7304a1e73832df59db47e1b0ce27 to your computer and use it in GitHub Desktop.
Save serycjon/808d7304a1e73832df59db47e1b0ce27 to your computer and use it in GitHub Desktop.
Telegram notification on fail
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()
@serycjon
Copy link
Author

serycjon commented Apr 8, 2021

  1. message https://telegram.me/BotFather
  2. send /newbot to BotFather and follow instructions
  3. run pip install telegram-send
  4. run telegram-send configure (enter details given by BotFather)

( https://medium.com/@robertbracco1/how-to-write-a-telegram-bot-to-send-messages-with-python-bcdf45d0a580 )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment