コード例 #1
0
def new_bot_submission(bot, update, chat_data, args=None, bot_checker=None):
    tg_user = update.message.from_user
    user = User.from_telegram_object(tg_user)
    if util.stop_banned(update, user):
        return
    reply_to = util.original_reply_id(update)

    if args:
        text = " ".join(args)
    else:
        text = update.message.text
        command_no_args = (len(re.findall(r"^/new\s*$", text)) > 0
                           or text.lower().strip() == "/new@botlistbot")
        if command_no_args:
            update.message.reply_text(
                util.action_hint(
                    "Please use this command with an argument. For example:\n/new @mybot 🔎"
                ),
                reply_to_message_id=reply_to,
            )
            return

    # `#new` is already checked by handler
    try:
        username = re.match(settings.REGEX_BOT_IN_TEXT, text).groups()[0]
        if username.lower() == "@" + settings.SELF_BOT_NAME.lower():
            log.info("Ignoring {}".format(text))
            return
    except AttributeError:
        if args:
            update.message.reply_text(
                util.failure(
                    "Sorry, but you didn't send me a bot `@username`."),
                quote=True,
                parse_mode=ParseMode.MARKDOWN,
                reply_to_message_id=reply_to,
            )
        log.info("Ignoring {}".format(text))
        # no bot username, ignore update
        return

    try:
        new_bot = Bot.by_username(username, include_disabled=True)
        if new_bot.disabled:
            update.message.reply_text(
                util.failure("{} is banned from the @BotList.".format(
                    new_bot.username)),
                reply_to_message_id=reply_to,
            )
        elif new_bot.approved:
            update.message.reply_text(
                util.action_hint(
                    "Sorry fool, but {} is already in the @BotList 😉".
                    format(new_bot.username)),
                reply_to_message_id=reply_to,
            )
        else:
            update.message.reply_text(
                util.action_hint(
                    "{} has already been submitted. Please have patience...".
                    format(new_bot.username)),
                reply_to_message_id=reply_to,
            )
        return
    except Bot.DoesNotExist:
        new_bot = Bot(
            revision=Revision.get_instance().next,
            approved=False,
            username=username,
            submitted_by=user,
        )

    new_bot.inlinequeries = "🔎" in text
    new_bot.official = "🔹" in text

    # find language
    languages = Country.select().execute()
    for lang in languages:
        if lang.emoji in text:
            new_bot.country = lang

    new_bot.date_added = datetime.date.today()

    description_reg = re.match(settings.REGEX_BOT_IN_TEXT + " -\s?(.*)", text)
    description_notify = ""
    if description_reg:
        description = description_reg.group(2)
        new_bot.description = description
        description_notify = " Your description was included."

    new_bot.save()

    if (util.is_private_message(update)
            and util.uid_from_update(update) in settings.MODERATORS):
        from components.explore import send_bot_details

        send_bot_details(bot, update, chat_data, new_bot)
    else:
        update.message.reply_text(
            util.success("You submitted {} for approval.{}".format(
                new_bot, description_notify)),
            parse_mode=ParseMode.MARKDOWN,
            reply_to_message_id=reply_to,
        )

        # Ask the user to fill in the bot details
        util.send_md_message(
            bot,
            update.effective_user.id,
            "Congratulations, you just submitted a bot to the @BotList. Please help us fill in the details below:",
        )
        edit_bot(bot, update, chat_data, to_edit=new_bot)

    try:
        check_submission(bot, bot_checker, new_bot)
    except Exception as e:
        log.exception(e)

    return ConversationHandler.END
コード例 #2
0
def submit():
    if not request.is_json:
        res = _error('MimeType must be application/json.')
        res.status_code = 400
        return res

    content = request.get_json()
    try:
        access = APIAccess.get(APIAccess.token == content.get('token'))
    except APIAccess.DoesNotExist:
        res = _error('The access token is invalid.')
        res.status_code = 401
        return res

    username = content.get('username')
    if username is None or not isinstance(username, str):
        res = _error('You must supply a username.')
        res.status_code = 400
        return res

    # insert `@` if omitted
    username = '******' + username if username[0] != '@' else username

    try:
        Bot.get(Bot.username == username)
        res = _error('The bot {} is already in the BotList.'.format(username))
        res.status_code = 409
        return res
    except Bot.DoesNotExist:
        b = Bot(username=username)

    name = content.get('name')
    description = content.get('description')
    inlinequeries = content.get('inlinequeries')

    try:
        if name:
            if isinstance(name, str):
                b.name = name
            else:
                raise AttributeError('The name field must be a string.')
        if description:
            if isinstance(description, str):
                b.description = description
            else:
                raise AttributeError('The description field must be a string.')
        if inlinequeries:
            if isinstance(inlinequeries, bool):
                b.inlinequeries = inlinequeries
            else:
                raise AttributeError(
                    'The inlinequeries field must be a boolean.')
    except Exception as e:
        res = _error(str(e))
        res.status_code = 400
        return res

    b.date_added = datetime.date.today()
    b.submitted_by = access.user
    b.approved = False

    b.save()
    res = jsonify({'success': '{} was submitted for approval.'.format(b)})
    res.status_code = 201
    return res