Пример #1
0
async def global_message(_, message):
    if len(message.command) > 1:
        msg = message.text.html[7:]
        logging.warning(
            f"{ADMINS[message.from_user.id]} [{message.from_user.id}] sent the following global message: {msg}"
        )

        missed = 0
        count = 0

        for tg_id in itertools.chain(*get_users()):
            count += 1
            result = await wrapper.send_message(tg_id, msg)

            if isinstance(result, Exception):
                logging.error(
                    f"Could not deliver the global message to {tg_id} because of {type(result).__name__}: {result}"
                )
                missed += 1

        logging.warning(
            f"{count - missed}/{count} global messages were successfully delivered"
        )
        await wrapper.send_message(
            message.chat.id,
            GLOBAL_MESSAGE_STATS.format(count=count,
                                        success=(count - missed),
                                        msg=msg))
    else:
        await wrapper.send_message(
            message.chat.id,
            f"{INVALID_SYNTAX.format(correct='/global message')}\n<b>HTML and Markdown styling supported</b>"
        )
Пример #2
0
async def count_users(_, message):
    if len(message.command) > 1:
        await wrapper.send_message(message.chat.id,
                                   plate("no_parameters", command="/count"))
    else:
        logging.warning(
            f"{ADMINS[message.from_user.id]} [{message.from_user.id}] sent /count"
        )
        users_count = len(get_users())
        await wrapper.send_message(
            message.chat.id, plate("users_count", users_count=users_count))
Пример #3
0
async def count_users(_, message):
    if len(message.command) > 1:
        await wrapper.send_message(message.chat.id,
                                   NO_PARAMETERS.format(command='/count'))
    else:
        logging.warning(
            f"{ADMINS[message.from_user.id]} [{message.from_user.id}] sent /count"
        )
        count = len(get_users())
        await wrapper.send_message(message.chat.id,
                                   USERS_COUNT.format(count=count))
Пример #4
0
async def get_random_user(_, message):
    logging.warning(
        f"{ADMINS[message.from_user.id]} [{message.from_user.id}] sent /getranduser"
    )
    if len(message.command) > 1:
        await wrapper.send_message(
            message.chat.id, plate("no_parameters", command="/getranduser"))
    else:
        user = random.choice(get_users())
        result = get_user(*user)
        text = format_user(result)
        await wrapper.send_message(message.chat.id, text)
Пример #5
0
async def get_random_user(_, message):
    logging.warning(
        f"{ADMINS[message.from_user.id]} [{message.from_user.id}] sent /getranduser"
    )
    if len(message.command) > 1:
        await wrapper.send_message(
            message.chat.id, NO_PARAMETERS.format(command='/getranduser'))
    else:
        user = random.choice(get_users())
        result = get_user(*user)
        text = format_user(result)
        await wrapper.send_message(message.chat.id, text)
Пример #6
0
async def start_handler(_, update):
    """Simply handles the /start command sending a pre-defined greeting
    and saving new users to the database"""
    update_wrapper = MethodWrapper(update)

    if update.from_user.first_name:
        name = update.from_user.first_name
    elif update.from_user.username:
        name = update.from_user.username
    else:
        name = "Anonymous"
    if update.from_user.id not in itertools.chain(*get_users()):
        logging.warning(
            f"New user detected ({update.from_user.id}), adding to database")
        set_user(
            update.from_user.id,
            update.from_user.username.lower()
            if update.from_user.username else None)
    if GREET:
        if isinstance(update, Message):
            await update_wrapper.reply(text=GREET.format(
                mention=f"[{name}](tg://user?id={update.from_user.id})",
                id=update.from_user.id,
                username=update.from_user.username),
                                       reply_markup=BUTTONS)
        elif isinstance(update, CallbackQuery):
            if CACHE[update.from_user.id][0] == "AWAITING_ADMIN":
                data = CACHE[update.from_user.id][-1]

                if isinstance(data, list):
                    for chatid, message_ids in data:
                        await wrapper.delete_messages(chatid, message_ids)

                for admin in ADMINS:
                    await wrapper.send_message(
                        chat_id=admin,
                        text=USER_LEFT_QUEUE.format(
                            user=f"[{name}]({NAME.format(update.from_user.id)})"
                        ))

            await update_wrapper.edit_message_text(text=GREET.format(
                mention=f"[{name}](tg://user?id={update.from_user.id})",
                id=update.from_user.id,
                username=update.from_user.username),
                                                   reply_markup=BUTTONS)

            del CACHE[update.from_user.id]
            await update_wrapper.answer()
Пример #7
0
async def whisper(_, message):
    if len(message.command) < 2:
        return await wrapper.send_message(
            message.chat.id,
            plate("invalid_syntax", correct="/whisper ID") +
            "\n<b>HTML and Markdown styling supported</b>",
        )

    if not message.command[1].isdigit():
        return await wrapper.send_message(
            message.chat.id,
            plate("error") + ":" + plate("non_numeric_id"))

    logging.warning(
        f"{ADMINS[message.from_user.id]} [{message.from_user.id}] sent {message.text.html}"
    )

    tg_id = int(message.command[1])
    msg = message.text.html[9:]
    msg = msg[re.search(message.command[1], msg).end():]

    if tg_id not in itertools.chain(*get_users()):
        return await wrapper.send_message(
            message.chat.id,
            plate("error") + ":" + plate("id_missing", tg_id=tg_id))

    result = await wrapper.send_message(
        tg_id,
        plate(
            "whisper_from",
            admin=
            f"[{ADMINS[message.from_user.id]}]({NAME.format(message.from_user.id)})",
            msg=msg,
        ),
    )

    if isinstance(result, Exception):
        logging.error(
            f"Could not whisper to {tg_id} because of {type(result).__name__}: {result}"
        )
        await wrapper.send_message(
            message.chat.id,
            plate("error") + f": {type(result).__name__} -> {result}")
    else:
        await wrapper.send_message(message.chat.id,
                                   plate("whisper_successful"))
Пример #8
0
async def whisper(_, message):
    if len(message.command) < 2:
        return await wrapper.send_message(
            message.chat.id,
            f"{INVALID_SYNTAX.format(correct='/whisper ID')}\n<b>HTML and Markdown styling supported</b>"
        )

    if not message.command[1].isdigit():
        return await wrapper.send_message(message.chat.id,
                                          f"{ERROR}: {NON_NUMERIC_ID}")

    logging.warning(
        f"{ADMINS[message.from_user.id]} [{message.from_user.id}] sent {message.text.html}"
    )

    tg_id = int(message.command[1])
    msg = message.text.html[9:]
    msg = msg[re.search(message.command[1], msg).end():]

    if tg_id not in itertools.chain(*get_users()):
        return await wrapper.send_message(
            message.chat.id, f"{ERROR}: {ID_MISSING.format(tg_id=tg_id)}")

    result = await wrapper.send_message(
        tg_id,
        WHISPER_FROM.format(
            admin=
            f"[{ADMINS[message.from_user.id]}]({NAME.format(message.from_user.id)})",
            msg=msg))

    if isinstance(result, Exception):
        logging.error(
            f"Could not whisper to {tg_id} because of {type(result).__name__}: {result}"
        )
        await wrapper.send_message(
            message.chat.id, f"{ERROR}: {type(result).__name__} -> {result}")
    else:
        await wrapper.send_message(message.chat.id, WHISPER_SUCCESSFUL)
Пример #9
0
async def global_message(_, message):
    if len(message.command) > 1:
        msg = message.text.html[7:]
        logging.warning(
            f"{ADMINS[message.from_user.id]} [{message.from_user.id}] sent the following global message: {msg}"
        )

        missed = 0
        attempts = 0

        for tg_id in itertools.chain(*get_users()):
            attempts += 1
            result = await wrapper.send_message(tg_id, msg)

            if isinstance(result, Exception):
                logging.error(
                    f"Could not deliver the global message to {tg_id} because of {type(result).__name__}: {result}"
                )
                missed += 1

        logging.warning(
            f"{attempts - missed}/{attempts} global messages were successfully delivered"
        )
        await wrapper.send_message(
            message.chat.id,
            plate("global_message_stats",
                  attempts=attempts,
                  success=(attempts - missed),
                  msg=msg),
        )
    else:
        await wrapper.send_message(
            message.chat.id,
            plate("invalid_syntax", correct="/global message") +
            "\n<b>HTML and Markdown styling supported</b>",
        )
Пример #10
0
async def start_handler(_, update):
    """Simply handles the /start command sending a pre-defined greeting
    and saving new users to the database"""
    update_wrapper = MethodWrapper(update)

    if update.from_user.first_name:
        name = update.from_user.first_name
    elif update.from_user.username:
        name = update.from_user.username
    else:
        name = "Anonymous"
    if update.from_user.id not in itertools.chain(*get_users()):
        logging.warning(
            f"New user detected ({update.from_user.id}), adding to database"
        )
        set_user(
            update.from_user.id,
            update.from_user.username.lower() if update.from_user.username else None,
        )
    if GREET:
        if isinstance(update, Message):
            await update_wrapper.reply(
                text=plate(
                    "greet",
                    mention=f"[{name}](tg://user?id={update.from_user.id})",
                    id=update.from_user.id,
                    username=update.from_user.username,
                    website_status="Online"
                    if await get_website_status()
                    else "Offline",
                    qr_code_api_status="Online"
                    if await get_qr_api_status()
                    else "Offline",
                    api_status="Online" if await get_api_status() else "Offline",
                    api_docs_status="Online"
                    if await get_api_docs_status()
                    else "Offline",
                    internal_api_status="Online"
                    if await get_internal_api_status()
                    else "Offline",
                ),
                reply_markup=BUTTONS,
            )
        elif isinstance(update, CallbackQuery):
            if CACHE[update.from_user.id][0] == "AWAITING_ADMIN":
                data = CACHE[update.from_user.id][-1]

                if isinstance(data, list):
                    for chatid, message_ids in data:
                        await wrapper.delete_messages(chatid, message_ids)

                for admin in ADMINS:
                    await wrapper.send_message(
                        chat_id=admin,
                        text=plate(
                            "user_left_queue",
                            user=f"[{name}]({NAME.format(update.from_user.id)})",
                        ),
                    ),

            await update_wrapper.edit_message_text(
                text=plate(
                    "greet",
                    mention=f"[{name}](tg://user?id={update.from_user.id})",
                    id=update.from_user.id,
                    username=update.from_user.username,
                    website_status="Online"
                    if await get_website_status()
                    else "Offline",
                    qr_code_api_status="Online"
                    if await get_qr_api_status()
                    else "Offline",
                    api_status="Online" if await get_api_status() else "Offline",
                    api_docs_status="Online"
                    if await get_api_docs_status()
                    else "Offline",
                    internal_api_status="Online"
                    if await get_internal_api_status()
                    else "Offline",
                ),
                reply_markup=BUTTONS,
            )

            del CACHE[update.from_user.id]
            await update_wrapper.answer()