diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6a07bff --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +local_settings.py diff --git a/bot.py b/bot.py new file mode 100644 index 0000000..5325421 --- /dev/null +++ b/bot.py @@ -0,0 +1,18 @@ +from telegram.ext import Application +from telegram.ext import CommandHandler +import syms as _bot_syms + +async def bot_start(update, context): + chat_id = update.effective_chat.id + await update.message.reply_text(f'wb_alert bot: {chat_id}', parse_mode='HTML') + print(f'alert_bot bot_start: {chat_id}') + +def main(): + application = Application.builder().token(_bot_syms.TOKEN).build() + application.add_handler(CommandHandler('start', bot_start)) + + print('alert_bot main: Bot is running...') + application.run_polling() + +if __name__ == '__main__': + main() diff --git a/msg_sender.py b/msg_sender.py new file mode 100644 index 0000000..8bdc97e --- /dev/null +++ b/msg_sender.py @@ -0,0 +1,30 @@ +import requests as _rqsts +import syms as _bot_syms + + +def _wb_telegram_send_alert(message: str): + users= _bot_syms.USERS + + for user in users: + print(f"wb_alert_bot _wb_telegram_send_alert: sending message to {user}...") + payload = { + 'chat_id': user, + 'text': message, + 'parse_mode': 'Markdown' + } + + try: + res = _rqsts.post(_bot_syms.TELEGRAM_API_URL, data=payload, timeout=3) + res.raise_for_status() + + jres = res.json() + if jres.get('ok'): + print(f"wb_alert_bot _wb_telegram_send_alert: message sent to {user}!") + else: + print(f"wb_alert_bot _wb_telegram_send_alert: Message not sent to {user}: {jres}") + + except _rqsts.exceptions.RequestException as e: + print(f"wb_alert_bot _wb_telegram_send_alert: Connection error: {e}") + +def wb_telegram_send_alert(msg): + _wb_telegram_send_alert(msg) diff --git a/requirements/requirements.txt b/requirements/requirements.txt new file mode 100644 index 0000000..2aa2cfc --- /dev/null +++ b/requirements/requirements.txt @@ -0,0 +1,2 @@ +python-telegram-bot[job-queue]==22.5 +requests==2.32.5 diff --git a/syms.py b/syms.py new file mode 100644 index 0000000..06689d3 --- /dev/null +++ b/syms.py @@ -0,0 +1,13 @@ +try: + from local_settings import TOKEN +except: + print('alert_bot FATAL: add bot token here: local_settings.py') + TOKEN= '' + +try: + from local_settings import USERS +except: + print('alert_bot FATAL: add bot users here: local_settings.py') + USERS= [] + +TELEGRAM_API_URL = f'https://api.telegram.org/bot{TOKEN}/sendMessage'