refined vbot init CLI command

This commit is contained in:
missytake 2023-05-19 20:26:24 +02:00
parent f02baa2d4e
commit 3b770c3cc9
2 changed files with 24 additions and 17 deletions

View file

@ -28,10 +28,10 @@ where = src
[options.entry_points] [options.entry_points]
console_scripts = console_scripts =
verificationbot = verificationbot:main verificationbot = verificationbot.cli:cli_main
[tox:tox] [tox:tox]
envlist = lint, py38, py39, py310 envlist = lint, py310
isolated_build = True isolated_build = True
[testenv:lint] [testenv:lint]
@ -39,11 +39,9 @@ skip_install = True
deps = deps =
black black
flake8 flake8
mypy
commands = commands =
black --check --diff src tests black --check --diff src tests
flake8 src tests flake8 src tests
mypy --disallow-untyped-defs src tests
[testenv] [testenv]
deps = deps =
@ -52,4 +50,4 @@ commands =
pytest tests pytest tests
[flake8] [flake8]
max_line_length = 88 max_line_length = 100

View file

@ -1,23 +1,24 @@
import os
import time
import click import click
import deltachat import deltachat
def get_control_group(ctx, ac: deltachat.account.Account) -> deltachat.chat.Chat | None: def get_control_group(ac: deltachat.account.Account) -> deltachat.chat.Chat | None:
for chat in ac.get_chats(): for chat in ac.get_chats():
if chat.get_contacts() == 1 and chat.is_protected() and chat.is_group(): if len(chat.get_contacts()) == 1 and chat.is_protected() and chat.is_group():
if chat.get_name() == "verification bot control group": if chat.get_name() == "verification bot control group":
return chat return chat
return return
@click.command( @click.command()
cls=click.Group, context_settings={"help_option_names": ["-h", "--help"]}
)
@click.option( @click.option(
"--db-path", "--db-path",
type=str, type=str,
help="The directory where the Delta Chat database should be stored", help="The directory where the Delta Chat database should be stored",
default="~/.config/DeltaChat/", default=os.getenv("HOME") + "/.config/DeltaChat/verificationbot/db.sqlite",
show_default=True, show_default=True,
) )
@click.option( @click.option(
@ -30,7 +31,7 @@ def get_control_group(ctx, ac: deltachat.account.Account) -> deltachat.chat.Chat
def init(ctx, db_path, from_backup) -> None: def init(ctx, db_path, from_backup) -> None:
ac = deltachat.account.Account(db_path) ac = deltachat.account.Account(db_path)
if not ac.is_configured(): if not ac.is_configured():
if from_backup is not " ": if from_backup != " ":
ac.import_all(from_backup) ac.import_all(from_backup)
else: else:
ctx.fail( ctx.fail(
@ -38,7 +39,7 @@ def init(ctx, db_path, from_backup) -> None:
) )
# ensure that the account owner has bcc_self enabled # ensure that the account owner has bcc_self enabled
if ac.get_config("bcc_self") == "0": if ac.get_config("bcc_self") != "1":
ctx.fail( ctx.fail(
"You need to enable 'Send Copy to Self' in the Advanced settings for this bot to work." "You need to enable 'Send Copy to Self' in the Advanced settings for this bot to work."
) )
@ -48,16 +49,24 @@ def init(ctx, db_path, from_backup) -> None:
ac.set_config("mdns_enabled", "0") ac.set_config("mdns_enabled", "0")
# create control group # create control group
if not get_control_group(ctx, ac): if not get_control_group(ac):
ac.start_io()
control_group = ac.create_group_chat( control_group = ac.create_group_chat(
"verification bot control group", verified=True "verification bot control group", verified=True
) )
control_group.send_text( control_group.send_text(
"This is the control group for the verification bot. Send /help for available commands." "This is the control group for the verification bot. Send /help for available commands."
) )
click.secho("Verification bot for %s was set up successfully. To start the bot, run:") # wait until message is sent out
while not control_group.get_messages()[-1].is_out_delivered():
time.sleep(0.1)
click.secho(
"Verification bot for %s was set up successfully. To start the bot, run:"
% (ac.get_config("addr"),)
)
click.secho("") click.secho("")
click.secho(" verificationbot run") click.secho(" verificationbot run\n")
@click.command( @click.command(
@ -65,7 +74,7 @@ def init(ctx, db_path, from_backup) -> None:
) )
@click.version_option() @click.version_option()
@click.pass_context @click.pass_context
def cli_main(context): def cli_main(ctx):
"""e-mail account creation admin tool and web service.""" """e-mail account creation admin tool and web service."""