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
def activate_chat(msg: deltachat.Message):
def set_admin_chat(chat: deltachat.Chat):
"""Activate a Chat after the user sent /start"""
file_path = get_file_path(msg.chat)
if not os.path.exists(file_path):
reply(
msg.chat,
"You first need to send me a file path with the /file command.",
quote=msg,
)
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())
chat.account.set_config("ui.admin_chat", chat.id)
def get_admin_chat(account: deltachat.Account) -> deltachat.Chat:
"""Get the current admin chat"""
id = account.get_config("ui.admin_chat")
return account.get_chat_by_id(int(id))
def add_keyword(msg: deltachat.Message):
@ -51,9 +44,9 @@ def add_keyword(msg: deltachat.Message):
def list_keywords():
"""List current keywords"""
def rm_keyword(text: deltachat.Message):
def rm_keyword(msg: deltachat.Message):
"""Remove keywords from the bot"""

View file

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