Help! My Raspberry Pi 4 is acting up. I recently updated to Bookworm and now my Python scripts are failing. Here's a snippet of my code:
import logging
from telegram_bot import TelegramUpdater, CmdHandler
from telegram_bot import TelegramUpdate, TelegramContext
from telegram_bot import TelegramBot
import network
def run():
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
network_ip = get_network_ip()
notify_user(network_ip)
bot_updater = TelegramUpdater(API_KEY)
dispatcher = bot_updater.dispatcher
dispatcher.add_handler(CmdHandler("begin", begin))
dispatcher.add_handler(CmdHandler("end", end))
bot_updater.start_polling()
bot_updater.idle()
if __name__ == '__main__':
run()
When I run this, I get:
Traceback (most recent call last):
File "/home/pi/bot/main.py", line 50, in <module>
run()
File "/home/pi/bot/main.py", line 42, in run
bot_updater = TelegramUpdater(API_KEY)
^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: TelegramUpdater.__init__() got an unexpected keyword argument 'use_context'
Any ideas on how to fix this? It used to work fine before the update.
It appears the issue is due to changes in the python-telegram-bot library. The use_context parameter is no longer necessary, so update your code accordingly.
Change your import to: from telegram.ext import Updater, CommandHandler.
Then, initialize your updater with Updater(API_KEY) instead of using any keyword arguments. Also, replace CmdHandler with CommandHandler when adding handlers. This adjustment should resolve the error. If the problem persists, consider downgrading the library or reviewing the latest documentation.
hey mate, sounds like a version mismatch with python-telegram-bot. try upgrading it to the latest version (v13 or higher). run ‘pip install --upgrade python-telegram-bot’ and see if that sorts it. if not, u might need to tweak ur code a bit for the newer API. good luck!
I’ve encountered similar issues after OS updates on my Pi. The error suggests a compatibility problem between your code and the installed python-telegram-bot version. Here’s what worked for me:
First, check your current version with ‘pip show python-telegram-bot’. If it’s older than 13.0, upgrade as others suggested. If that doesn’t help, try creating a new virtual environment and installing the library there.
Also, the API has changed significantly in recent versions. You might need to update your imports and method calls. For instance, ‘from telegram.ext import Updater, CommandHandler’ is now the correct import statement.
If all else fails, consider rolling back to your previous OS version temporarily while you sort out the compatibility issues. It’s a hassle, but sometimes necessary when dealing with mission-critical scripts.