From 1d0b9c5d1c90c4f7c7a6eb0c3cf32ffb843f2533 Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Sun, 11 Jun 2023 02:07:51 +0300 Subject: telegram bots: get rid of requests logging via webapi --- bin/inverter_bot.py | 2 -- bin/polaris_kettle_bot.py | 4 ---- bin/pump_bot.py | 2 -- bin/sensors_bot.py | 4 ---- bin/sound_bot.py | 4 +--- bin/web_api.py | 26 +------------------------- include/py/homekit/api/types/__init__.py | 1 - include/py/homekit/api/types/types.py | 11 ----------- include/py/homekit/api/web_api_client.py | 10 ---------- include/py/homekit/database/bots.py | 10 ---------- include/py/homekit/telegram/_botutil.py | 17 ----------------- include/py/homekit/telegram/bot.py | 29 +++++++++-------------------- test/test_api.py | 5 ++--- 13 files changed, 13 insertions(+), 112 deletions(-) diff --git a/bin/inverter_bot.py b/bin/inverter_bot.py index fdfe436..7da21aa 100755 --- a/bin/inverter_bot.py +++ b/bin/inverter_bot.py @@ -28,7 +28,6 @@ from homekit.inverter.types import ( OutputSourcePriority ) from homekit.database.inverter_time_formats import FormatDate -from homekit.api.types import BotType from homekit.api import WebApiClient from telegram import ReplyKeyboardMarkup, InlineKeyboardMarkup, InlineKeyboardButton @@ -921,7 +920,6 @@ class InverterStore(bot.BotDatabase): inverter.init(host=config['inverter']['ip'], port=config['inverter']['port']) bot.set_database(InverterStore()) -bot.enable_logging(BotType.INVERTER) bot.add_conversation(SettingsConversation(enable_back=True)) bot.add_conversation(ConsumptionConversation(enable_back=True)) diff --git a/bin/polaris_kettle_bot.py b/bin/polaris_kettle_bot.py index 3a24fe0..05c2aae 100755 --- a/bin/polaris_kettle_bot.py +++ b/bin/polaris_kettle_bot.py @@ -10,7 +10,6 @@ import threading import paho.mqtt.client as mqtt from homekit.telegram import bot -from homekit.api.types import BotType from homekit.mqtt import Mqtt from homekit.config import config from homekit.util import chunks @@ -738,9 +737,6 @@ if __name__ == '__main__': kc = KettleController() - if 'api' in config: - bot.enable_logging(BotType.POLARIS_KETTLE) - bot.run() # bot library handles signals, so when sigterm or something like that happens, we should stop all other threads here diff --git a/bin/pump_bot.py b/bin/pump_bot.py index 08d0dc6..2583c5f 100755 --- a/bin/pump_bot.py +++ b/bin/pump_bot.py @@ -11,7 +11,6 @@ from homekit.config import config, is_development_mode from homekit.telegram import bot from homekit.telegram._botutil import user_any_name from homekit.relay.sunxi_h3_client import RelayClient -from homekit.api.types import BotType from homekit.mqtt import MqttNode, MqttWrapper, MqttPayload from homekit.mqtt.module.relay import MqttPowerStatusPayload, MqttRelayModule from homekit.mqtt.module.temphum import MqttTemphumDataPayload @@ -248,7 +247,6 @@ if __name__ == '__main__': mqtt.connect_and_loop(loop_forever=False) - bot.enable_logging(BotType.PUMP) bot.run() try: diff --git a/bin/sensors_bot.py b/bin/sensors_bot.py index c2b0070..43932e1 100755 --- a/bin/sensors_bot.py +++ b/bin/sensors_bot.py @@ -20,7 +20,6 @@ from homekit.telegram import bot from homekit.util import chunks, MySimpleSocketClient from homekit.api import WebApiClient from homekit.api.types import ( - BotType, TemperatureSensorLocation ) @@ -176,7 +175,4 @@ def markup(ctx: Optional[bot.Context]) -> Optional[ReplyKeyboardMarkup]: if __name__ == '__main__': - if 'api' in config: - bot.enable_logging(BotType.SENSORS) - bot.run() diff --git a/bin/sound_bot.py b/bin/sound_bot.py index 518151d..fa22ba7 100755 --- a/bin/sound_bot.py +++ b/bin/sound_bot.py @@ -11,7 +11,7 @@ from typing import Optional, List, Dict, Tuple from homekit.config import config from homekit.api import WebApiClient -from homekit.api.types import SoundSensorLocation, BotType +from homekit.api.types import SoundSensorLocation from homekit.api.errors import ApiResponseError from homekit.media import SoundNodeClient, SoundRecordClient, SoundRecordFile, CameraNodeClient from homekit.soundsensor import SoundSensorServerGuardClient @@ -884,7 +884,5 @@ if __name__ == '__main__': finished_handler=record_onfinished, download_on_finish=True) - if 'api' in config: - bot.enable_logging(BotType.SOUND) bot.run() record_client.stop() diff --git a/bin/web_api.py b/bin/web_api.py index 0e0fd0b..e543d22 100755 --- a/bin/web_api.py +++ b/bin/web_api.py @@ -11,7 +11,7 @@ from homekit import http from homekit.config import config, is_development_mode from homekit.database import BotsDatabase, SensorsDatabase, InverterDatabase from homekit.database.inverter_time_formats import * -from homekit.api.types import BotType, TemperatureSensorLocation, SoundSensorLocation +from homekit.api.types import TemperatureSensorLocation, SoundSensorLocation from homekit.media import SoundRecordStorage @@ -126,30 +126,6 @@ class WebAPIServer(http.HTTPServer): BotsDatabase().add_sound_hits(hits, datetime.now()) return self.ok() - async def POST_bot_request_log(self, req: http.Request): - data = await req.post() - - try: - user_id = int(data['user_id']) - except KeyError: - user_id = 0 - - try: - message = data['message'] - except KeyError: - message = '' - - bot = BotType(int(data['bot'])) - - # validate message - if message.strip() == '': - raise ValueError('message can\'t be empty') - - # add record to the database - BotsDatabase().add_request(bot, user_id, message) - - return self.ok() - async def POST_openwrt_log(self, req: http.Request): data = await req.post() diff --git a/include/py/homekit/api/types/__init__.py b/include/py/homekit/api/types/__init__.py index 9f27ff6..22ce4e6 100644 --- a/include/py/homekit/api/types/__init__.py +++ b/include/py/homekit/api/types/__init__.py @@ -1,5 +1,4 @@ from .types import ( - BotType, TemperatureSensorDataType, TemperatureSensorLocation, SoundSensorLocation diff --git a/include/py/homekit/api/types/types.py b/include/py/homekit/api/types/types.py index 981e798..294a712 100644 --- a/include/py/homekit/api/types/types.py +++ b/include/py/homekit/api/types/types.py @@ -1,17 +1,6 @@ from enum import Enum, auto -class BotType(Enum): - INVERTER = auto() - PUMP = auto() - SENSORS = auto() - ADMIN = auto() - SOUND = auto() - POLARIS_KETTLE = auto() - PUMP_MQTT = auto() - RELAY_MQTT = auto() - - class TemperatureSensorLocation(Enum): BIG_HOUSE_1 = auto() BIG_HOUSE_2 = auto() diff --git a/include/py/homekit/api/web_api_client.py b/include/py/homekit/api/web_api_client.py index 15c1915..f9a8963 100644 --- a/include/py/homekit/api/web_api_client.py +++ b/include/py/homekit/api/web_api_client.py @@ -57,16 +57,6 @@ class WebApiClient: # api methods # ----------- - def log_bot_request(self, - bot: BotType, - user_id: int, - message: str): - return self._post('log/bot_request/', { - 'bot': bot.value, - 'user_id': str(user_id), - 'message': message - }) - def log_openwrt(self, lines: List[Tuple[int, str]], access_point: int): diff --git a/include/py/homekit/database/bots.py b/include/py/homekit/database/bots.py index cde48b9..fb5f326 100644 --- a/include/py/homekit/database/bots.py +++ b/include/py/homekit/database/bots.py @@ -2,7 +2,6 @@ import pytz from .mysql import mysql_now, MySQLDatabase, datetime_fmt from ..api.types import ( - BotType, SoundSensorLocation ) from typing import Optional, List, Tuple @@ -27,15 +26,6 @@ class OpenwrtLogRecord: class BotsDatabase(MySQLDatabase): - def add_request(self, - bot: BotType, - user_id: int, - message: str): - with self.cursor() as cursor: - cursor.execute("INSERT INTO requests_log (user_id, message, bot, time) VALUES (%s, %s, %s, %s)", - (user_id, message, bot.name.lower(), mysql_now())) - self.commit() - def add_openwrt_logs(self, lines: List[Tuple[datetime, str]], access_point: int): diff --git a/include/py/homekit/telegram/_botutil.py b/include/py/homekit/telegram/_botutil.py index 111a704..4fbbf28 100644 --- a/include/py/homekit/telegram/_botutil.py +++ b/include/py/homekit/telegram/_botutil.py @@ -3,9 +3,6 @@ import traceback from html import escape from telegram import User -from homekit.api import WebApiClient as APIClient -from homekit.api.types import BotType -from homekit.api.errors import ApiResponseError _logger = logging.getLogger(__name__) @@ -24,20 +21,6 @@ def user_any_name(user: User) -> str: return name -class ReportingHelper: - def __init__(self, client: APIClient, bot_type: BotType): - self.client = client - self.bot_type = bot_type - - def report(self, message, text: str = None) -> None: - if text is None: - text = message.text - try: - self.client.log_bot_request(self.bot_type, message.chat_id, text) - except ApiResponseError as error: - _logger.exception(error) - - def exc2text(e: Exception) -> str: tb = ''.join(traceback.format_tb(e.__traceback__)) return f'{e.__class__.__name__}: ' + escape(str(e)) + "\n\n" + escape(tb) diff --git a/include/py/homekit/telegram/bot.py b/include/py/homekit/telegram/bot.py index 2e33bea..5ed8b06 100644 --- a/include/py/homekit/telegram/bot.py +++ b/include/py/homekit/telegram/bot.py @@ -21,12 +21,10 @@ from telegram.ext.filters import BaseFilter from telegram.error import TimedOut from homekit.config import config -from homekit.api import WebApiClient -from homekit.api.types import BotType from ._botlang import lang, languages from ._botdb import BotDatabase -from ._botutil import ReportingHelper, exc2text, IgnoreMarkup, user_any_name +from ._botutil import exc2text, IgnoreMarkup from ._botcontext import Context @@ -39,7 +37,6 @@ _cancel_and_back_filter = filters.Text(lang.all('back') + lang.all('cancel')) _logger = logging.getLogger(__name__) _application: Optional[Application] = None -_reporting: Optional[ReportingHelper] = None _exception_handler: Optional[Coroutine] = None _dispatcher = None _markup_getter: Optional[callable] = None @@ -511,22 +508,14 @@ async def _default_any_handler(ctx: Context): await ctx.reply(ctx.lang('invalid_command')) -def _logging_message_handler(update: Update, context: CallbackContext): - if _reporting: - _reporting.report(update.message) - - -def _logging_callback_handler(update: Update, context: CallbackContext): - if _reporting: - _reporting.report(update.callback_query.message, text=update.callback_query.data) - - -def enable_logging(bot_type: BotType): - api = WebApiClient(timeout=3) - api.enable_async() - - global _reporting - _reporting = ReportingHelper(api, bot_type) +# def _logging_message_handler(update: Update, context: CallbackContext): +# if _reporting: +# _reporting.report(update.message) +# +# +# def _logging_callback_handler(update: Update, context: CallbackContext): +# if _reporting: +# _reporting.report(update.callback_query.message, text=update.callback_query.data) def notify_all(text_getter: callable, diff --git a/test/test_api.py b/test/test_api.py index 80ab62a..b35a597 100755 --- a/test/test_api.py +++ b/test/test_api.py @@ -2,12 +2,11 @@ import __py_include from homekit.api import WebApiClient -from homekit.api.types import BotType from homekit.config import config if __name__ == '__main__': config.load_app('test_api') - api = WebApiClient() - print(api.log_bot_request(BotType.ADMIN, 1, "test_api.py")) + # api = WebApiClient() + # print(api.log_bot_request(BotType.ADMIN, 1, "test_api.py")) -- cgit v1.2.3