telegram bots: get rid of requests logging via webapi
This commit is contained in:
parent
00b3cd120f
commit
1d0b9c5d1c
@ -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))
|
||||
|
@ -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
|
||||
|
@ -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:
|
||||
|
@ -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()
|
||||
|
@ -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()
|
||||
|
@ -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()
|
||||
|
||||
|
@ -1,5 +1,4 @@
|
||||
from .types import (
|
||||
BotType,
|
||||
TemperatureSensorDataType,
|
||||
TemperatureSensorLocation,
|
||||
SoundSensorLocation
|
||||
|
@ -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()
|
||||
|
@ -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):
|
||||
|
@ -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):
|
||||
|
@ -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)
|
||||
|
@ -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,
|
||||
|
@ -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"))
|
||||
|
Loading…
x
Reference in New Issue
Block a user