pythontelegram-botaiogram

How to get channel ids?


I'm trying to write a bot for telegram that would send the id of this channel in response to a sent channel, but I don't understand how to do this if the channel was sent via KeyboardButtonRequestChat. Could you help me with this somehow?

from aiogram import Bot as AiogramBot, Dispatcher, executor, types
from aiogram.types.reply_keyboard import (ReplyKeyboardMarkup, KeyboardButton, KeyboardButtonRequestChat,
                                          KeyboardButtonRequestUser)

bot_token = ""

bot = AiogramBot(bot_token)
dp = Dispatcher(bot)


@dp.message_handler(content_types=['text'])
async def text_handler(message: types.Message):
    await message.answer("I'm here", reply_markup=ReplyKeyboardMarkup(
        [
            [
                KeyboardButton("test1", request_chat=KeyboardButtonRequestChat(
                    request_id=1,
                    user_is_bot=False,
                    chat_is_channel=True,
                    chat_is_forum=False
                )),


                KeyboardButton("test2", request_user=KeyboardButtonRequestUser(
                    request_id=2,
                    user_is_bot=False,
                    chat_is_channel=False,
                    chat_is_forum=False
                )),
            ]
        ]
    ))

I tried adding a handler, but apparently I'm doing something wrong

@dp.message_handler(content_types=['text'], chat_type=['channel'])
async def handle_channel_text(message: types.Message):
    chat_id = message.chat.id
    channel_id = message.chat.id
    await bot.send_message(chat_id, f"Channel ID: {channel_id}")

if __name__ == "__main__":
    executor.start_polling(dp)

Thank you in advance.

This may be a stupid question, but I've only recently been dealing with this.


Solution

  • In aiogram 3.x you can do it something like that

    import asyncio
    
    from aiogram import Bot, Dispatcher, F
    from aiogram.filters.callback_data import CallbackData
    from aiogram.fsm.storage.memory import MemoryStorage
    from aiogram.filters import Command, BaseFilter
    from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton, CallbackQuery, ReplyKeyboardMarkup, \
        KeyboardButtonRequestChat
    from aiogram.utils.keyboard import InlineKeyboardBuilder
    from aiogram.types import (
        KeyboardButton,
        Message)
    
    
    TOKEN = ""
    
    bot = Bot(TOKEN)
    dp = Dispatcher(storage=MemoryStorage())
    
    
    @dp.message(F.text.contains("/start"))
    async def text_handler(message: Message):
        await message.answer("I'm here", reply_markup=ReplyKeyboardMarkup(keyboard=
            [
                [
                    KeyboardButton(text="test1", request_chat=KeyboardButtonRequestChat(
                        request_id=1,
                        user_is_bot=False,
                        chat_is_channel=True,
                        chat_is_forum=False
                    )),
                    KeyboardButton(text="test2", request_chat=KeyboardButtonRequestChat(
                        request_id=2,
                        user_is_bot=False,
                        chat_is_channel=False,
                        chat_is_forum=False
                    )),
                ]
            ]
        ))
    
    
    @dp.message(F.chat)
    async def handle_request_chat(msg: Message):
        chat_info = msg.chat_shared.chat_id
        await bot.send_message(msg.from_user.id, f"You choose a chanel with ID: {chat_info}")
    
    async def main():
        await dp.start_polling(bot)
    
    if __name__ == "__main__":
        asyncio.run(main())
    

    P.S. This code can also get a chat id