eternnoir/pyTelegramBotAPI

Too Many Requests: 429

muhammadmiziev opened this issue · 3 comments

does not working Flask WebHook
Used: Python3/2

when running I get:

 * Running on https://0.0.0.0:8443/ (Press CTRL+C to quit)
   * Restarting with stat
   Traceback (most recent call last):
    File "./Bot.py", line 81, in <module>
      bot.set_webhook(url=WEBHOOK_URL_BASE+WEBHOOK_URL_PATH,       certificate=open(WEBHOOK_SSL_CERT, 'r'))
    File "/usr/local/lib/python2.7/dist-packages/telebot/__init__.py", line 79, in set_webhook
       return apihelper.set_webhook(self.token, url, certificate)
     File "/usr/local/lib/python2.7/dist-packages/telebot/apihelper.py", line 127, in set_webhook
       return _make_request(token, method_url, params=payload, files=files)
       File "/usr/local/lib/python2.7/dist-packages/telebot/apihelper.py", line 36, in _make_request
       return _check_result(method_name, result)['result']
     File "/usr/local/lib/python2.7/dist-packages/telebot/apihelper.py", line 55, in _check_result
        raise ApiException(msg, method_name, result)
    telebot.apihelper.ApiException: A request to the Telegram API was unsuccessful. The server          returned HTTP 429 Too Many Requests. Response body:
       [{"ok":false,"error_code":429,"description":"Too Many Requests: retry after 1","parameters": {"retry_after":1}}]

bot:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# This is a simple echo bot using decorators and webhook with flask
# It echoes any incoming text messages and does not use the polling method.

import flask
import telebot
import logging


API_TOKEN = 'token'

WEBHOOK_HOST = 'ip'
WEBHOOK_PORT = 8443  # 443, 80, 88 or 8443 (port need to be 'open')
WEBHOOK_LISTEN = '0.0.0.0'  # In some VPS you may need to put here the IP addr

WEBHOOK_SSL_CERT = './webhook_cert.pem'  # Path to the ssl certificate
WEBHOOK_SSL_PRIV = './webhook_pkey.pem'  # Path to the ssl private key

# Quick'n'dirty SSL certificate generation:
#
# openssl genrsa -out webhook_pkey.pem 2048
# openssl req -new -x509 -days 3650 -key webhook_pkey.pem -out webhook_cert.pem
#
# When asked for "Common Name (e.g. server FQDN or YOUR name)" you should reply
# with the same value in you put in WEBHOOK_HOST

WEBHOOK_URL_BASE = "https://%s:%s" % (WEBHOOK_HOST, WEBHOOK_PORT)
WEBHOOK_URL_PATH = "/%s/" % (API_TOKEN)


logger = telebot.logger
telebot.logger.setLevel(logging.INFO)

bot = telebot.TeleBot(API_TOKEN)

app = flask.Flask(__name__)


# Empty webserver index, return nothing, just http 200
@app.route('/', methods=['GET', 'HEAD'])
def index():
    return ''


# Process webhook calls
@app.route(WEBHOOK_URL_PATH, methods=['POST'])
def webhook():
    if flask.request.headers.get('content-type') == 'application/json':
        json_string = flask.request.get_data().encode('utf-8')
        update = telebot.types.Update.de_json(json_string)
        bot.process_new_messages([update.message])
        return ''
    else:
        flask.abort(403)


# Handle '/start' and '/help'
@bot.message_handler(commands=['help', 'start'])
def send_welcome(message):
     bot.reply_to(message,
                  ("Hi there, I am EchoBot.\n"
                  "I am here to echo your kind words back to you."))


# Handle all other messages
@bot.message_handler(func=lambda message: True, content_types=['text'])
def echo_message(message):
     if(message.text == "make"):
         bot.reply_to(message, 'SUCCESS')
     else:
        bot.reply_to(message, message.text)


# Remove webhook, it fails sometimes the set if there is a previous webhook
bot.remove_webhook()

bot.set_webhook(url=WEBHOOK_URL_BASE+WEBHOOK_URL_PATH, certificate=open(WEBHOOK_SSL_CERT, 'r'))

# Start flask server
app.run(host=WEBHOOK_LISTEN,
        port=WEBHOOK_PORT,
        ssl_context=(WEBHOOK_SSL_CERT, WEBHOOK_SSL_PRIV),
        debug=True)

Remove this line — bot.remove_webhook()

Or add sleep(1) between remove and set

Thanks! deleting bot.remove_webhook() and adding sleep(1) - helped!