简体   繁体   中英

bad webhook: An HTTPS URL must be provided for webhook pyTelegramBotAPI pythonanywhere

import telebot
from telebot import types, TeleBot
from flask import Flask, request


class Exception_Handler:
    def handle(self, e: Exception):
        # Here you can write anything you want for every type of exceptions
        if isinstance(e, ApiTelegramException):
            if e.description == "Forbidden: bot was blocked by the user":
                print("Attention please! The user {} has blocked the bot. I can't send anything to them".format(message.from_user.username))


secret = "RANDOM_NUMBER"
bot: TeleBot = telebot.TeleBot('TOKEN', threaded=False, exception_handler = Exception_Handler())
bot.set_webhook("http://NAME.pythonanywhere.com/{}".format(secret), max_connections=1)

app = Flask(__name__)
@app.route('/{}'.format(secret), methods=["POST"])
def webhook():
    if flask.request.headers.get('content-type') == 'application/json':
        json_string = flask.request.get_data().decode('utf-8')
        update = telebot.types.Update.de_json(json_string)
        bot.process_new_updates([update])
        return ''
    else:
        flask.abort(403)

what needs to be done to make it work?

I expected the webhook to work normally, maybe it's a certificate, if so, how to get it for the pythonanywhere service?

In your code you have this line:

bot.set_webhook("http://NAME.pythonanywhere.com/{}".format(secret), max_connections=1)

That's using a non-HTTPS URL for the webhook, as the error message says. Try using this instead:

bot.set_webhook("https://NAME.pythonanywhere.com/{}".format(secret), max_connections=1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM