你好。请帮我创建简单的 Telegram 机器人

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

我需要一个用于 Telegram 群组的机器人,该机器人可以删除含有禁用词语的消息。

Chat GPT 建议使用 Python 和 python-telegram-bot。

并编写了代码。但他提供的代码不起作用。而且他无法修复他的错误。

建议我聊天 GPT 的代码:

from telegram.ext import Updater, MessageHandler, Filters
from telegram import ChatAction

# Replace 'YOUR_BOT_TOKEN' with your bot token
TOKEN = 'YOUR_BOT_TOKEN'

# Forbidden words
forbidden_words = ['word1', 'word2', 'word3']

# Handler function to delete messages with forbidden words
def delete_messages(update, context):
    chat_id = update.message.chat_id
    message_text = update.message.text.lower()

    for word in forbidden_words:
        if word in message_text:
            # Send a "typing" action to the chat (to reduce bot spam)
            context.bot.send_chat_action(chat_id=chat_id, action=ChatAction.TYPING)
            # Delete the message
            update.message.delete()

# Main function
def main():
    # Create an Updater object and pass it your bot token
    updater = Updater(token=TOKEN, use_context=True)

    # Get the dispatcher to register handlers
    dp = updater.dispatcher

    # Register the handler for messages with forbidden words
    dp.add_handler(MessageHandler(Filters.text, delete_messages))

    # Start the bot
    updater.start_polling()

    # The bot will keep running until we interrupt the program
    updater.idle()

if __name__ == '__main__':
    main()

当我编写这段代码时,它开始产生以下错误:

  File "C:\Users\Dipset\bot.py", line 1, in <module>
    from telegram.ext import Updater, MessageHandler, Filters
ImportError: cannot import name 'Filters' from 'telegram.ext' (C:\Users\Dipset\AppData\Local\Programs\Python\Python312\Lib\site-packages\telegram\ext\__init__.py). Did you mean: 'filters'?

还有

Traceback (most recent call last):
  File "C:\Users\Dipset\bot.py", line 2, in <module>
    from telegram import ChatAction
ImportError: cannot import name 'ChatAction' from 'telegram' (C:\Users\Dipset\AppData\Local\Programs\Python\Python312\Lib\site-packages\telegram\__init__.py). Did you mean: 'ChatLocation'?

帮助:(

python bots telegram google-publisher-tag
1个回答
-1
投票
    from telegram.ext import Updater, MessageHandler, Filters

# Replace 'YOUR_BOT_TOKEN' with your bot token
TOKEN = 'YOUR_BOT_TOKEN'

# Forbidden words
forbidden_words = ['word1', 'word2', 'word3']

# Handler function to delete messages with forbidden words
def delete_messages(update, context):
    chat_id = update.message.chat_id
    message_text = update.message.text.lower()

    for word in forbidden_words:
        if word in message_text:
            # Delete the message
            context.bot.delete_message(chat_id=chat_id, message_id=update.message.message_id)
            break  # Stop checking for other forbidden words in the message

# Main function
def main():
    # Create an Updater object and pass it your bot token
    updater = Updater(token=TOKEN, use_context=True)

    # Get the dispatcher to register handlers
    dp = updater.dispatcher

    # Register the handler for messages with forbidden words
    dp.add_handler(MessageHandler(Filters.text & (~Filters.command), delete_messages))

    # Start the bot
    updater.start_polling()

    # The bot will keep running until we interrupt the program
    updater.idle()

if __name__ == '__main__':
    main()
© www.soinside.com 2019 - 2024. All rights reserved.