我最近使用pyTelegramBotAPI(telebot)为电报创建了一个简单的机器人。我添加了一个消息处理程序,该消息处理程序应该处理every消息,包括当新用户加入时出现在组中的消息处理程序,它们仍然是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()
即使如此,当我添加新用户时,该漫游器也不会回答“我收到消息”字符串,尽管它确实捕获了其他消息。
为什么会这样?这是关于消息处理程序的问题吗?也许有一个更通用的处理程序可以确保捕获每个更新?
谢谢
您应将“ new_chat_members
”指定为content-types
。
这里是欢迎新用户使用的示例代码工作代码:
import telebot
bot = telebot.TeleBot(TOKEN)
@bot.message_handler(content_types=[
"new_chat_members"
])
def foo(message):
bot.reply_to(message, "welcome")
bot.polling()