Пример #1
0
def ban_handler(bot, update, args, chat_data, ban_state: bool):
    if args:
        query = " ".join(args) if isinstance(args, list) else args

        entity_to_ban = lookup_entity(query, exact=True)

        if isinstance(entity_to_ban, User):
            ban_user(bot, update, entity_to_ban, ban_state)
        elif isinstance(entity_to_ban, Bot):
            ban_bot(bot, update, chat_data, entity_to_ban, ban_state)
        else:
            update.message.reply_text(
                mdformat.failure("Can only ban users and bots."))
    else:
        # no search term
        update.message.reply_text(
            messages.BAN_MESSAGE if ban_state else messages.UNBAN_MESSAGE,
            reply_markup=ForceReply(selective=True),
        )
    return ConversationHandler.END
Пример #2
0
def notify_submittant_rejected(bot, admin_user, notify_submittant, reason,
                               to_reject):
    notification_successful = False
    msg = "{} rejected by {}.".format(to_reject.username, admin_user)
    if notify_submittant or reason:
        try:
            if reason:
                bot.send_message(
                    to_reject.submitted_by.chat_id,
                    util.failure(
                        messages.REJECTION_WITH_REASON.format(
                            to_reject.username, reason=reason)),
                )
            else:
                bot.sendMessage(
                    to_reject.submitted_by.chat_id,
                    util.failure(
                        messages.REJECTION_PRIVATE_MESSAGE.format(
                            to_reject.username)),
                )
            msg += "\nUser {} was notified.".format(str(
                to_reject.submitted_by))
            notification_successful = True
        except TelegramError:
            msg += "\nUser {} could NOT be contacted/notified in private.".format(
                str(to_reject.submitted_by))
            notification_successful = False

    text = util.success("{} rejected.".format(to_reject.username))
    if notification_successful is True:
        text += " User {} was notified.".format(
            to_reject.submitted_by.plaintext)
    elif notification_successful is False:
        try:
            text += " " + mdformat.failure("Could not contact {}.".format(
                to_reject.submitted_by.plaintext))
        except:
            pass
    else:
        text += " No notification sent."
    return msg
Пример #3
0
def share_with_moderator(bot, update, bot_in_question, moderator):
    user = User.from_update(update)

    buttons = [[
        InlineKeyboardButton(
            "Yea, let me take this one!",
            callback_data=util.callback_for_action(
                CallbackActions.APPROVE_REJECT_BOTS,
                {"id": bot_in_question.id}),
        )
    ]]
    reply_markup = InlineKeyboardMarkup(buttons)
    text = "{} thinks that you have the means to inspect this bot submission:\n▶️ {}".format(
        user.markdown_short, bot_in_question)
    try:
        util.send_md_message(
            bot,
            moderator.chat_id,
            text,
            reply_markup=reply_markup,
            disable_web_page_preview=True,
        )
        answer_text = mdformat.success(
            "I will ask {} to have a look at this submission.".format(
                moderator.plaintext))
    except Exception as e:
        answer_text = mdformat.failure(
            f"Could not contact {moderator.plaintext}: {e}")

    if update.callback_query:
        update.callback_query.answer(text=answer_text)

    Statistic.of(
        update,
        "share",
        "submission {} with {}".format(bot_in_question.username,
                                       moderator.plaintext),
    )
Пример #4
0
def reject_bot_submission(bot,
                          update,
                          args=None,
                          to_reject=None,
                          verbose=True,
                          notify_submittant=True,
                          reason=None):
    uid = util.uid_from_update(update)
    user = User.from_update(update)

    if to_reject is None:
        if not update.message.reply_to_message:
            bot.send_message(
                update.effective_user.id,
                util.failure("You must reply to a message of mine."))
            return

        text = update.message.reply_to_message.text
        reason = reason if reason else (" ".join(args) if args else None)

        try:
            update.message.delete()
        except:
            pass

        username = helpers.find_bots_in_text(text, first=True)
        if not username:
            bot.send_message(
                update.effective_user.id,
                util.failure(
                    "No username in the message that you replied to."))
            return

        try:
            to_reject = Bot.by_username(username)
        except Bot.DoesNotExist:
            bot.send_message(update.effective_user.id,
                             util.failure("Rejection failed: {} is not present in the " \
                                          "database.".format(username)))
            return

        if to_reject.approved is True:
            msg = "{} has already been accepted, so it cannot be rejected anymore.".format(
                username)
            bot.sendMessage(uid, util.failure(msg))
            return

    Statistic.of(update, 'reject', to_reject.username)
    log_msg = "{} rejected by {}.".format(to_reject.username, user)
    notification_successful = None
    if notify_submittant or reason:
        try:
            if reason:
                bot.send_message(
                    to_reject.submitted_by.chat_id,
                    util.failure(
                        messages.REJECTION_WITH_REASON.format(
                            to_reject.username, reason=reason)))
            else:
                bot.sendMessage(
                    to_reject.submitted_by.chat_id,
                    util.failure(
                        messages.REJECTION_PRIVATE_MESSAGE.format(
                            to_reject.username)))
            log_msg += "\nUser {} was notified.".format(
                str(to_reject.submitted_by))
            notification_successful = True
        except TelegramError:
            log_msg += "\nUser {} could NOT be contacted/notified in private.".format(
                str(to_reject.submitted_by))
            notification_successful = False
    to_reject.delete_instance()
    log.info(log_msg)

    text = util.success("{} rejected.".format(to_reject.username))
    if notification_successful is True:
        text += " User {} was notified.".format(
            to_reject.submitted_by.plaintext)
    elif notification_successful is False:
        text += ' ' + mdformat.failure("Could not contact {}.".format(
            to_reject.submitted_by.plaintext))
    else:
        text += " No notification sent."

    if verbose:
        bot.sendMessage(uid, text)

    if update.callback_query:
        update.callback_query.answer(text=text)
Пример #5
0
 def send_failure(self, chat_id, text: str, **kwargs):
     text = str.strip(text)
     if text[-1] != '.':
         text += '.'
     return self.bot.sendMessage(chat_id, failure(text),
                                 **self._set_defaults(kwargs))
Пример #6
0
def reject_bot_submission(bot,
                          update,
                          to_reject=None,
                          verbose=True,
                          notify_submittant=True):
    uid = util.uid_from_update(update)
    user = User.from_update(update)

    if to_reject is None:
        if not update.message.reply_to_message:
            update.message.reply_text(
                util.failure("You must reply to a message of mine."))
            return
        text = update.message.reply_to_message.text

        try:
            username = re.match(settings.REGEX_BOT_IN_TEXT, text).groups()[0]
        except AttributeError:
            log.info("No username in the message that was replied to.")
            # no bot username, ignore update
            return

        try:
            to_reject = Bot.by_username(username)
        except Bot.DoesNotExist:
            log.info("Rejection failed: could not find {}".format(username))
            return

        if to_reject.approved is True:
            msg = "{} has already been accepted, so it cannot be rejected anymore.".format(
                username)
            log.warning("Race condition detected: " + msg)
            bot.sendMessage(uid, util.failure(msg))
            return

    Statistic.of(update, 'reject', to_reject.username)
    log_msg = "{} rejected by {}.".format(to_reject.username, user)
    notification_successful = None
    if notify_submittant:
        try:
            bot.sendMessage(
                to_reject.submitted_by.chat_id,
                util.failure(
                    messages.REJECTION_PRIVATE_MESSAGE.format(
                        to_reject.username)))
            log_msg += "\nUser {} was notified.".format(
                str(to_reject.submitted_by))
            notification_successful = True
        except TelegramError:
            log_msg += "\nUser {} could NOT be contacted/notified in private.".format(
                str(to_reject.submitted_by))
            notification_successful = False
    to_reject.delete_instance()
    log.info(log_msg)

    text = util.success("{} rejected.".format(to_reject.username))
    if notification_successful is True:
        text += " User {} was notified.".format(
            to_reject.submitted_by.plaintext)
    elif notification_successful is False:
        text += ' ' + mdformat.failure("Could not contact {}.".format(
            to_reject.submitted_by.plaintext))
    else:
        text += " No notification sent."

    if verbose:
        bot.sendMessage(uid, text)

    if update.callback_query:
        update.callback_query.answer(text=text)