1

我使用此代码从我的群组中删除用户加入/留言它工作正常,但唯一无法删除的消息是“用户通过邀请链接加入群组”如何删除通过邀请链接进入的人的加入消息关联?

import telebot

TOKEN = "5299828032:AAH9J-Z92tHI3GY_6mJlsyMlwIx-ILMKf5I"

bot = telebot.TeleBot(TOKEN)


@bot.message_handler(content_types=['new_chat_members'])
def delete_join_message(m):
    
    # If bot is not admin, then it will not be able to delete message.
    try:
        bot.delete_message(m.chat.id,m.message_id)
    except:
        if m.new_chat_member.id != bot.get_me().id:
            bot.send_message(m.chat.id,"Please make me an admin in order for me to remove the join and leave messages on this group!")
        else:
            bot.send_message(m.chat.id,"Hi! I am your trusty GroupSilencer Bot! Thanks for adding me! To use me, make me an admin and I will be able to delete all the pesky notification when a member joins or leaves the group!")
        
        
@bot.message_handler(content_types=['left_chat_member'])
def delete_leave_message(m):

    # If bot is the one that is being removed, it will not be able to delete the leave message.
    if m.left_chat_member.id != bot.get_me().id:
        try:
            bot.delete_message(m.chat.id,m.message_id)
        except:
            bot.send_message(m.chat.id,"Please make me an admin in order for me to remove the join and leave messages on this group!")

    

bot.infinity_polling()  
4

1 回答 1

1
 from telegram.ext import Updater, CallbackContext
from telegram import Update
from telegram.ext import MessageHandler, Filters


API_KEY = API_KEY

def onjoin(update: Update, context: CallbackContext):
    context.bot.delete_message(chat_id=update.message.chat_id,message_id=update.message.message_id)
     

def main():
    updater = Updater(API_KEY, use_context=True)
    updater.start_polling()
    dp = updater.dispatcher
    dp.add_handler(MessageHandler(Filters.status_update.new_chat_members,onjoin))
    dp.add_error_handler(MessageHandler(Filters.status_update.left_chat_member,onjoin))


main()

您可以使用 python-telegram-bot 中的此脚本获取有关加入或留下消息的任何特定消息

于 2022-02-15T10:58:22.440 回答