I’m developing a Telegram bot that retrieves stock prices from financial sites and sends them to users when they send a text in the chat. The intention is to gather information for two stock indices and format it clearly.
bot_script.py", line 15, in <module>
app = Application.builder().token(bot_token).build()
AttributeError: module 'telegram' has no attribute 'Application'
I attempted to reinstall the python-telegram-bot library, yet the problem persists. How should I correctly initialize the bot application in the latest version?
This is a version compatibility issue between different python-telegram-bot releases. I had the same problem when I upgraded - the old telegram package mixed with python-telegram-bot and caused conflicts. Run pip list | grep telegram to see what’s installed. You’ll probably find both packages there. Clean them out completely, then install only python-telegram-bot>=20.0. Also, scraping with data-reactid attributes is pretty fragile since Yahoo changes their DOM structure all the time. I switched to the yfinance library instead - it’s way more stable for stock data and you don’t have to deal with HTML parsing headaches.
The AttributeError indicates that you’re using the wrong Telegram library. Ensure you’re using the python-telegram-bot package, as the Application class is only available in version 20 and above. First, remove any existing installations by executing pip uninstall telegram python-telegram-bot. Then, install the correct package with pip install python-telegram-bot. Additionally, be cautious with web scraping Yahoo Finance; their HTML structure changes frequently, potentially causing your bot to fail. Consider leveraging a stable financial API like Alpha Vantage or Yahoo Finance API for better reliability.
yeah, sounds like you’re on an old version of the pkg. try running pip install python-telegram-bot==20.7 since that’s when the Application class was added. also, be aware that yahoo keeps changing their page layout, so ur scraping code might fail too.