I am trying to get group messages with Python I have this code
我正在尝试使用Python获取群组消息,我有以下代码
from telegram.ext import Updater, MessageHandler, Filters
BOT_TOKEN = 'YOUR_BOT_TOKEN'
GROUP_CHAT_ID = -123456789 # Replace with your group's chat_id
def is_group_message(update):
return update.message.chat_id == GROUP_CHAT_ID
def message_handler(update, context):
if is_group_message(update):
message = update.message
user_id = message.from_user.id
text = message.text
print(f"Received message in group {GROUP_CHAT_ID} from user {user_id}: {text}")
def main():
updater = Updater(token=BOT_TOKEN, use_context=True)
dispatcher = updater.dispatcher
# Register a message handler with a custom filter function
message_handler_obj = MessageHandler(Filters.text & Filters.update(message=is_group_message), message_handler)
dispatcher.add_handler(message_handler_obj)
# Start the bot
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
But I got this error message:
但我收到了这个错误消息:
Traceback (most recent call last):
File "C:\Users\FS\PycharmProjects\telegram-bot\bot-father.py", line 1, in <module>
from telegram.ext import Updater, MessageHandler, Filters
ImportError: cannot import name 'Filters' from 'telegram.ext' (C:\Users\FS\PycharmProjects\telegram-bot\lib\site-packages\telegram\ext\__init__.py)
I am trying to get telegram group chat messages I created bot with botfather he gave me a token I used it on my code but have problem with libraries.
我正在尝试获取电报群聊消息,我与僵尸父亲创建了机器人,他给了我一个令牌,我在代码上使用了它,但在库上有问题。
更多回答
优秀答案推荐
As you may find in the documentation, you need to import telegram.ext.filters.Filters
. Also, disable the "Privacy mode" & allow groups inside @BotFather bot settings.
正如您可能在文档中所看到的,您需要导入Telegram.ext.filters.Filters。另外,禁用“隐私模式”并允许在@BotParent机器人内部设置群组。
更多回答
我是一名优秀的程序员,十分优秀!