I wrote a simple aiogram chat-bot code, which can write down messages to mongodb database:
`
@router.message(Command(‘memorize’), IsChat([‘group’, ‘supergroup’, ‘chat’, ‘channel’]), IsAdmin(admin_id))
async def memorize_handler(message: Message) -> None:
reply = message.reply_to_message
if not reply:
await message.answer(“⚙ Can’t find user”)
return
text = reply.text
if reply.from_user.username:
username = reply.from_user.username
else:
username = reply.from_user.full_name
try:
user_id = reply.from_user.id
mention = reply.from_user.mention_html(reply.from_user.first_name)
memory_id = str(uuid.uuid4())
memorize(memory_id=memory_id, text=text, username=username, user_id=user_id)
await message.answer(f'???? Succesfully saved message from {mention}')
except:
await message.answer(f'❗ Couldnt save message from {mention}')
`
Filter code here:
`
class IsChat(BaseFilter):
def __init__(self, chat_type: str | List[str]) -> None:
self.chat_type = chat_type
async def __call__(self, message: Message) -> bool:
if isinstance(self.chat_type, str):
return message.chat.type == self.chat_type
return message.chat.type in self.chat_type
`
The problem is, when you call bot from channel-chat – he doesn’t handle updates for some reason.
However, bot still works in private chats, groups and supergroups.
I already tried to expand IsChat filter with ‘channel’ and ‘chat’, but it didn’t work out
Max is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.