104 lines
3.3 KiB
Python
104 lines
3.3 KiB
Python
import deltachat
|
|
|
|
|
|
def setup(email: str, password: str, db: str, debug: bool) -> deltachat.Account:
|
|
"""Set up the Delta Chat account of the bot.
|
|
|
|
:param email: the email account for the bot, e.g. bot@example.org
|
|
:param password: the password for the email account
|
|
:param db: the path to the database
|
|
:param debug: whether to show low-level deltachat FFI events
|
|
:return: a configured and running Delta Chat account object
|
|
"""
|
|
|
|
ac = deltachat.account.Account(db_path=db)
|
|
ac.set_config("delete_server_after", "2")
|
|
ac.set_config("displayname", "remember, remember")
|
|
ac.run_account(email, password, show_ffi=debug)
|
|
print(ac.get_setup_contact_qr())
|
|
return ac
|
|
|
|
|
|
def set_admin_chat(chat: deltachat.Chat):
|
|
"""Activate a Chat after the user sent /start"""
|
|
chat.account.set_config("ui.admin_chat", chat.id)
|
|
|
|
|
|
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 = 10
|
|
return account.get_chat_by_id(int(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("|"):
|
|
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 in msg.text:
|
|
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 messages which match keywords.
|
|
/list\tList current keywords
|
|
/add word1 word2\tAdd keywords to check for
|
|
/remove word1 word2\tRemove keywords from the list
|
|
/stop\tStop getting messages from this bot.
|
|
"""
|
|
reply(msg.chat, help_text, quote=msg)
|
|
|
|
|
|
def reply(
|
|
chat: deltachat.Chat,
|
|
text: str,
|
|
quote: deltachat.Message = None,
|
|
attachment: str = None,
|
|
):
|
|
"""Reply to a message from a user.
|
|
|
|
:param chat: the chat where the user's message appeared
|
|
:param text: the text of the reply
|
|
:param quote: (optional) if you want it as a reply to a specific message
|
|
:param attachment: the file path of an attachment
|
|
"""
|
|
print(str(chat.id), text)
|
|
our_reply = deltachat.message.Message.new_empty(chat.account, "text")
|
|
our_reply.set_text(text)
|
|
if quote:
|
|
our_reply.quote = quote
|
|
if attachment:
|
|
our_reply.set_file(attachment)
|
|
try:
|
|
chat.send_msg(our_reply)
|
|
except ValueError:
|
|
print(text)
|