하얀설표 블로그




해결)Too Many Requests: retry after {num}





( 수정됨)


에러 예시

예시코드

import telepot
import tqdm
token = {api 토큰}
id = {chat id}
text = 'telegram maessage text'
bot = telepot.Bot(token)
for i in tqdm.tqdm(range(40)):
    bot.sendMessage(
        chat_id=id,
        text=text,
    )

>> 50%|██████████████████████████                       | 20/40 [00:21<00:21,  1.06s/it]
Traceback (most recent call last):
  File "c:\seolpyo\example.py", line 8, in <module>
    bot.sendMessage(
  File "C:\seolpyo\Lib\site-packages\telepot\__init__.py", line 513, in sendMessage
    return self._api_request('sendMessage', _rectify(p))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\seolpyo\Lib\site-packages\telepot\__init__.py", line 491, in _api_request
    return api.request((self._token, method, params, files), **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\seolpyo\Lib\site-packages\telepot\api.py", line 155, in request
    return _parse(r)
           ^^^^^^^^^
  File "C:\seolpyo\Lib\site-packages\telepot\api.py", line 147, in _parse
    raise e(description, error_code, data)
telepot.exception.TooManyRequestsError: ('Too Many Requests: retry after 40', 429, {'ok': False, 'error_code': 429, 'description': 'Too Many Requests: retry after 40', 'parameters': {'retry_after': 40}})

해결방법

메세지를 연속으로 보내야 하는 경우 적절한 대기시간을 줘야 한다.

설명

텔레그램봇 faq 페이지에서 텔레그램봇으로 메세지를 전송할 때 적용되는 제한사항을 확인할 수 있다.
확인해야하는 제한사항과 기계번역 결과는 다음과 같다.

If you're sending bulk notifications to multiple users, the API will not allow more than 30 messages per second or so. Consider spreading out notifications over large intervals of 8—12 hours for best results.
Also note that your bot will not be able to send more than 20 messages per minute to the same group.

>> 여러 사용자에게 대량 알림을 보내는 경우 API는 초당 30개 이상의 메시지를 허용하지 않습니다. 최상의 결과를 얻으려면 8~12시간의 큰 간격으로 알림을 분산하는 것이 좋습니다.
또한 봇은 동일한 그룹에 분당 20개 이상의 메시지를 보낼 수 없습니다.

In order to avoid hitting our limits when sending out mass notifications, consider spreading them over longer intervals, e.g. 8-12 hours. The API will not allow bulk notifications to more than ~30 users per second, if you go over that, you'll start getting 429 errors.

>> 대량 알림을 보낼 때 한도에 도달하지 않도록 하려면 더 긴 간격(예: 8-12시간)에 걸쳐 알림을 분산하는 것을 고려하십시오. API는 초당 최대 30명 이상의 사용자에게 대량 알림을 허용하지 않습니다. 이 값을 초과하면 429 오류가 발생하기 시작합니다.

위 규칙은 봇이 "여러 사용자" 또는 "동일 그룹"에 메세지를 보낼 때 적용된다고 되어있다.
테스트해본 결과 개인에게 1:1 메세지를 보낼 때는 적용되지 않고, 채널이나 그룹에 메세지를 보낼 때 적용되는 것으로 보인다.

따라서 채널 또는 그룹에 메세지를 보낼 때는 1초에 30개의 메세지를 보냈다면 1초의 time sleep을, 또한 1분에 20개의 메세지를 보냈다면 1분의 time sleep을 해줘야 이런 에러가 발생하는 것을 방지할 수 있다.

덤으로 에러 발생시 전달되는 메세지인 "Too Many Requests: retry after 40"에 40에 해당하는 값은 에러가 발생할 때마다 다른 값이 전달된다.
이는 "Too Many Requests: retry after {num}"에서 {num}에 해당하는 초(sec)만큼 대기한 다음 다시 요청하라는 뜻이기 때문이다.


공감 : 0







white.seolpyo.com