store admin chat in the database

This commit is contained in:
missytake 2025-08-27 11:42:31 +02:00
parent 29e4249f18
commit 1deabe7fb8
Signed by: missytake
GPG key ID: 04CC6658320518DF
2 changed files with 48 additions and 37 deletions

View file

@ -27,22 +27,15 @@ def setup(email: str, password: str, db: str, debug: bool) -> deltachat.Account:
return ac return ac
def activate_chat(msg: deltachat.Message): def set_admin_chat(chat: deltachat.Chat):
"""Activate a Chat after the user sent /start""" """Activate a Chat after the user sent /start"""
file_path = get_file_path(msg.chat) chat.account.set_config("ui.admin_chat", chat.id)
if not os.path.exists(file_path):
reply(
msg.chat, def get_admin_chat(account: deltachat.Account) -> deltachat.Chat:
"You first need to send me a file path with the /file command.", """Get the current admin chat"""
quote=msg, id = account.get_config("ui.admin_chat")
) return account.get_chat_by_id(int(id))
msg.mark_seen()
return
if msg.chat.get_ephemeral_timer():
msg.chat.set_ephemeral_timer(0)
reply(msg.chat, f"I will send you daily reminders from the file {file_path}.")
msg.chat.set_ephemeral_timer(60 * 60 * 24)
remind_chat(msg.get_sender_contact().create_chat())
def add_keyword(msg: deltachat.Message): def add_keyword(msg: deltachat.Message):
@ -51,9 +44,9 @@ def add_keyword(msg: deltachat.Message):
def list_keywords(): def list_keywords():
"""List current keywords""" """List current keywords"""
def rm_keyword(text: deltachat.Message):
def rm_keyword(msg: deltachat.Message):
"""Remove keywords from the bot""" """Remove keywords from the bot"""

View file

@ -2,35 +2,53 @@ import time
import deltachat import deltachat
from .commands import activate_chat, reply, send_help, add_keyword, list_keywords, rm_keyword, check_and_forward from .commands import (
set_admin_chat,
reply,
send_help,
add_keyword,
list_keywords,
rm_keyword,
check_and_forward,
get_admin_chat,
)
def loop(ac: deltachat.Account): def loop(ac: deltachat.Account):
admin_chat = None admin_chat = get_admin_chat(ac)
while not admin_chat: while not admin_chat:
for chat in ac.get_chats(): for msg in ac.get_fresh_messages():
if chat.is_protected(): if msg.text.startswith("/start"):
if "/start" in [msg.text for msg in chat.get_messages()]: chat = msg.create_chat()
admin_chat = chat set_admin_chat(chat)
if msg.text.startswith("/help"):
send_help(msg)
msg.mark_seen()
time.sleep(3) time.sleep(3)
print(f"Selected {admin_chat.name} as admin chat") print(f"Selected {admin_chat.get_name()} as admin chat")
while True: while True:
for msg in ac.get_fresh_messages(): for msg in ac.get_fresh_messages():
handle_incoming_message(msg, admin_chat) handle_incoming_message(msg)
msg.mark_seen()
time.sleep(3) time.sleep(3)
def handle_incoming_message(msg: deltachat.Message, admin_chat: deltachat.Chat): def handle_incoming_message(msg: deltachat.Message):
if msg.text.startswith("/start"): admin_chat = get_admin_chat(msg.account)
activate_chat(msg) if msg.text.startswith("/start") and msg.chat.is_protected():
elif msg.text.startswith("/add"): reply(msg.chat, set_admin_chat(msg))
add_keyword(msg) admin_chat.send_text(
elif msg.text.startswith("/list"): f"I will forward appropriate messages to {msg.chat.get_name()} now."
reply(msg.chat, list_keywords(), quote=msg) )
elif msg.text.startswith("/remove"): set_admin_chat(msg.chat)
rm_keyword(msg.text) elif msg.chat == admin_chat:
elif msg.text.startswith("/help"): if msg.text.startswith("/add"):
send_help(msg) reply(msg.chat, add_keyword(msg))
elif msg.text.startswith("/list"):
reply(msg.chat, list_keywords(), quote=msg)
elif msg.text.startswith("/remove"):
reply(msg.chat, rm_keyword(msg))
elif msg.text.startswith("/help"):
send_help(msg)
else: else:
check_and_forward(msg, admin_chat) check_and_forward(msg, admin_chat)
msg.mark_seen()