diff options
author | Evgeny Zinoviev <me@ch1p.io> | 2021-11-16 02:59:27 +0300 |
---|---|---|
committer | Evgeny Zinoviev <me@ch1p.io> | 2021-11-16 02:59:27 +0300 |
commit | 0bb4adb10af5b719c89ca8fa50c637b620831e68 (patch) | |
tree | 26784d0f4593ded844be3267c8f86a0199faf78c | |
parent | aced1ab769417e07d29e36e38b4492a2c87b4611 (diff) |
report requests to solarmon api
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | main.py | 15 | ||||
-rw-r--r-- | requirements.txt | 2 |
3 files changed, 20 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cd762ce --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +__pycache__ +/.idea +/venv
\ No newline at end of file @@ -1,6 +1,8 @@ #!/usr/bin/env python3 import logging +import solarmon_api +from typing import Optional from argparse import ArgumentParser import socket from telegram import ( @@ -18,6 +20,9 @@ from telegram.ext import ( from telegram.error import TimedOut +solarmon: Optional[solarmon_api.Client] = None + + class RelayClient: def __init__(self, port=8307, host='127.0.0.1'): self._host = host @@ -103,6 +108,8 @@ def msg_status(update: Update, context: CallbackContext) -> None: status = 'Включен ✅' if response == 'on' else 'Выключен ❌' reply(update, status) + + solarmon.log_bot_request(solarmon_api.BotType.PUMP, update.message.chat_id, 'Статус') except Exception as e: handle_exc(update, e) @@ -114,6 +121,8 @@ def msg_on(update: Update, context: CallbackContext) -> None: relay.on() reply(update, 'Готово') + + solarmon.log_bot_request(solarmon_api.BotType.PUMP, update.message.chat_id, 'Включить') except Exception as e: handle_exc(update, e) @@ -125,6 +134,8 @@ def msg_off(update: Update, context: CallbackContext) -> None: relay.off() reply(update, 'Готово') + + solarmon.log_bot_request(solarmon_api.BotType.PUMP, update.message.chat_id, 'Выключить') except Exception as e: handle_exc(update, e) @@ -142,6 +153,7 @@ if __name__ == '__main__': help='ID of users allowed to use the bot') parser.add_argument('--gpio-relayd-host', default='127.0.0.1', type=str) parser.add_argument('--gpio-relayd-port', default=8307, type=int) + parser.add_argument('--solarmon-api-token', type=str, required=True) args = parser.parse_args() whitelist = list(map(lambda x: int(x), args.users_whitelist)) @@ -164,6 +176,9 @@ if __name__ == '__main__': dispatcher.add_handler(MessageHandler(Filters.all & user_filter, msg_all)) + # create api client instance + solarmon = solarmon_api.Client(args.solarmon_api_token, timeout=3) + solarmon.enable_async() # start the bot updater.start_polling() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..91e02d0 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +python-telegram-bot~=13.1 +git+ssh://git-hidden@ch1p.io/home/git-hidden/solarmon_api.git
\ No newline at end of file |