I’m facing an import error every time I try to run my bot script. The error indicates that Python can’t find the telegram.parsemode module, although I have installed the telegram library.
Here’s the error traceback I’m encountering:
(venv) C:\Users\MyUser\Desktop\chat_bot>python my_telegram_bot.py
Traceback (most recent call last):
File "C:\Users\MyUser\Desktop\chat_bot\my_telegram_bot.py", line 2, in <module>
from telegram.parsemode import parse_mode
ModuleNotFoundError: No module named 'telegram.parsemode'
I’ve tried reinstalling the python-telegram-bot package several times and experimenting with the different version combinations, but the problem continues to occur.
Here’s how my bot code begins:
# THE SECTION WHERE THE ERROR OCCURS
from telegram import Update, ParseMode
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes
import random
import time
# BOT CONFIGURATION
API_TOKEN = '987654321:EXAMPLE_TOKEN_STRING_HERE'
# Game state variables
lotto_numbers = [str(num) for num in range(1, 75)] # Numbers 1-75
active_game = False
drawn_numbers = []
user_cards = {}
winner_count = {}
administrator_ids = [123456789] # Admin user IDs
# Generate random numbers for players
def create_player_card() -> dict:
"""Creates random number cards for each participant"""
participant_cards = {}
for user_id in user_cards:
participant_cards[user_id] = random.sample(lotto_numbers, 4*4) # 16 numbers per card
return participant_cards
# Welcome command
async def welcome(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Initial greeting message"""
await update.message.reply_text(
"Welcome to Number Game Bot! Only group admins can start new games using /begin.\n"
"Use /info to learn the rules.",
parse_mode=ParseMode.MARKDOWN
)
# Help command
async def info(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Display help information"""
await update.message.reply_text(
"To play the number game, use /begin to start a new round.\n"
"Numbers will be called out and you mark the ones on your card!",
parse_mode=ParseMode.MARKDOWN
)
# Start game function (admin only)
async def begin_game(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Initiates the number game"""
global active_game, drawn_numbers, user_cards
current_user = update.message.from_user.id