# Buggy code
def blueprint(self, on_new_message):
    telegram_webhook = Blueprint("telegram_webhook", __name__)
    out_channel = self.get_output_channel()

    @telegram_webhook.route("/", methods=["GET"])
    async def health(request: Request):
        return response.json({"status": "ok"})

    @telegram_webhook.route("/set_webhook", methods=["GET", "POST"])
    async def set_webhook(request: Request):
        s = out_channel.setWebhook(self.webhook_url)
        if s:
            logger.info("Webhook Setup Successful")
            return response.text("Webhook setup successful")
        else:
            logger.warning("Webhook Setup Failed")
            return response.text("Invalid webhook")

    @telegram_webhook.route("/webhook", methods=["GET", "POST"])
    async def message(request: Request):
        if request.method == "POST":
            if not out_channel.get_me()["username"] == self.verify:
                logger.debug("Invalid access token, check it matches Telegram")
                return response.text("failed")

            update = Update.de_json(request.json, out_channel)
            if self._is_button(update):
                msg = update.callback_query.message
                text = update.callback_query.data
            else:
                msg = update.message
                if self._is_user_message(msg):
                    text = msg.text.replace("/bot", "")
                elif self._is_location(msg):
                    text = '{{"lng":{0}, "lat":{1}}}'.format(
                        msg.location.longitude, msg.location.latitude
                    )
                else:
                    return response.text("success")
            sender_id = msg.chat.id
            try:
                if text == (INTENT_MESSAGE_PREFIX + USER_INTENT_RESTART):
                    await on_new_message(
                        UserMessage(
                            text, out_channel, sender_id, input_channel=self.name()
                        )
                    )
                    await on_new_message(
                        UserMessage(
                            "/start",
                            out_channel,
                            sender_id,
                            input_channel=self.name(),
                        )
                    )
                else:
                    await on_new_message(
                        UserMessage(
                            text, out_channel, sender_id, input_channel=self.name()
                        )
                    )
            except Exception as e:
                logger.error("Exception when trying to handle message.{0}".format(e))
                logger.debug(e, exc_info=True)
                if self.debug_mode:
                    raise
                pass

            return response.text("success")

    out_channel.setWebhook(self.webhook_url)
    return telegram_webhook


# Traceback (telegram.error)
rasa_1  | 2019-07-30 03:32:07 DEBUG    telegram.vendor.ptb_urllib3.urllib3.connectionpool  - https://api.telegram.org:443 "POST /bot<cut>/setWebhook HTTP/1.1" 429 109
rasa_1  | Traceback (most recent call last):
rasa_1  |   File "/usr/local/bin/rasa", line 10, in <module>
rasa_1  |     sys.exit(main())
rasa_1  |   File "/usr/local/lib/python3.6/site-packages/rasa/__main__.py", line 76, in main
rasa_1  |     cmdline_arguments.func(cmdline_arguments)
rasa_1  |   File "/usr/local/lib/python3.6/site-packages/rasa/cli/run.py", line 88, in run
rasa_1  |     rasa.run(**vars(args))
rasa_1  |   File "/usr/local/lib/python3.6/site-packages/rasa/run.py", line 55, in run
rasa_1  |     **kwargs
rasa_1  |   File "/usr/local/lib/python3.6/site-packages/rasa/core/run.py", line 165, in serve_application
rasa_1  |     log_file=log_file,
rasa_1  |   File "/usr/local/lib/python3.6/site-packages/rasa/core/run.py", line 106, in configure_app
rasa_1  |     rasa.core.channels.channel.register(input_channels, app, route=route)
rasa_1  |   File "/usr/local/lib/python3.6/site-packages/rasa/core/channels/channel.py", line 72, in register
rasa_1  |     app.blueprint(channel.blueprint(handler), url_prefix=p)
rasa_1  |   File "/usr/local/lib/python3.6/site-packages/rasa/core/channels/telegram.py", line 242, in blueprint
rasa_1  |     out_channel.setWebhook(self.webhook_url)
rasa_1  |   File "/usr/local/lib/python3.6/site-packages/telegram/bot.py", line 65, in decorator
rasa_1  |     result = func(self, *args, **kwargs)
rasa_1  |   File "/usr/local/lib/python3.6/site-packages/telegram/bot.py", line 2070, in set_webhook
rasa_1  |     result = self._request.post(url_, data, timeout=timeout)
rasa_1  |   File "/usr/local/lib/python3.6/site-packages/telegram/utils/request.py", line 309, in post
rasa_1  |     headers={'Content-Type': 'application/json'})
rasa_1  |   File "/usr/local/lib/python3.6/site-packages/telegram/utils/request.py", line 216, in _request_wrapper
rasa_1  |     message = self._parse(resp.data)
rasa_1  |   File "/usr/local/lib/python3.6/site-packages/telegram/utils/request.py", line 174, in _parse
rasa_1  |     raise RetryAfter(retry_after)
rasa_1  | telegram.error.RetryAfter: Flood control exceeded. Retry in 1 seconds
it-sd-bot_rasa_1 exited with code 1

# Fixed code
def blueprint(self, on_new_message):
    telegram_webhook = Blueprint("telegram_webhook", __name__)
    out_channel = self.get_output_channel()

    @telegram_webhook.route("/", methods=["GET"])
    async def health(request: Request):
        return response.json({"status": "ok"})

    @telegram_webhook.route("/set_webhook", methods=["GET", "POST"])
    async def set_webhook(request: Request):
        s = out_channel.setWebhook(self.webhook_url)
        if s:
            logger.info("Webhook Setup Successful")
            return response.text("Webhook setup successful")
        else:
            logger.warning("Webhook Setup Failed")
            return response.text("Invalid webhook")

    @telegram_webhook.route("/webhook", methods=["GET", "POST"])
    async def message(request: Request):
        if request.method == "POST":
            if not out_channel.get_me()["username"] == self.verify:
                logger.debug("Invalid access token, check it matches Telegram")
                return response.text("failed")

            update = Update.de_json(request.json, out_channel)
            if self._is_button(update):
                msg = update.callback_query.message
                text = update.callback_query.data
            else:
                msg = update.message
                if self._is_user_message(msg):
                    text = msg.text.replace("/bot", "")
                elif self._is_location(msg):
                    text = '{{"lng":{0}, "lat":{1}}}'.format(
                        msg.location.longitude, msg.location.latitude
                    )
                else:
                    return response.text("success")
            sender_id = msg.chat.id
            try:
                if text == (INTENT_MESSAGE_PREFIX + USER_INTENT_RESTART):
                    await on_new_message(
                        UserMessage(
                            text, out_channel, sender_id, input_channel=self.name()
                        )
                    )
                    await on_new_message(
                        UserMessage(
                            "/start",
                            out_channel,
                            sender_id,
                            input_channel=self.name(),
                        )
                    )
                else:
                    await on_new_message(
                        UserMessage(
                            text, out_channel, sender_id, input_channel=self.name()
                        )
                    )
            except Exception as e:
                logger.error("Exception when trying to handle message.{0}".format(e))
                logger.debug(e, exc_info=True)
                if self.debug_mode:
                    raise
                pass

            return response.text("success")

    return telegram_webhook