Python Telegram Bot - Missing Updater Class Issue

I’m trying to build a bot that fetches stock data from financial websites and sends it to users when they message the bot. However, I keep getting errors about the Updater class not being found.

Here’s my current code:

import requests
from telegram.ext import Application, CommandHandler
from bs4 import BeautifulSoup

def fetch_stock_data(ticker):
    endpoint = f"https://finance.yahoo.com/quote/{ticker}"
    response = requests.get(endpoint)
    parser = BeautifulSoup(response.content, "html.parser")
    stock_value = parser.find("span", {"data-reactid": "14"}).get_text()
    return stock_value

def send_stocks(update, context):
    tickers = ["^IXIC", "^RUT"]
    values = [fetch_stock_data(ticker) for ticker in tickers]
    reply = "NASDAQ: " + values[0] + "\n" + "Russell: " + values[1]
    context.bot.send_message(chat_id=update.message.chat_id, text=reply)

my_token = "YOUR_BOT_TOKEN"
app = Application.builder().token(my_token).build()

stock_cmd = CommandHandler("stocks", send_stocks)
app.add_handler(stock_cmd)

app.run_polling()

The error I receive is:

AttributeError: module 'telegram' has no attribute 'Updater'

I’ve tried reinstalling the telegram library, but I’m still having issues. What’s the proper way to handle this in newer versions of the library?

Your code looks appropriate for python-telegram-bot v20 and above. The Updater error you’re encountering suggests that there might be conflicts with your installations or that you’re inadvertently using old code from previous versions. I faced similar issues when transitioning from v13 to v20 due to the removal of the Updater class in favor of the Application method you’re correctly using. To resolve this, execute pip uninstall python-telegram-bot followed by pip install python-telegram-bot==20.7 to ensure a clean installation. Also, check your files for any imports related to Updater, as these could lead to further complications. The shift from Updater to Application is substantial, and mixing versions can trigger these attribute errors.

Hmm, that’s weird - your code looks fine to me. Check if you’ve got multiple Telegram packages installed. Sometimes telegram and python-telegram-bot clash with each other. Try running pip uninstall telegram first, then keep only python-telegram-bot. Also heads up - that Yahoo scraping might break since they change their HTML structure all the time.