如何解决 TypeError: __init__() 缺少 1 个必需的位置参数:'update_queue'?

Nic*_*las 5 python python-3.x telegram python-telegram-bot telegram-bot

我想创建一个 Telegram 机器人来检查网站上是否有新帖子(目前出于测试目的每 15 秒检查一次)。如果是这样,它应该将包含帖子内容的消息发送到 Telegram 频道。

为此,我已经有了以下“代码骨架”:(格式化和添加方面的精细工作稍后进行)

import requests
import asyncio
from bs4 import BeautifulSoup
from telegram import InputMediaPhoto
from telegram.ext import Updater

# Telegram Bot API Token
API_TOKEN = 'XXXXXXXXXXXXXXXXX'

# URL of the website
URL = 'https://chemiehalle.de'

# List for storing seen posts
seen_posts = []

# Function for fetching posts
def get_posts():
    # Send request to the website
    res = requests.get(URL)
    # Parse HTML content
    soup = BeautifulSoup(res.content, 'html.parser')
    # Find all posts on the website
    posts = soup.find_all('article')
    # Iterate over each post
    for post in posts:
        # Get title of the post
        title = post.find('h2', class_='entry-title').text
        # Check if post has already been seen
        if title not in seen_posts:
            # Get image URL
            image_src = post.find('img')['src']
            # Get short text of the post
            text = post.find('div', class_='entry-content clearfix').find('p').text
            # Send image, title, and text as message
            bot.bot.send_media_group(chat_id='@chemiehalleBot', media=[InputMediaPhoto(media=image_src, caption=title + '\n\n' + text)])
            # Add title of the post to the list of seen posts
            seen_posts.append(title)

# Main loop
async def main():
    while True:
        # Call get_posts function every 15s
        get_posts()
        print("Check for new posts")
        await asyncio.sleep(15)

# Initialize Telegram Bot
updater = Updater(API_TOKEN)
bot = updater.bot

# Start main loop
asyncio.run(main())

Run Code Online (Sandbox Code Playgroud)

到目前为止,我发现这updater = Updater(API_TOKEN, use_context=True)会产生错误,因此我已use_context=True按照本网站上其他帖子的说明删除了该内容。

从那以后我遇到了该TypeError: __init__() missing 1 required positional argument: 'update_queue'行的错误updater = Updater(API_TOKEN)

但不幸的是我不知道该改变什么。据此,Updater的构造函数需要一个额外的参数update_queue。但我不知道这应该是哪一个以及我应该从哪里得到它。

你能帮我吗?

非常感谢您的支持!

小智 4

您可能使用了错误的电报版本。

我有点守旧,但对我来说 13 版仍然很棒。

因此,只需通过运行以下命令替换您的库版本:

pip install python-telegram-bot==13.13
Run Code Online (Sandbox Code Playgroud)

  • 为什么要降级而不是修复代码以正确使用当前版本? (8认同)