The problem with writing a bot on aiogram 3.x

37 Views Asked by At

I have two commands in admin_panel.py: /add_chat and /add_channel. I just can't figure out one thing - why /add_channel is being processed, but /add_chat doesn't want to work.

from aiogram import Bot, Router, F
from aiogram.types import Message
from aiogram.filters import Command
from config import TOKEN, ADMIN_IDS
from db_manager import db
 
bot = Bot(token=TOKEN)
admin_router = Router()
 
@admin_router.message(Command(commands=["add_chat"]), F.chat.type == "private", F.from_user.id.in_(ADMIN_IDS))
async def add_chat(message: Message):
    args = message.text.split()[1:]
    if not args:
        await message.answer("Use the command in the format: /add_chat <chat link>")
        return
 
    chat_link = args[0]
    if chat_link.startswith("https://t.me/"):
        chat_username = chat_link.split('/')[-1]
        try:
            chat = await bot.get_chat(chat_username)
            db.add_chat(chat_link, "@" + chat.username, chat.id)
            await message.answer(f"Chat {chat_link} added with ID {chat.id}")
        except Exception as e:
            await message.answer(f"Error adding chat: {e}")
    else:
        await message.answer("An incorrect chat link was provided.")
 
@admin_router.message(Command(commands=["add_channel"]), F.chat.type == "private", F.from_user.id.in_(ADMIN_IDS))
async def add_channel(message: Message):
    args = message.text.split()[1:]
    if len(args) < 2:
        await message.answer("Use the command in the format: /add_channel <channel link> <chat link>")
        return
 
    channel_link, chat_link = args
    try:
        chat_username = chat_link.split('/')[-1]
        chat = await bot.get_chat(chat_username)
        chat_id = chat.id
 
        channel_username = channel_link.split('/')[-1]
        channel = await bot.get_chat(channel_username)
        channel_id = channel.id
 
        db.add_channel(channel_link, "@" + channel.username, channel_id)
        db.link_channel_to_chat(chat_id, channel_id)
        await message.answer(f"Channel {channel_link} added and linked to chat {chat_link}")
    except Exception as e:
        await message.answer(f"Error adding channel: {e}")

I can share the entire code if needed.

Tried various approaches.

0

There are 0 best solutions below