需要电报提及机器人的Python脚本帮助

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

我已经在 Windows 8.1 32 位中安装了最新版本的 python。我创建了一个电报机器人。我还为我的机器人编写了一个脚本。我已经安装了 pip、telegram、python telegram bot 以及运行我的 telegram bot 所需的其他程序。这是我为电报机器人编写的代码:

import logging
from telegram import Update

# from telegram.update import Update

from telegram.ext import Updater, CommandHandler, MessageHandler, CallbackContext, Filters


logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)

TOKEN = 'I removed it for now'

# Initialize the Updater
updater = Updater(token=TOKEN, use_context=True)
dispatcher = updater.dispatcher

# Command handler
def start(update: Update, context: CallbackContext) -> None:
    update.message.reply_text("Hello! I am your mention bot. Use @everyone to mention all members.")

start_handler = CommandHandler('start', start)
dispatcher.add_handler(start_handler)

# Mention handler
def handle_mentions(update: Update, context: CallbackContext) -> None:
    message_text = update.message.text
    if "@everyone" in message_text:
        chat_id = update.message.chat_id
        
        # Get the list of all members in the group
        all_members = context.bot.get_chat_members(chat_id)
        
        # Prepare the mention message
        mention_message = " ".join([f"@{member.user.username}" for member in all_members if member.user.username])
        if mention_message:
            mention_message = f"Mentioning all members: {mention_message}"
        else:
            mention_message = "No active members to mention."
        
        # Send the mention message
        context.bot.send_message(chat_id, mention_message, reply_to_message_id=update.message.message_id)

mention_handler = MessageHandler(Filters.text & ~Filters.command, handle_mentions)
dispatcher.add_handler(mention_handler)

# Start the bot
updater.start_polling()

# Run the bot until you press Ctrl-C
updater.idle()


每当我尝试通过命令 cd c:\python ot\python提及_bot.py 运行这些保存为提及机器人.py 的脚本时 它总是向我显示此错误:C:\Python ot>python提及_bot.py Traceback(最近一次调用最后): 文件“C:\Python ot\mention_bot.py”,第 2 行,位于 从电报导入更新 ImportError:无法从“telegram”导入名称“Update”(C:\Python\Lib\site-pa kages elegram___init.py>

请帮助我消除这个错误。 预先感谢。

我正在尝试创建一个提及机器人。但我每次都会收到这个错误。请帮助我消除这个错误。

python telegram-bot python-telegram-bot
1个回答
0
投票

我已经安装了电报

这就是问题所在。运行

pip uninstall telegram
卸载 telegram 库,然后再次运行 pip install python-telegram-bot。


免责声明:我目前是

python-telegram-bot
的维护者。

© www.soinside.com 2019 - 2024. All rights reserved.