I wrote Python code for a basic Telegram bot through replit.
In the shell I wrote: pip install python-telegram-bot==13.7
Until now it was working for me, but from now on, it writes me things like: "Update" is unknown import symbol, "Filters" is unknown import symbol, and the main error is:
Traceback (most recent call last):
File "/home/runner/PeachpuffSquareTraining/main.py", line 1, in <module>
from telegram import Update
ImportError: cannot import name 'Update' from 'telegram' (/home/runner/PeachpuffSquareTraining/.pythonlibs/lib/python3.10/site-packages/telegram/__init__.py)
This is the code I tried, and until now with the above pip it was working for me:
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext
# Replace 'YOUR_BOT_TOKEN' with the actual token you obtained from BotFather
TOKEN = 'the token is secret'
def start(update: Update, context: CallbackContext) -> None:
update.message.reply_text('Hello! I am your bot.')
def echo(update: Update, context: CallbackContext) -> None:
update.message.reply_text(update.message.text)
def main() -> None:
updater = Updater(TOKEN)
dp = updater.dispatcher
# Command handlers
dp.add_handler(CommandHandler("start", start))
# Message handler for echoing messages
dp.add_handler(MessageHandler(Filters.text & ~Filters.command, echo))
# Start the Bot
updater.start_polling()
# Run the bot until you send a signal to stop it
updater.idle()
if __name__ == '__main__':
main()
What is the problem?
2
Answers
You should import Update from the
telegram
lib:I wrote Python code for a basic Telegram bot through replit.
In the shell I wrote:
pip install python-telegram-bot==13.7
Until now it was working for me, but from now on, it writes me things like: "Update" is unknown import symbol, "Filters" is unknown import symbol, and the main error is:
Here’s the corrected version of your code with the proper import statements:
Make sure you have the correct version of the python-telegram-bot library installed. You mentioned using version 13.7, but there might be updates or changes. You can try upgrading to the latest version by running:
What is the problem?