Compare commits
19 commits
main
...
wohnungssu
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b8182f0452 | ||
|
|
1d45972740 | ||
|
|
327a6c60d7 | ||
|
|
6e93a63c40 | ||
|
|
72853709ee | ||
|
|
3db5550fb8 | ||
|
|
d6fb5e5ed0 | ||
|
|
b184eeb539 | ||
|
|
cc7bd33fb6 | ||
|
|
c585e0b9e6 | ||
|
|
becc27825d | ||
|
|
7e5f720467 | ||
|
|
02341180b8 | ||
|
|
f220df12dc | ||
|
|
51d6ec297f | ||
|
|
74c3391b6e | ||
|
|
1deabe7fb8 | ||
|
|
29e4249f18 | ||
|
|
6f522b1218 |
|
|
@ -21,7 +21,7 @@ packages = find:
|
||||||
python_requires = >=3.8
|
python_requires = >=3.8
|
||||||
install_requires =
|
install_requires =
|
||||||
click
|
click
|
||||||
deltachat
|
deltachat<=1.154.0
|
||||||
|
|
||||||
[options.packages.find]
|
[options.packages.find]
|
||||||
where = src
|
where = src
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,4 @@
|
||||||
import os
|
|
||||||
|
|
||||||
import deltachat
|
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:
|
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 = deltachat.account.Account(db_path=db)
|
||||||
ac.set_config("delete_server_after", "2")
|
ac.set_config("delete_server_after", "0")
|
||||||
ac.set_config("displayname", "remember, remember")
|
ac.set_config("displayname", "Looking for flats")
|
||||||
ac.run_account(email, password, show_ffi=debug)
|
ac.run_account(email, password, show_ffi=debug)
|
||||||
print(ac.get_setup_contact_qr())
|
print(ac.get_setup_contact_qr())
|
||||||
return ac
|
return ac
|
||||||
|
|
||||||
|
|
||||||
def remind_chat(chat: deltachat.Chat):
|
def set_admin_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):
|
|
||||||
"""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,
|
|
||||||
"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())
|
|
||||||
|
|
||||||
|
|
||||||
def store_file(msg: deltachat.Message):
|
def get_admin_chat(account: deltachat.Account) -> deltachat.Chat:
|
||||||
"""Store a received file and reply without timer to the chat where it was stored"""
|
"""Get the current admin chat"""
|
||||||
new_filename = "./files/" + str(msg.chat.id)
|
try:
|
||||||
with open(msg.filename, "r") as read_file:
|
id = int(account.get_config("ui.admin_chat"))
|
||||||
os.makedirs(os.path.dirname(new_filename), exist_ok=True)
|
except ValueError:
|
||||||
with open(new_filename, "w+") as write_file:
|
id = account.get_self_contact().create_chat().id
|
||||||
write_file.write(read_file.read())
|
return account.get_chat_by_id(id)
|
||||||
reply(msg.chat, f"Thanks, I will use this file for daily reminders now.", quote=msg)
|
|
||||||
|
|
||||||
|
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):
|
def send_help(msg: deltachat.Message):
|
||||||
"""Reply to the user with a help message."""
|
"""Reply to the user with a help message."""
|
||||||
help_text = """
|
help_text = """
|
||||||
/start\tStart getting daily reminders.
|
/start\tStart getting messages which match keywords.
|
||||||
/stop\tStop getting daily reminders.
|
/stop\tStop getting messages from this bot.
|
||||||
/file\t(with an attachment) Add a list of entries which I can remind you of at specific dates.
|
/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)
|
reply(msg.chat, help_text, quote=msg)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,34 +2,49 @@ import time
|
||||||
|
|
||||||
import deltachat
|
import deltachat
|
||||||
|
|
||||||
from remember_remember_bot.commands import remind_chat, activate_chat, reply, store_file, send_help
|
from .commands import (
|
||||||
from remember_remember_bot.util import check_new_day, update_day
|
set_admin_chat,
|
||||||
|
reply,
|
||||||
|
send_help,
|
||||||
|
add_keywords,
|
||||||
|
list_keywords,
|
||||||
|
rm_keyword,
|
||||||
|
check_and_forward,
|
||||||
|
get_admin_chat,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def loop(ac: deltachat.Account):
|
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:
|
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():
|
for msg in ac.get_fresh_messages():
|
||||||
handle_incoming_message(msg)
|
|
||||||
time.sleep(1)
|
|
||||||
|
|
||||||
|
|
||||||
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)
|
|
||||||
else:
|
|
||||||
send_help(msg)
|
|
||||||
msg.mark_seen()
|
msg.mark_seen()
|
||||||
|
handle_incoming_message(msg, ac)
|
||||||
|
time.sleep(3)
|
||||||
|
|
||||||
|
|
||||||
|
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:
|
||||||
|
check_and_forward(msg, admin_chat)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue