python-telegram-bot 库函数中的属性错误

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

好吧,嗯...我不是创建机器人的真正专家,但我的朋友需要一个,所以我用聊天 gpt 试了一下,它一直显示这个错误:

 Traceback (most recent call last):
  File "/home/alien/codes/python/bot.py", line 69, in <module>
    main()
  File "/home/alien/codes/python/bot.py", line 51, in main
    updater = Updater(bot.token, use_context=True, job_queue=job_queue)
TypeError: Updater.__init__() got an unexpected keyword argument 'use_context'

我尝试删除“use_context”属性,但随后“job_queue”属性出现错误。这是怎么回事?

这里是代码:

import telegram
from datetime import datetime, timedelta
from telegram.ext import Updater, CommandHandler, MessageHandler, filters, JobQueue

# Enter your bot token here
TOKEN = "xxxxx"

def start(update, context):
    """Handler function for the /start command"""
    update.message.reply_text("Welcome! To add a user to a group, please send me the following details:\n/group_chat_id user_id duration_in_seconds")

def add_user_to_group(update, context):
    """Handler function for adding a user to a group"""
    try:
        # Get the group chat ID, user ID, and duration from the user's message
        chat_id, user_id, duration = map(int, update.message.text.split())

        # Get the current time
        now = datetime.now()

        # Calculate the time after which the user should be removed from the group
        remove_time = now + timedelta(seconds=duration)

        # Add the user to the group chat
        context.bot.add_chat_member(chat_id=chat_id, user_id=user_id)

        # Schedule the removal of the user from the group chat
        context.job_queue.run_once(remove_user, duration, context=(chat_id, user_id))

        # Send a confirmation message to the user
        update.message.reply_text(f"The user has been added to the group chat for {timedelta(seconds=duration)}. The user will be removed from the group on {remove_time}.")

    except Exception as e:
        update.message.reply_text(f"Error: {e}. Please make sure you send the group chat ID, user ID, and duration in seconds separated by spaces.")

def remove_user(context):
    """Function to remove the user from the group chat"""
    job = context.job
    chat_id, user_id = job.context
    context.bot.kick_chat_member(chat_id=chat_id, user_id=user_id)

def main():
    """Main function to start the bot"""
    # Create an instance of the telegram bot
    bot = telegram.Bot(token=TOKEN)

    # Create a job queue
    job_queue = JobQueue()

    # Create an updater for the bot
    updater = Updater(bot.token, use_context=True, job_queue=job_queue)

    # Get the dispatcher for handling commands
    dispatcher = updater.dispatcher

    # Add a handler for the /start command
    dispatcher.add_handler(CommandHandler("start", start))

    # Add a handler for adding a user to a group
    dispatcher.add_handler(MessageHandler(filters.text & ~filters.command, add_user_to_group))

    # Start the bot
    updater.start_polling()

    # Run the bot until it is stopped
    updater.idle()

if __name__ == '__main__':
    main()

再次声明,我只知道 python,不会制作机器人或使用电报。我错过了什么?

python-telegram-bot 版本是 20.0 ,我浏览了其他帖子,但我似乎没有找到答案,但版本确实造成了麻烦,所以我应该提一下。如果您需要更多信息,请告诉我。

python bots telegram telegram-bot python-telegram-bot
© www.soinside.com 2019 - 2024. All rights reserved.