【问题标题】:Aiogram bot to answer my message in private chats but to reply my message in group chatsAiogram 机器人在私人聊天中回复我的消息,但在群聊中回复我的消息
【发布时间】:2022-11-11 04:03:57
【问题描述】:

对于电报 api,Message.reply_html("Hello") 的报价在群组中默认为 True,在私人聊天中默认为 False (https://docs.python-telegram-bot.org/en/v20.0a4/telegram.message.html#telegram.Message.reply_html)。 aiogram 有类似的特征吗?

基本上我希望机器人不在私人聊天中回复我的消息,而是在群聊中回复我的消息。这是因为在频道中创建讨论组时,为了让机器人的回复显示在讨论组中,它必须回复发送消息的人的消息。

我知道 aiogram 有 message.reply 回复用户,无论他是否在一个组中,以及 message.answer 回答用户,无论他是否在一个组中。还有一个聊天类型过滤器,用于过滤发送给私人用户的消息和发送给超级组的消息,但要做到这一点,我需要重复我的代码两次,这在唯一的区别是消息和回复时有点毫无意义?

我试过message.reply('text', reply=False),但这只是从群组和私人聊天中删除了回复,相当于message.answer。任何帮助表示赞赏!谢谢!

【问题讨论】:

    标签: telegram telegram-bot aiogram


    【解决方案1】:

    对不起,我的英语不好。 Aiogram 有Filters,即aiogram.dispatcher.filters.ChatTypeFilter,继承BounbdFilter。我们可以使用它为我们的任务创建可定制的过滤器。您可以在文档中找到更多信息。我写了一个只在一个组中工作的机器人的简单例子。在私人聊天中,他不工作。

    from aiogram.dispatcher.filters import BoundFilter
    from aiogram import types
    
    # filter.py
    class IsGroup(BoundFilter):
        async def check(self, message: types.Message) -> bool:
            return message.chat.type in (
                types.ChatType.GROUP,
                types.ChatType.SUPER_GROUP,
            )
    
    
    # handlers.py
    @dp.message_handler(IsGroup(), content_types=types.ContentType.NEW_CHAT_MEMBERS)
    async def new_member(message: types.Message):
        members = ", ".join([m.get_mention(as_html=True) for m in message.new_chat_members])
        await message.reply(f"Hello, {members}.")
    

    我们正在创建一个聊天,如果我们添加新成员,机器人会写“你好,{member}”。如果你只想在聊天中使用机器人,你需要添加装饰器@dp.message_handler是组()并使其成为第一个论点

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-20
      • 2020-12-09
      • 1970-01-01
      • 1970-01-01
      • 2019-06-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多