【问题标题】:Telegram bot (pyTelegramBotAPI) does not handle new user joining group电报机器人(pyTelegramBotAPI)不处理新用户加入组
【发布时间】:2020-09-24 23:27:55
【问题描述】:

我最近使用 pyTelegramBotAPI (telebot) 创建了一个简单的电报机器人。 我添加了一个消息处理程序,它应该处理 每条 消息,包括新用户加入时出现在组中的消息,它们仍然是 Message 对象和非空 new_chat_members 属性。

import telebot
bot = telebot.TeleBot(TOKEN)

[...]

@bot.message_handler(func=lambda m: True)
def foo(message):
    bot.send_message(message.chat.id,"I got the message")



bot.polling()

即便如此,当我添加新用户时,机器人不会回复“我收到消息”字符串,尽管它会捕获其他消息。

为什么会这样?这是消息处理程序的问题吗?是否有更通用的处理程序可以确保捕获每个更新?

谢谢

【问题讨论】:

    标签: python telegram telegram-bot py-telegram-bot-api


    【解决方案1】:

    您应该将“new_chat_members”指定为content-types

    这是一个欢迎新用户的示例工作 sn-p:

    import telebot
    bot = telebot.TeleBot(TOKEN)
    
    @bot.message_handler(content_types=[
        "new_chat_members"
    ])
    def foo(message):
        bot.reply_to(message, "welcome")
    
    bot.polling()
    

    【讨论】:

    • 谢谢。我仍然想知道如果您指定该过滤器,它为什么会处理这些消息,但当您要求他处理所有事情时它不会。
    • 是的,问题是content_types 默认为['text'](根据文档)。因此,如果您希望在同一处理程序中捕获多个更新类型,则必须指定要捕获的所有 content-types
    • 所以即使你指定bot.message_handler(func=lambda m: True)。仍然有一个隐含的content-types 限制,将其范围仅限于文本消息。
    猜你喜欢
    • 1970-01-01
    • 2019-09-12
    • 1970-01-01
    • 2022-11-10
    • 2019-02-04
    • 2017-08-05
    • 1970-01-01
    • 2017-08-18
    • 2018-03-31
    相关资源
    最近更新 更多