store keywords in database

This commit is contained in:
missytake 2025-08-27 12:00:57 +02:00
parent 1deabe7fb8
commit 74c3391b6e
Signed by: missytake
GPG key ID: 04CC6658320518DF
2 changed files with 21 additions and 9 deletions

View file

@ -38,12 +38,19 @@ def get_admin_chat(account: deltachat.Account) -> deltachat.Chat:
return account.get_chat_by_id(int(id))
def add_keyword(msg: deltachat.Message):
def add_keywords(msg: deltachat.Message):
"""Add keywords the bot listens on"""
current_words = list_keywords(msg.account)
[current_words.add(word) for word in msg.text.split()[1:]]
msg.account.set_config("ui.keywords", "|".join(current_words))
def list_keywords():
def list_keywords(ac: deltachat.Account) -> set:
"""List current keywords"""
keywords = set()
for word in ac.get_config("ui.keywords").split("|"):
keywords.add(word)
return keywords
def rm_keyword(msg: deltachat.Message):

View file

@ -6,7 +6,7 @@ from .commands import (
set_admin_chat,
reply,
send_help,
add_keyword,
add_keywords,
list_keywords,
rm_keyword,
check_and_forward,
@ -28,27 +28,32 @@ def loop(ac: deltachat.Account):
print(f"Selected {admin_chat.get_name()} as admin chat")
while True:
for msg in ac.get_fresh_messages():
handle_incoming_message(msg)
handle_incoming_message(msg, ac)
msg.mark_seen()
time.sleep(3)
def handle_incoming_message(msg: deltachat.Message):
admin_chat = get_admin_chat(msg.account)
def handle_incoming_message(msg: deltachat.Message, ac: deltachat.Account):
admin_chat = get_admin_chat(ac)
if msg.text.startswith("/start") and msg.chat.is_protected():
reply(msg.chat, set_admin_chat(msg))
reply(msg.chat, set_admin_chat(msg.chat))
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))
add_keywords(msg)
repl = f"Stored {', '.join(list_keywords(ac))} as keywords."
reply(msg.chat, repl, quote=msg)
elif msg.text.startswith("/list"):
reply(msg.chat, list_keywords(), quote=msg)
repl = f"The current keywords are: {', '.join(list_keywords(ac))}"
reply(msg.chat, repl, quote=msg)
elif msg.text.startswith("/remove"):
reply(msg.chat, rm_keyword(msg))
elif msg.text.startswith("/help"):
send_help(msg)
elif msg.text.startswith("/stop"):
set_admin_chat(msg.account.get_chat_by_id(0))
else:
check_and_forward(msg, admin_chat)