def config(token, db_url, op_name, values):
operation, name = op_name
try:
digestbot = hdbot.HDBot(token, db_url)
cfg = digestbot.get_config()
logging.info("Using configuration at %s", db_url)
except Exception as e:
raise CLIError(e)
with contextlib.closing(digestbot):
if operation == '--add' and name == 'chat':
# validation
if len(values) < 2:
raise CLIError("Not enough arguments for --add chat")
# get values
name = values[0]
email = values[1]
# validations
util.validate_username(name, exception=CLIError)
util.validate_email_address(email, exception=CLIError)
# get extra values if provided
try:
extra = dict(v.split('=') for v in values[2:])
except ValueError:
raise CLIError("Bad format in extra values for --add chat")
# ensure the format @groupname
name = '@'+name if name[0] != '@' else name
# get chat_id
try:
tgchat = digestbot.get_chat(name)
except TelegramError:
raise CLIError("chat '%s' not found" % name)
# add this chat to the config database
try:
if cfg.has_chat(tgchat.id):
if confirm("Chat %s already in config, you are about to update it.\n"
"Do you want to continue?" % name, default=True):
whatdone = 'updated'
else:
raise CLIError("Configuration aborted by user")
else:
whatdone = 'added'
cfg.add_chat(chat_id=tgchat.id, name=name[1:], sendto=email, **extra)
print("config: chat %s was %s" % (name, whatdone))
except TypeError as e:
raise CLIError(e)
评论列表
文章目录