Compare commits

..

2 Commits

Author SHA1 Message Date
link2xt
355420b6fe echobot: ignore info messages 2024-03-25 14:37:17 +00:00
link2xt
b5628cbe6b Fix echobot logging
Do not put log messages into format string
and enable INFO level when bot is started
via main() as it happens with systemd.
2024-03-25 13:47:52 +00:00
2 changed files with 31 additions and 13 deletions

View File

@@ -18,14 +18,14 @@ hooks = events.HookCollection()
@hooks.on(events.RawEvent)
def log_event(event):
if event.kind == EventType.INFO:
logging.info(event.msg)
logging.info("%s", event.msg)
elif event.kind == EventType.WARNING:
logging.warning(event.msg)
logging.warning("%s", event.msg)
@hooks.on(events.RawEvent(EventType.ERROR))
def log_error(event):
logging.error(event.msg)
logging.error("%s", event.msg)
@hooks.on(events.MemberListChanged)
@@ -48,6 +48,9 @@ def on_group_name_changed(event):
@hooks.on(events.NewMessage(func=lambda e: not e.command))
def echo(event):
snapshot = event.message_snapshot
if snapshot.is_info:
# Ignore info messages
return
if snapshot.text or snapshot.file:
snapshot.chat.send_message(text=snapshot.text, file=snapshot.file)
@@ -59,6 +62,7 @@ def help_command(event):
def main():
logging.basicConfig(level=logging.INFO)
path = os.environ.get("PATH")
venv_path = sys.argv[0].strip("echobot")
os.environ["PATH"] = path + ":" + venv_path
@@ -80,5 +84,4 @@ def main():
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
main()

View File

@@ -1,6 +1,6 @@
import pwd
from pathlib import Path
import pathlib
from queue import Queue
from threading import Thread
from socketserver import (
@@ -13,6 +13,7 @@ import sys
import logging
import os
import requests
import marshal
DICTPROXY_LOOKUP_CHAR = "L"
@@ -28,19 +29,33 @@ class Notifier:
self.metadata_dir = metadata_dir
self.to_notify_queue = Queue()
def set_token(self, guid, token):
def get_metadata(self, guid):
guid_path = self.metadata_dir.joinpath(guid)
if guid_path.exists():
with guid_path.open("rb") as f:
return marshal.load(f)
return {}
def set_metadata(self, guid, guid_data):
guid_path = self.metadata_dir.joinpath(guid)
write_path = guid_path.with_suffix(".tmp")
write_path.write_text(token)
write_path.rename(guid_path)
with write_path.open("wb") as f:
marshal.dump(guid_data, f)
os.rename(write_path, guid_path)
def set_token(self, guid, token):
guid_data = self.get_metadata(guid)
guid_data["token"] = token
self.set_metadata(guid, guid_data)
def del_token(self, guid):
self.metadata_dir.joinpath(guid).unlink(missing_ok=True)
guid_data = self.get_metadata(guid)
if "token" in guid_data:
del guid_data["token"]
self.set_metadata(guid, guid_data)
def get_token(self, guid):
guid_path = self.metadata_dir / guid
if guid_path.exists():
return guid_path.read_text()
return self.get_metadata(guid).get("token")
def new_message_for_guid(self, guid):
self.to_notify_queue.put(guid)
@@ -136,7 +151,7 @@ def main():
# XXX config is not currently used
config = read_config(config)
metadata_dir = Path(metadata_dir)
metadata_dir = pathlib.Path(metadata_dir)
if not metadata_dir.exists():
metadata_dir.mkdir()
notifier = Notifier(metadata_dir)