Telegram Bot: Proactive New Member Verification Command

Background

While reviewing the bot's logs today, I found 4 exceptions.

The exact cause of the exception is not fully clear. I suspect it's a network issue—the bot program timed out when requesting the Telegram server to set user permissions.

This creates a vulnerability: these 4 new users were not muted and did not go through the human verification process.

Requirement

Need the ability to proactively initiate the same flow for a specific user as when a new member joins the group (mute, require completing human verification in a private chat with the bot, then unmute).

Practice

After reading the code, I found that in the new member verification flow, the track_chat_member function only requires chat and user as external data. It's well-suited for refactoring.

So once again, we program with GPT in mind
对 track_chat_member 函数进行重构, 
目的是为机器人新增一个命令 /new_member_verify 
这样可以主动将某个成员当作好像新成员一样进行验证流程. (禁言, 与机器人私聊, 回答问题, 等等)
The result was very satisfying. Not only did it refactor the function as I wanted and generate the new new_member_verify function, but the new function also considered the identity of the user invoking the command (must be an admin or group owner).
The only thing is that GPT modified parts of the original program that didn't need changing (for example, rewording the text descriptions in comments, rewording the prompt messages, ...). I didn't just select all, copy, and paste—I reviewed the code and manually merged. Since the original program has a small code base and the modifications involved were minimal, the merging work was relatively easy.

During debugging, I found errors in GPT's code.
The code generated by GPT assumed that the get_chat_member function could accept not only a userid but also a username, and even wrote related code in a pretentious way. 
It threw an error when a username was passed in. I then checked the official documentation and found that only userid is supported.

I made a few small optimizations of my own. 
All prompt messages sent by the bot in the group are automatically deleted after a period of time. 
In the group, the /start command is ignored.

Push to Github



========

Postscript

Beyond solving the original problem, this new_member_verify command has another benefit:
When your bot has muted a certain member, and the bot's subsequent processing errors out and stalls, you have to terminate the program. Or it could just be that you happened to terminate the program after a member was muted but before they had time to answer the question. 
When you restart the bot, the bot has no record of this member's verification question and answer. In other words, this member is muted in the group, but sending /start to the bot will not trigger a question.
At this point, as a group admin, you can use the new_member_verify command to restart the verification flow for this member.

========

Related Recommendations



Comments