我的电报机器人禁止使用的用户不会被禁止吗?

发布于 2025-01-20 12:23:32 字数 4205 浏览 0 评论 0原文

TL;DR:“尝试”中列出的方法未按预期工作。每当我运行这些 Python 方法时,它们最终只会踢用户(或者最多禁止它们 2-3 分钟),无论我是否包含 until_date 。所有组权限均已启用,并且在 BotFather 中,组隐私模式和“允许组?”被禁用。


我是 Stack Overflow 的新手,所以我不确定我的问题的格式是否正确,以及冗长的描述是否更可取。

背景:我有一个(私人)Telegram 群组,拥有 700 多名用户。还有另外两名管理员和五个机器人(Rose群组帮助Combot 和我编写的两个)。在过去的几周里,我一直面临着通过我自己的机器人禁止用户的问题,而 Rose 和 Group Help 机器人似乎能够做到这一点。

问题:我的机器人限制群组的条目数量(每个用户),以避免有人潜伏,或者在我共享邀请链接时突袭群组。如果人们无法解决验证码并在进入后的前 5 分钟内展示自己,团队将帮助机器人和 Rose 将其踢出。我保留 Combot 是为了避免让已知的垃圾邮件发送者出现在我的组中。我面临的问题是,每当我自己的机器人发出禁令时,用户就会被踢出。我尝试了各种方法来禁止我的 Python 脚本中的用户,但没有任何效果。我尝试从 python-telegram-bot 切换到 AIOgram,但仍然不起作用。我不确定这是否是由于机器人之间的冲突(这没有多大意义)、错误的方法引起的,或者是否是与组/帐户相关的问题。我在“测试组”中尝试了我的机器人,效果很好。

来自组日志频道的异常示例的屏幕截图因为我可以'还没有嵌入图片,这是 imgur 链接。

使用 AIOgram 的当前(伪)代码的一部分

@dp.message_handler(content_types=["new_chat_members"])
async def newUser(message: types.Message):
    for user in message.new_chat_members:
        con = psycopg2.connect(...)
        #[...SQL query fetching number of entries of a user...]

        if "DB is empty":
            #..Initialize..

        else:
            cur.execute(sql_script) #cur is the cursor
            #Returns a list with at most one tuple, where the first instance is the number of entries
            entries_list = cur.fetchall()

            if len(entries_list) == 0 or None:
                #Adds user to a table in Heroku DB and initializes entry = 1
                insertUser(tablename, userid, 1)
                await bot.send_message(channel_id, log_message)
                con.close()
            
            elif entries_list[0][0] < 2:
                updateUserEntries(tablename, entries_list[0][0] + 1, userid)
                await bot.send_message(channel_id, log_message)
                con.close()

            else: #Ban on third entry
                await chat.kick(userid, until_date = datetime.now() + timedelta(hours = 6))
                await bot.send_message(chat.id, ban_message)
                await bot.send_message(channel_id, log_message)
                #Removes user from the table (i.e. resets the entry count)
                removeUser(tablename, userid)
                con.close()

尝试:我尝试用来禁止用户的方法包括:

  • update. effective_chat.ban_member(chat_id, user_id) (python-telegram-bot),
  • bot.kick_chat_member(chat_id, user_id) (python-telegram-bot),
  • bot.ban_chat_member(chat_id, user_id)python-telegram-bot – 建议我使用 kick_chat_member)和
  • message.chat.kick(user_id) (aiogram – 每当新用户通过以下方式加入群组时都会传递消息 Dispatcher.message_handler(content_types='new_chat_members'))。

我本来只是想暂时禁止用户,所以还应该有一个额外的 until_date 参数。但我也尝试永久禁止它们,根据 文档 任何时间少于 30秒或超过 366 天将被视为永久禁令。什么都没起作用。 (顺便说一句,该脚本总体上运行良好。我不会共享完整的脚本,因为我不喜欢它被公开,而且问题也与整个脚本无关。)

更多详细信息:我的机器人已启用除添加管理员和“保持匿名”之外的所有组权限。在 BotFather 中,它具有群组隐私模式和“允许群组?”禁用(因为我只将它用于我的组)。目前,我用于机器人的库(Python)是 AIOgram。以前,它都是使用 python-telegram-bot 编写的。我将它与 psycopg2 结合使用,将用户数据存储在 Heroku 的数据库中。有一次,我什至尝试在我的数据库中创建一个“禁令表”,但它仍然无法将用户拒之门外,因为禁令实际上并没有禁止人们。该禁令实际上只是起到“踢”的作用。

我正在考虑设置一个 webhook,但我对 Flask 或 Django 以及一般的 webhook 并不熟悉。所以这可能需要我一些时间。但是,我不确定它会产生影响,因为除了速度和内存方面之外,它应该不会有太大差异。

我不确定我做错了什么。我尝试以我能做的一切方式解决这个问题(考虑到我对 Telegram API 和 Telegram python 库,尤其是 AIOgram 还很陌生)。

如果有人对我如何解决这个问题有任何想法,我将非常感激!

(旁注:我一个多星期前联系了 Telegram 支持,但没有收到任何回复。)

TL;DR: Methods listed in "Attempts" are not working as they should. Whenever I run those Python methods, they end up only kicking the user (or maybe banning them for at maximum 2-3 minutes), regardless if I include until_date or not. All group permissions are enabled and in BotFather both group privacy mode and "allow groups?" are disabled.


I am new to Stack Overflow, so I'm not sure if the formatting of my question is fine and if a lengthy description is preferable.

Context: I have a (private) Telegram group with a little bit over 700 users. There are two other admins and five bots (Rose, Group Help, Combot and two that I coded). In the past couple of weeks I have been facing issues with banning users through my own bot, while Rose and Group Help bots seem to be able to.

Issue: My bot limits the amount of entries (per user) to the group to avoid people lurking, or just raiding the group when I share an invitation link. Group help bot and Rose kick people out if they don't solve the captcha and present themselves in the first 5 minutes upon entering. I keep Combot to avoid letting known spammers in my group. The problem I'm facing is that whenever the ban is issued by my own bot, the user will simply be kicked out. I tried various methods to ban a user in my Python script, but nothing worked. I tried switching from python-telegram-bot to AIOgram, but it still doesn't work. I am not sure if it's caused by a conflict between bots (which wouldn't make much sense), wrong methods, or if it's a group/account-related problem. I tried my bot in a "test group" and it worked fine.

Screenshot of an example of the anomaly from the group logs channel: Since I can't embed pictures yet, here is the imgur link.

Part of the current (pseudo)code using AIOgram:

@dp.message_handler(content_types=["new_chat_members"])
async def newUser(message: types.Message):
    for user in message.new_chat_members:
        con = psycopg2.connect(...)
        #[...SQL query fetching number of entries of a user...]

        if "DB is empty":
            #..Initialize..

        else:
            cur.execute(sql_script) #cur is the cursor
            #Returns a list with at most one tuple, where the first instance is the number of entries
            entries_list = cur.fetchall()

            if len(entries_list) == 0 or None:
                #Adds user to a table in Heroku DB and initializes entry = 1
                insertUser(tablename, userid, 1)
                await bot.send_message(channel_id, log_message)
                con.close()
            
            elif entries_list[0][0] < 2:
                updateUserEntries(tablename, entries_list[0][0] + 1, userid)
                await bot.send_message(channel_id, log_message)
                con.close()

            else: #Ban on third entry
                await chat.kick(userid, until_date = datetime.now() + timedelta(hours = 6))
                await bot.send_message(chat.id, ban_message)
                await bot.send_message(channel_id, log_message)
                #Removes user from the table (i.e. resets the entry count)
                removeUser(tablename, userid)
                con.close()

Attempts: The methods I tried using to ban users with are:

  • update.effective_chat.ban_member(chat_id, user_id) (python-telegram-bot),
  • bot.kick_chat_member(chat_id, user_id) (python-telegram-bot),
  • bot.ban_chat_member(chat_id, user_id) (python-telegram-bot – which suggested me to use kick_chat_member instead) and
  • message.chat.kick(user_id) (aiogram – where message is passed whenever a new user joins the group through Dispatcher.message_handler(content_types='new_chat_members')).

I originally only wanted to temporarily ban the users, so there should be an extra until_date parameter too. But I also tried permanently banning them, which according to the documentation any time less than 30 seconds or more than 366 days counts as a permanent ban. Nothing worked. (By the way, the script in general works and runs great. I am not sharing the full script because I am not comfortable with it being public, and the problem also does not pertain to the whole script anyways.)

Further details: My bot has all group permissions enabled except for adding admins and "remain anonymous". In BotFather it has both group privacy mode and "allow groups?" disabled (because I only use it for my group). At the moment the library I'm using for the bot (in Python) is AIOgram. Previously, it was all written using python-telegram-bot. I am using it in conjunction with psycopg2 to store user data in Heroku's database. At one point I even tried creating a "ban table" in my database, but it still failed to keep the users out since the ban didn't actually ban people. The ban actually only acts as a "kick".

I was thinking about setting up a webhook, but I am not familiar with Flask or Django, and webhooks in general. So that might take me some time. However, I'm not sure it would make a difference since it should not differ much, except maybe speed and memory wise.

I am not sure what I'm doing wrong. I tried to solve the problem in every way that I am capable of (considering that I am pretty new to Telegram API and Telegram python libraries, especially AIOgram).

If anyone has any ideas on how I can go about solving this issue, I would really appreciate it!

(Sidenote: I contacted Telegram support more than a week ago, but I haven't received any response.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

勿挽旧人 2025-01-27 12:23:32

对我来说,这里出了什么问题并不明显,但我有一些提示/评论:

  • 您使用哪个库来发出 api 请求应该是无关紧要的。你甚至可以通过浏览器/curl/任何方式手动发出请求,如果你喜欢
  • webhooks 与此无关 - 你如何获取更新独立于向 api 发出请求
  • python-telegram-bot将 timezone-naive 日期时间解释为 UTC - 这可能会影响您传递 until_date 的方式

免责声明:我目前是 的维护者python-telegram-bot

It's not obvious to me what goes wrong here, but I have a few hints/remarks:

  • which library you use to make the api request should be irrelevant. you can even make the request manually via the browser/curl/whatever if you like to
  • webhooks have nothing to do with this - how you fetch updates is indpendent of making requests to the api
  • python-telegram-bot interprets timezone-naive datetimes as UTC - that may have an effect how you pass the until_date

Dislaimer: I'm currently the maintainer of python-telegram-bot.

打小就很酷 2025-01-27 12:23:32

谢谢,@callmestag的讲话!我最终如下所述“解决”了问题。虽然,我仍然不确定为什么这起作用了,但是使用了问题中列出的任何方法。


免责声明:这不是解决问题的有效方法,我不保证它会起作用。我也没有加入该小组的人的交通量很高,因此这些解决方案可能对您不起作用。

部分(非 - )解决方案:我认为我的机器人和电报之间存在一个“幻影问题”。不管我是否包括,直到_date,以某种方式电报(或我不确定Python,我不确定)是否将“踢”方法理解为“禁令”。 (此外,电报支持是不存在的)。因此,我想到了三种(或四个)替代方法来禁止某人而不必实际禁止他们:

  • 解决方案1 ​​:在数据库中创建一个表格,其中包含您最终将禁止的所有用户。对于您的“禁令”功能,请包括一个聆听新聊天成员的消息处理程序。每当新用户加入时(使用new_chat_members捕获事件),使其通过您创建的表中的所有入口进行运行(例如,您可以从{bantableName}中使用选择count(*)其中userId = {userId}),以便告诉您是否禁止用户。如果查询返回大于0的值,则只需在语句中制作并将用户踢出即可。 (显然,您可以通过手动添加直至_DATE来使其变得更加复杂。缺点是,由于运行SQL查询时会有一些延迟/延迟,因此无法将用户排除在外。
  • 解决方案2 :此解决方案仍然要求您在数据库中创建一个被禁止的用户表。但是,不要让人们直接加入您的小组,而是首先将他们引导到您的机器人聊天中。为“/start”添加命令处理程序。每当用户启动机器人时,使其通过表条目运行,并根据结果的不同,该机器人将使用组链接回复或消息通知他们无法加入(或什么都没有)。
  • 解决方案3 :设置Webhook。我还不知道该怎么做,所以您应该在互联网上查找它。这将需要您至少学习烧瓶或Django。与其他方面相比,这绝对是一个更好的解决方案。通常,此“禁令问题”与您是否使用Webhook无关(例如 callmestag 说)。但是,与我的下一个建议(以某种方式起作用)相比,这绝对是一种更好的做法。
  • 解决方案4 (验证) :我很好奇,想要一个更直接的解决方案,所以我决定只手动尝试

进一步的疑问:我不确定解决方案4是否有效,但是直接使用python-telegram-botaiogram 不使用。我没有尝试 telethonpyrogram 。它们应该相似,但是它们还允许您作为用户登录(即它将通过机器人开始您的帐户会话),这可能有效。

更新:它不起作用。被禁止的用户仍然不会被禁止。它仅在我出于某种原因手动进行时起作用。整个事情确实没有任何意义。就像是在轮询时无法正常工作一样,但是如果我手动发送www.api.telegram.org/bot&bot&token&token&token&ggt;/banchatmember?chat_id =&lt =&lt = chatid>>>

Thank you, @CallMeStag for your remarks! I ended up "solving" the problem as I describe below. Although, I am still not sure why this worked but using any of the methods I listed in the question didn't.


Disclaimer: This is not an efficient way to approach the issue, and I do not guarantee it will work. I also don't have a high traffic of people joining the group, so these solutions might not work for you.

Partial (non-)solutions: I think there is a "phantom issue" between my bot and Telegram. Somehow Telegram (or Python, I'm not sure) doesn't understand the "kick" methods as "ban", whether I include until_date or not. (Also, Telegram support is non-existent). So, I thought of three (or four) alternative ways to ban someone without having to actually ban them:

  • Solution 1: Create a table in your database that will contain all the users that you will eventually ban. For your "ban" function, include a message handler that listens for new chat members. Whenever a new user joins (use new_chat_members to catch the event), make it run through all the entries in the table you created (e.g. you could use SELECT COUNT(*) FROM {BANTABLENAME} WHERE USERID = {USERID}) so that it will tell you if the user is banned. If the query returns a value greater than 0, you can just make an IF statement and kick the user out. (You can obviously make it a bit more complicated by manually adding an until_date). The downside is that it can fail to keep a user out since there is a bit of latency/delay when you run SQL queries.
  • Solution 2: This solution still requires that you create a table of banned users in your database. But instead of letting people join your group directly, direct them to your bot's chat first. Add a command handler for "/start". Whenever a user starts the bot, make it run through the table entries and depending on the result, the bot will either reply with the group link or a message informing them that they can't join (or nothing).
  • Solution 3: Set up a webhook. I don't know how to do it yet, so you should look it up on the internet. It will require you to learn at least Flask, or Django. This is definitely a nicer solution compared to the other ones. Usually, this "ban problem" does not pertain to whether you're using a webhook or not (like CallMeStag said). But compared to my next proposal (which works somehow), this is definitely a better practice.
  • Solution 4 (Verified): I was curious and wanted a more direct solution, so I decided I would just try manually making requests through www.api.telegram.org/bot<TOKEN>/METHOD?PARAMETERS using GET and POST HTTPS methods in Python. This is definitely not an ideal solution if you have a high traffic of users. Because you might get errors due to too many requests. Also, depending on what you're trying to do, it might be too slow. So definitely use webhooks instead. In my case, I only need to POST the ban using the banChatMember method, which also happens with a pretty low frequency. So I should not encounter problems. (I will update if I do).

Further doubts: I am not sure why solution 4 works, but directly banning with python-telegram-bot or AIOgram doesn't. I did not try Telethon or Pyrogram. They should be similar, but they also allow you to log-in as a user (i.e. it will start a session of your account through the bot), which could potentially work.

Update: It doesn't work. Banned users still don't get banned. It only works when I manually do it for some reason. The whole thing really doesn't make any sense. It's like it doesn't work while polling, but it works if I manually send out www.api.telegram.org/bot<token>/banChatMember?chat_id=<chatid>&user_id=<userid>.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文