Need help in a python script for telegram mention bot

I’ve installed latest version python in my windows 8.1 32 bit. I have created a telegram bot. I’ve also written a script for my bot. I’ve installed pip, telegram, python telegram bot and others which are needed to run my telegram bot. Here’s the code I’ve written for my telegram bot:

import logging
from telegram import Update

# from telegram.update import Update

from telegram.ext import Updater, CommandHandler, MessageHandler, CallbackContext, Filters

# Set up logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)

# Replace 'YOUR_API_TOKEN' with your actual bot API token
TOKEN = 'I removed it for now'

# Initialize the Updater
updater = Updater(token=TOKEN, use_context=True)
dispatcher = updater.dispatcher

# Command handler
def start(update: Update, context: CallbackContext) -> None:
    update.message.reply_text("Hello! I am your mention bot. Use @everyone to mention all members.")

start_handler = CommandHandler('start', start)
dispatcher.add_handler(start_handler)

# Mention handler
def handle_mentions(update: Update, context: CallbackContext) -> None:
    message_text = update.message.text
    if "@everyone" in message_text:
        chat_id = update.message.chat_id
        
        # Get the list of all members in the group
        all_members = context.bot.get_chat_members(chat_id)
        
        # Prepare the mention message
        mention_message = " ".join([f"@{member.user.username}" for member in all_members if member.user.username])
        if mention_message:
            mention_message = f"Mentioning all members: {mention_message}"
        else:
            mention_message = "No active members to mention."
        
        # Send the mention message
        context.bot.send_message(chat_id, mention_message, reply_to_message_id=update.message.message_id)

mention_handler = MessageHandler(Filters.text & ~Filters.command, handle_mentions)
dispatcher.add_handler(mention_handler)

# Start the bot
updater.start_polling()

# Run the bot until you press Ctrl-C
updater.idle()

Whenever I try to run these script saved as mention_bot.py by commanding cd c:\python\bot\python mention_bot.py
It always shows me this error: C:\Python\bot>python mention_bot.py Traceback (most recent call last):
File “C:\Python\bot\mention_bot.py”, line 2, in
from telegram import Update
ImportError: cannot import name ‘Update’ from ‘telegram’ (C:\Python\Lib\site-pa kages\telegram___init.py>

Please help me to get rid of this error, please.
Thanks in advance.

Please read the pinned thread in order to understand how to make the code show up properly.

1 Like

Thanks. I’ve edited the code.

(It was clear enough this time; but in the future it’s a good idea to show any terminal output, such as errors, like formatted code as well. Python errors are designed to look right in the terminal, especially in more recent versions: for example, they might use ^ symbols to try to point at certain parts of a line of code, and this only makes sense with a monospaced font like in the terminal.)

I’ve installed pip, telegram, python telegram bot and others which are needed to run my telegram bot.

From a bit of Internet research, it appears the problem is: you should not install telegram; you should only install python-telegram-bot. The telegram package is much older and not maintained (in fact the GitHub repository for it has disappeared and it only ever had a 0.0.1 version number), and it will define a different package that has completely different functionality and does not match whatever documentation/tutorial/etc. you’re trying to follow along with.

In general, if you’re trying to set up a third-party library so that you can import something, you should only ever expect to run one Pip installation command. Whatever that package is, it is responsible for installing any other libraries it might require.

1 Like

I’ve install telegram by commanding

pip install telegram

And it shows me that requirement already satisfied: telegram in c:\python\lib\site-pakages <0.0.1>

Yes; I am saying that you should not install that. Start over (uninstall whatever you installed, or make a new blank virtual environment, etc.) and install only python-telegram-bot.

thanks.
okay. let me try it.