コード例 #1
0
def approve_yes_callback(
        update: Update,
        context: CallbackContext) -> Tuple[str, InlineKeyboardMarkup, int]:
    """Handles the approve_yes callback.
    Approves the post, deleting it from the pending_post table, publishing it in the channel \
    and putting it in the published post table

    Args:
        update (Update): update event
        context (CallbackContext): context passed by the handler

    Returns:
        Tuple[str, InlineKeyboardMarkup, int]: text and replyMarkup that make up the reply, new conversation state
    """
    info = get_callback_info(update, context)
    n_approve = MemeData.set_admin_vote(info['sender_id'], info['message_id'],
                                        info['chat_id'], True)

    # The post passed the approval phase and is to be published
    if n_approve >= config_map['meme']['n_votes']:
        message = update.callback_query.message
        user_id = MemeData.get_user_id(g_message_id=info['message_id'],
                                       group_id=info['chat_id'])
        published_post = send_post_to(message=message,
                                      bot=info['bot'],
                                      destination="channel")

        if config_map['meme'][
                'comments']:  # if comments are enabled, save the user_id, so the user can be credited
            context.bot_data[
                f"{published_post.chat_id},{published_post.message_id}"] = user_id

        info['bot'].send_message(
            chat_id=user_id,
            text="Il tuo ultimo post è stato approvato")  # notify the user

        # Shows the list of admins who approved the pending post and removes it form the db
        show_admins_votes(chat_id=info['chat_id'],
                          message_id=info['message_id'],
                          bot=info['bot'],
                          approve=True)
        MemeData.remove_pending_meme(info['message_id'], info['chat_id'])
        return None, None, None

    if n_approve != -1:  # the vote changed
        keyboard = update.callback_query.message.reply_markup.inline_keyboard
        return None, update_approve_kb(keyboard,
                                       info['message_id'],
                                       info['chat_id'],
                                       approve=n_approve), None

    return None, None, None
コード例 #2
0
def approve_no_callback(
        update: Update,
        context: CallbackContext) -> Tuple[str, InlineKeyboardMarkup, int]:
    """Handles the approve_yes callback.
    Approves the post, deleting it from the pending_post table, publishing it in the channel \
    and putting it in the published post table

    Args:
        update (Update): update event
        context (CallbackContext): context passed by the handler

    Returns:
        Tuple[str, InlineKeyboardMarkup, int]: text and replyMarkup that make up the reply, new conversation state
    """
    info = get_callback_info(update, context)
    n_reject = MemeData.set_admin_vote(info['sender_id'], info['message_id'],
                                       info['chat_id'], False)

    # The post has been refused
    if n_reject >= config_map['meme']['n_votes']:
        user_id = MemeData.get_user_id(g_message_id=info['message_id'],
                                       group_id=info['chat_id'])
        info['bot'].send_message(
            chat_id=user_id,
            text=
            "Il tuo ultimo post è stato rifiutato\nPuoi controllare le regole con /rules"
        )  # notify the user

        # Shows the list of admins who refused the pending post and removes it form the db
        show_admins_votes(chat_id=info['chat_id'],
                          message_id=info['message_id'],
                          bot=info['bot'],
                          approve=False)
        MemeData.remove_pending_meme(info['message_id'], info['chat_id'])
        return None, None, None

    if n_reject != -1:  # the vote changed
        keyboard = update.callback_query.message.reply_markup.inline_keyboard
        return None, update_approve_kb(keyboard,
                                       info['message_id'],
                                       info['chat_id'],
                                       reject=n_reject), None

    return None, None, None
コード例 #3
0
def reply_cmd(update: Update, context: CallbackContext):
    """Handles the /reply command
    Send a message to a user by replying to one of his pending posts with /reply + the message you want to send

    Args:
        update (Update): update event
        context (CallbackContext): context passed by the handler
    """
    info = get_message_info(update, context)
    if info['chat_id'] == config_map['meme']['group_id']:  # you have to be in the admin group
        g_message_id = update.message.reply_to_message.message_id
        user_id = MemeData.get_user_id(g_message_id=g_message_id, group_id=info['chat_id'])
        if user_id is None or len(info['text']) <= 7:
            info['bot'].send_message(
                chat_id=info['chat_id'],
                text=
                "Per mandare un messaggio ad un utente, rispondere al suo post con /reply seguito da ciò che gli si vuole dire"
            )
            return
        info['bot'].send_message(chat_id=user_id,
                                 text="COMUNICAZIONE DEGLI ADMIN SUL TUO ULTIMO POST:\n" + info['text'][7:].strip())
コード例 #4
0
def ban_cmd(update: Update, context: CallbackContext):
    """Handles the /ban command
    Ban a user by replying to one of his pending posts with /ban

    Args:
        update (Update): update event
        context (CallbackContext): context passed by the handler
    """
    info = get_message_info(update, context)
    if info['chat_id'] == config_map['meme']['group_id']:  # you have to be in the admin group
        g_message_id = update.message.reply_to_message.message_id
        user_id = MemeData.get_user_id(g_message_id=g_message_id, group_id=info['chat_id'])

        if user_id is None:
            info['bot'].send_message(chat_id=info['chat_id'], text="Per bannare qualcuno, rispondi al suo post con /ban")
            return

        MemeData.ban_user(user_id=user_id)
        MemeData.remove_pending_meme(g_message_id=g_message_id, group_id=info['chat_id'])
        info['bot'].delete_message(chat_id=info['chat_id'], message_id=g_message_id)
        info['bot'].send_message(chat_id=info['chat_id'], text="L'utente è stato bannato")