skip to Main Content

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


  1. from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext
    from telegram import Update
    
    # 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()
    

    You should import Update from the telegram lib:
    Telegram 13.7 document

    Login or Signup to reply.
  2. 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)
    

    Here’s the corrected version of your code with the proper import statements:

    from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext, Update
    
    # 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()
    

    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:

    pip install --upgrade python-telegram-bot
    

    What is the problem?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search