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.