Compare commits

...

19 commits

Author SHA1 Message Date
missytake b8182f0452
pin working deltachat version 2025-08-31 19:20:34 +02:00
missytake 1d45972740
actually requests isn't needed 2025-08-31 19:19:00 +02:00
missytake 327a6c60d7
make sure messages are marked as read before they get handled 2025-08-31 19:18:10 +02:00
missytake 6e93a63c40
ignore case of keywords 2025-08-30 00:08:41 +02:00
missytake 72853709ee
don't return '' as keyword 2025-08-29 23:26:41 +02:00
missytake 3db5550fb8
allow commands to be mixed-case 2025-08-29 11:29:14 +02:00
missytake d6fb5e5ed0
adjust config for wohnungssuche use case 2025-08-28 22:33:02 +02:00
missytake b184eeb539
adjust help message 2025-08-28 22:29:27 +02:00
missytake cc7bd33fb6
adjust help message 2025-08-28 22:28:28 +02:00
missytake c585e0b9e6
use Saved Messages as admin chat if none exists 2025-08-28 22:24:15 +02:00
missytake becc27825d
set Saved Messages to admin chat by default 2025-08-28 22:17:07 +02:00
missytake 7e5f720467
in the beginning, ui.admin_chat is empty 2025-08-27 14:09:42 +02:00
missytake 02341180b8
adjust help message 2025-08-27 12:37:02 +02:00
missytake f220df12dc
Add function to check and forward messages 2025-08-27 12:34:38 +02:00
missytake 51d6ec297f
add function to remove keywords 2025-08-27 12:04:37 +02:00
missytake 74c3391b6e
store keywords in database 2025-08-27 12:01:29 +02:00
missytake 1deabe7fb8
store admin chat in the database 2025-08-27 11:44:37 +02:00
missytake 29e4249f18
throw out methods we don't need, add stubs 2025-08-27 11:16:15 +02:00
missytake 6f522b1218
wohnungssuche: set admin group from verified chat 2025-08-27 11:10:05 +02:00
3 changed files with 88 additions and 70 deletions

View file

@ -21,7 +21,7 @@ packages = find:
python_requires = >=3.8
install_requires =
click
deltachat
deltachat<=1.154.0
[options.packages.find]
where = src

View file

@ -1,12 +1,4 @@
import os
import deltachat
from remember_remember_bot.util import (
chat_is_active,
get_file_path,
get_lines_from_file,
today_lines,
)
def setup(email: str, password: str, db: str, debug: bool) -> deltachat.Account:
@ -20,57 +12,68 @@ def setup(email: str, password: str, db: str, debug: bool) -> deltachat.Account:
"""
ac = deltachat.account.Account(db_path=db)
ac.set_config("delete_server_after", "2")
ac.set_config("displayname", "remember, remember")
ac.set_config("delete_server_after", "0")
ac.set_config("displayname", "Looking for flats")
ac.run_account(email, password, show_ffi=debug)
print(ac.get_setup_contact_qr())
return ac
def remind_chat(chat: deltachat.Chat):
"""Remind a chat from the last sent file, with all lines which fit today's date."""
if chat_is_active(chat):
file_path = get_file_path(chat)
lines = get_lines_from_file(file_path)
message_text = "\n".join(today_lines(lines))
print(message_text)
chat.send_text(message_text)
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 store_file(msg: deltachat.Message):
"""Store a received file and reply without timer to the chat where it was stored"""
new_filename = "./files/" + str(msg.chat.id)
with open(msg.filename, "r") as read_file:
os.makedirs(os.path.dirname(new_filename), exist_ok=True)
with open(new_filename, "w+") as write_file:
write_file.write(read_file.read())
reply(msg.chat, f"Thanks, I will use this file for daily reminders now.", quote=msg)
def get_admin_chat(account: deltachat.Account) -> deltachat.Chat:
"""Get the current admin chat"""
try:
id = int(account.get_config("ui.admin_chat"))
except ValueError:
id = account.get_self_contact().create_chat().id
return account.get_chat_by_id(id)
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(ac: deltachat.Account) -> set:
"""List current keywords"""
keywords = set()
for word in ac.get_config("ui.keywords").split("|"):
if word:
keywords.add(word)
return keywords
def rm_keyword(msg: deltachat.Message):
"""Remove keywords from the bot"""
current_words = list_keywords(msg.account)
[current_words.discard(word) for word in msg.text.split()[1:]]
msg.account.set_config("ui.keywords", "|".join(current_words))
def check_and_forward(msg: deltachat.Message, admin_chat: deltachat.Chat):
"""Check a message for keywords, and forward it to the admin chat if one matches."""
for word in list_keywords(msg.account):
if word.lower() in msg.text.lower():
admin_chat.send_msg(msg)
print(f"Forwarding message because of '{word}': {msg.text}")
break
print(f"Not forwarding message without keywords: {msg.text}")
def send_help(msg: deltachat.Message):
"""Reply to the user with a help message."""
help_text = """
/start\tStart getting daily reminders.
/stop\tStop getting daily reminders.
/file\t(with an attachment) Add a list of entries which I can remind you of at specific dates.
/start\tStart getting messages which match keywords.
/stop\tStop getting messages from this bot.
/list\t\tList current keywords
/add gay romance\tAdd keywords to check for
/remove straight ex\tRemove keywords from the list
"""
reply(msg.chat, help_text, quote=msg)

View file

@ -2,34 +2,49 @@ import time
import deltachat
from remember_remember_bot.commands import remind_chat, activate_chat, reply, store_file, send_help
from remember_remember_bot.util import check_new_day, update_day
from .commands import (
set_admin_chat,
reply,
send_help,
add_keywords,
list_keywords,
rm_keyword,
check_and_forward,
get_admin_chat,
)
def loop(ac: deltachat.Account):
current_day = 0
admin_chat = get_admin_chat(ac)
print(f"Selected {admin_chat.get_name()} as admin chat")
while True:
if check_new_day(current_day):
for chat in ac.get_chats():
remind_chat(chat)
current_day = update_day()
for msg in ac.get_fresh_messages():
handle_incoming_message(msg)
time.sleep(1)
msg.mark_seen()
handle_incoming_message(msg, ac)
time.sleep(3)
def handle_incoming_message(msg: deltachat.Message):
print(str(msg.chat.id), msg.text)
if "/start" in msg.text:
activate_chat(msg)
elif "/stop" in msg.text:
msg.chat.set_ephemeral_timer(0)
reply(msg.chat, "I will stop sending you messages now.", quote=msg)
elif "/file" in msg.text:
if msg.filename:
store_file(msg)
else:
activate_chat(msg)
def handle_incoming_message(msg: deltachat.Message, ac: deltachat.Account):
admin_chat = get_admin_chat(ac)
if msg.text.lower().startswith("/start") and msg.chat.is_protected():
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.lower().startswith("/add"):
add_keywords(msg)
repl = f"Stored {', '.join(list_keywords(ac))} as keywords."
reply(msg.chat, repl, quote=msg)
elif msg.text.lower().startswith("/list"):
repl = f"The current keywords are: {', '.join(list_keywords(ac))}"
reply(msg.chat, repl, quote=msg)
elif msg.text.lower().startswith("/remove"):
reply(msg.chat, rm_keyword(msg))
elif msg.text.lower().startswith("/help"):
send_help(msg)
elif msg.text.lower().startswith("/stop"):
set_admin_chat(msg.account.get_chat_by_id(0))
else:
send_help(msg)
msg.mark_seen()
check_and_forward(msg, admin_chat)