Exemplo n.º 1
0
async def cmd_start(message: types.Message):
    """
    /start command handler for private chats
    :param message: Telegram message with "/start" command
    """
    await message.answer(f"Your Telegram ID is <code>{message.chat.id}</code>\nHelp and source code: /help")
    logs.track("/start")
Exemplo n.º 2
0
async def cmd_id(message: types.Message):
    """
    /id command handler for all chats
    :param message: Telegram message with "/id" command
    """
    await message.answer(
        f"This {message.chat.type} chat ID is <code>{message.chat.id}</code>")
    logs.track("/id")
Exemplo n.º 3
0
async def new_chat(update: types.ChatMemberUpdated):
    """
    Handler bot being added to group or supergroup
    :param update: Update of type ChatMemberUpdated, where old_chat_member.status is "left",
        new_chat_member.status is "member" and chat.type is either "group" or "supergroup"
    """
    await update.bot.send_message(update.chat.id, f"This {update.chat.type} chat ID is <code>{update.chat.id}</code>")
    logs.track("Added to group")
Exemplo n.º 4
0
async def get_channel_id(message: types.Message):
    """
    Handler for message forwarded from channel to some other chat
    :param message: Telegram message with "forward_from_chat" field not empty
    """
    msg = f"This channel's ID is <code>{message.forward_from_chat.id}</code>"
    if message.sticker:
        msg += f"\nAlso this sticker's ID is <code>{message.sticker.file_id}</code>"
    await message.reply(msg)
    logs.track("Get channel ID")
Exemplo n.º 5
0
async def private_chat(message: types.Message):
    """
    Handler for messages in private chat (one-to-one dialogue)
    :param message: Telegram message sent to private chat (one-to-one dialogue)
    """
    msg = f"Your Telegram ID is <code>{message.chat.id}</code>"
    if message.sticker:
        msg += f"\n\nAlso this sticker's ID is <code>{message.sticker.file_id}</code>"
    await message.reply(msg)
    logs.track("Any message in PM")
Exemplo n.º 6
0
async def get_user_id_with_privacy(message: types.Message):
    """
    Handler for message forwarded from other user who hides their ID
    :param message: Telegram message with "forward_sender_name" field not empty
    """
    msg = f"This user decided to <b>hide</b> their ID.\n\nLearn more about this feature " \
        f"<a href=\"https://telegram.org/blog/unsend-privacy-emoji#anonymous-forwarding\">here</a>."
    if message.sticker:
        msg += f"\n\nAlso this sticker's ID is <code>{message.sticker.file_id}</code>"
    await message.reply(msg)
    logs.track("Check user or bot")
Exemplo n.º 7
0
async def group_upgrade_to(message: types.Message):
    """
    When group is migrated to supergroup, sends new chat ID.
    Notice that the first argument of send_message is message.migrate_to_chat_id, not message.chat.id!
    Otherwise, MigrateChat exception will raise
    :param message: Telegram message with "migrate_to_chat_id" field not empty
    """
    await bot.send_message(
        message.migrate_to_chat_id, f"Group upgraded to supergroup.\n"
        f"New ID: <code>{message.migrate_to_chat_id}</code>")
    logs.track("Group migrate")
Exemplo n.º 8
0
async def get_user_id_no_privacy(message: types.Message):
    """
    Handler for message forwarded from other user who doesn't hide their ID
    :param message: Telegram message with "forward_from" field not empty
    """
    if message.forward_from.is_bot:
        msg = f"This bot's ID is <code>{message.forward_from.id}</code>"
    else:
        msg = f"This user's ID is <code>{message.forward_from.id}</code>"
    if message.sticker:
        msg += f"\nAlso this sticker's ID is <code>{message.sticker.file_id}</code>"
    await message.reply(msg)
    logs.track("Check user or bot")
Exemplo n.º 9
0
async def show_help(message: types.Message):
    """
    /help command handler for all chats
    :param message: Telegram message with "/help" command
    """
    await message.answer('Use this bot to get ID for different entities across Telegram:\n'
                         '• Forward message from channel to get channel ID;\n'
                         '• Forward message from user to get their ID (unless they restrict from doing so);\n'
                         '• Send a sticker to get its file_id (currently you can use the sticker\'s file_id with any bot);\n'
                         '• Add bot to group to get its ID (it will even tell you when you migrate from group to supergroup);\n'
                         '• Use inline mode to send your Telegram ID to any chat.\n\n'
                         'Source code: https://github.com/MasterGroosha/my-id-bot.')
    logs.track("/help")
Exemplo n.º 10
0
async def new_chat(message: types.Message):
    """
    Handler for "new_chat_members" action when bot is added to chat.
    A special check is performed so that this handler will only be fired once per chat, when
    bot itself is added to group (bot's ID is the first part of token before ":" symbol)
    :param message: Telegram message with "new_chat_members" field not empty
    """
    for user in message.new_chat_members:
        if user.id == bot.id:
            await bot.send_message(
                message.chat.id,
                f"This {message.chat.type} chat ID is <code>{message.chat.id}</code>"
            )
            logs.track("Added to group")
Exemplo n.º 11
0
async def inline_message(query: types.InlineQuery):
    """
    Handler for inline queries
    :param query: Inline query with any text
    """
    result = types.InlineQueryResultArticle(
        id=".",
        title=f"Your ID is {query.from_user.id}",
        description="Tap to send your ID to current chat",
        input_message_content=types.InputTextMessageContent(
            message_text=f"My Telegram ID is <code>{query.from_user.id}</code>"
        )
    )
    # Do not forget about is_personal parameter! Otherwise all people will see the same ID
    await bot.answer_inline_query(query.id, [result], cache_time=3600, is_personal=True)
    logs.track("Inline mode")