민프

[Python][Error] python-telegram-bot Error telegram.error.RetryAfter: Flood control exceeded. Retry in 15.0 seconds공식 깃헙 위키의 도배 판단의 기준 본문

[Python Error]

[Python][Error] python-telegram-bot Error telegram.error.RetryAfter: Flood control exceeded. Retry in 15.0 seconds공식 깃헙 위키의 도배 판단의 기준

민프야 2023. 4. 27. 08:45

 

이 에러는 아래 공식 텔레그램 봇의 도배 판단 기준을 보면

최대 메시지 양은 모든 일반 메시지의 경우 초당 최대 30개, 그룹 메시지의 경우 분당 최대 20개로 제한됩니다.

라고 나와있는데 이를 해결하기 위해서는 몇가지 방법을 찾아보았다. 

https://github.com/python-telegram-bot/python-telegram-bot/wiki/Avoiding-flood-limits

 

Avoiding flood limits

We have made you a wrapper you can't refuse. Contribute to python-telegram-bot/python-telegram-bot development by creating an account on GitHub.

github.com

 

먼저

1. 메세지 길이를 짧게하는 방법
https://stackoverflow.com/questions/51423139/python-telegram-bot-flood-control-exceeded

 

Python telegram bot flood control exceeded

I develop telegram bot using python-telegram-bot. Despite the usage of MessageQueue (just as mentioned in the docs) I keep getting telegram.error.RetryAfter: Flood control exceeded. Retry in N seco...

stackoverflow.com

위 글에 의하면 Telegram API에서는 512바이트보다 크면 큰 메세지로 간주되게 되는데 메세지를 짧게 만들면 해결할 수 있다고 한다.

 

2. 일정한 딜레이 추가하기

계속적으로 요청이 되면 그만큼 작업 빈도도 늘어나기 때문에 Time모듈을 사용해서 딜레이를 추가해보자

 

3. python-telegram-bot MessageQueue사용하기

https://docs.python-telegram-bot.org/en/v13.13/telegram.ext.messagequeue.html

 

telegram.ext.MessageQueue — python-telegram-bot 13.13 documentation

Processes callables in throughput-limiting queues to avoid hitting limits (specified with burst_limit and time_limit. Note Method is designed to accept telegram.utils.promise.Promise as promise argument, but other callables could be used too. For example,

docs.python-telegram-bot.org

python-telegram-bot에서 MessageQueue기능을 제공하고 있다.

MessageQueue를 이용하면 텔레그램API에 대한 요청을 큐에 저장하고 일정 시간 동안 차단한 다음,

일정 시간이 지나면 큐에 있는 요청을 처리하게 된다.

 

사용하는 방법은 아래와 같다

from telegram import Bot, Update
from telegram.ext import MessageQueue, Updater, CallbackContext

def echo(update: Update, context: CallbackContext) -> None:
    context.bot.send_message(chat_id=update.message.chat_id, text=update.message.text)

def main() -> None:
    bot = Bot(token=TOKEN)
    mq = MessageQueue(all_burst_limit=29, all_time_limit_ms=1017)
    updater = Updater(bot=bot, use_context=True)
    mq.set_dispatcher(updater.dispatcher)

    updater.dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, echo))
    updater.start_polling()

if __name__ == '__main__':
    main()

 

현재 진행중인 프로젝트 성향 상 메세지큐에 담아서 나중에 처리가 되면 안되기 때문에 
2번 방법인 딜레이를 주는 방법을 선택하기로 했다.

만약 위 와 같은 에러가 들어오게 되면
15~20초 정도 딜레이를 주고 난 후 다시 진행하는 방법으로 해야겠다. 

Comments