使用 python-telegram-bot 触发函数而不使用命令

问题描述 投票:0回答:1

我是初学者,正在开发电报机器人。我希望每当消息发送到电报组时就调用函数“AP”。但现在我的代码是这样的,只要命令 /AP“后跟消息”发送到聊天,就会触发 MessageHandler。我怎样才能实现它?

async def AP(update: Update, context: ContextTypes.DEFAULT_TYPE):
    # function implementation
if __name__ == '__main__':
    application = ApplicationBuilder().token('1234:ABCDEF9101112').build()
    
    start_handler = CommandHandler('start', start) #To tell your bot to listen to /start commands,

    AP_handler = MessageHandler(filters.ALL, AP)#Autosend/AutoPost the messages
    
    application.add_handler(start_handler) 
    application.add_handler(AP_handler)
  
    application.run_polling() # application.run_polling() runs the bot until you hit CTRL+C

我尝试更改过滤器,但无法自动调用 AP 函数。我认为由于我缺乏知识,我不知道到哪里去寻找问题的解决方案。

python bots telegram telegram-bot python-telegram-bot
1个回答
0
投票

首先,要接收聊天中的所有消息,您需要将机器人添加为管理员,或者需要禁用隐私模式(https://core.telegram.org/bots/features#privacy-mode )。 要禁用隐私模式,请在 Telegram 中访问 BotFather (https://t.me/BotFather),发送命令 /mybots ,然后选择您的机器人,选择“机器人设置”,选择“组隐私”并在那里禁用它(“关闭”)(如果您还没有这样做)。 然后您需要将该机器人重新添加到您的群组中。

© www.soinside.com 2019 - 2024. All rights reserved.