Hellp guys i tried to make a Telegram bot but when i try to run it its give me this error in line 3 :
Exception has occurred: ImportError
cannot import name 'Filters' from 'telegram.ext' (C:\Users\Mustafa\AppData\Local\Programs\Python\Python311\Lib\site-packages\telegram\ext\__init__.py)
File "C:\Users\Mustafa\Desktop\Telegram bot\my_telegram_bot.py", line 3, in <module>
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext
ImportError: cannot import name 'Filters' from 'telegram.ext' (C:\Users\Mustafa\AppData\Local\Programs\Python\Python311\Lib\site-packages\telegram\ext\__init__.py)
i used 2 packages pytube and python_telegram_bot , also i have the latest version of everything idk where is the wrong .
The code :
import os
from telegram import Update
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext
from pytube import YouTube
def start(update: Update, context: CallbackContext) -> None:
update.message.reply_text('Send me a YouTube link with /download to get the video.')
def download_video(update: Update, context: CallbackContext) -> None:
url = context.args[0]
yt = YouTube(url)
video = yt.streams.get_highest_resolution()
video.download()
filename = f"{yt.title}.mp4"
os.rename(video.default_filename, filename)
update.message.reply_document(document=open(filename, 'rb'))
os.remove(filename)
def main() -> None:
# Replace 'YOUR_API_TOKEN' with your actual API token
updater = Updater(token='MY telegram token', use_context=True)
dispatcher = updater.dispatcher
dispatcher.add_handler(CommandHandler('start', start))
dispatcher.add_handler(CommandHandler('download', download_video, pass_args=True))
dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, start))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
Change:
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext
from telegram.ext import Updater, CommandHandler, MessageHandler, CallbackContext
import telegram.ext.filters as filters
Also change Filters.text
to filters.TEXT
and change Filters.command
to filters.COMMAND
.
Now i get this error code when i run it :
Traceback (most recent call last):
File "C:\Users\Mustafa\Desktop\Telegram bot\my_telegram_bot.py", line 32, in <module>
main()
File "C:\Users\Mustafa\Desktop\Telegram bot\my_telegram_bot.py", line 21, in main
updater = Updater(token='6460570557xxxxxxxxxxxxxxxxxoFmQM', use_context=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Updater.__init__() got an unexpected keyword argument 'token'
It looks like it’s another change to the API. (There seem to have been quite a few changes to it over the years!)
You’ll need to look for a tutorial and study the docs.