구글검색 등으로 파이썬 텔레그램봇 코드를 복붙하면 아래와 같은 코드가 나오는 경우가 종종 있다.
from telegram.ext.filters import Filters
ImportError: cannot import name 'Filters' from 'telegram.ext.filters' (----\venv\Lib\site-packages\telegram\ext\filters.py)
2023년초에 python-telegram-bot 패키지가 v13에서 v20으로 확 건너뛰게 되면서 호출방식 등이 많이 바뀌었다.
그래서 2023년 이전에 작성한 코드들은 v20 이상에서는 이상한 오류들을 내뱉으면서 작동하지 않는것.
stackoverflow에서 Filters를 filters로 수정하라는 내용도 있었는데 그러면 다시 다른 오류메세지들이 튀어나옴.
해결방법은 두가지가 있다.
1. 텔레그램봇 패키지 다운그레이드
v20 이상에서 호출방식이 바뀌었다면 바뀌기 전 버전으로 돌아가면 된다. 텔레그램봇 버전 리스트를 한번 보자.
from versions: 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0, 2.1, 2.2, 2.3, 2.4, 2.5, 2.5.1, 2.5.2, 2.5.3, 2.6.0, 2.6.1, 2.7, 2.7.1, 2.8, 2.8.1, 2.8.2, 2.8.3, 2.8.4, 2.8.5, 2.8.6, 2.8.7, 2.9, 2.9.1, 2.9.2, 3.0.0, 3.1.0, 3.1.1, 3.1.2, 3.2.0, 3.3b1, 3.3, 3.4, 4.0rc1, 4.0.0, 4.0.1, 4.0.2, 4.0.3, 4.1, 4.1.1, 4.1.2, 4.2.0, 4.2.1, 4.3, 4.3.1, 4.3.2, 4.3.3, 4.3.4, 5.0.0, 5.1.0, 5.1.1, 5.2.0, 5.3.0, 5.3.1, 6.0.0, 6.0.1, 6.0.2, 6.0.3, 6.1b0, 6.1b1, 6.1b2, 6.1.0, 7.0.0, 7.0.1, 8.0, 8.1.0, 8.1.1, 9.0.0, 10.0.0, 10.0.1, 10.0.2, 10.1.0, 11.0.0, 11.1.0, 12.0.0b1, 12.0.0, 12.1.0, 12.1.1, 12.2.0, 12.3.0, 12.4.0, 12.4.1, 12.4.2, 12.5, 12.5.1, 12.6, 12.6.1, 12.7, 12.8, 13.0, 13.1, 13.2, 13.3, 13.4, 13.4.1, 13.5, 13.6, 13.7, 13.8, 13.8.1, 13.9, 13.10, 13.11, 13.12, 13.13, 13.14, 13.15, 20.0a0, 20.0a1, 20.0a2, 20.0a3, 20.0a4, 20.0a5, 20.0a6, 20.0b0, 20.0, 20.1, 20.2, 20.3, 20.4, 20.5, 20.6, 20.7, 20.8, 21.0, 21.0.1, 21.1, 21.1.1)
v20 직전 버전은 13.15로 보인다. v13.15로 다운그레이드를 해주자.
pip install python-telegram-bot==13.15
2. 코드를 바꾸자.
저렇게 하면 왠만하면 작동하긴 할텐데 다른 패키지 의존성 문제가 튀어나오거나, 앞으로 유지보수를 해야한다는 입장에서는 좀 찝찝할 수 있다. 그렇다면 v20이상에서 작동하는 기본 코드를 만들어서 다시 구축을 해보자. 봇 작동 기본코드는 아래를 참조하자
from telegram import Update
from telegram.ext import filters, ApplicationBuilder, ContextTypes, MessageHandler
bot_token = "your bot token"
#들어온 메시지에 대한 답변. 예시는 보내준 메시지를 그대로 답변하는 함수
async def answer(update: Update, context: ContextTypes.DEFAULT_TYPE):
message = update.message.text
await update.message.reply_text(message))
# 이미지 전송 함수
async def send_img(update: Update, context: ContextTypes.DEFAULT_TYPE):
# 이미지 경로
image_path = "image.jpg"
# 이미지 파일을 읽어 들여 채팅방으로 전송
with open(image_path, "rb") as image_file:
await context.bot.send_photo(chat_id=update.effective_chat.id, photo=image_file, caption="서버에서 보낸 이미지 파일입니다.")
# 이미지 수신 함수
async def receive_img(update: Update, context: ContextTypes.DEFAULT_TYPE):
# 채팅방의 이미지를 가져옴
file = await context.bot.get_file(update.message.photo[-1].file_id)
# 가져온 이미지를 파일로 저장
await file.download_to_drive(f"image_{update.message.message_id}.jpg")
# bytearray 형태로도 저장 가능
img_array = await file.download_as_bytearray()
if __name__ == '__main__':
# 챗봇 application 인스턴스 생성
application = ApplicationBuilder().token(bot_token).build()
# 핸들러 생성
msg_handler = MessageHandler(filters.TEXT & (~filters.COMMAND), answer)
# 핸들러 추가
application.add_handler(msg_handler)
# 이미지 핸들러 생성
img_handler = MessageHandler(filters.PHOTO & (~filters.COMMAND), receive_img)
# 이미지 핸들러 추가
application.add_handler(img_handler)
# 폴링 방식으로 실행
application.run_polling()
'trouble shooting' 카테고리의 다른 글
| 아두이노 ESP32 에러 (Packet content transfer stopped) (0) | 2024.05.11 |
|---|