使用 python-telegram-bot 创建欢迎机器人
Creating a Welcome Bot Using python-telegram-bot
我主持一个电报组已经有一段时间了,使用 python-telegram-bot 包没有遇到任何问题。我真的很喜欢它。但是,当新用户加入时,我似乎无法收到有效的“欢迎消息”。
现在,我已经尝试像使用命令和消息处理程序那样构建函数:
def welcome(update,context):
#name = from_user.username
#update.message.reply_text("""Welcome blah blah blah to the group!""")
然后像这样称它为我的主要函数:
dp = updater.dispatcher
dp.add_handler(ChatMemberHandler(welcome, ChatMemberHandler.CHAT_MEMBER))
updater.start_polling(allowed_updates=Update.ALL_TYPES)
updater.idle()
但是我收到一个错误:AttributeError: 'NoneType' object has no attribute 'reply_text'
所以我猜弹出的“成员已加入群组”不被视为消息。但我不知道如何提取该事件或使用哪个处理程序。任何帮助将不胜感激!谢谢!
正如 thethiny 已经指出的那样,聊天成员更新没有关联消息:update.message
将是 None
,而 update.chat_member
将是 ChatMemberUpdated
的实例。请注意,Message.reply_text
只是 Bot.send_message(chat_id=message.chat.id, ...)
的快捷方式,因此只要您拥有 chat_id
,您就可以使用例如context.bot.send_message
- 你可以从 ChatMemberUpdated.chat
得到 chat_id
。事实上,您仍然可以使用 PTB 快捷方式,例如update.effective_chat.send_message
.
请查看
的文档
ChatMemberUpdated
(official and PTB)
Update.effective_chat
Chat.send_message
以及 PTB 提供的 chatmemberbot.py
example。
免责声明:我目前是 python-telegram-bot
的维护者
我主持一个电报组已经有一段时间了,使用 python-telegram-bot 包没有遇到任何问题。我真的很喜欢它。但是,当新用户加入时,我似乎无法收到有效的“欢迎消息”。
现在,我已经尝试像使用命令和消息处理程序那样构建函数:
def welcome(update,context):
#name = from_user.username
#update.message.reply_text("""Welcome blah blah blah to the group!""")
然后像这样称它为我的主要函数:
dp = updater.dispatcher
dp.add_handler(ChatMemberHandler(welcome, ChatMemberHandler.CHAT_MEMBER))
updater.start_polling(allowed_updates=Update.ALL_TYPES)
updater.idle()
但是我收到一个错误:AttributeError: 'NoneType' object has no attribute 'reply_text'
所以我猜弹出的“成员已加入群组”不被视为消息。但我不知道如何提取该事件或使用哪个处理程序。任何帮助将不胜感激!谢谢!
正如 thethiny 已经指出的那样,聊天成员更新没有关联消息:update.message
将是 None
,而 update.chat_member
将是 ChatMemberUpdated
的实例。请注意,Message.reply_text
只是 Bot.send_message(chat_id=message.chat.id, ...)
的快捷方式,因此只要您拥有 chat_id
,您就可以使用例如context.bot.send_message
- 你可以从 ChatMemberUpdated.chat
得到 chat_id
。事实上,您仍然可以使用 PTB 快捷方式,例如update.effective_chat.send_message
.
请查看
的文档ChatMemberUpdated
(official and PTB)Update.effective_chat
Chat.send_message
以及 PTB 提供的 chatmemberbot.py
example。
免责声明:我目前是 python-telegram-bot