diff options
Diffstat (limited to 'ch1p/functions.py')
-rw-r--r-- | ch1p/functions.py | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/ch1p/functions.py b/ch1p/functions.py new file mode 100644 index 0000000..c4f9c99 --- /dev/null +++ b/ch1p/functions.py @@ -0,0 +1,34 @@ +import os, requests + +from typing import List, Tuple, AnyStr + + +def _get_vars(params: List[Tuple], kw: dict) -> List[AnyStr]: + result = [] + + for kw_name, env_name in params: + env_name = f'MY_{env_name}' + if kw_name in kw: + result.append(kw[kw_name]) + elif env_name in os.environ: + result.append(os.environ[env_name]) + else: + raise RuntimeError("missing parameter %s or variable %s" % (kw_name, env_name)) + + return result + + +def telegram_notify(text: str, parse_mode: str = 'html', **kwargs): + token, chat_id = _get_vars([ + ('chat_id', 'TELEGRAM_NOTIFY_CHAT_ID'), + ('token', 'TELEGRAM_NOTIFY_TOKEN') + ], kwargs) + + r = requests.post('https://api.telegram.org/bot%s/sendMessage' % token, data={ + 'chat_id': chat_id, + 'text': text, + 'parse_mode': parse_mode + }) + + if r.status_code != 200: + raise RuntimeError("telegram returned %d" % r.status_code) |